feat: Move install lifecycle to single remote script (ds_install.sh)
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 35s
Build-Test-Publish / build (linux/arm64) (push) Successful in 1m14s

This commit is contained in:
j
2026-03-21 21:21:55 +13:00
parent 1e06dc89bc
commit bf34bbbd8d
2 changed files with 180 additions and 54 deletions

View File

@@ -110,8 +110,8 @@ namespace dropshell
// Validate service.env matches template service.env
{
std::filesystem::path template_service_env = tinfo.local_template_service_env_path(); //tinfo.local_template_path() / "config" / "service.env";
std::filesystem::path template_info_env = tinfo.local_template_info_env_path(); //tinfo.local_template_path() / "config" / ".template_info.env";
std::filesystem::path template_service_env = tinfo.local_template_service_env_path();
std::filesystem::path template_info_env = tinfo.local_template_info_env_path();
std::string service_env_file = localfile::service_env(server, service);
std::vector<std::string> missing_vars;
@@ -144,72 +144,59 @@ namespace dropshell
}
}
// Run install-pre.sh if it exists in the local template cache (reduces downtime by e.g. pulling images before uninstall)
// Only sync the single script, NOT the full template — the old uninstall.sh must stay matching the installed version.
bool has_install_pre = std::filesystem::exists(tinfo.local_template_path() / "install-pre.sh");
if (has_install_pre && server_env.check_remote_dir_exists(remote_service_path, user))
{
info << "Running pre-install script to prepare for update..." << std::endl;
// ── Stage new template + config to _staging folder on remote ──
std::string staging_path = remote_service_path + "/_staging";
std::string staging_template = staging_path + "/template";
std::string staging_config = staging_path + "/config";
// Copy only install-pre.sh to the existing remote template directory
std::string local_script = (tinfo.local_template_path() / "install-pre.sh").string();
std::string remote_script = remotepath(server, user).service_template(service) + "/install-pre.sh";
if (!shared_commands::rsync_file_to_remote(local_script, remote_script, server_env, false, user))
{
warning << "Failed to copy install-pre.sh to remote, skipping pre-install" << std::endl;
}
else
{
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
if (!server_env.run_remote_template_command(service, "install-pre", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}}, NULL))
{
warning << "Pre-install script failed, continuing with install..." << std::endl;
}
}
}
info << "Syncing new template and config to staging..." << std::endl;
// Uninstall the old service (this removes the remote service directory)
if (server_env.check_remote_dir_exists(remote_service_path, user))
// Ensure staging directory exists
std::string mkdir_cmd = "mkdir -p " + quote(staging_path);
if (!execute_ssh_command(server_env.get_SSH_INFO(user), sCommand("", mkdir_cmd, {}), cMode::Silent))
{
info << "Service " << service << " is already installed on " << server << std::endl;
shared_commands::uninstall_service(server_env, service);
}
// Sync template and config files to remote
if (!shared_commands::rsync_service_config(server_env, service, false))
{
error << "Failed to sync service configuration to remote" << std::endl;
error << "Failed to create staging directory on remote" << std::endl;
return false;
}
// Run install script
// Sync template files to staging
if (!rsync_tree_to_remote(tinfo.local_template_path().string(), staging_template,
server_env, true, user))
{
info << "Running " << service_info.template_name << " install script on " << server << "..." << std::endl;
error << "Failed to sync template to staging" << std::endl;
return false;
}
// Check if install.sh exists on remote
std::string install_script_path = remotepath(server,user).service_template(service) + "/install.sh";
if (!server_env.check_remote_file_exists(install_script_path, user))
{
error << "Install script not found on remote server: " << install_script_path << std::endl;
error << "Make sure the template '" << service_info.template_name << "' contains an install.sh script" << std::endl;
return false;
}
// Sync config files to staging
if (!rsync_tree_to_remote(localpath::service(server, service), staging_config,
server_env, true, user))
{
error << "Failed to sync config to staging" << std::endl;
return false;
}
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
if (!server_env.run_remote_template_command(service, "install", {}, false, {{"TEMP_DIR", remote_temp_folder.path()}},NULL))
{
error << "Failed to run install script for service '" << service << "' on server '" << server << "'" << std::endl;
error << "Template: " << service_info.template_name << std::endl;
error << "Script path: " << install_script_path << std::endl;
error << "Check that the script is executable and has no syntax errors" << std::endl;
return false;
}
// ── Run ds_install.sh on remote (single SSH session) ──
info << "Running remote install..." << std::endl;
shared_commands::cRemoteTempFolder remote_temp_folder(server_env, user);
std::string ds_install = remotepath(server, user).agent() + "/ds_install.sh";
sCommand install_cmd("", ds_install + " " + requote(service) + " " + requote(remote_temp_folder.path()), {});
// Add agent hash for version checking
std::string agent_hash = get_local_agent_hash();
if (!agent_hash.empty())
install_cmd.add_env_var("AGENT_HASH", agent_hash);
if (!execute_ssh_command(server_env.get_SSH_INFO(user), install_cmd, cMode::Defaults, nullptr))
{
error << "Failed to install service '" << service << "' on server '" << server << "'" << std::endl;
error << "Template: " << service_info.template_name << std::endl;
return false;
}
// print health tick
info << "Health: " << shared_commands::healthtick(server, service) << std::endl;
return true;
}