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

This commit is contained in:
Your Name 2025-05-13 20:16:39 +12:00
parent 5201e92cb3
commit b260c9813e
23 changed files with 224 additions and 234 deletions

View File

@ -1,5 +1,6 @@
#include "command_registry.hpp"
#include "utils/utils.hpp"
#include "config.hpp"
namespace dropshell {
@ -37,7 +38,13 @@ std::vector<std::string> CommandRegistry::list_primary_commands(bool include_hid
for (const auto& cmd : all_commands_) {
if (!cmd->hidden || include_hidden) {
if (cmd->names.size() > 0)
out.insert(cmd->names[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());

View File

@ -24,7 +24,8 @@ struct CommandInfo {
std::function<int(const CommandContext&)> handler;
std::function<void(const CommandContext&)> autocomplete; // optional
bool hidden = false;
bool requires_config = 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]

View File

@ -28,6 +28,7 @@ struct HelpCommandRegister {
help_autocomplete,
false, // hidden
false, // requires_config
false, // requires_install
0, // min_args (after command)
1, // max_args (after command)
"help [COMMAND]",

View File

@ -12,151 +12,178 @@
#include <filesystem>
#include <libassert/assert.hpp>
namespace dropshell {
namespace dropshell
{
int install_handler(const CommandContext& ctx);
int install_handler(const CommandContext &ctx);
static std::vector<std::string> install_name_list={"install"};
static std::vector<std::string> install_name_list = {"install"};
// Static registration
struct InstallCommandRegister {
InstallCommandRegister() {
CommandRegistry::instance().register_command({
install_name_list,
install_handler,
std_autocomplete,
false, // hidden
false, // requires_config
1, // min_args (after command)
2, // max_args (after command)
"install SERVER [SERVICE]",
"Install/reinstall/update service(s). Safe/non-destructive.",
// heredoc
R"(
Install a service(s) on a server.
install <server> <service> (re)install the given service on the given server.
install <server> (re)install all configured services on the given server.
// 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
1, // min_args (after command)
2, // max_args (after command)
"install SERVER [SERVICE]",
"Install/reinstall/update service(s). Safe/non-destructive.",
// 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());
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::None) );
}
// ------------------------------------------------------------------------------------------------
// 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;
}
// ------------------------------------------------------------------------------------------------
// install command implementation
// ------------------------------------------------------------------------------------------------
int install_handler(const CommandContext& ctx) {
if (ctx.args.size() < 1) {
std::cerr << "Error: install requires a server and (optionally) a service" << std::endl;
return 1;
}
std::string server = safearg(ctx.args,0);
if (ctx.args.size() == 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;
} 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());
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::None));
}
std::string service = safearg(ctx.args,1);
return install_service(server, service, false) ? 0 : 1;
}
// ------------------------------------------------------------------------------------------------
// 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;
}
int install_host()
{
// install the local dropshell agent.
}
int install_server(const std::string &server)
{
// install the dropshell agent on the given server.
}
// ------------------------------------------------------------------------------------------------
// 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

View File

@ -23,6 +23,7 @@ namespace dropshell {
// defined in standard_autocomplete.cpp
void std_autocomplete(const CommandContext& ctx);
void std_autocomplete_allowallservices(const CommandContext& ctx);
} // namespace dropshell
#endif

View File

@ -10,7 +10,7 @@ namespace dropshell {
void std_autocomplete(const CommandContext &ctx)
{
if (ctx.args.size() == 0) { // just the command health.
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) {
@ -26,4 +26,11 @@ void std_autocomplete(const CommandContext &ctx)
}
}
void std_autocomplete_allowallservices(const CommandContext &ctx)
{
std_autocomplete(ctx);
if (ctx.args.size() == 1)
std::cout << "*" << std::endl;
}
} // namespace dropshell

View File

@ -112,13 +112,17 @@ bool config::save_config(bool create_aux_directories)
return true;
}
bool config::is_config_set() const
{
return mIsConfigSet;
}
bool config::is_agent_installed()
{
return std::filesystem::exists(localpath::agent() + "/bb64");
}
std::string config::get_local_tempfiles_path() {
return mConfig["tempfiles"];
}

View File

@ -15,7 +15,7 @@ class config {
bool save_config(bool create_aux_directories);
bool is_config_set() const;
static bool is_agent_installed();
std::string get_local_tempfiles_path();
std::string get_local_backup_path();

View File

@ -60,6 +60,12 @@ int main(int argc, char* argv[]) {
std::cerr << "Please run 'dropshell edit' to set up the dropshell configuration." << std::endl;
return 1;
}
if (info->requires_install && !gConfig().is_agent_installed()) {
std::cerr << "Dropshell agent not installed for command: " << ctx.command << std::endl;
std::cerr << "Please run 'dropshell install' to install the local dropshell agent." << std::endl;
return 1;
}
int arg_count = ctx.args.size();
if (arg_count < info->min_args || (info->max_args != -1 && arg_count > info->max_args)) {
std::cerr << "Invalid number of arguments for command: " << ctx.command << std::endl;

View File

@ -63,8 +63,8 @@ namespace localpath {
return ((template_cache_path.empty() || service_name.empty()) ? "" :
(template_cache_path+"/remote_versions/"+service_name+".json"));
}
std::string local_bin(){
return current_user_home() + "/.local/bin";
std::string agent(){
return current_user_home() + "/.local/dropshell_agent";
}
std::string current_user_home(){
char * homedir = std::getenv("HOME");
@ -79,16 +79,20 @@ namespace localpath {
} // namespace localpath
// ------------------------------------------------------------------------------------------
// remote paths
// DROPSHELL_DIR
// |-- service name
// |-- config
// |-- service.env
// |-- (user config files)
// |-- template
// |-- (script files)
// |-- backups
// |-- backups
// |-- temp_files
// |-- agent
// |-- services
// |-- service name
// |-- config
// |-- service.env
// |-- template
// |-- (script files)
// |-- config
// |-- service.env
// |-- (other config files for specific server&service)
namespace remotefile {
@ -142,10 +146,10 @@ namespace remotepath {
return (dsp.empty() ? "" : (dsp + "/temp_files"));
}
std::string executables(const std::string &server_name)
std::string agent(const std::string &server_name)
{
std::string dsp = DROPSHELL_DIR(server_name);
return (dsp.empty() ? "" : (dsp + "/executables"));
return (dsp.empty() ? "" : (dsp + "/agent"));
}
std::string service_env(const std::string &server_name, const std::string &service_name)

View File

@ -56,7 +56,7 @@ namespace dropshell {
std::string remote_versions(const std::string &server_name, const std::string &service_name);
std::string local_bin();
std::string agent();
std::string current_user_home();
} // namespace local
@ -66,7 +66,7 @@ namespace dropshell {
// DROPSHELL_DIR
// |-- backups
// |-- temp_files
// |-- executables
// |-- agent
// |-- services
// |-- service name
// |-- config
@ -89,7 +89,7 @@ namespace dropshell {
std::string service_template(const std::string &server_name, const std::string &service_name);
std::string backups(const std::string &server_name);
std::string temp_files(const std::string &server_name);
std::string executables(const std::string &server_name);
std::string agent(const std::string &server_name);
} // namespace remotepath
//------------------------------------------------------------------------------------------------

View File

@ -122,11 +122,11 @@ namespace dropshell
ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "")
<< ssh_info.user << "@" << ssh_info.host;
std::string remote_executables_path = remotepath::executables(ssh_info.server_ID);
std::string remote_agent_path = remotepath::agent(ssh_info.server_ID);
bool rval = execute_local_command(
"", // directory to run in
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_executables_path), // local command to run
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path), // local command to run
{}, // environment variables
output, // output string
mode // mode
@ -137,23 +137,23 @@ namespace dropshell
std::cerr << std::endl
<< std::endl;
std::cerr << "Error: Failed to execute ssh command:" << std::endl;
std::cerr << "\033[90m" << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_executables_path) << "\033[0m" << std::endl;
std::cerr << "\033[90m" << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path) << "\033[0m" << std::endl;
std::cerr << std::endl
<< std::endl;
}
return rval;
}
std::string sCommand::makesafecmd(std::string executables_path, const std::string &command) const
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const
{
if (command.empty())
return "";
std::string encoded = base64_encode(dequote(trim(command)));
std::string commandstr = executables_path + "/bb64 " + encoded;
std::string commandstr = agent_path + "/bb64 " + encoded;
return commandstr;
}
std::string sCommand::construct_cmd(std::string executables_path) const
std::string sCommand::construct_cmd(std::string agent_path) const
{
if (mCmd.empty())
return "";
@ -170,7 +170,8 @@ namespace dropshell
cmdstr += mCmd;
cmdstr = makesafecmd(executables_path, cmdstr);
if (!agent_path.empty())
cmdstr = makesafecmd(agent_path, cmdstr);
return cmdstr;
}

View File

@ -53,10 +53,10 @@ class sCommand {
bool empty() const { return mCmd.empty(); }
std::string construct_cmd(std::string executables_path) const;
std::string construct_cmd(std::string agent_path) const;
private:
std::string makesafecmd(std::string executables_path, const std::string& command) const;
std::string makesafecmd(std::string agent_path, const std::string& command) const;
private:
std::string mDir;

View File

@ -1,3 +0,0 @@
DropShell agent.
Required for health checks etc.

View File

@ -1 +0,0 @@
#

View File

@ -1,4 +0,0 @@
# Template to use - always required!
TEMPLATE=dropshell-agent

View File

@ -1,60 +0,0 @@
#!/bin/bash
# INSTALL SCRIPT
# The install script is required for all templates.
# It is used to install the service on the server.
# It is called with the path to the server specific env file as an argument.
check_prerequisites() {
# prerequisites:
# - bash
# - curl
# - wget
# - docker
PREREQUISITES=("bash" "curl" "wget" "jq" "docker")
# check if all prerequisites are installed
for prerequisite in "${PREREQUISITES[@]}"; do
if ! command -v "${prerequisite}" &> /dev/null; then
echo "Prerequisite: ${prerequisite} is not installed."
exit 1
fi
done
}
install_bb64() {
# install bb64 into EXECUTABLES
_check_required_env_vars EXECUTABLES
ARCH=$(uname -m)
if [[ "$ARCH" == "x86_64" ]]; then
BIN=bb64.amd64
elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
BIN=bb64.arm64
else
echo "Unsupported architecture: $ARCH" >&2
exit 1
fi
# create the executables directory if it doesn't exist
mkdir -p ${EXECUTABLES}
# download bb64
if ! curl -fsSL https://gitea.jde.nz/public/bb64/releases/download/latest/bb64.${ARCH} -o ${EXECUTABLES}/bb64; then
echo "Failed to download bb64 for architecture: $ARCH" >&2
exit 1
fi
chmod +x ${EXECUTABLES}/bb64
}
check_prerequisites
# install bb64
install_bb64
exit 0

View File

@ -1 +0,0 @@
#!/bin/bash