This commit is contained in:
@@ -1,126 +1,444 @@
|
||||
#include "servers.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "utils/tableprint.hpp"
|
||||
#include "utils/envmanager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "services.hpp"
|
||||
#include "config.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "contrib/transwarp.hpp"
|
||||
#include "utils/output.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/execute.hpp"
|
||||
#include "output.hpp"
|
||||
#include "utils/assert.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <wordexp.h> // For potential shell-like expansion if needed
|
||||
|
||||
namespace dropshell {
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
std::vector<ServerInfo> get_configured_servers() {
|
||||
std::vector<ServerInfo> servers;
|
||||
server_config::server_config(const std::string &server_name) : mValid(false), mServerName(server_name)
|
||||
{
|
||||
if (server_name.empty())
|
||||
return;
|
||||
|
||||
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty())
|
||||
return servers;
|
||||
// Construct the full path to server.json
|
||||
std::string server_env_path = localfile::server_json(server_name);
|
||||
|
||||
for (auto servers_dir : lsdp) {
|
||||
if (!servers_dir.empty() && std::filesystem::exists(servers_dir)) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(servers_dir)) {
|
||||
if (std::filesystem::is_directory(entry)) {
|
||||
std::string server_name = entry.path().filename().string();
|
||||
// Check if file exists
|
||||
if (!std::filesystem::exists(server_env_path))
|
||||
{
|
||||
std::cerr << "Server environment file not found: " + server_env_path << " for server " << server_name << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (server_name.empty() || server_name[0]=='.' || server_name[0]=='_')
|
||||
continue;
|
||||
try
|
||||
{
|
||||
// Use envmanager to handle the environment file
|
||||
nlohmann::json server_env_json = nlohmann::json::parse(std::ifstream(server_env_path));
|
||||
if (server_env_json.empty())
|
||||
{
|
||||
std::cerr << "Error: Failed to parse server environment file: " + server_env_path << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl;
|
||||
continue;
|
||||
// get the variables from the json
|
||||
for (const auto &var : server_env_json.items())
|
||||
{
|
||||
std::string value;
|
||||
if (var.value().is_string())
|
||||
value = var.value();
|
||||
else if (var.value().is_number_integer())
|
||||
value = std::to_string(var.value().get<int>());
|
||||
else if (var.value().is_boolean())
|
||||
value = var.value() ? "true" : "false";
|
||||
else
|
||||
value = var.value().dump();
|
||||
mVariables[var.key()] = replace_with_environment_variables_like_bash(value);
|
||||
}
|
||||
|
||||
// Verify required variables exist
|
||||
for (const auto &var : {"SSH_HOST", "SSH_PORT", "USERS"})
|
||||
{
|
||||
if (mVariables.find(var) == mVariables.end())
|
||||
{
|
||||
// Print the variables identified in the file
|
||||
std::cout << "Variables identified in the file:" << std::endl;
|
||||
for (const auto &v : mVariables)
|
||||
{
|
||||
std::cout << " " << v.first << std::endl;
|
||||
}
|
||||
throw std::runtime_error("Missing required variable: " + std::string(var));
|
||||
}
|
||||
}
|
||||
|
||||
// Parse users array
|
||||
if (!server_env_json.contains("USERS") || !server_env_json["USERS"].is_array())
|
||||
{
|
||||
std::cerr << "Error: USERS array not found or invalid in server configuration" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto &user_json : server_env_json["USERS"])
|
||||
{
|
||||
UserConfig user;
|
||||
user.user = user_json["USER"].get<std::string>();
|
||||
user.dir = user_json["DIR"].get<std::string>();
|
||||
mUsers.push_back(user);
|
||||
}
|
||||
|
||||
if (mUsers.empty())
|
||||
{
|
||||
std::cerr << "Error: No users defined in server configuration" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
mValid = true;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "Failed to parse server environment file: " + std::string(e.what()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
nlohmann::json server_env_json;
|
||||
server_env_json["SSH_HOST"] = SSH_HOST;
|
||||
server_env_json["SSH_PORT"] = std::stoi(SSH_PORT);
|
||||
|
||||
// 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["USERS"] = users_array;
|
||||
|
||||
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::string server_config::get_user_dir(const std::string &user) const
|
||||
{
|
||||
for (const auto &u : mUsers)
|
||||
{
|
||||
if (u.user == user)
|
||||
{
|
||||
return u.dir;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string server_config::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(),
|
||||
[&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 "";
|
||||
}
|
||||
|
||||
sSSHInfo server_config::get_SSH_INFO(std::string user) const
|
||||
{
|
||||
ASSERT(!user.empty(), "User is empty, cannot get SSH info.");
|
||||
// Find user in mUsers vector
|
||||
auto it = std::find_if(mUsers.begin(), mUsers.end(),
|
||||
[&user](const UserConfig &u)
|
||||
{ return u.user == user; });
|
||||
ASSERT(it != mUsers.end(), ("User " + user + " not found in server environment."));
|
||||
return sSSHInfo{get_SSH_HOST(), user, get_SSH_PORT(), get_server_name()};
|
||||
}
|
||||
|
||||
bool server_config::check_remote_dir_exists(const std::string &dir_path, std::string user) const
|
||||
{
|
||||
sCommand scommand("", "test -d " + quote(dir_path), {});
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent);
|
||||
}
|
||||
|
||||
bool server_config::check_remote_file_exists(const std::string &file_path, std::string user) const
|
||||
{
|
||||
sCommand scommand("", "test -f " + quote(file_path), {});
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand, cMode::Silent);
|
||||
}
|
||||
|
||||
bool server_config::check_remote_items_exist(const std::vector<std::string> &file_paths, std::string user) const
|
||||
{
|
||||
// convert file_paths to a single string, separated by spaces
|
||||
std::string file_paths_str;
|
||||
std::string file_names_str;
|
||||
for (const auto &file_path : file_paths)
|
||||
{
|
||||
file_paths_str += quote(file_path) + " ";
|
||||
file_names_str += std::filesystem::path(file_path).filename().string() + " ";
|
||||
}
|
||||
// check if all items in the vector exist on the remote server, in a single command.
|
||||
sCommand scommand("", "for item in " + file_paths_str + "; do test -f $item; done", {});
|
||||
|
||||
sSSHInfo sshinfo = get_SSH_INFO(user);
|
||||
bool okay = execute_ssh_command(sshinfo, scommand, cMode::Silent);
|
||||
if (!okay)
|
||||
{
|
||||
std::cerr << "Error: Required items not found on remote server: " << file_names_str << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_config::remove_remote_dir(
|
||||
const std::string &dir_path, bool silent, std::string user) const
|
||||
{
|
||||
std::filesystem::path path(dir_path);
|
||||
std::filesystem::path parent_path = path.parent_path();
|
||||
std::string target_dir = path.filename().string();
|
||||
|
||||
if (parent_path.empty())
|
||||
parent_path = "/";
|
||||
|
||||
if (target_dir.empty())
|
||||
return false;
|
||||
|
||||
if (!silent)
|
||||
std::cout << "Removing remote directory " << target_dir << " in " << parent_path << " on " << mServerName << std::endl;
|
||||
std::string remote_cmd =
|
||||
"docker run --rm -v " + quote(parent_path.string()) + ":/parent " +
|
||||
" alpine rm -rf \"/parent/" + target_dir + "\"";
|
||||
|
||||
// if (!silent)
|
||||
// std::cout << "Running command: " << remote_cmd << std::endl;
|
||||
|
||||
sCommand scommand("", remote_cmd, {});
|
||||
cMode mode = (silent ? cMode::Silent : cMode::Defaults);
|
||||
|
||||
sSSHInfo sshinfo = get_SSH_INFO(user);
|
||||
return execute_ssh_command(sshinfo, scommand, mode);
|
||||
}
|
||||
|
||||
bool server_config::run_remote_template_command(
|
||||
const std::string &service_name,
|
||||
const std::string &command,
|
||||
std::vector<std::string> args,
|
||||
bool silent,
|
||||
std::map<std::string, std::string> extra_env_vars) const
|
||||
{
|
||||
std::string user = get_user_for_service(mServerName, service_name);
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, silent);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto &[key, value] : extra_env_vars)
|
||||
scommand->add_env_var(key, value);
|
||||
|
||||
if (scommand->get_command_to_run().empty())
|
||||
return false;
|
||||
cMode mode = (command == "ssh") ? (cMode::Interactive) : (silent ? cMode::Silent : cMode::Defaults);
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), mode);
|
||||
}
|
||||
|
||||
bool server_config::run_remote_template_command_and_capture_output(
|
||||
const std::string &service_name,
|
||||
const std::string &command,
|
||||
std::vector<std::string> args,
|
||||
std::string &output,
|
||||
bool silent,
|
||||
std::map<std::string, std::string> extra_env_vars) const
|
||||
{
|
||||
std::string user = get_user_for_service(mServerName, service_name);
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, false);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto &[key, value] : extra_env_vars)
|
||||
scommand->add_env_var(key, value);
|
||||
|
||||
return execute_ssh_command(get_SSH_INFO(user), scommand.value(), cMode::Defaults, &output);
|
||||
}
|
||||
|
||||
std::string server_config::get_variable(const std::string &name) const
|
||||
{
|
||||
auto it = mVariables.find(name);
|
||||
if (it == mVariables.end())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::optional<sCommand> server_config::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const
|
||||
{
|
||||
if (command.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::string user = get_user_for_service(mServerName, 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";
|
||||
|
||||
std::map<std::string, std::string> env_vars;
|
||||
if (!get_all_service_env_vars(mServerName, service_name, env_vars))
|
||||
{
|
||||
std::cerr << "Error: Failed to get all service env vars for " << service_name << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::string argstr = "";
|
||||
for (const auto &arg : args)
|
||||
{
|
||||
argstr += " " + quote(dequote(trim(arg)));
|
||||
}
|
||||
|
||||
if (env_vars.find("RUNAS") == env_vars.end())
|
||||
{
|
||||
error << "Error: RUNAS is not set in .template_info.env for the service." << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string runas = env_vars.find("RUNAS")->second;
|
||||
if (runas != "root" && runas != "user")
|
||||
{
|
||||
error << "Error: RUNAS is not set to root or user in .template_info.env for the service." << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
bool run_as_root = runas == "root";
|
||||
|
||||
if (run_as_root && !get_ALLOW_ROOT_SERVICES())
|
||||
{
|
||||
error << "Error: The service " << service_name << " is set to run as root, but the server environment does not allow root services." << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
sCommand sc(
|
||||
remote_service_template_path,
|
||||
quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""),
|
||||
env_vars,
|
||||
run_as_root);
|
||||
|
||||
if (sc.empty())
|
||||
{
|
||||
std::cerr << "Error: Failed to construct command for " << service_name << " " << command << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
return sc;
|
||||
}
|
||||
|
||||
std::vector<server_config> get_configured_servers()
|
||||
{
|
||||
std::vector<server_config> servers;
|
||||
|
||||
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty())
|
||||
return servers;
|
||||
|
||||
for (auto servers_dir : lsdp)
|
||||
{
|
||||
if (!servers_dir.empty() && std::filesystem::exists(servers_dir))
|
||||
{
|
||||
for (const auto &entry : std::filesystem::directory_iterator(servers_dir))
|
||||
{
|
||||
if (std::filesystem::is_directory(entry))
|
||||
{
|
||||
std::string server_name = entry.path().filename().string();
|
||||
|
||||
if (server_name.empty() || server_name[0] == '.' || server_name[0] == '_')
|
||||
continue;
|
||||
|
||||
server_config env(server_name);
|
||||
if (!env.is_valid())
|
||||
{
|
||||
std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl;
|
||||
continue;
|
||||
}
|
||||
servers.push_back(env);
|
||||
}
|
||||
servers.push_back({
|
||||
server_name,
|
||||
env.get_SSH_HOST(),
|
||||
env.get_SSH_UNPRIVILEGED_USER(),
|
||||
env.get_SSH_PORT()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return servers;
|
||||
}
|
||||
|
||||
return servers;
|
||||
}
|
||||
bool create_server(const std::string &server_name)
|
||||
{
|
||||
// 1. check if server name already exists
|
||||
std::string server_existing_dir = localpath::server(server_name);
|
||||
if (!server_existing_dir.empty())
|
||||
{
|
||||
error << "Error: Server name already exists: " << server_name << std::endl;
|
||||
info << "Current server path: " << server_existing_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
ServerInfo get_server_info(const std::string &server_name)
|
||||
{
|
||||
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty())
|
||||
return ServerInfo();
|
||||
// 2. create a new directory in the user config directory
|
||||
auto lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty() || lsdp[0].empty())
|
||||
{
|
||||
error << "Error: Local server definition path not found" << std::endl;
|
||||
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string server_dir = lsdp[0] + "/" + server_name;
|
||||
std::filesystem::create_directory(server_dir);
|
||||
|
||||
for (auto &config_dir : lsdp) {
|
||||
std::string server_dir = config_dir + "/" + server_name;
|
||||
if (std::filesystem::exists(server_dir)) {
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << server_dir << std::endl;
|
||||
continue;
|
||||
}
|
||||
return ServerInfo({server_name, env.get_SSH_HOST(), env.get_SSH_UNPRIVILEGED_USER(), env.get_SSH_PORT()});
|
||||
// 3. create a template server.env file in the server directory
|
||||
std::string user = getenv("USER");
|
||||
std::string server_env_path = server_dir + "/server.json";
|
||||
std::ofstream server_env_file(server_env_path);
|
||||
server_env_file << "{" << std::endl;
|
||||
server_env_file << " \"SSH_HOST\": \"" << server_name << "\"," << std::endl;
|
||||
server_env_file << " \"SSH_UNPRIVILEGED_USER\": \"" << user << "\"," << std::endl;
|
||||
server_env_file << " \"SSH_PORT\": " << 22 << "," << std::endl;
|
||||
server_env_file << " \"DROPSHELL_DIR\": \"" << "/home/" + user + "/.dropshell\"" << std::endl;
|
||||
server_env_file << "}" << std::endl;
|
||||
server_env_file.close();
|
||||
|
||||
std::cout << "Server created successfully: " << server_name << std::endl;
|
||||
std::cout << "Please complete the installation:" << std::endl;
|
||||
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;
|
||||
std::cout << "2) install the server: dropshell install " << server_name << std::endl;
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void get_all_used_commands(std::set<std::string> &commands)
|
||||
{
|
||||
std::vector<server_config> servers = get_configured_servers();
|
||||
for (const auto &server : servers)
|
||||
{
|
||||
auto services = get_server_services_info(server.get_server_name());
|
||||
for (const auto &service : services)
|
||||
commands.merge(get_used_commands(server.get_server_name(), service.service_name));
|
||||
}
|
||||
}
|
||||
return ServerInfo();
|
||||
}
|
||||
|
||||
|
||||
bool create_server(const std::string &server_name)
|
||||
{
|
||||
// 1. check if server name already exists
|
||||
std::string server_existing_dir = localpath::server(server_name);
|
||||
if (!server_existing_dir.empty()) {
|
||||
error << "Error: Server name already exists: " << server_name << std::endl;
|
||||
info << "Current server path: " << server_existing_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. create a new directory in the user config directory
|
||||
auto lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty() || lsdp[0].empty()) {
|
||||
error << "Error: Local server definition path not found" << std::endl;
|
||||
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string server_dir = lsdp[0] + "/" + server_name;
|
||||
std::filesystem::create_directory(server_dir);
|
||||
|
||||
// 3. create a template server.env file in the server directory
|
||||
std::string user = getenv("USER");
|
||||
std::string server_env_path = server_dir + "/server.json";
|
||||
std::ofstream server_env_file(server_env_path);
|
||||
server_env_file << "{" << std::endl;
|
||||
server_env_file << " \"SSH_HOST\": \"" << server_name << "\"," << std::endl;
|
||||
server_env_file << " \"SSH_UNPRIVILEGED_USER\": \"" << user << "\"," << std::endl;
|
||||
server_env_file << " \"SSH_PORT\": " << 22 << "," << std::endl;
|
||||
server_env_file << " \"DROPSHELL_DIR\": \"" << "/home/"+user+"/.dropshell\"" << std::endl;
|
||||
server_env_file << "}" << std::endl;
|
||||
server_env_file.close();
|
||||
|
||||
std::cout << "Server created successfully: " << server_name << std::endl;
|
||||
std::cout << "Please complete the installation:" <<std::endl;
|
||||
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;
|
||||
std::cout << "2) install the server: dropshell install " << server_name << std::endl;
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void get_all_used_commands(std::set<std::string> &commands)
|
||||
{
|
||||
std::vector<ServerInfo> servers = get_configured_servers();
|
||||
for (const auto& server : servers)
|
||||
{
|
||||
auto services = dropshell::get_server_services_info(server.name);
|
||||
for (const auto& service : services)
|
||||
commands.merge(dropshell::get_used_commands(server.name, service.service_name));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user