-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpull:docker.c
49 lines (40 loc) · 1.19 KB
/
pull:docker.c
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
// Import image from local docker instance into.
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <plash.h>
#define USAGE "usage: plash import-docker IMAGE\n"
void call_docker_pull(char *image) {
int status, exit;
pid_t pid = fork();
if (pid == 0) {
// redirect stdout to stderr
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
pl_fatal("dup2");
execlp("docker", "docker", "pull", image, NULL);
pl_fatal("execlp");
}
if (waitpid(pid, &status, 0) < 0)
pl_fatal("waitpid");
if (!WIFEXITED(status))
pl_fatal("docker pull subprocess exited abnormally");
exit = WEXITSTATUS(status);
if (exit != 0) {
pl_fatal("docker pull failed with exit status %d", exit);
}
}
int pull_docker_main(int argc, char *argv[]) {
if (argc < 2) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
char *image = argv[1];
call_docker_pull(image);
char *container_id = pl_firstline(
pl_check_output((char *[]){"docker", "create", image, "sh", NULL}));
pl_pipe((char *[]){"docker", "export", container_id, NULL},
(char *[]){"/proc/self/exe", "pull:tarfile", NULL});
return EXIT_SUCCESS;
}