1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include "omc.h"
#include "docker.h"
int docker_exec(const char *args, unsigned flags) {
struct Process proc;
char cmd[PATH_MAX];
memset(&proc, 0, sizeof(proc));
memset(cmd, 0, sizeof(cmd));
snprintf(cmd, sizeof(cmd) - 1, "docker %s", args);
if (flags & OMC_DOCKER_QUIET) {
strcpy(proc.f_stdout, "/dev/null");
strcpy(proc.f_stderr, "/dev/null");
}
fprintf(stderr, "%s\n", cmd);
shell(&proc, cmd);
return proc.returncode;
}
int docker_script(const char *image, char *data, unsigned flags) {
FILE *infile;
FILE *outfile;
char cmd[PATH_MAX];
memset(cmd, 0, sizeof(cmd));
snprintf(cmd, sizeof(cmd) - 1, "docker run --rm -i %s /bin/bash -", image);
if (flags & OMC_DOCKER_QUIET) {
}
fprintf(stderr, "%s\n", cmd);
outfile = popen(cmd, "w");
infile = fmemopen(data, strlen(data), "r");
char buffer[OMC_BUFSIZ];
do {
fgets(buffer, sizeof(buffer) - 1, infile);
fputs(buffer, outfile);
} while (!feof(infile));
fclose(infile);
pclose(outfile);
return 0;
}
int docker_build(const char *dirpath, const char *args, int engine) {
char cmd[PATH_MAX];
char build[15];
memset(build, 0, sizeof(build));
memset(cmd, 0, sizeof(cmd));
if (engine & OMC_DOCKER_BUILD) {
strcpy(build, "build");
}
if (engine & OMC_DOCKER_BUILD_X) {
strcpy(build, "buildx build");
}
snprintf(cmd, sizeof(cmd) - 1, "%s %s %s", build, args, dirpath);
return docker_exec(cmd, 0);
}
int docker_save(const char *image, const char *destdir) {
char cmd[PATH_MAX];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "save %s -o \"%s/%s.tar\"", image, destdir, image);
return docker_exec(cmd, 0);
}
static int docker_exists() {
if (find_program("docker")) {
return true;
}
return false;
}
char *docker_ident() {
FILE *fp = NULL;
char *tempfile = NULL;
char line[PATH_MAX];
struct Process proc;
tempfile = xmkstemp(&fp);
if (!fp || !tempfile) {
return NULL;
}
memset(&proc, 0, sizeof(proc));
strcpy(proc.f_stdout, tempfile);
strcpy(proc.f_stderr, "/dev/null");
shell(&proc, "docker -v");
if (!freopen(tempfile, "r", fp)) {
return NULL;
}
if (!fgets(line, sizeof(line) - 1, fp)) {
free(tempfile);
return NULL;
}
fclose(fp);
remove(tempfile);
return strdup(line);
}
int docker_capable(struct DockerCapabilities *result) {
char *version = NULL;
memset(result, 0, sizeof(*result));
if (!docker_exists()) {
// docker isn't available
return false;
}
result->available = true;
if (docker_exec("ps", OMC_DOCKER_QUIET)) {
// user cannot connect to the socket
return false;
}
version = docker_ident();
if (version && startswith(version, "podman")) {
result->podman = true;
}
guard_free(version)
if (!docker_exec("buildx build --help", OMC_DOCKER_QUIET)) {
result->build |= OMC_DOCKER_BUILD;
}
if (!docker_exec("build --help", OMC_DOCKER_QUIET)) {
result->build |= OMC_DOCKER_BUILD_X;
}
if (!result->build) {
// can't use docker without a build plugin
return false;
}
result->usable = true;
return true;
}
|