...
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled

This commit is contained in:
Your Name
2025-05-24 11:15:23 +12:00
parent e1c631dc72
commit 0934179053
17 changed files with 523 additions and 366 deletions

View File

@@ -49,7 +49,7 @@ namespace dropshell
return;
}
// get the variables from the json
// get the variables from the json, converting everything to strings.
for (const auto &var : server_env_json.items())
{
std::string value;
@@ -111,35 +111,19 @@ namespace dropshell
}
}
bool server_config::create_server_json_file(const std::string &server_env_path, const std::string &SSH_HOST, const std::string &SSH_PORT, const std::vector<UserConfig> &users)
std::string server_config::get_SSH_HOST() const
{
nlohmann::json server_env_json;
server_env_json["SSH_HOST"] = SSH_HOST;
server_env_json["SSH_PORT"] = std::stoi(SSH_PORT);
return get_variable("SSH_HOST");
}
// Create users array
nlohmann::json users_array = nlohmann::json::array();
for (const auto &user : users)
{
nlohmann::json user_json;
user_json["USER"] = user.user;
user_json["DIR"] = user.dir;
users_array.push_back(user_json);
}
server_env_json["SSH_USERS"] = users_array;
std::string server_config::get_SSH_PORT() const
{
return get_variable("SSH_PORT");
}
try
{
std::ofstream server_env_file(server_env_path);
server_env_file << server_env_json.dump(4);
server_env_file.close();
return true;
}
catch (const std::exception &e)
{
std::cerr << "Failed to create server environment file: " + std::string(e.what()) << std::endl;
return false;
}
std::vector<UserConfig> server_config::get_users() const
{
return mUsers;
}
std::string server_config::get_user_dir(const std::string &user) const
@@ -154,22 +138,24 @@ namespace dropshell
return "";
}
std::string server_config::get_user_for_service(const std::string &server, const std::string &service)
std::string server_config::get_server_name() const
{
return mServerName;
}
std::string server_config::get_user_for_service(const std::string &service) const
{
return dropshell::get_user_for_service(mServerName, service);
}
std::string get_user_for_service(const std::string &server, const std::string &service)
{
auto services_info = get_server_services_info(server);
if (std::find_if(services_info.begin(), services_info.end(),
auto it = std::find_if(services_info.begin(), services_info.end(),
[&service](const LocalServiceInfo &si)
{ return si.service_name == service; }) != services_info.end())
{
// found a service with matching name.
auto it = std::find_if(services_info.begin(), services_info.end(),
[&service](const LocalServiceInfo &si)
{ return si.service_name == service; });
if (it != services_info.end())
{
return it->user;
}
}
{ return si.service_name == service; });
if (it != services_info.end() && SIvalid(*it))
return it->user;
return "";
}
@@ -184,6 +170,31 @@ namespace dropshell
return sSSHInfo(get_SSH_HOST(), it->user, get_SSH_PORT(), get_server_name(), it->dir);
}
bool server_config::hasRootUser() const
{
auto it = std::find_if(mUsers.begin(), mUsers.end(),[](const UserConfig &u)
{ return u.user == "root"; });
return it != mUsers.end();
}
bool server_config::hasDocker() const
{
return get_variable("HAS_DOCKER") == "true";
}
bool server_config::hasRootDocker() const
{
return get_variable("DOCKER_ROOTLESS") == "false";
}
bool server_config::hasUser(const std::string &user) const
{
auto it = std::find_if(mUsers.begin(), mUsers.end(),
[&user](const UserConfig &u)
{ return u.user == user; });
return it != mUsers.end();
}
bool server_config::check_remote_dir_exists(const std::string &dir_path, std::string user) const
{
sCommand scommand("", "test -d " + quote(dir_path), {});
@@ -255,7 +266,7 @@ namespace dropshell
bool silent,
std::map<std::string, std::string> extra_env_vars) const
{
std::string user = get_user_for_service(mServerName, service_name);
std::string user = get_user_for_service(service_name);
auto scommand = construct_standard_template_run_cmd(service_name, command, args, silent);
if (!scommand.has_value())
return false;
@@ -278,7 +289,7 @@ namespace dropshell
bool silent,
std::map<std::string, std::string> extra_env_vars) const
{
std::string user = get_user_for_service(mServerName, service_name);
std::string user = get_user_for_service(service_name);
auto scommand = construct_standard_template_run_cmd(service_name, command, args, false);
if (!scommand.has_value())
return false;
@@ -305,7 +316,7 @@ namespace dropshell
if (command.empty())
return std::nullopt;
std::string user = get_user_for_service(mServerName, service_name);
std::string user = get_user_for_service(service_name);
std::string remote_service_template_path = remotepath(mServerName, user).service_template(service_name);
std::string script_path = remote_service_template_path + "/" + command + ".sh";
@@ -336,9 +347,9 @@ namespace dropshell
}
bool run_as_root = runas == "root";
if (run_as_root && !get_ALLOW_ROOT_SERVICES())
if (run_as_root && !hasRootUser())
{
error << "Error: The service " << service_name << " is set to run as root, but the server environment does not allow root services." << std::endl;
error << "Error: The service " << service_name << " is set to run as root on the remote server, but the server environment does not allow root services." << std::endl;
return std::nullopt;
}