From b53e0f665429a817c50d76109f50e046e7feaebd Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 21 Apr 2025 20:34:48 +1200 Subject: [PATCH] Tidying --- src/dropshell-completion.bash | 2 +- src/server_service.cpp | 69 ++++++++++++++++++++++++++++++++++ src/server_service.hpp | 5 +++ src/servers.cpp | 20 ++++++++-- templates/squashkiwi/_ports.sh | 5 +++ templates/squashkiwi/start.sh | 7 ++++ templates/squashkiwi/stop.sh | 2 + 7 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 templates/squashkiwi/_ports.sh diff --git a/src/dropshell-completion.bash b/src/dropshell-completion.bash index fcc6434..fa51aa5 100755 --- a/src/dropshell-completion.bash +++ b/src/dropshell-completion.bash @@ -7,7 +7,7 @@ _dropshell_completions() { prev="${COMP_WORDS[COMP_CWORD-1]}" # List of main commands - opts="help version status servers templates autocomplete_list_servers autocomplete_list_services autocomplete_list_commands install backup" + opts="help version servers templates autocomplete_list_servers autocomplete_list_services autocomplete_list_commands install backup" # add all commands to opts local commands=($(dropshell autocomplete_list_commands)) diff --git a/src/server_service.cpp b/src/server_service.cpp index 401e08f..d37c69e 100644 --- a/src/server_service.cpp +++ b/src/server_service.cpp @@ -322,4 +322,73 @@ std::string server_service::healthtick() return red_cross; } +std::vector server_service::get_ports() +{ + std::vector ports; + if (!m_server_env) { + std::cerr << "Error: Server service not initialized" << std::endl; + return ports; + } + + std::string ssh_cmd = construct_ssh_cmd(); + std::string script_dir = get_script_dir(); + std::string script_path = script_dir + "/_ports.sh"; + + // Check if service directory exists + if (!check_service_dir_exists(ssh_cmd)) { + return ports; + } + + // Check if ports script exists + if (!check_remote_file_exists(ssh_cmd, script_path)) { + return ports; + } + + // Run the ports script and capture output + std::string run_cmd = "'cd " + script_dir + + " && /bin/bash " + script_path + " " + get_env_path() + "'"; + + // Create a temporary file to store the output + std::string temp_file = "/tmp/dropshell_ports_" + std::to_string(getpid()); + std::string full_cmd = ssh_cmd + run_cmd + " > " + temp_file; + + if (!execute_local_command(full_cmd, "Failed to run ports script")) { + return ports; + } + + // Read the output file + std::ifstream in(temp_file); + if (!in.is_open()) { + std::cerr << "Error: Failed to read ports output" << std::endl; + return ports; + } + + // Read the entire file content + std::string content((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + + // Clean up temporary file + std::remove(temp_file.c_str()); + + // Process the content + std::stringstream ss(content); + std::string token; + + // First split by commas + while (std::getline(ss, token, ',')) { + // Then split each comma-separated part by whitespace + std::stringstream token_ss(token); + std::string port_str; + while (token_ss >> port_str) { // This handles all whitespace including newlines + try { + int port = std::stoi(port_str); + ports.push_back(port); + } catch (const std::exception& e) { + std::cerr << "Warning: Invalid port number: " << port_str << std::endl; + } + } + } + + return ports; +} + } // namespace dropshell \ No newline at end of file diff --git a/src/server_service.hpp b/src/server_service.hpp index b4786b5..3266764 100644 --- a/src/server_service.hpp +++ b/src/server_service.hpp @@ -50,6 +50,11 @@ class server_service { // 2. return the output of the status.sh script bool is_healthy(); + // get the ports of the service + // 1. run _ports.sh on the server + // 2. return the output of the _ports.sh script, handling a port per line or comma separated. + std::vector get_ports(); + std::string healthtick(); private: diff --git a/src/servers.cpp b/src/servers.cpp index 3ae8961..802b82c 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -131,6 +131,7 @@ void show_server_details(const std::string& server_name) { //--------------------- // list services, and run healthcheck on each + std::cout << std::endl; std::cout << "Services: " << server_name << std::endl; std::cout << std::string(40, '-') << std::endl; @@ -140,13 +141,26 @@ void show_server_details(const std::string& server_name) { std::vector services = get_server_services(server_name); for (const auto& service : services) { bool healthy = false; + std::vector ports; server_service ss; if (ss.init(server_name, service)) + { if (ss.is_healthy()) healthy=true; - - std::cout << service << ": " << (healthy ? green_tick : red_cross) << std::endl; - } + ports = ss.get_ports(); + } + std::cout << " " << (healthy ? green_tick : red_cross) << " " << service << ", ports: "; + std::cout << "("; + bool first = true; + for (const auto& port : ports) { + if (!first) { + std::cout << ", "; + } + std::cout << port; + first = false; + } + std::cout << ")" << std::endl; + } } diff --git a/templates/squashkiwi/_ports.sh b/templates/squashkiwi/_ports.sh new file mode 100644 index 0000000..7290b43 --- /dev/null +++ b/templates/squashkiwi/_ports.sh @@ -0,0 +1,5 @@ +#!/bin/bash +source "$(dirname "$0")/_common.sh" +load_env "$1" || die "Failed to load environment variables" + +echo $HOST_PORT diff --git a/templates/squashkiwi/start.sh b/templates/squashkiwi/start.sh index 8ca6e3b..bcf98e7 100755 --- a/templates/squashkiwi/start.sh +++ b/templates/squashkiwi/start.sh @@ -13,3 +13,10 @@ DOCKER_RUN_CMD="docker run -d \ if ! create_and_start_container "$DOCKER_RUN_CMD"; then die "Failed to start container ${CONTAINER_NAME}" fi + +# Check if the container is running +if ! _is_container_running "$CONTAINER_NAME"; then + die "Container ${CONTAINER_NAME} is not running" +fi + +echo "Container ${CONTAINER_NAME} started" diff --git a/templates/squashkiwi/stop.sh b/templates/squashkiwi/stop.sh index 7a9ab85..c50b108 100755 --- a/templates/squashkiwi/stop.sh +++ b/templates/squashkiwi/stop.sh @@ -3,3 +3,5 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" + +echo "Container ${CONTAINER_NAME} stopped"