diff --git a/include/proxysql_admin.h b/include/proxysql_admin.h index 925a246fc..71076607f 100644 --- a/include/proxysql_admin.h +++ b/include/proxysql_admin.h @@ -119,6 +119,8 @@ class ProxySQL_Admin { char * load_mysql_query_rules_to_runtime(); void save_mysql_query_rules_from_runtime(); + int save_config_to_cluster(char *config, char * type); + void load_admin_variables_to_runtime() { flush_admin_variables___database_to_runtime(admindb, true); } void save_admin_variables_from_runtime() { flush_admin_variables___runtime_to_database(admindb, true, true, false); } diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index c5278dea6..92383f86d 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -1,10 +1,13 @@ #include "proxysql.h" #include "cpp.h" +#include #include #include #include #include +#include +#include #include #include #include @@ -692,8 +695,6 @@ bool admin_handler_command_load_or_save(char *query_no_space, unsigned int query } - // Will add a new command here to push mysql servers runtime config to cluster. - // Was thinking of 'SAVE MYSQL SERVERS TO CLUSTER'. if ((query_no_space_length>19) && ( (!strncasecmp("SAVE MYSQL SERVERS ", query_no_space, 19)) || (!strncasecmp("LOAD MYSQL SERVERS ", query_no_space, 19))) ) { if ( @@ -3282,7 +3283,37 @@ void ProxySQL_Admin::load_mysql_servers_to_runtime() { bool ProxySQL_Admin::save_mysql_servers_to_cluster() { // TODO read runtime mysql servers config and pass it to external script for distribution // TODO return an error message instead of bool - return true; + char *config = (char *)"test config"; + char *type = (char *)"mysql_servers"; + int status_code = save_config_to_cluster(config, type); + return status_code == 0; +} + +// Runs external middleman passing it the given config and the config type. +int ProxySQL_Admin::save_config_to_cluster(char *config, char *type) { + pid_t pid = fork(); + if (pid == 0) { + // child + errno = 0; + int exec_status = execl("/usr/local/bin/middleman.sh", "/usr/local/bin/middleman.sh", config, type, (char *) 0); + if (exec_status == -1) { + proxy_error("Exec failed for config save script with errno: %d.\n", errno); + } + _exit(0); + } else if (pid < 0) { + // failed to fork + proxy_error("Failed to fork for saving config.\n"); + return -1; + } else { + // parent + int status_code; + waitpid(pid, &status_code, 0); + if (!WIFEXITED(status_code)) { + proxy_error("Config save script exited with an error: %d.\n", WEXITSTATUS(status_code)); + } + proxy_info("Config save script exited with status code: %d.\n", status_code); + return status_code; + } } char * ProxySQL_Admin::load_mysql_query_rules_to_runtime() {