Improving handling of config file

Added ProxySQL_ConfigFile::CloseFile()
Implemented LOAD MYSQL USERS FROM CONFIG (issue #232)
pull/248/head
René Cannaò 11 years ago
parent 022341548a
commit 02ac381aa1

@ -14,9 +14,10 @@ class ProxySQL_ConfigFile {
struct stat statbuf;
char *filename;
public:
Config cfg;
Config *cfg;
ProxySQL_ConfigFile();
bool OpenFile(const char *);
void CloseFile();
bool ReadGlobals();
bool configVariable(const char *, const char *, int &, int, int, int, int);
bool configVariable(const char *, const char *, int64_t &, int64_t, int64_t, int64_t, int64_t);

@ -217,9 +217,6 @@ class Standard_ProxySQL_Admin: public ProxySQL_Admin {
void delete_credentials(char *credentials);
#endif /* DEBUG */
void Read_Global_Variables_from_configfile(const char *prefix);
void Read_MySQL_Users_from_configfile();
void Read_MySQL_Servers_from_configfile();
public:
SQLite3DB *admindb; // in memory
@ -262,6 +259,12 @@ class Standard_ProxySQL_Admin: public ProxySQL_Admin {
void stats___mysql_query_rules();
void stats___mysql_commands_counters();
void Read_Global_Variables_from_configfile(const char *prefix);
void Read_MySQL_Users_from_configfile();
void Read_MySQL_Servers_from_configfile();
};
static Standard_ProxySQL_Admin *SPA=NULL;
@ -603,6 +606,30 @@ bool admin_handler_command_load_or_save(char *query_no_space, unsigned int query
return false;
}
if (
(query_no_space_length==strlen("LOAD MYSQL USERS FROM CONFIG") && !strncasecmp("LOAD MYSQL USERS FROM CONFIG",query_no_space, query_no_space_length))
) {
proxy_debug(PROXY_DEBUG_ADMIN, 4, "Received %s command\n", query_no_space);
if (GloVars.configfile_open) {
if (GloVars.confFile->OpenFile(NULL)==true) {
Standard_ProxySQL_Admin *SPA=(Standard_ProxySQL_Admin *)pa;
SPA->Read_MySQL_Users_from_configfile();
proxy_debug(PROXY_DEBUG_ADMIN, 4, "Loaded mysql users from CONFIG\n");
SPA->send_MySQL_OK(&sess->client_myds->myprot, NULL);
GloVars.confFile->CloseFile();
} else {
char *s=(char *)"Unable to open or parse config file %s";
char *m=(char *)malloc(strlen(s)+strlen(GloVars.config_file)+1);
sprintf(m,s,GloVars.config_file);
SPA->send_MySQL_ERR(&sess->client_myds->myprot, m);
free(m);
}
} else {
SPA->send_MySQL_ERR(&sess->client_myds->myprot, (char *)"Config file unknown");
}
return false;
}
if (
(query_no_space_length==strlen("SAVE MYSQL USERS TO MEMORY") && !strncasecmp("SAVE MYSQL USERS TO MEMORY",query_no_space, query_no_space_length))
||
@ -1494,11 +1521,13 @@ bool Standard_ProxySQL_Admin::init() {
flush_mysql_variables___database_to_runtime(admindb,true);
if (GloVars.__cmd_proxysql_reload || GloVars.__cmd_proxysql_initial) {
Read_MySQL_Servers_from_configfile();
Read_MySQL_Users_from_configfile();
Read_Global_Variables_from_configfile("admin");
Read_Global_Variables_from_configfile("mysql");
__insert_or_replace_disktable_select_maintable();
if (GloVars.configfile_open) {
Read_MySQL_Servers_from_configfile();
Read_MySQL_Users_from_configfile();
Read_Global_Variables_from_configfile("admin");
Read_Global_Variables_from_configfile("mysql");
__insert_or_replace_disktable_select_maintable();
}
}
@ -2571,7 +2600,7 @@ char * Standard_ProxySQL_Admin::load_mysql_query_rules_to_runtime() {
}
void Standard_ProxySQL_Admin::Read_Global_Variables_from_configfile(const char *prefix) {
const Setting& root = GloVars.confFile->cfg.getRoot();
const Setting& root = GloVars.confFile->cfg->getRoot();
char *groupname=(char *)malloc(strlen(prefix)+strlen((char *)"_variables")+1);
sprintf(groupname,"%s%s",prefix,"_variables");
if (root.exists(groupname)==false) {
@ -2610,7 +2639,7 @@ void Standard_ProxySQL_Admin::Read_Global_Variables_from_configfile(const char *
}
void Standard_ProxySQL_Admin::Read_MySQL_Users_from_configfile() {
const Setting& root = GloVars.confFile->cfg.getRoot();
const Setting& root = GloVars.confFile->cfg->getRoot();
if (root.exists("mysql_users")==false) return;
const Setting &mysql_users = root["mysql_users"];
int count = mysql_users.getLength();
@ -2640,7 +2669,7 @@ void Standard_ProxySQL_Admin::Read_MySQL_Users_from_configfile() {
}
void Standard_ProxySQL_Admin::Read_MySQL_Servers_from_configfile() {
const Setting& root = GloVars.confFile->cfg.getRoot();
const Setting& root = GloVars.confFile->cfg->getRoot();
if (root.exists("mysql_servers")==false) return;
const Setting &mysql_servers = root["mysql_servers"];
int count = mysql_servers.getLength();

@ -48,11 +48,16 @@ ProxySQL_ConfigFile::ProxySQL_ConfigFile() {
bool ProxySQL_ConfigFile::OpenFile(const char *__filename) {
filename=strdup(__filename);
cfg = new Config();
if (__filename) {
filename=strdup(__filename);
} else {
assert(filename);
}
if (FileUtils::isReadable(filename)==false) return false;
try
{
cfg.readFile(filename);
cfg->readFile(filename);
}
catch(const FileIOException &fioex)
{
@ -63,12 +68,19 @@ bool ProxySQL_ConfigFile::OpenFile(const char *__filename) {
{
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
exit(EXIT_FAILURE);
if (__filename) {
// exit with failure only if it is the first time it is opened
exit(EXIT_FAILURE);
}
return false;
}
return true;
};
void ProxySQL_ConfigFile::CloseFile() {
delete cfg;
}
bool ProxySQL_ConfigFile::ReadGlobals() {
/*
const Setting& root = cfg.getRoot();
@ -136,7 +148,7 @@ bool ProxySQL_ConfigFile::ReadGlobals() {
bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, int &variable, int defValue, int minValue, int maxValue, int multiplier) {
const Setting& root = cfg.getRoot();
const Setting& root = cfg->getRoot();
if (root.exists(group)==true) {
const Setting& mygroup=root[group];
if (mygroup.isGroup()==true) {
@ -175,7 +187,7 @@ bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, int
}
bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, int64_t &variable, int64_t defValue, int64_t minValue, int64_t maxValue, int64_t multiplier) {
const Setting& root = cfg.getRoot();
const Setting& root = cfg->getRoot();
if (root.exists(group)==true) {
const Setting& mygroup=root[group];
if (mygroup.isGroup()==true) {
@ -214,7 +226,7 @@ bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, int
}
bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, bool & variable, bool defValue) {
const Setting& root = cfg.getRoot();
const Setting& root = cfg->getRoot();
if (root.exists(group)==true) {
const Setting& mygroup=root[group];
if (mygroup.isGroup()==true) {
@ -243,7 +255,7 @@ bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, boo
}
bool ProxySQL_ConfigFile::configVariable(const char *group, const char *key, char **variable, const char *defValue) {
const Setting& root = cfg.getRoot();
const Setting& root = cfg->getRoot();
if (root.exists(group)==true) {
const Setting& mygroup=root[group];
if (mygroup.isGroup()==true) {

@ -249,7 +249,7 @@ int main(int argc, const char * argv[]) {
if (GloVars.__cmd_proxysql_datadir==NULL) {
// datadir was not specified , try to read config file
if (GloVars.configfile_open==true) {
const Setting& root = GloVars.confFile->cfg.getRoot();
const Setting& root = GloVars.confFile->cfg->getRoot();
if (root.exists("datadir")==true) {
// reading datadir from config file
std::string datadir;
@ -467,6 +467,9 @@ __start_label:
GloAdmin->init();
}
if (GloVars.configfile_open) {
GloVars.confFile->CloseFile();
}
GloMyAuth = create_MySQL_Authentication();
GloMyAuth->print_version();

Loading…
Cancel
Save