#988 remove legacy configuration file

Add getopt parser and use json configuration file instead.
master
OpenGnSys Support Team 2020-10-02 15:21:36 +02:00
parent a67f27a092
commit 7500700777
5 changed files with 34 additions and 206 deletions

View File

@ -13,5 +13,8 @@
},
"wol" : {
"interface" : "lo"
},
"repository" : {
"directory" : "/opt/opengnsys/images"
}
}

View File

@ -17,6 +17,12 @@
#include "core.h"
#include "cfg.h"
#include <syslog.h>
#include <getopt.h>
static struct option og_server_opts[] = {
{ "config-file", 1, 0, 'f' },
{ NULL },
};
#define OG_SERVER_CFG_JSON "/opt/opengnsys/cfg/ogserver.json"
@ -24,7 +30,9 @@ struct og_server_cfg cfg;
int main(int argc, char *argv[])
{
char config_file[PATH_MAX + 1] = OG_SERVER_CFG_JSON;
struct ev_io ev_io_server_rest, ev_io_agent_rest;
int val;
og_loop = ev_default_loop(0);
@ -33,19 +41,32 @@ int main(int argc, char *argv[])
openlog("ogserver", LOG_PID, LOG_DAEMON);
if (!validacionParametros(argc, argv, 1)) // Valida parámetros de ejecución
exit(EXIT_FAILURE);
while (1) {
val = getopt_long(argc, argv, "f:l:d:", og_server_opts, NULL);
if (val < 0)
break;
if (parse_json_config(OG_SERVER_CFG_JSON, &cfg) < 0) {
syslog(LOG_INFO, "Falling back to legacy configuration file at %s\n",
szPathFileCfg);
if (!tomaConfiguracion(szPathFileCfg))
exit(EXIT_FAILURE);
} else {
from_json_to_legacy(&cfg);
switch (val) {
case 'f':
snprintf(config_file, sizeof(config_file), "%s", optarg);
break;
case 'l':
case 'd':
/* ignore, legacy options */
break;
case '?':
return EXIT_FAILURE;
default:
break;
}
}
socket_rest = og_socket_server_init("8888");
if (parse_json_config(config_file, &cfg) < 0)
return EXIT_FAILURE;
from_json_to_legacy(&cfg);
socket_rest = og_socket_server_init(cfg.rest.port);
if (socket_rest < 0) {
syslog(LOG_ERR, "Cannot open REST API server socket\n");
exit(EXIT_FAILURE);

View File

@ -15,99 +15,6 @@
#include <sys/socket.h>
#include "ogAdmLib.h"
char szPathFileCfg[4096],szPathFileLog[4096];
int ndebug;
//______________________________________________________________________________________________________
// Función: ValidacionParametros
//
// Descripción:
// Valida que los parametros de ejecución del programa sean correctos
// Parámetros:
// - argc: Número de argumentos
// - argv: Puntero a cada argumento
// - eje: Tipo de ejecutable (1=Servicio,2=Repositorio o 3=Cliente)
// Devuelve:
// - TRUE si los argumentos pasados son correctos
// - FALSE en caso contrario
// Especificaciones:
// La sintaxis de los argumentos es la siguiente
// -f Archivo de configuración del servicio
// -l Archivo de logs
// -d Nivel de debuger (mensages que se escribirán en el archivo de logs)
// Devuelve:
// TRUE: Si el proceso es correcto
// FALSE: En caso de ocurrir algún error
//______________________________________________________________________________________________________
BOOLEAN validacionParametros(int argc, char*argv[],int eje) {
int i;
switch(eje){
case 1: // Administrador
strcpy(szPathFileCfg, "ogserver.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogserver.log"); // de configuración y de logs
break;
case 2: // Repositorio
strcpy(szPathFileCfg, "ogAdmRepo.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmRepo.log"); // de configuración y de logs
break;
case 3: // Cliente OpenGnsys
strcpy(szPathFileCfg, "ogAdmClient.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmClient.log"); // de configuración y de logs
break;
case 4: // Servicios DHCP,BOOTP Y TFTP
strcpy(szPathFileCfg, "ogAdmBoot.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmBoot.log"); // de configuración y de logs
break;
case 5: // Agente
strcpy(szPathFileCfg, "ogAdmAgent.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmAgent.log"); // de configuración y de logs
break;
case 6: // Agente
strcpy(szPathFileCfg, "ogAdmWinClient.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmWinClient.log"); // de configuración y de logs
break;
case 7: // Agente
strcpy(szPathFileCfg, "ogAdmnxClient.cfg"); // Valores por defecto de archivos
strcpy(szPathFileLog, "ogAdmLnxClient.log"); // de configuración y de logs
break;
}
ndebug = 1; // Nivel de debuger por defecto
for (i = 1; (i + 1) < argc; i += 2) {
if (argv[i][0] == '-') {
switch (tolower(argv[i][1])) {
case 'f':
if (argv[i + 1] != NULL)
strcpy(szPathFileCfg, argv[i + 1]);
else {
return (FALSE);
}
break;
case 'l':
if (argv[i + 1] != NULL)
strcpy(szPathFileLog, argv[i + 1]);
else {
return (FALSE);
}
break;
case 'd':
if (argv[i + 1] != NULL) {
ndebug = atoi(argv[i + 1]);
if (ndebug < 1)
ndebug = 1; // Por defecto el nivel de debug es 1
} else
ndebug = 1; // Por defecto el nivel de debug es 1
break;
default:
exit(EXIT_FAILURE);
break;
}
}
}
return (TRUE);
}
// ________________________________________________________________________________________________________
// Función: splitCadena
//

View File

@ -73,9 +73,6 @@ typedef void* LPVOID;
#define TRUE 1
#define FALSE 0
extern char szPathFileCfg[4096],szPathFileLog[4096];
extern int ndebug; // Nivel de debuger
typedef struct{ // Estructura de las tramas
char arroba; // Caracter arroba siempre
char identificador[14]; // Identificador de la trama, siempre JMMLCAMDJ_MCDJ
@ -86,7 +83,6 @@ typedef struct{ // Estructura de las tramas
// ________________________________________________________________________________________________________
// Prototipo de funciones
// ________________________________________________________________________________________________________
BOOLEAN validacionParametros(int,char**,int);
int splitCadena(char **,char *, char);
char* StrToUpper(char *);
void FINCADaINTRO(TRAMA*);
@ -104,7 +100,3 @@ char* escaparCadena(char *cadena);
#define container_of(ptr, type, member) ({ \
typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#include <stdbool.h>
bool tomaConfiguracion(const char *filecfg);

View File

@ -41,101 +41,6 @@ struct og_dbi_config dbi_config = {
.database = catalog,
};
//________________________________________________________________________________________________________
// Función: tomaConfiguracion
//
// Descripción:
// Lee el fichero de configuración del servicio
// Parámetros:
// filecfg : Ruta completa al fichero de configuración
// Devuelve:
// true: Si el proceso es correcto
// false: En caso de ocurrir algún error
//________________________________________________________________________________________________________
bool tomaConfiguracion(const char *filecfg)
{
char buf[1024], *line;
char *key, *value;
FILE *fcfg;
if (filecfg == NULL || strlen(filecfg) == 0) {
syslog(LOG_ERR, "No configuration file has been specified\n");
return false;
}
fcfg = fopen(filecfg, "rt");
if (fcfg == NULL) {
syslog(LOG_ERR, "Cannot open configuration file `%s'\n",
filecfg);
return false;
}
servidoradm[0] = '\0'; //inicializar variables globales
line = fgets(buf, sizeof(buf), fcfg);
while (line != NULL) {
const char *delim = "=";
line[strlen(line) - 1] = '\0';
key = strtok(line, delim);
value = strtok(NULL, delim);
if (!strcmp(str_toupper(key), "SERVIDORADM"))
snprintf(servidoradm, sizeof(servidoradm), "%s", value);
else if (!strcmp(str_toupper(key), "PUERTO"))
snprintf(puerto, sizeof(puerto), "%s", value);
else if (!strcmp(str_toupper(key), "USUARIO"))
snprintf(usuario, sizeof(usuario), "%s", value);
else if (!strcmp(str_toupper(key), "PASSWORD"))
snprintf(pasguor, sizeof(pasguor), "%s", value);
else if (!strcmp(str_toupper(key), "DATASOURCE"))
snprintf(datasource, sizeof(datasource), "%s", value);
else if (!strcmp(str_toupper(key), "CATALOG"))
snprintf(catalog, sizeof(catalog), "%s", value);
else if (!strcmp(str_toupper(key), "INTERFACE"))
snprintf(interface, sizeof(interface), "%s", value);
else if (!strcmp(str_toupper(key), "APITOKEN"))
snprintf(auth_token, sizeof(auth_token), "%s", value);
line = fgets(buf, sizeof(buf), fcfg);
}
/* Default value to preserve legacy config file support */
snprintf(db_port, sizeof(db_port), "3306");
fclose(fcfg);
if (!servidoradm[0]) {
syslog(LOG_ERR, "Missing SERVIDORADM in configuration file\n");
return false;
}
if (!puerto[0]) {
syslog(LOG_ERR, "Missing PUERTO in configuration file\n");
return false;
}
if (!usuario[0]) {
syslog(LOG_ERR, "Missing USUARIO in configuration file\n");
return false;
}
if (!pasguor[0]) {
syslog(LOG_ERR, "Missing PASSWORD in configuration file\n");
return false;
}
if (!datasource[0]) {
syslog(LOG_ERR, "Missing DATASOURCE in configuration file\n");
return false;
}
if (!catalog[0]) {
syslog(LOG_ERR, "Missing CATALOG in configuration file\n");
return false;
}
if (!interface[0])
syslog(LOG_ERR, "Missing INTERFACE in configuration file\n");
return true;
}
#define OG_CMD_MAXLEN 64
// ________________________________________________________________________________________________________