Skip to content

Commit

Permalink
Merge pull request #5773 from BOINC/dpa_wsl_code
Browse files Browse the repository at this point in the history
Improve readability of WSL / Docker code
  • Loading branch information
AenBleidd authored Aug 21, 2024
2 parents 4c0a0ec + 3e70a0a commit ed714b8
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 324 deletions.
71 changes: 31 additions & 40 deletions client/client_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,32 @@ void CLIENT_STATE::show_host_info() {
);

#ifdef _WIN64
if (host_info.wsl_available) {
msg_printf(NULL, MSG_INFO, "WSL present:");
for (size_t i = 0; i < host_info.wsls.wsls.size(); ++i) {
const WSL& wsl = host_info.wsls.wsls[i];
if (wsl.is_default) {
msg_printf(NULL, MSG_INFO,
" [%s] <v%s> (default): %s (%s)", wsl.distro_name.c_str(), wsl.wsl_version.c_str(), wsl.os_name.c_str(), wsl.os_version.c_str()
if (host_info.wsl_distros.distros.empty()) {
msg_printf(NULL, MSG_INFO, "WSL: no usable distros found");
} else {
msg_printf(NULL, MSG_INFO, "Usable WSL distros:");
for (auto &wsl: host_info.wsl_distros.distros) {
msg_printf(NULL, MSG_INFO,
"- %s: WSL %d%s",
wsl.distro_name.c_str(),
wsl.wsl_version,
wsl.is_default?" (default)":""
);
msg_printf(NULL, MSG_INFO,
"- OS: %s (%s)",
wsl.os_name.c_str(), wsl.os_version.c_str()
);
if (wsl.is_docker_available) {
msg_printf(NULL, MSG_INFO, "- Docker version %s",
wsl.docker_version.c_str()
);
} else {
msg_printf(NULL, MSG_INFO,
" [%s] <v%s>: %s (%s)", wsl.distro_name.c_str(), wsl.wsl_version.c_str(), wsl.os_name.c_str(), wsl.os_version.c_str()
}
if (wsl.is_docker_compose_available) {
msg_printf(NULL, MSG_INFO, "- Docker compose version is %s",
wsl.docker_compose_version.c_str()
);
}
}
} else {
msg_printf(NULL, MSG_INFO, "WSL is not present or is not allowed by configuration file. For more details see https://github.com/BOINC/boinc/wiki/Client-configuration");
}
#endif

Expand All @@ -280,38 +290,19 @@ void CLIENT_STATE::show_host_info() {
}
#endif
}
#ifdef _WIN64
if (host_info.docker_available) {
msg_printf(NULL, MSG_INFO, "Docker is present on next WSLs:");
for (size_t i = 0; i < host_info.wsls.wsls.size(); ++i) {
const WSL& wsl = host_info.wsls.wsls[i];
if (wsl.is_docker_available) {
msg_printf(NULL, MSG_INFO, " [%s]: Docker version is: %s", wsl.distro_name.c_str(), wsl.docker_version.c_str());
}
}
#else

#ifndef _WIN64
if (host_info.docker_available && strlen(host_info.docker_version)) {
msg_printf(NULL, MSG_INFO, "Docker %s is present", host_info.docker_version);
#endif
} else {
msg_printf(NULL, MSG_INFO, "Docker is not present");
msg_printf(NULL, MSG_INFO, "Docker version %s found",
host_info.docker_version
);
}
#ifdef _WIN64
if (host_info.docker_compose_available) {
msg_printf(NULL, MSG_INFO, "Docker compose is present on next WSLs:");
for (size_t i = 0; i < host_info.wsls.wsls.size(); ++i) {
const WSL& wsl = host_info.wsls.wsls[i];
if (wsl.is_docker_compose_available) {
msg_printf(NULL, MSG_INFO, " [%s]: Docker compose version is: %s", wsl.distro_name.c_str(), wsl.docker_compose_version.c_str());
}
}
#else
if (host_info.docker_compose_available && strlen(host_info.docker_compose_version)) {
msg_printf(NULL, MSG_INFO, "Docker compose %s is present", host_info.docker_compose_version);
#endif
} else {
msg_printf(NULL, MSG_INFO, "Docker compose is not present");
msg_printf(NULL, MSG_INFO, "Docker compose version %s found",
host_info.docker_compose_version
);
}
#endif
}

// TODO: the following 3 should be members of COPROCS
Expand Down
105 changes: 61 additions & 44 deletions client/hostinfo_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@

#include "hostinfo.h"

bool HOST_INFO::parse_linux_os_info(FILE* file, const LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size, char* os_version, const int os_version_size) {

// functions for getting Linux OS and version from various sources
// (lsb_release -a, /etc/os-release, /etc/redhat-release)
//
// in all cases: return true if we get either os_name or os_version
//
bool HOST_INFO::parse_linux_os_info(
FILE* file, LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size,
char* os_version, const int os_version_size
) {
if (!file) {
return false;
}
Expand All @@ -46,27 +53,43 @@ bool HOST_INFO::parse_linux_os_info(FILE* file, const LINUX_OS_INFO_PARSER parse
lines.push_back(buf);
}

return parse_linux_os_info(lines, parser, os_name, os_name_size, os_version, os_version_size);
return parse_linux_os_info(
lines, parser, os_name, os_name_size, os_version, os_version_size
);
}

bool HOST_INFO::parse_linux_os_info(const std::string& line, const LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size, char* os_version, const int os_version_size) {
// input is a string (possibly multiple lines)
//
bool HOST_INFO::parse_linux_os_info(
const std::string& line, LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size,
char* os_version, const int os_version_size
) {
if (line.empty()) {
return false;
}

const char delim = '\n';

return parse_linux_os_info(split(line, delim), parser, os_name, os_name_size, os_version, os_version_size);
return parse_linux_os_info(
split(line, delim), parser, os_name, os_name_size,
os_version, os_version_size
);
}

bool HOST_INFO::parse_linux_os_info(const std::vector<std::string>& lines, const LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size, char* os_version, const int os_version_size) {
// input is a list of lines
//
bool HOST_INFO::parse_linux_os_info(
const std::vector<std::string>& lines, LINUX_OS_INFO_PARSER parser,
char* os_name, const int os_name_size,
char* os_version, const int os_version_size
) {
if (lines.empty()) {
return false;
}

bool found_something = false;
unsigned int i;
char buf[256], buf2[256];
char dist_pretty[256], dist_name[256], dist_version[256], dist_codename[256];
//string os_version_extra("");
Expand All @@ -76,8 +99,8 @@ bool HOST_INFO::parse_linux_os_info(const std::vector<std::string>& lines, const
strcpy(dist_codename, "");

switch (parser) {
case lsbrelease: {
for (unsigned int i = 0; i < lines.size(); ++i) {
case lsbrelease:
for (i = 0; i < lines.size(); ++i) {
safe_strcpy(buf, lines[i].c_str());
strip_whitespace(buf);
if (strstr(buf, "Description:")) {
Expand All @@ -102,9 +125,8 @@ bool HOST_INFO::parse_linux_os_info(const std::vector<std::string>& lines, const
}
}
break;
}
case osrelease: {
for (unsigned int i = 0; i < lines.size(); ++i) {
case osrelease:
for (i = 0; i < lines.size(); ++i) {
safe_strcpy(buf, lines[i].c_str());
strip_whitespace(buf);
// check if substr is at the beginning of the line
Expand Down Expand Up @@ -143,46 +165,41 @@ bool HOST_INFO::parse_linux_os_info(const std::vector<std::string>& lines, const
}
}
break;
}
case redhatrelease: {
case redhatrelease:
safe_strcpy(buf, lines.front().c_str());
found_something = true;
strip_whitespace(buf);
safe_strcpy(dist_pretty, buf);
break;
}
default: {
default:
return false;
}
}

if (found_something) {
strcpy(buf2, "");
if (strlen(dist_pretty)) {
safe_strcat(buf2, dist_pretty);
if (!found_something) {
return false;
}
strcpy(buf2, "");
if (strlen(dist_pretty)) {
safe_strcat(buf2, dist_pretty);
} else {
if (strlen(dist_name)) {
safe_strcat(buf2, dist_name);
strcat(buf2, " ");
}
else {
if (strlen(dist_name)) {
safe_strcat(buf2, dist_name);
strcat(buf2, " ");
}
if (strlen(dist_version)) {
safe_strcat(buf2, dist_version);
strcat(buf2, " ");
}
if (strlen(dist_codename)) {
safe_strcat(buf2, dist_codename);
strcat(buf2, " ");
}
strip_whitespace(buf2);
if (strlen(dist_version)) {
safe_strcat(buf2, dist_version);
strcat(buf2, " ");
}
strlcpy(os_version, buf2, os_version_size);
if (strlen(dist_name)) {
strlcpy(os_name, dist_name, os_name_size);
if (strlen(dist_codename)) {
safe_strcat(buf2, dist_codename);
strcat(buf2, " ");
}

return true;
strip_whitespace(buf2);
}

return false;
strlcpy(os_version, buf2, os_version_size);
strcpy(os_name, "");
if (strlen(dist_name)) {
strlcpy(os_name, dist_name, os_name_size);
}
return true;
}
4 changes: 2 additions & 2 deletions client/hostinfo_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ int HOST_INFO::get_virtualbox_version() {
}

// check if docker compose is installed on volunteer's host
// populates docker compose version and docker_compose_available on success
// populates docker compose version and docker_compose_present on success
bool HOST_INFO::get_docker_compose_info(){
FILE* f = popen(command_get_docker_compose_version, "r");
if (f) {
Expand All @@ -1253,7 +1253,7 @@ bool HOST_INFO::get_docker_compose_info(){


// check if docker is installed on volunteer's host
// populates docker version and docker_available on success
// populates docker version and docker_present on success
bool HOST_INFO::get_docker_info(){
FILE* f = popen(command_get_docker_version, "r");
if (f) {
Expand Down
4 changes: 3 additions & 1 deletion client/hostinfo_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,9 @@ int HOST_INFO::get_host_info(bool init) {
if (!cc_config.dont_use_wsl) {
OSVERSIONINFOEX osvi;
if (get_OSVERSIONINFO(osvi) && osvi.dwMajorVersion >= 10) {
get_wsl_information(cc_config.allowed_wsls, wsl_available, wsls, !cc_config.dont_use_docker, docker_available, docker_compose_available);
get_wsl_information(
cc_config.allowed_wsls, wsl_distros, !cc_config.dont_use_docker
);
}
}
#endif
Expand Down
Loading

0 comments on commit ed714b8

Please sign in to comment.