This commit is contained in:
Your Name 2025-04-21 16:12:06 +12:00
parent 7d1db266b0
commit bd27109a2b
5 changed files with 64 additions and 65 deletions

View File

@ -18,14 +18,14 @@ void print_help() {
std::cout << "Commands:" << std::endl; std::cout << "Commands:" << std::endl;
std::cout << " help Show this help message" << std::endl; std::cout << " help Show this help message" << std::endl;
std::cout << " version Show version information" << std::endl; std::cout << " version Show version information" << std::endl;
std::cout << " init DIR Initialize the user directory for server configurations" << std::endl; std::cout << " init DIR Set the local directory for all server configs and backups" << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << " servers List configured servers" << std::endl; std::cout << " servers Summary of all configured servers" << std::endl;
std::cout << " servers NAME Show details for specific server" << std::endl; std::cout << " servers NAME Show details for specific server" << std::endl;
std::cout << " templates List available templates" << std::endl; std::cout << " templates List all available templates" << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << " install SERVER SERVICE (Re)install a service on a server." << std::endl; std::cout << " install SERVER [SERVICE] (Re)install a specific service on a server." << std::endl;
std::cout << " backup SERVER SERVICE Backup a service on a server." << std::endl; std::cout << " backup SERVER [SERVICE] Backup a specific service on a server." << std::endl;
std::cout << " run SERVER SERVICE COMMAND Run a command on a specific service" << std::endl; std::cout << " run SERVER SERVICE COMMAND Run a command on a specific service" << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << "Examples:" << std::endl; std::cout << "Examples:" << std::endl;
@ -118,22 +118,33 @@ int main(int argc, char* argv[]) {
} }
if (cmd == "install") { if (cmd == "install") {
if (argc < 4) { if (argc < 3) {
std::cerr << "Error: install command requires server name and service name" << std::endl; std::cerr << "Error: install command requires server name and optionally service name" << std::endl;
return 1; return 1;
} }
std::string server_name = argv[2]; std::string server_name = argv[2];
std::string service_name = argv[3];
std::vector<std::string> servicelist;
dropshell::server_service service; if (argc <= 3) {
if (!service.init(server_name, service_name)) { servicelist = dropshell::get_server_services(server_name);
std::cerr << "Error: Failed to initialize service" << std::endl; if (servicelist.empty()) {
return 1; std::cerr << "Error: No services found for server " << server_name << std::endl;
} return 1;
}
if (!service.install()) { } else
std::cerr << "Error: Failed to install service" << std::endl; servicelist.push_back(argv[3]);
return 1;
for (const auto& service_name : servicelist) {
dropshell::server_service service;
if (!service.init(server_name, service_name)) {
std::cerr << "Error: Failed to initialize service" << std::endl;
return 1;
}
if (!service.install()) {
std::cerr << "Error: Failed to install service" << std::endl;
return 1;
}
} }
return 0; return 0;
} }
@ -161,22 +172,32 @@ int main(int argc, char* argv[]) {
} }
if (cmd == "backup") { if (cmd == "backup") {
if (argc < 4) { if (argc < 3) {
std::cerr << "Error: backup command requires server name and service name" << std::endl; std::cerr << "Error: backup command requires server name and optionally service name" << std::endl;
return 1; return 1;
} }
std::string server_name = argv[2]; std::string server_name = argv[2];
std::string service_name = argv[3];
std::vector<std::string> servicelist;
dropshell::server_service service; if (argc <= 3) {
if (!service.init(server_name, service_name)) { servicelist = dropshell::get_server_services(server_name);
std::cerr << "Error: Failed to initialize service" << std::endl; if (servicelist.empty()) {
return 1; std::cerr << "Error: No services found for server " << server_name << std::endl;
} return 1;
}
if (!service.backup()) { } else
std::cerr << "Backup failed." << std::endl; servicelist.push_back(argv[3]);
return 1;
for (const auto& service_name : servicelist) {
dropshell::server_service service;
if (!service.init(server_name, service_name)) {
std::cerr << "Error: Failed to initialize service" << std::endl;
return 1;
}
if (!service.backup()) {
std::cerr << "Backup failed." << std::endl;
return 1;
}
} }
return 0; return 0;
} }

View File

@ -131,7 +131,14 @@ bool server_service::execute_local_command(const std::string& command, const std
return okay; return okay;
} }
void server_service::maketitle(const std::string& title) const {
std::cout << std::string(title.length() + 4, '-') << std::endl;
std::cout << "| " << title << " |" << std::endl;
std::cout << std::string(title.length() + 4, '-') << std::endl;
}
bool server_service::install() { bool server_service::install() {
maketitle("Installing " + m_service_name + " on " + m_server_name);
if (!m_server_env) { if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl; std::cerr << "Error: Server service not initialized" << std::endl;
return false; return false;
@ -226,6 +233,8 @@ bool server_service::run_command(const std::string& command) {
} }
bool server_service::backup() { bool server_service::backup() {
maketitle("Backing up " + m_service_name + " on " + m_server_name);
if (!m_server_env) { if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl; std::cerr << "Error: Server service not initialized" << std::endl;
return false; return false;

View File

@ -66,6 +66,7 @@ class server_service {
bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const; bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const;
bool execute_ssh_command(const std::string& command, const std::string& error_msg) const; bool execute_ssh_command(const std::string& command, const std::string& error_msg) const;
bool execute_local_command(const std::string& command, const std::string& error_msg) const; bool execute_local_command(const std::string& command, const std::string& error_msg) const;
void maketitle(const std::string& title) const;
}; };
} // namespace dropshell } // namespace dropshell

View File

@ -33,8 +33,8 @@ fi
echo "Successfully pulled the docker image from the registry" echo "Successfully pulled the docker image from the registry"
# remove and restart, as the env may have changed. # remove and restart, as the env may have changed.
_stop_container $CONTAINER_NAME _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}"
_remove_container $CONTAINER_NAME _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}"
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}" create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
echo "Installation complete" echo "Installation complete"

View File

@ -1,32 +0,0 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_dockerhelper.sh"
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || exit 1
# check can pull image on remote host and exit if fails
if ! docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"; then
echo "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"
exit 1
fi
echo "Successfully pulled the docker image from the registry"
# stop the old container
_stop_container ${CONTAINER_NAME}
# remove the old container
grey_start
if ! docker rm "$CONTAINER_NAME"; then
echo "Failed to remove container $CONTAINER_NAME"
exit 1
fi
grey_end
echo "Successfully removed the old container"
# start the new container
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"