diff --git a/installer/sashimono-install.sh b/installer/sashimono-install.sh index c61d21d..7b09807 100755 --- a/installer/sashimono-install.sh +++ b/installer/sashimono-install.sh @@ -20,6 +20,7 @@ cgroupsuffix="-cg" registryuser="sashidockerreg" registryport=4444 script_dir=$(dirname "$(realpath "$0")") +def_cgrulesengd_service="cgrulesengdsvc" quiet=$1 [ -d $sashimono_bin ] && [ -n "$(ls -A $sashimono_bin)" ] && @@ -90,57 +91,13 @@ selfip=$(hostname -I) # [ "$?" == "1" ] && rollback # registry_addr=$selfip:$registryport -# Setting up cgroup rules. -! groupadd $group && echo "Group creation failed." && rollback -! echo "@$group cpu,memory %u$cgroupsuffix" >>/etc/cgrules.conf && echo "Cgroup rule creation failed." && rollback - # Setting up Sashimono admin group. ! groupadd $admin_group && echo "Admin group creation failed." && rollback # Setup Sashimono data dir. cp -r "$script_dir"/contract_template $sashimono_data -$sashimono_bin/sagent new $sashimono_data $selfip $registry_addr -# Install Sashimono Agent cgcreate service. -# This is a onshot service which runs only once. -echo "[Unit] -Description=Sashimono cgroup creation service. -After=network.target -[Service] -User=root -Group=root -Type=oneshot -ExecStart=$sashimono_bin/user-cgcreate.sh $sashimono_data -[Install] -WantedBy=multi-user.target" >/etc/systemd/system/$cgcreate_service.service - -# Install Sashimono Agent systemd service. -# StartLimitIntervalSec=0 to make unlimited retries. RestartSec=5 is to keep 5 second gap between restarts. -echo "[Unit] -Description=Running and monitoring sashimono agent. -After=network.target -StartLimitIntervalSec=0 -[Service] -User=root -Group=root -Type=simple -WorkingDirectory=$sashimono_bin -ExecStart=$sashimono_bin/sagent run $sashimono_data -Restart=on-failure -RestartSec=5 -[Install] -WantedBy=multi-user.target" >/etc/systemd/system/$sashimono_service.service - -systemctl daemon-reload -systemctl enable $cgcreate_service -systemctl start $cgcreate_service -systemctl enable $sashimono_service -systemctl start $sashimono_service -# Both of these services needed to be restarted if sa.cfg max instance resources are manually changed. - -# Setup xrpl message board. -echo "Installing Evernode xrpl message board..." -if [ "$quiet"=="-q" ]; then +if [ "$quiet" == "-q" ]; then # We are in the quiet mode. Hence we auto-generate an XRPL test account and token details for the host. # (This is done for testing purposes during development) @@ -148,7 +105,7 @@ if [ "$quiet"=="-q" ]; then xrpl_faucet_url="https://hooks-testnet.xrpl-labs.com/newcreds" hook_secret="sh77XLdVqt4tKwoHknkHijiEjenJb" func_url="https://func-hotpocket.azurewebsites.net/api/evrfaucet?code=pPUyV1q838ryrihA5NVlobVXj8ZGgn9HsQjGGjl6Vhgxlfha4/xCgQ==" - + # Generate new fauset account. echo "Generating XRP faucet account..." new_acc=$(curl -X POST $xrpl_faucet_url) @@ -197,12 +154,81 @@ else while [[ -z "$xrp_secret" ]]; do read -p "XRPL account secret? " xrp_secret >/etc/cgrules.conf && echo "Cgroup rule creation failed." && rollback +# Restart the service to apply the cgrules config. +echo "Restarting the $cgrulesengd_service.service." +systemctl restart $cgrulesengd_service || rollback + +# Install Sashimono Agent cgcreate service. +# This is a onshot service which runs only once. +echo "[Unit] +Description=Sashimono cgroup creation service. +After=network.target +[Service] +User=root +Group=root +Type=oneshot +ExecStart=$sashimono_bin/user-cgcreate.sh $sashimono_data +[Install] +WantedBy=multi-user.target" >/etc/systemd/system/$cgcreate_service.service + +# Install xrpl message board systemd service. +echo "Initiating the sashimono agent..." +# Rollback if 'sagent new' failed. +$sashimono_bin/sagent new $sashimono_data $cgrulesengd_service $selfip $registry_addr || rollback + +# Install Sashimono Agent systemd service. +# StartLimitIntervalSec=0 to make unlimited retries. RestartSec=5 is to keep 5 second gap between restarts. +echo "[Unit] +Description=Running and monitoring sashimono agent. +After=network.target +StartLimitIntervalSec=0 +[Service] +User=root +Group=root +Type=simple +WorkingDirectory=$sashimono_bin +ExecStart=$sashimono_bin/sagent run $sashimono_data +Restart=on-failure +RestartSec=5 +[Install] +WantedBy=multi-user.target" >/etc/systemd/system/$sashimono_service.service + +systemctl daemon-reload +systemctl enable $cgcreate_service +systemctl start $cgcreate_service +systemctl enable $sashimono_service +systemctl start $sashimono_service +# Both of these services needed to be restarted if sa.cfg max instance resources are manually changed. + +# Install xrpl message board systemd service. +echo "Installing Evernode xrpl message board..." + cp -r "$script_dir"/mb-xrpl $sashimono_bin (! echo "{\"host\":{\"location\":\"$location\",\"instanceSize\":\"$instance_size\"},\"xrpl\":{\"address\":\"$xrp_address\",\"secret\":\"$xrp_secret\",\"token\":\"$token\",\"hookAddress\":\"$hook_address\",\"regFeeHash\":\"\"}}" | jq . >$mb_xrpl_conf) && rollback -# Install xrpl message board systemd service. # StartLimitIntervalSec=0 to make unlimited retries. RestartSec=5 is to keep 5 second gap between restarts. echo "[Unit] Description=Running and monitoring evernode xrpl transactions. @@ -226,5 +252,4 @@ systemctl start $mb_xrpl_service echo "Installed Evernode xrpl message board." echo "Sashimono installed successfully." -echo "Please restart your cgroup rule generator service or reboot your server for changes to apply." exit 0 diff --git a/installer/sashimono-uninstall.sh b/installer/sashimono-uninstall.sh index c9824a7..67a1bb0 100755 --- a/installer/sashimono-uninstall.sh +++ b/installer/sashimono-uninstall.sh @@ -7,6 +7,7 @@ sashimono_bin=/usr/bin/sashimono-agent mb_xrpl_bin=$sashimono_bin/mb-xrpl docker_bin=$sashimono_bin/dockerbin sashimono_data=/etc/sashimono +sashimono_conf=$sashimono_data/sa.cfg mb_xrpl_data=$sashimono_data/mb-xrpl sashimono_service="sashimono-agent" cgcreate_service="sashimono-cgcreate" @@ -21,10 +22,14 @@ quiet=$1 if [ "$quiet" != "-q" ]; then echo "Are you sure you want to uninstall Sashimono?" - read -p "Type 'yes' to confirm uninstall: " confirmation < /dev/tty + read -p "Type 'yes' to confirm uninstall: " confirmation (); + } + catch (const std::exception &e) + { + print_missing_field_error(jpath, e); + return -1; + } + } + return 0; } @@ -365,6 +383,15 @@ namespace conf d.insert_or_assign("log", log_config); } + // Service configs. + { + jsoncons::ojson service_config; + + service_config.insert_or_assign("cgrulesengd", cfg.service.cgrulesengd); + + d.insert_or_assign("service", service_config); + } + return write_json_file(ctx.config_file, d); } diff --git a/src/conf.hpp b/src/conf.hpp index 3e132f7..f42231f 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -87,6 +87,11 @@ namespace conf std::unordered_map images; }; + struct service_config + { + std::string cgrulesengd; // Cgroup rule generator service. + }; + struct sa_config { std::string version; @@ -94,6 +99,7 @@ namespace conf system_config system; docker_config docker; log_config log; + service_config service; }; struct sa_context @@ -123,7 +129,7 @@ namespace conf int init(); - int create(std::string_view host_addr, std::string_view registry_addr); + int create(std::string_view cgrulesengd_service, std::string_view host_addr, std::string_view registry_addr); void set_dir_paths(std::string exepath, std::string datadir); diff --git a/src/main.cpp b/src/main.cpp index 7a6fef2..a21fa9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,15 +13,15 @@ #include "util/util.hpp" #include "killswitch/killswitch.h" -#define PARSE_ERROR \ - { \ - std::cerr << "Arguments mismatch.\n"; \ - std::cerr << "Usage:\n"; \ - std::cerr << "sagent version\n"; \ - std::cerr << "sagent new [data_dir] [host_addr] [registry_addr]\n"; \ - std::cerr << "sagent run [data_dir]\n"; \ - std::cerr << "Example: sagent run /etc/sashimono\n"; \ - return -1; \ +#define PARSE_ERROR \ + { \ + std::cerr << "Arguments mismatch.\n"; \ + std::cerr << "Usage:\n"; \ + std::cerr << "sagent version\n"; \ + std::cerr << "sagent new [data_dir] [cgrulesengd_service] [host_addr] [registry_addr]\n"; \ + std::cerr << "sagent run [data_dir]\n"; \ + std::cerr << "Example: sagent run /etc/sashimono\n"; \ + return -1; \ } /** @@ -36,7 +36,7 @@ int parse_cmd(int argc, char **argv) { conf::ctx.command = argv[1]; - if ((conf::ctx.command == "new" && argc >= 2 && argc <= 5) || + if ((conf::ctx.command == "new" && argc >= 2 && argc <= 6) || (conf::ctx.command == "run" && argc >= 2 && argc <= 3) || (conf::ctx.command == "version" && argc == 2)) return 0; @@ -107,7 +107,6 @@ int main(int argc, char **argv) signal(SIGSEGV, &segfault_handler); signal(SIGABRT, &segfault_handler); - // Disable SIGPIPE to avoid crashing on broken pipe IO. { sigset_t mask; @@ -130,9 +129,10 @@ int main(int argc, char **argv) conf::set_dir_paths(argv[0], (argc >= 3) ? argv[2] : ""); // This will create a new config. - const std::string host_addr = (argc >= 4) ? argv[3] : ""; - const std::string registry_addr = (argc >= 5) ? argv[4] : ""; - if (conf::create(host_addr, registry_addr) != 0) + const std::string cgrulesengd_service = (argc >= 4) ? argv[3] : ""; + const std::string host_addr = (argc >= 5) ? argv[4] : ""; + const std::string registry_addr = (argc >= 6) ? argv[5] : ""; + if (conf::create(cgrulesengd_service, host_addr, registry_addr) != 0) return -1; } else if (conf::ctx.command == "run")