This commit is contained in:
75
source/src/commands/command_registry.cpp
Normal file
75
source/src/commands/command_registry.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
CommandRegistry& CommandRegistry::instance() {
|
||||
static CommandRegistry reg;
|
||||
return reg;
|
||||
}
|
||||
|
||||
void CommandRegistry::register_command(const CommandInfo& info) {
|
||||
auto ptr = std::make_shared<CommandInfo>(info);
|
||||
for (const auto& name : info.names) {
|
||||
command_map_[name] = ptr;
|
||||
}
|
||||
all_commands_.push_back(ptr);
|
||||
}
|
||||
|
||||
const CommandInfo* CommandRegistry::find_command(const std::string& name) const {
|
||||
auto it = command_map_.find(name);
|
||||
if (it != command_map_.end()) return it->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandRegistry::list_commands(bool include_hidden) const {
|
||||
std::set<std::string> out;
|
||||
for (const auto& cmd : all_commands_) {
|
||||
if (!cmd->hidden || include_hidden) {
|
||||
for (const auto& name : cmd->names) out.insert(name);
|
||||
}
|
||||
}
|
||||
return std::vector<std::string>(out.begin(), out.end());
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandRegistry::list_primary_commands(bool include_hidden) const {
|
||||
std::set<std::string> out;
|
||||
for (const auto& cmd : all_commands_) {
|
||||
if (!cmd->hidden || include_hidden) {
|
||||
if (cmd->names.size() > 0)
|
||||
{
|
||||
if (cmd->requires_config && !gConfig().is_config_set())
|
||||
continue;
|
||||
if (cmd->requires_install && !gConfig().is_agent_installed())
|
||||
continue;
|
||||
out.insert(cmd->names[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::vector<std::string>(out.begin(), out.end());
|
||||
}
|
||||
|
||||
|
||||
void CommandRegistry::autocomplete(const CommandContext& ctx) const {
|
||||
// dropshell autocomplete <command> <arg> <arg> ...
|
||||
if (ctx.args.size() < 1) {
|
||||
for (const auto& name : list_primary_commands(false)) {
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ctx command is autocomplete, so recreate ctx with the first arg removed
|
||||
CommandContext childcontext = {
|
||||
ctx.exename,
|
||||
ctx.args[0],
|
||||
std::vector<std::string>(ctx.args.begin() + 1, ctx.args.end())
|
||||
};
|
||||
auto* info = find_command(childcontext.command);
|
||||
if (info && info->autocomplete) {
|
||||
info->autocomplete(childcontext);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
60
source/src/commands/command_registry.hpp
Normal file
60
source/src/commands/command_registry.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef COMMAND_REGISTRY_HPP
|
||||
#define COMMAND_REGISTRY_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
struct CommandContext {
|
||||
std::string exename;
|
||||
std::string command;
|
||||
std::vector<std::string> args;
|
||||
|
||||
// Add more fields as needed (e.g., config pointer, output stream, etc.)
|
||||
};
|
||||
|
||||
struct CommandInfo {
|
||||
std::vector<std::string> names;
|
||||
std::function<int(const CommandContext&)> handler;
|
||||
std::function<void(const CommandContext&)> autocomplete; // optional
|
||||
bool hidden = false;
|
||||
bool requires_config = true;
|
||||
bool requires_install = true;
|
||||
int min_args = 0;
|
||||
int max_args = -1; // -1 = unlimited
|
||||
std::string help_usage; // install SERVER [SERVICE]
|
||||
std::string help_description; // Install/reinstall/update service(s). Safe/non-destructive.
|
||||
std::string full_help; // detailed help for the command
|
||||
};
|
||||
|
||||
class CommandRegistry {
|
||||
public:
|
||||
static CommandRegistry& instance();
|
||||
|
||||
void register_command(const CommandInfo& info);
|
||||
|
||||
// Returns nullptr if not found
|
||||
const CommandInfo* find_command(const std::string& name) const;
|
||||
|
||||
// List all commands (optionally including hidden)
|
||||
std::vector<std::string> list_commands(bool include_hidden = false) const;
|
||||
std::vector<std::string> list_primary_commands(bool include_hidden = false) const;
|
||||
|
||||
// For autocomplete
|
||||
void autocomplete(const CommandContext& ctx) const;
|
||||
|
||||
private:
|
||||
CommandRegistry() = default;
|
||||
std::map<std::string, std::shared_ptr<CommandInfo>> command_map_;
|
||||
std::vector<std::shared_ptr<CommandInfo>> all_commands_;
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
#endif // COMMAND_REGISTRY_HPP
|
180
source/src/commands/edit.cpp
Normal file
180
source/src/commands/edit.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int edit_handler(const CommandContext& ctx);
|
||||
|
||||
static std::vector<std::string> edit_name_list={"edit"};
|
||||
|
||||
// Static registration
|
||||
struct EditCommandRegister {
|
||||
EditCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
edit_name_list,
|
||||
edit_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"edit [SERVER] [SERVICE]",
|
||||
"Edit dropshell, server or service configuration",
|
||||
// heredoc
|
||||
R"(
|
||||
Edit dropshell, server or service configuration.
|
||||
edit edit the dropshell config.
|
||||
edit <server> edit the server config.
|
||||
edit <server> <service> edit the service config.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} edit_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// utility function to edit a file
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool edit_file(const std::string &file_path, bool has_bb64)
|
||||
{
|
||||
// make sure parent directory exists.
|
||||
std::string parent_dir = get_parent(file_path);
|
||||
std::filesystem::create_directories(parent_dir);
|
||||
|
||||
std::string editor_cmd;
|
||||
const char* editor_env = std::getenv("EDITOR");
|
||||
|
||||
if (editor_env && std::strlen(editor_env) > 0) {
|
||||
editor_cmd = std::string(editor_env) + " " + quote(file_path);
|
||||
} else if (isatty(STDIN_FILENO)) {
|
||||
// Check if stdin is connected to a terminal if EDITOR is not set
|
||||
editor_cmd = "nano -w " + quote(file_path);
|
||||
} else {
|
||||
std::cerr << "Error: Standard input is not a terminal and EDITOR environment variable is not set." << std::endl;
|
||||
std::cerr << "Try setting the EDITOR environment variable (e.g., export EDITOR=nano) or run in an interactive terminal." << std::endl;
|
||||
std::cerr << "You can manually edit the file at: " << file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "Editing file: " << file_path << std::endl;
|
||||
|
||||
if (has_bb64) {
|
||||
return execute_local_command(editor_cmd, nullptr, cMode::Interactive);
|
||||
}
|
||||
else {
|
||||
// might not have bb64 at this early stage. Direct edit.
|
||||
int ret = system(editor_cmd.c_str());
|
||||
return EXITSTATUSCHECK(ret);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit config
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_config()
|
||||
{
|
||||
if (!gConfig().is_config_set())
|
||||
gConfig().save_config(false); // save defaults.
|
||||
|
||||
std::string config_file = localfile::dropshell_json();
|
||||
if (!edit_file(config_file, false) || !std::filesystem::exists(config_file))
|
||||
return die("Error: Failed to edit config file.");
|
||||
|
||||
gConfig().load_config();
|
||||
if (!gConfig().is_config_set())
|
||||
return die("Error: Failed to load and parse edited config file!");
|
||||
|
||||
gConfig().save_config(true);
|
||||
|
||||
std::cout << "Successfully edited config file at " << config_file << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit server
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_server(const std::string &server_name)
|
||||
{
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
if (serverpath.empty()) {
|
||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ostringstream aftertext;
|
||||
aftertext << "If you have changed DROPSHELL_DIR, you should manually move the files to the new location NOW.\n"
|
||||
<< "You can ssh in to the remote server with: dropshell ssh "<<server_name<<"\n"
|
||||
<< "Once moved, reinstall all services with: dropshell install " << server_name;
|
||||
|
||||
std::string config_file = serverpath + "/server.env";
|
||||
if (!edit_file(config_file, true)) {
|
||||
std::cerr << "Error: Failed to edit server.env" << std::endl;
|
||||
std::cerr << "You can manually edit this file at: " << config_file << std::endl;
|
||||
std::cerr << "After editing, " << aftertext.str() << std::endl;
|
||||
}
|
||||
else
|
||||
std::cout << aftertext.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit service config
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_service_config(const std::string &server, const std::string &service)
|
||||
{
|
||||
std::string config_file = localfile::service_env(server, service);
|
||||
if (!std::filesystem::exists(config_file))
|
||||
{
|
||||
std::cerr << "Error: Service config file not found: " << config_file << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (edit_file(config_file, true) && std::filesystem::exists(config_file))
|
||||
std::cout << "To apply your changes, run:\n dropshell install " + server + " " + service << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit command handler
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_handler(const CommandContext& ctx) {
|
||||
// edit dropshell config
|
||||
if (ctx.args.size() < 1)
|
||||
return edit_config();
|
||||
|
||||
// edit server config
|
||||
if (ctx.args.size() < 2) {
|
||||
edit_server(safearg(ctx.args,0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// edit service config
|
||||
if (ctx.args.size() < 3) {
|
||||
edit_service_config(safearg(ctx.args,0), safearg(ctx.args,1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "Edit handler called with " << ctx.args.size() << " args\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
138
source/src/commands/health.cpp
Normal file
138
source/src/commands/health.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "transwarp.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int health_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> health_name_list = {"health", "check", "healthcheck", "status"};
|
||||
|
||||
// Static registration
|
||||
struct HealthCommandRegister
|
||||
{
|
||||
HealthCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({health_name_list,
|
||||
health_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
1, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"health SERVER",
|
||||
"Check the health of a server.",
|
||||
R"(
|
||||
health <server>
|
||||
)"});
|
||||
}
|
||||
} health_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// health command implementation
|
||||
|
||||
HealthStatus is_healthy(const std::string &server, const std::string &service)
|
||||
{
|
||||
server_env_manager env(server);
|
||||
if (!env.is_valid())
|
||||
{
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return HealthStatus::ERROR;
|
||||
}
|
||||
|
||||
if (!env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
{
|
||||
return HealthStatus::NOTINSTALLED;
|
||||
}
|
||||
|
||||
std::string script_path = remotepath::service_template(server, service) + "/status.sh";
|
||||
if (!env.check_remote_file_exists(script_path))
|
||||
{
|
||||
return HealthStatus::UNKNOWN;
|
||||
}
|
||||
|
||||
// Run status script, does not display output.
|
||||
if (!env.run_remote_template_command(service, "status", {}, true, {}))
|
||||
return HealthStatus::UNHEALTHY;
|
||||
return HealthStatus::HEALTHY;
|
||||
}
|
||||
|
||||
std::string healthtick(const std::string &server, const std::string &service)
|
||||
{
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
std::string yellow_exclamation = "\033[33m!\033[0m";
|
||||
std::string unknown = "\033[37m✓\033[0m";
|
||||
|
||||
HealthStatus status = is_healthy(server, service);
|
||||
if (status == HealthStatus::HEALTHY)
|
||||
return green_tick;
|
||||
else if (status == HealthStatus::UNHEALTHY)
|
||||
return red_cross;
|
||||
else if (status == HealthStatus::UNKNOWN)
|
||||
return unknown;
|
||||
else
|
||||
return yellow_exclamation;
|
||||
}
|
||||
|
||||
std::string HealthStatus2String(HealthStatus status)
|
||||
{
|
||||
if (status == HealthStatus::HEALTHY)
|
||||
return ":tick:";
|
||||
else if (status == HealthStatus::UNHEALTHY)
|
||||
return ":cross:";
|
||||
else if (status == HealthStatus::UNKNOWN)
|
||||
return ":greytick:";
|
||||
else if (status == HealthStatus::NOTINSTALLED)
|
||||
return ":warning:";
|
||||
else
|
||||
return ":error:";
|
||||
}
|
||||
|
||||
std::string healthmark(const std::string &server, const std::string &service)
|
||||
{
|
||||
HealthStatus status = is_healthy(server, service);
|
||||
return HealthStatus2String(status);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// health command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int health_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{
|
||||
std::cerr << "Error: Server name is required" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1) {
|
||||
// get all services on server
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
transwarp::parallel exec{services.size()};
|
||||
auto task = transwarp::for_each(exec, services.begin(), services.end(), [&](const LocalServiceInfo& service) {
|
||||
std::string status = healthtick(server, service.service_name);
|
||||
std::cout << status << " " << service.service_name << " (" << service.template_name << ")" << std::endl << std::flush;
|
||||
});
|
||||
task->wait();
|
||||
return 0;
|
||||
} else {
|
||||
// get service status
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
std::cout << healthtick(server, service) << " " << service << " (" << service_info.template_name << ")" << std::endl << std::flush;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
178
source/src/commands/help.cpp
Normal file
178
source/src/commands/help.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
void help_autocomplete(const CommandContext& ctx);
|
||||
int help_handler(const CommandContext& ctx);
|
||||
|
||||
static std::vector<std::string> help_name_list={"help","h","--help","-h"};
|
||||
|
||||
// Static registration
|
||||
struct HelpCommandRegister {
|
||||
HelpCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
help_name_list,
|
||||
help_handler,
|
||||
help_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
1, // max_args (after command)
|
||||
"help [COMMAND]",
|
||||
"Show help for dropshell, or detailed help for a specific command.",
|
||||
// heredoc
|
||||
R"(
|
||||
Show help for dropshell, or detailed help for a specific command.
|
||||
If you want to see documentation, please visit the DropShell website:
|
||||
https://dropshell.app
|
||||
)"
|
||||
});
|
||||
}
|
||||
} help_command_register;
|
||||
|
||||
|
||||
void help_autocomplete(const CommandContext& ctx) {
|
||||
if (ctx.args.size() == 1) {
|
||||
// list all commands
|
||||
for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) {
|
||||
std::cout << cmd << std::endl;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void show_command(const std::string& cmd) {
|
||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
if (!cmd_info)
|
||||
std::cout << "Unknown command: " << cmd << std::endl;
|
||||
|
||||
std::cout << " ";
|
||||
print_left_aligned(cmd_info->help_usage, 30);
|
||||
std::cout << cmd_info->help_description << std::endl;
|
||||
}
|
||||
|
||||
extern const std::string VERSION;
|
||||
extern const std::string RELEASE_DATE;
|
||||
extern const std::string AUTHOR;
|
||||
extern const std::string LICENSE;
|
||||
|
||||
|
||||
int show_command_help(const std::string& cmd) {
|
||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
if (!cmd_info)
|
||||
{
|
||||
std::cout << "Unknown command: " << cmd << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "Usage: " << std::endl;
|
||||
std::cout << " ";
|
||||
print_left_aligned(cmd_info->help_usage, 30);
|
||||
std::cout << cmd_info->help_description << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << " Equivalent names: ";
|
||||
bool first = true;
|
||||
for (const auto& name : cmd_info->names) {
|
||||
if (!first) std::cout << ", ";
|
||||
std::cout << name;
|
||||
first = false;
|
||||
}
|
||||
std::cout << std::endl << std::endl;
|
||||
|
||||
std::cout << cmd_info->full_help << std::endl << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int help_handler(const CommandContext& ctx) {
|
||||
|
||||
if (ctx.args.size() > 0)
|
||||
return show_command_help(ctx.args[0]);
|
||||
|
||||
std::cout << std::endl;
|
||||
maketitle("DropShell version " + VERSION);
|
||||
std::cout << std::endl;
|
||||
std::cout << "A tool for managing remote servers, by " << AUTHOR << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "dropshell ..." << std::endl;
|
||||
|
||||
show_command("help");
|
||||
show_command("edit");
|
||||
|
||||
if (gConfig().is_config_set())
|
||||
{
|
||||
// show more!
|
||||
show_command("list");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// void show_command(const std::string& cmd) {
|
||||
// const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
// if (cmd_info) {
|
||||
// std::cout << " " << cmd_info->help_usage
|
||||
// << std::string(' ', std::min(1,(int)(30-cmd_info->help_usage.length())))
|
||||
// << cmd_info->help_description << std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
// bool print_help() {
|
||||
// std::cout << std::endl;
|
||||
// maketitle("DropShell version " + VERSION);
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "A tool for managing server configurations" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "dropshell ..." << std::endl;
|
||||
// show_command("help");
|
||||
// show_command("edit");
|
||||
|
||||
// if (gConfig().is_config_set()) {
|
||||
// std::cout << " templates List all available templates" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl;
|
||||
// std::cout << " list [SERVER] [SERVICE] List status/details of all servers/server/service." << std::endl;
|
||||
// std::cout << " edit [SERVER] [SERVICE] Edit the configuration of dropshell/server/service." << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Safe/non-destructive." << std::endl;
|
||||
// std::cout << " uninstall SERVER [SERVICE] Uninstalls the service on the remote server. Leaves data intact." << std::endl;
|
||||
// std::cout << " nuke SERVER SERVICE Nuke the service, deleting ALL local and remote data." << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s), e.g." << std::endl;
|
||||
// std::cout << " backup, restore, start, stop, logs" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " ssh SERVER SERVICE Launch an interactive shell on a server or service" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Creation commands: (apply to the first local config directory)"<<std::endl;
|
||||
// std::cout << " create-template TEMPLATE" << std::endl;
|
||||
// std::cout << " create-server SERVER" << std::endl;
|
||||
// std::cout << " create-service SERVER TEMPLATE SERVICE" << std::endl;
|
||||
// }
|
||||
// else {
|
||||
// show_command("help");
|
||||
// show_command("edit");
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Other commands available once initialised." << std::endl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
381
source/src/commands/install.cpp
Normal file
381
source/src/commands/install.cpp
Normal file
@ -0,0 +1,381 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
#include "autogen/_agent.hpp"
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int install_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> install_name_list = {"install", "reinstall", "update"};
|
||||
|
||||
// Static registration
|
||||
struct InstallCommandRegister
|
||||
{
|
||||
InstallCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({install_name_list,
|
||||
install_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"install SERVER [SERVICE]",
|
||||
"Install/reinstall service(s). Safe/non-destructive way to update.",
|
||||
// heredoc
|
||||
R"(
|
||||
Install components on a server. This is safe to re-run (non-destructive) and used to update
|
||||
servers and their services.
|
||||
|
||||
install (re)install dropshell components on this computer.
|
||||
install SERVER (re)install dropshell agent on the given server.
|
||||
install SERVER [SERVICE|*] (re)install the given service (or all services) on the given server.
|
||||
|
||||
Note you need to create the service first with:
|
||||
dropshell create-service <server> <template> <service>
|
||||
)"});
|
||||
}
|
||||
} install_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// rsync_tree_to_remote : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool rsync_tree_to_remote(
|
||||
const std::string &local_path,
|
||||
const std::string &remote_path,
|
||||
server_env_manager &server_env,
|
||||
bool silent)
|
||||
{
|
||||
ASSERT(!local_path.empty() && !remote_path.empty(), "Local or remote path not specified. Can't rsync.");
|
||||
|
||||
std::string rsync_cmd = "rsync --delete --mkpath -zrpc -e 'ssh -p " + server_env.get_SSH_PORT() + "' " +
|
||||
quote(local_path + "/") + " " +
|
||||
quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
||||
remote_path + "/");
|
||||
return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// install service over ssh : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool install_service(const std::string &server, const std::string &service, bool silent)
|
||||
{
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
return false;
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
return false;
|
||||
|
||||
maketitle("Installing " + service + " (" + service_info.template_name + ") on " + server);
|
||||
|
||||
if (!server_env.is_valid())
|
||||
return false; // should never hit this.
|
||||
|
||||
// Check if template exists
|
||||
template_info tinfo = gTemplateManager().get_template_info(service_info.template_name);
|
||||
if (!tinfo.is_set())
|
||||
return false;
|
||||
|
||||
// Create service directory
|
||||
std::string remote_service_path = remotepath::service(server, service);
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Silent))
|
||||
{
|
||||
std::cerr << "Failed to create service directory " << remote_service_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if rsync is installed on remote host
|
||||
std::string check_rsync_cmd = "which rsync";
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", check_rsync_cmd, {}), cMode::Silent))
|
||||
{
|
||||
std::cerr << "rsync is not installed on the remote host" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy template files
|
||||
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_template(server, service) << "/" << std::endl;
|
||||
if (!rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(server, service),
|
||||
server_env, silent))
|
||||
{
|
||||
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy service files
|
||||
std::cout << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_config(server, service) << std::endl;
|
||||
if (!rsync_tree_to_remote(localpath::service(server, service), remotepath::service_config(server, service),
|
||||
server_env, silent))
|
||||
{
|
||||
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Run install script
|
||||
{
|
||||
server_env.run_remote_template_command(service, "install", {}, silent, {});
|
||||
}
|
||||
|
||||
// print health tick
|
||||
std::cout << "Health: " << healthtick(server, service) << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// get_arch : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
std::string get_arch()
|
||||
{
|
||||
// determine the architecture of the system
|
||||
std::string arch;
|
||||
#ifdef __aarch64__
|
||||
arch = "arm64";
|
||||
#elif __x86_64__
|
||||
arch = "amd64";
|
||||
#endif
|
||||
return arch;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// update_dropshell
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
std::string _exec(const char *cmd)
|
||||
{
|
||||
char buffer[128];
|
||||
std::string result = "";
|
||||
FILE *pipe = popen(cmd, "r");
|
||||
if (!pipe)
|
||||
{
|
||||
throw std::runtime_error("popen() failed!");
|
||||
}
|
||||
while (!feof(pipe))
|
||||
{
|
||||
if (fgets(buffer, 128, pipe) != nullptr)
|
||||
result += buffer;
|
||||
}
|
||||
pclose(pipe);
|
||||
return trim(result);
|
||||
}
|
||||
|
||||
int update_dropshell()
|
||||
{
|
||||
// determine path to this executable
|
||||
std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe");
|
||||
std::filesystem::path parent_path = dropshell_path.parent_path();
|
||||
|
||||
// determine the architecture of the system
|
||||
std::string arch = get_arch();
|
||||
|
||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||
|
||||
// download new version, preserve permissions and ownership
|
||||
std::string bash_script;
|
||||
bash_script += "docker run --rm -v " + parent_path.string() + ":/target";
|
||||
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
||||
bash_script += " sh -c \"";
|
||||
bash_script += " curl -fsSL " + url + " -o /target/dropshell_temp &&";
|
||||
bash_script += " chmod --reference=/target/dropshell /target/dropshell_temp &&";
|
||||
bash_script += " chown --reference=/target/dropshell /target/dropshell_temp";
|
||||
bash_script += "\"";
|
||||
|
||||
std::string cmd = "bash -c '" + bash_script + "'";
|
||||
int rval = system(cmd.c_str());
|
||||
if (rval != 0)
|
||||
{
|
||||
std::cerr << "Failed to download new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check if the new version is the same as the old version
|
||||
uint64_t new_hash = hash_file(parent_path / "dropshell_temp");
|
||||
uint64_t old_hash = hash_file(parent_path / "dropshell");
|
||||
if (new_hash == old_hash)
|
||||
{
|
||||
std::cout << "Confirmed dropshell is the latest version." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string runvercmd = (parent_path / "dropshell").string() + " version";
|
||||
std::string currentver = _exec(runvercmd.c_str());
|
||||
runvercmd = (parent_path / "dropshell_temp").string() + " version";
|
||||
std::string newver = _exec(runvercmd.c_str());
|
||||
|
||||
if (currentver >= newver)
|
||||
{
|
||||
std::cout << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl;
|
||||
std::cout << "No update needed." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
std::string bash_script_2 = "docker run --rm -v " + parent_path.string() + ":/target gitea.jde.nz/public/debian-curl:latest " +
|
||||
"sh -c \"mv /target/dropshell_temp /target/dropshell\"";
|
||||
rval = system(bash_script_2.c_str());
|
||||
if (rval != 0)
|
||||
{
|
||||
std::cerr << "Failed to install new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Successfully updated " << dropshell_path << " to the latest " << arch << " version." << std::endl;
|
||||
|
||||
// execute the new version
|
||||
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
|
||||
std::cerr << "Failed to execute new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int install_local_agent()
|
||||
{
|
||||
std::vector<std::filesystem::path> paths = {
|
||||
gConfig().get_local_template_cache_path(),
|
||||
gConfig().get_local_backup_path(),
|
||||
gConfig().get_local_tempfiles_path(),
|
||||
localpath::agent()};
|
||||
for (auto &p : gConfig().get_local_server_definition_paths())
|
||||
paths.push_back(p);
|
||||
|
||||
for (auto &p : paths)
|
||||
if (!std::filesystem::exists(p))
|
||||
{
|
||||
std::cout << "Creating directory: " << p << std::endl;
|
||||
std::filesystem::create_directories(p);
|
||||
}
|
||||
|
||||
// download bb64.
|
||||
if (!std::filesystem::exists(localpath::agent() + "bb64"))
|
||||
{
|
||||
std::string cmd = "cd " + localpath::agent() + " && curl -fsSL -o bb64 https://gitea.jde.nz/public/bb64/releases/download/latest/bb64.amd64 && chmod a+x bb64";
|
||||
int ret = system(cmd.c_str());
|
||||
if (EXITSTATUSCHECK(ret))
|
||||
std::cout << "Downloaded bb64 to " << localpath::agent() << std::endl;
|
||||
else
|
||||
std::cerr << "Failed to download bb64 to " << localpath::agent() << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Updating bb64..." << std::endl;
|
||||
system((localpath::agent() + "bb64 -u").c_str()); // update.
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Creating local agent files..." << std::endl;
|
||||
recreate_agent::recreate_tree(localpath::agent());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_host()
|
||||
{
|
||||
// update dropshell.
|
||||
// install the local dropshell agent.
|
||||
|
||||
int rval = update_dropshell();
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
||||
rval = install_local_agent();
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
||||
std::cout << "Installation complete." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_server(const std::string &server)
|
||||
{
|
||||
// install the dropshell agent on the given server.
|
||||
std::cout << "Installing dropshell agent on " << server << std::endl;
|
||||
|
||||
std::string agent_path = remotepath::agent(server);
|
||||
if (agent_path.empty())
|
||||
{
|
||||
std::cerr << "Failed to get agent path for " << server << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
std::cerr << "Invalid server environment for " << server << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// first install bb64.
|
||||
std::cout << "Installing bb64 on " << server << std::endl
|
||||
<< std::flush;
|
||||
std::string remote_cmd =
|
||||
"ssh -p " + server_env.get_SSH_INFO().port + " " + server_env.get_SSH_INFO().user + "@" + server_env.get_SSH_INFO().host +
|
||||
" 'mkdir -p " + quote(agent_path) + " && curl -fsSL \"https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh\" | bash -s -- " +
|
||||
quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'";
|
||||
|
||||
std::cout << "Executing: " << remote_cmd << std::endl;
|
||||
if (!execute_local_command(remote_cmd, nullptr, cMode::Silent))
|
||||
std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl;
|
||||
else
|
||||
std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl;
|
||||
|
||||
// now create the agent.
|
||||
// copy across from the local agent files.
|
||||
#pragma message("TODO: copy across from the local agent files.")
|
||||
|
||||
return 0; // NOTIMPL
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// install command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int install_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{ // install host
|
||||
return install_host();
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1)
|
||||
{ // install server
|
||||
return install_server(server);
|
||||
}
|
||||
|
||||
// install service(s)
|
||||
if (safearg(ctx.args, 1) == "*")
|
||||
{
|
||||
// install all services on the server
|
||||
bool okay = true;
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!install_service(server, service.service_name, false))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
else
|
||||
{ // install the specific service.
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return install_service(server, service, false) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
184
source/src/commands/list.cpp
Normal file
184
source/src/commands/list.cpp
Normal file
@ -0,0 +1,184 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "tableprint.hpp"
|
||||
#include "transwarp.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int list_handler(const CommandContext& ctx);
|
||||
void show_server_details(const std::string& server_name);
|
||||
void list_servers();
|
||||
|
||||
static std::vector<std::string> list_name_list={"list","ls","info","-l"};
|
||||
|
||||
// Static registration
|
||||
struct ListCommandRegister {
|
||||
ListCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
list_name_list,
|
||||
list_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"list [SERVER] [SERVICE]",
|
||||
"List server or service information and status",
|
||||
// heredoc
|
||||
R"(
|
||||
List details for servers and services controller by dropshell.
|
||||
list list all servers.
|
||||
list server list all services for the given server.
|
||||
list server service list the given service details on the given server.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} list_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// list command handler
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int list_handler(const CommandContext& ctx) {
|
||||
if (ctx.args.size() == 0) {
|
||||
list_servers();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx.args.size() == 1) {
|
||||
show_server_details(ctx.args[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "List handler called with " << ctx.args.size() << " args\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// https://github.com/bloomen/transwarp?tab=readme-ov-file#range-functions
|
||||
void list_servers() {
|
||||
auto servers = get_configured_servers();
|
||||
|
||||
if (servers.empty()) {
|
||||
std::cout << "No servers found" << std::endl;
|
||||
std::cout << "Please run 'dropshell edit' to set up dropshell." << std::endl;
|
||||
std::cout << "Then run 'dropshell create-server' to create a server." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
tableprint tp("All DropShell Servers");
|
||||
tp.add_row({"Name", "User", "Address", "Health", "Ports"});
|
||||
|
||||
std::cout << "Checking "<<servers.size() << " servers: " << std::flush;
|
||||
int checked = 0;
|
||||
|
||||
transwarp::parallel exec{servers.size()};
|
||||
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const ServerInfo& server) {
|
||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server.name);
|
||||
|
||||
std::set<int> ports_used;
|
||||
std::string serviceticks = "";
|
||||
for (const auto& [service_name, service_status] : status) {
|
||||
ports_used.insert(service_status.ports.begin(), service_status.ports.end());
|
||||
serviceticks += HealthStatus2String(service_status.health) + " ";
|
||||
}
|
||||
std::string ports_used_str = "";
|
||||
for (const auto& port : ports_used)
|
||||
ports_used_str += std::to_string(port) + " ";
|
||||
|
||||
tp.add_row({server.name, server.ssh_user, server.ssh_host, serviceticks, ports_used_str});
|
||||
++checked;
|
||||
// print out a tick character for each server checked.
|
||||
std::cout << checked << " ✓ " << std::flush;
|
||||
});
|
||||
task->wait();
|
||||
std::cout << std::endl << std::endl;
|
||||
tp.print();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void show_server_details(const std::string& server_name) {
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
//---------------------
|
||||
// Check if server is reachable via SSH
|
||||
std::string ssh_address = env.get_SSH_HOST();
|
||||
std::string ssh_user = env.get_SSH_USER();
|
||||
std::string ssh_port = env.get_SSH_PORT();
|
||||
if (!ssh_address.empty()) {
|
||||
std::cout << std::endl << "Server Status:" << std::endl;
|
||||
std::cout << std::string(40, '-') << std::endl;
|
||||
|
||||
// Try to connect to the server
|
||||
std::string cmd = "ssh -o ConnectTimeout=5 " + ssh_user + "@" + ssh_address + " -p " + ssh_port + " 'echo connected' 2>/dev/null";
|
||||
int result = system(cmd.c_str());
|
||||
if (result == 0) {
|
||||
std::cout << "Status: Online" << std::endl;
|
||||
|
||||
// // Get uptime if possible
|
||||
// cmd = "ssh " + ssh_address + " 'uptime' 2>/dev/null";
|
||||
// int rval = system(cmd.c_str());
|
||||
// if (rval != 0) {
|
||||
// std::cout << "Error: Failed to get uptime" << std::endl;
|
||||
// }
|
||||
} else {
|
||||
std::cout << "Status: Offline" << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
//---------------------
|
||||
{
|
||||
std::cout << std::endl;
|
||||
tableprint tp("Server Configuration: " + server_name, true);
|
||||
tp.add_row({"Key", "Value"});
|
||||
for (const auto& [key, value] : env.get_variables()) {
|
||||
tp.add_row({key, value});
|
||||
}
|
||||
tp.print();
|
||||
}
|
||||
|
||||
//---------------------
|
||||
// list services, and run healthcheck on each
|
||||
{
|
||||
tableprint tp("Services: " + server_name, false);
|
||||
tp.add_row({"Status", "Service", "Ports"});
|
||||
|
||||
|
||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server_name);
|
||||
|
||||
std::set<int> ports_used;
|
||||
std::string serviceticks = "";
|
||||
for (const auto& [service_name, service_status] : status) {
|
||||
std::string healthy = HealthStatus2String(service_status.health);
|
||||
|
||||
std::string ports_str = "";
|
||||
for (const auto& port : service_status.ports)
|
||||
ports_str += std::to_string(port) + " ";
|
||||
|
||||
tp.add_row({healthy, service_name, ports_str});
|
||||
} // end of for (const auto& service : services)
|
||||
tp.print();
|
||||
} // end of list services
|
||||
} // end of show_server_details
|
||||
|
||||
|
||||
} // namespace dropshell
|
106
source/src/commands/nuke.cpp
Normal file
106
source/src/commands/nuke.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "config.hpp"
|
||||
#include "services.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
#pragma message ("TODO: Fix issues with Nuke below.")
|
||||
|
||||
/*
|
||||
|
||||
j@twelve:~/code/dropshell$ ds nuke localhost test-squashkiwi
|
||||
---------------------------------------
|
||||
| Nuking test-squashkiwi on localhost |
|
||||
---------------------------------------
|
||||
---------------------------------------------
|
||||
| Uninstalling test-squashkiwi on localhost |
|
||||
---------------------------------------------
|
||||
Service is not installed: test-squashkiwi
|
||||
bash: line 1: cd: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template: Permission denied
|
||||
bash: line 1: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template/nuke.sh: Permission denied
|
||||
Warning: Failed to run nuke script: test-squashkiwi
|
||||
|
||||
*/
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int nuke_handler(const CommandContext& ctx);
|
||||
static std::vector<std::string> nuke_name_list={"nuke"};
|
||||
|
||||
// Static registration
|
||||
struct NukeCommandRegister {
|
||||
NukeCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
nuke_name_list,
|
||||
nuke_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
2, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"nuke SERVER [SERVICE|*] ",
|
||||
"Nuke a service on a server. Destroys everything, both local and remote!",
|
||||
// heredoc
|
||||
R"(
|
||||
Nuke a service on a server. Destroys everything, both local and remote!
|
||||
nuke SERVER SERVICE nuke the given service on the given server.
|
||||
nuke SERVER * nuke all services on the given server.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} nuke_command_register;
|
||||
|
||||
int nuke_handler(const CommandContext &ctx)
|
||||
{
|
||||
ASSERT(ctx.args.size() > 1, "Usage: nuke <server> <service>");
|
||||
ASSERT(gConfig().is_config_set(), "No configuration found. Please run 'dropshell config' to set up your configuration.");
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
|
||||
maketitle("Nuking " + service + " on " + server);
|
||||
|
||||
server_env_manager server_env(server);
|
||||
LocalServiceInfo service_info;
|
||||
|
||||
if (server_env.is_valid())
|
||||
{
|
||||
service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
std::cerr << "Warning: Invalid service: " << service << std::endl;
|
||||
|
||||
if (!uninstall_service(server, service, false))
|
||||
std::cerr << "Warning: Failed to uninstall service: " << service << std::endl;
|
||||
|
||||
// run the nuke script on the remote server if it exists.
|
||||
if (gTemplateManager().template_command_exists(service_info.template_name, "nuke"))
|
||||
{
|
||||
if (!server_env.run_remote_template_command(service, "nuke", {}, false, {}))
|
||||
std::cerr << "Warning: Failed to run nuke script: " << service << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
std::cerr << "Warning: Invalid server: " << server << std::endl;
|
||||
|
||||
// remove the local service directory
|
||||
std::string local_service_path = service_info.local_service_path;
|
||||
if (local_service_path.empty() || !std::filesystem::exists(local_service_path))
|
||||
{
|
||||
std::cerr << "Warning: Local service directory not found: " << local_service_path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto ret = std::filesystem::remove_all(local_service_path);
|
||||
if (ret != 0)
|
||||
std::cerr << "Warning: Failed to remove local service directory" << std::endl;
|
||||
|
||||
return ret == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
31
source/src/commands/shared_commands.hpp
Normal file
31
source/src/commands/shared_commands.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef SHARED_COMMANDS_HPP
|
||||
#define SHARED_COMMANDS_HPP
|
||||
|
||||
#include "servers.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// defined in install.cpp
|
||||
bool rsync_tree_to_remote(
|
||||
const std::string &local_path,
|
||||
const std::string &remote_path,
|
||||
server_env_manager &server_env,
|
||||
bool silent);
|
||||
|
||||
// defined in install.cpp
|
||||
bool install_service(const std::string& server, const std::string& service, bool silent);
|
||||
bool uninstall_service(const std::string& server, const std::string& service, bool silent);
|
||||
std::string get_arch();
|
||||
|
||||
// defined in health.cpp
|
||||
std::string healthtick(const std::string& server, const std::string& service);
|
||||
std::string HealthStatus2String(HealthStatus status);
|
||||
|
||||
// defined in standard_autocomplete.cpp
|
||||
void std_autocomplete(const CommandContext& ctx);
|
||||
void std_autocomplete_allowallservices(const CommandContext& ctx);
|
||||
|
||||
|
||||
} // namespace dropshell
|
||||
#endif
|
36
source/src/commands/standard_autocomplete.cpp
Normal file
36
source/src/commands/standard_autocomplete.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "shared_commands.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
#include "servers.hpp"
|
||||
#include "services.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
void std_autocomplete(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() == 0) { // just the command, no args yet.
|
||||
// list servers
|
||||
std::vector<ServerInfo> servers = get_configured_servers();
|
||||
for (const auto& server : servers) {
|
||||
std::cout << server.name << std::endl;
|
||||
}
|
||||
}
|
||||
else if (ctx.args.size() == 1) {
|
||||
// list services
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(ctx.args[0]);
|
||||
for (const auto& service : services) {
|
||||
std::cout << service.service_name << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void std_autocomplete_allowallservices(const CommandContext &ctx)
|
||||
{
|
||||
std_autocomplete(ctx);
|
||||
if (ctx.args.size() == 1)
|
||||
std::cout << "*" << std::endl;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
108
source/src/commands/uninstall.cpp
Normal file
108
source/src/commands/uninstall.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int uninstall_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> uninstall_name_list = {"uninstall", "remove"};
|
||||
|
||||
// Static registration
|
||||
struct UninstallCommandRegister
|
||||
{
|
||||
UninstallCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({uninstall_name_list,
|
||||
uninstall_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
1, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"uninstall SERVER [SERVICE|*]",
|
||||
"Uninstall a service on a server. Does not remove configuration or user data.",
|
||||
// heredoc
|
||||
R"(
|
||||
Uninstall a service on a server. Does not remove configuration or user data.
|
||||
uninstall SERVER SERVICE uninstall the given service on the given server.
|
||||
uninstall SERVER * uninstall all services on the given server.
|
||||
)"});
|
||||
}
|
||||
} uninstall_command_register;
|
||||
|
||||
int uninstall_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{
|
||||
std::cerr << "Error: uninstall requires a server and (optionally) a service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1)
|
||||
{
|
||||
// uninstall all services on the server
|
||||
bool okay = true;
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!uninstall_service(server, service.service_name, false))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return uninstall_service(server, service, false) ? 0 : 1;
|
||||
}
|
||||
|
||||
bool uninstall_service(const std::string &server, const std::string &service, bool silent)
|
||||
{
|
||||
if (!silent)
|
||||
maketitle("Uninstalling " + service + " on " + server);
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
std::cerr << "Invalid server: " << server << std::endl;
|
||||
return false; // should never hit this.
|
||||
}
|
||||
|
||||
// 2. Check if service directory exists on server
|
||||
if (!server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
{
|
||||
std::cerr << "Service is not installed: " << service << std::endl;
|
||||
return true; // Nothing to uninstall
|
||||
}
|
||||
|
||||
// 3. Run uninstall script if it exists
|
||||
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
|
||||
if (gTemplateManager().template_command_exists(service, "uninstall"))
|
||||
if (server_env.check_remote_file_exists(uninstall_script))
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
|
||||
if (!silent)
|
||||
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
|
||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), silent))
|
||||
{
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||
if (!silent)
|
||||
std::cout << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
||||
}
|
||||
else if (!silent)
|
||||
std::cerr << "Warning: Failed to remove remote service directory" << std::endl;
|
||||
|
||||
if (!silent)
|
||||
std::cout << "Completed service " << service << " uninstall on " << server << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
45
source/src/commands/version.cpp
Normal file
45
source/src/commands/version.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "version.hpp"
|
||||
namespace dropshell {
|
||||
|
||||
int version_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> version_name_list = {"version","v","ver","-v","-ver","--version"};
|
||||
|
||||
void version_autocomplete(const CommandContext &ctx)
|
||||
{
|
||||
}
|
||||
|
||||
// Static registration
|
||||
struct VersionCommandRegister
|
||||
{
|
||||
VersionCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({version_name_list,
|
||||
version_handler,
|
||||
version_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
0, // max_args (after command)
|
||||
"version",
|
||||
"Uninstall a service on a server. Does not remove configuration or user data.",
|
||||
// heredoc
|
||||
R"(
|
||||
Uninstall a service on a server. Does not remove configuration or user data.
|
||||
uninstall <server> <service> uninstall the given service on the given server.
|
||||
uninstall <server> uninstall all services on the given server.
|
||||
)"});
|
||||
}
|
||||
} version_command_register;
|
||||
|
||||
|
||||
int version_handler(const CommandContext &ctx)
|
||||
{
|
||||
std::cout << VERSION << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user