Skip to content

Commit

Permalink
docker_wrapper: get suspend/resume working on Win with podman
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpanderson committed Oct 24, 2024
1 parent b0498cd commit 462f345
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ int run_command(char *cmd, vector<string> &out) {

// run the program, and return handles to write to and read from it
//
int run_command_pipe(
int run_program_pipe(
char *cmd, HANDLE &write_handle, HANDLE &read_handle, HANDLE &proc_handle
) {
HANDLE in_read, in_write, out_read, out_write;
Expand Down
2 changes: 1 addition & 1 deletion lib/win_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct WSL_CMD {

// Use wsl.exe to run a shell as root in the WSL container
//
int setup_root();
int setup_root(const char* distro_name);

// run command, direct both stdout and stderr to the out pipe
// Use read_from_pipe() to get the output.
Expand Down
59 changes: 30 additions & 29 deletions samples/docker_wrapper/docker_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
// slot dir: .
// project dir: project/

// enable standalone tests on Win
//
#define WIN_STANDALONE_COPY 0
#define WIN_STANDALONE_MOUNT 0

#include <cstdio>
#include <string>
#include <vector>
Expand Down Expand Up @@ -376,6 +381,14 @@ int copy_files_from_container() {

////////// JOB CONTROL ////////////

int container_op(const char *op) {
char cmd[1024];
vector<string> out;
sprintf(cmd, "%s %s %s", cli_prog, op, container_name);
int retval = run_docker_command(cmd, out);
return retval;
}

// Clean up at end of job.
// Show log output if verbose;
// remove container and image
Expand All @@ -388,52 +401,44 @@ void cleanup() {
sprintf(cmd, "%s logs %s", cli_prog, container_name);
run_docker_command(cmd, out);
fprintf(stderr, "container log:\n");
for (string line: out) {
for (string line : out) {
fprintf(stderr, " %s\n", line.c_str());
}
}

container_op("stop");

sprintf(cmd, "%s container rm %s", cli_prog, container_name);
run_docker_command(cmd, out);

sprintf(cmd, "%s image rm %s", cli_prog, image_name);
run_docker_command(cmd, out);
}

int resume() {
char cmd[1024];
vector<string> out;
sprintf(cmd, "%s unpause %s", cli_prog, container_name);
int retval = run_docker_command(cmd, out);
return retval;
}

int suspend() {
char cmd[1024];
vector<string> out;
sprintf(cmd, "%s pause %s", cli_prog, container_name);
int retval = run_docker_command(cmd, out);
return retval;
}

void poll_client_msgs() {
BOINC_STATUS status;
boinc_get_status(&status);
if (status.no_heartbeat || status.quit_request || status.abort_request) {
fprintf(stderr, "got quit/abort from client\n");
suspend();
container_op("stop");
exit(0);
}
if (status.suspended) {
if (verbose) {
fprintf(stderr, "client: suspended\n");
}
if (running) suspend();
if (running) {
container_op("pause");
running = false;
}
} else {
if (verbose) {
fprintf(stderr, "client: not suspended\n");
}
if (!running) resume();
if (!running) {
container_op("unpause");
running = true;
}
}
}

Expand Down Expand Up @@ -478,7 +483,7 @@ int wsl_init() {
retval = ctl_wc.run_program_in_wsl(distro_name, "", true);
if (retval) return retval;
} else if (docker_type == PODMAN) {
int retval = ctl_wc.setup_root(distro_name);
int retval = ctl_wc.setup_root(distro_name.c_str());
if (retval) return retval;
} else {
return -1;
Expand All @@ -505,18 +510,15 @@ int main(int argc, char** argv) {
}
}

#ifdef _WIN32
// standalone tests; comment out if using client
#if 0
#if WIN_STANDALONE_COPY
SetCurrentDirectoryA("C:/ProgramData/BOINC/slots/test_docker_copy");
config_file = "job_copy.toml";
dockerfile = "Dockerfile_copy";
#endif
#if 0
#if WIN_STANDALONE_MOUNT
SetCurrentDirectoryA("C:/ProgramData/BOINC/slots/test_docker_mount");
config_file = "job_mount.toml";
dockerfile = "Dockerfile_mount";
#endif
#endif

memset(&options, 0, sizeof(options));
Expand All @@ -541,7 +543,6 @@ int main(int argc, char** argv) {
}
if (verbose) config.print();


#ifdef _WIN32
retval = wsl_init();
if (retval) {
Expand Down Expand Up @@ -569,8 +570,8 @@ int main(int argc, char** argv) {
boinc_finish(1);
}
}
if (verbose) fprintf(stderr, "resuming container\n");
retval = resume();
if (verbose) fprintf(stderr, "starting container\n");
retval = container_op("start");
if (retval) {
fprintf(stderr, "resume() failed: %d\n", retval);
cleanup();
Expand Down

0 comments on commit 462f345

Please sign in to comment.