create-server
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled

This commit is contained in:
Your Name 2025-05-21 19:30:10 +12:00
parent fc6b310b89
commit 9063edb45f
4 changed files with 82 additions and 17 deletions

View File

@ -0,0 +1,63 @@
#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 create_server_autocomplete(const CommandContext& ctx);
int create_server_handler(const CommandContext& ctx);
static std::vector<std::string> create_server_name_list={"create-server"};
// Static registration
struct CreateServerCommandRegister {
CreateServerCommandRegister() {
CommandRegistry::instance().register_command({
create_server_name_list,
create_server_handler,
create_server_autocomplete,
false, // hidden
false, // requires_config
false, // requires_install
0, // min_args (after command)
1, // max_args (after command)
"create-server [SERVER]",
"Create a new server entry on this host.",
// heredoc
R"(
Create a new server entry on this host.
Note you will need to use ds install SERVER to prepare the service for use.
create-server SERVER
)"
});
}
} create_server_command_register;
void create_server_autocomplete(const CommandContext& ctx) {
return; // can't autocomplete as it's a new server!
}
int create_server_handler(const CommandContext& ctx) {
// create a new server entry on this host
if (ctx.args.size() == 0) {
error << "No server name provided" << std::endl;
return 1;
}
bool ok = create_server(ctx.args[0]);
return ok ? 0 : 1;
}
} // namespace dropshell

View File

@ -14,7 +14,7 @@ namespace dropshell
{ {
int nuke_handler(const CommandContext &ctx); int nuke_handler(const CommandContext &ctx);
static std::vector<std::string> nuke_name_list = {"nuke"}; static std::vector<std::string> nuke_name_list = {"destroy","nuke"};
// Static registration // Static registration
struct NukeCommandRegister struct NukeCommandRegister
@ -29,15 +29,15 @@ namespace dropshell
true, // requires_install true, // requires_install
2, // min_args (after command) 2, // min_args (after command)
2, // max_args (after command) 2, // max_args (after command)
"nuke SERVER SERVICE|all", "destroy SERVER SERVICE|all",
"Nuke a service on a server. Destroys everything, both local and remote!", "Destroy a service on a server. Erases everything, both local and remote!",
// heredoc // heredoc
R"( R"(
Nuke a service. Nuke a service.
Examples: Examples:
nuke SERVER SERVICE nuke the given service on the given server. destroy SERVER SERVICE destroy the given service on the given server.
nuke SERVER all nuke all services on the given server. destroy SERVER all destroy all services on the given server.
Note: This command is destructive and will destroy all data and all configuration, Note: This command is destructive and will destroy all data and all configuration,
both on the dropshell host and on the remote server. both on the dropshell host and on the remote server.

View File

@ -148,6 +148,7 @@ int help_handler(const CommandContext& ctx) {
info << std::endl; info << std::endl;
show_command("ssh"); show_command("ssh");
info << std::endl; info << std::endl;
show_command("create-server");
show_command("create-service"); show_command("create-service");
} }
return 0; return 0;

View File

@ -7,6 +7,7 @@
#include "config.hpp" #include "config.hpp"
#include "templates.hpp" #include "templates.hpp"
#include "contrib/transwarp.hpp" #include "contrib/transwarp.hpp"
#include "utils/output.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -76,16 +77,16 @@ bool create_server(const std::string &server_name)
// 1. check if server name already exists // 1. check if server name already exists
std::string server_existing_dir = localpath::server(server_name); std::string server_existing_dir = localpath::server(server_name);
if (!server_existing_dir.empty()) { if (!server_existing_dir.empty()) {
std::cerr << "Error: Server name already exists: " << server_name << std::endl; error << "Error: Server name already exists: " << server_name << std::endl;
std::cerr << "Current server path: " << server_existing_dir << std::endl; info << "Current server path: " << server_existing_dir << std::endl;
return false; return false;
} }
// 2. create a new directory in the user config directory // 2. create a new directory in the user config directory
auto lsdp = gConfig().get_local_server_definition_paths(); auto lsdp = gConfig().get_local_server_definition_paths();
if (lsdp.empty() || lsdp[0].empty()) { if (lsdp.empty() || lsdp[0].empty()) {
std::cerr << "Error: Local server definition path not found" << std::endl; error << "Error: Local server definition path not found" << std::endl;
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl; info << "Run 'dropshell edit' to configure DropShell" << std::endl;
return false; return false;
} }
std::string server_dir = lsdp[0] + "/" + server_name; std::string server_dir = lsdp[0] + "/" + server_name;
@ -93,20 +94,20 @@ bool create_server(const std::string &server_name)
// 3. create a template server.env file in the server directory // 3. create a template server.env file in the server directory
std::string user = getenv("USER"); std::string user = getenv("USER");
std::string server_env_path = server_dir + "/server.env"; std::string server_env_path = server_dir + "/server.json";
std::ofstream server_env_file(server_env_path); std::ofstream server_env_file(server_env_path);
server_env_file << "SSH_HOST=" << server_name << std::endl; server_env_file << "{" << std::endl;
server_env_file << "SSH_USER=" << user << std::endl; server_env_file << " \"SSH_HOST\": \"" << server_name << "\"," << std::endl;
server_env_file << "SSH_PORT=" << 22 << std::endl; server_env_file << " \"SSH_USER\": \"" << user << "\"," << std::endl;
server_env_file << std::endl; server_env_file << " \"SSH_PORT\": " << 22 << "," << std::endl;
server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl; server_env_file << " \"DROPSHELL_DIR\": \"" << "/home/"+user+"/.dropshell\"" << std::endl;
server_env_file << "}" << std::endl;
server_env_file.close(); server_env_file.close();
std::cout << "Server created successfully: " << server_name << std::endl; std::cout << "Server created successfully: " << server_name << std::endl;
std::cout << "Please complete the installation:" <<std::endl; std::cout << "Please complete the installation:" <<std::endl;
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl; std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;
std::cout << "2) test ssh is working: dropshell ssh " << server_name << std::endl; std::cout << "2) install the server: dropshell install " << server_name << std::endl;
std::cout << "3) install the server: dropshell install " << server_name << std::endl;
std::cout << std::endl; std::cout << std::endl;
return true; return true;
} }