source: ogServer-Git/src/main.c @ 79e7e2b

Last change on this file since 79e7e2b was 7d74d42, checked in by OpenGnSys Support Team <soporte-og@…>, 3 years ago

#1042 Update database schema automatically

This patch adds database schema management capabilities to ogServer:

  • ogServer now tracks the version of its database schema, if no version

is detected, creates a 'version' table with a single row starting at 0.

  • ogServer can upgrade its database schema to a newer version if

detected. (ogServer ships required SQL commands to do so)

If ogServer is unable to upgrade the schema at startup (if needed be) it
*will not* start.

Defines schema update v1 which upgrades database engine tables of
ogServer database (usually named 'ogAdmBD') from myISAM to innoDB.

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation, version 3.
7 */
8
9#include "ogAdmServer.h"
10#include "dbi.h"
11#include "utils.h"
12#include "list.h"
13#include "rest.h"
14#include "client.h"
15#include "json.h"
16#include "schedule.h"
17#include "core.h"
18#include "cfg.h"
19#include <syslog.h>
20#include <getopt.h>
21
22static struct option og_server_opts[] = {
23        { "config-file", 1, 0, 'f' },
24        { NULL },
25};
26
27#define OG_SERVER_CFG_JSON      "/opt/opengnsys/cfg/ogserver.json"
28#define OG_SERVER_REPO_PATH     "/opt/opengnsys/images"
29
30struct og_server_cfg ogconfig = {
31        .repo   = {
32                .dir    = OG_SERVER_REPO_PATH,
33        },
34};
35
36int main(int argc, char *argv[])
37{
38        char config_file[PATH_MAX + 1] = OG_SERVER_CFG_JSON;
39        struct ev_io ev_io_server_rest, ev_io_agent_rest;
40        int val;
41
42        og_loop = ev_default_loop(0);
43
44        if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
45                exit(EXIT_FAILURE);
46
47        openlog("ogserver", LOG_PID, LOG_DAEMON);
48
49        while (1) {
50                val = getopt_long(argc, argv, "f:l:d:", og_server_opts, NULL);
51                if (val < 0)
52                        break;
53
54                switch (val) {
55                case 'f':
56                        snprintf(config_file, sizeof(config_file), "%s", optarg);
57                        break;
58                case 'l':
59                case 'd':
60                        /* ignore, legacy options */
61                        break;
62                case '?':
63                        return EXIT_FAILURE;
64                default:
65                        break;
66                }
67        }
68
69        if (parse_json_config(config_file, &ogconfig) < 0)
70                return EXIT_FAILURE;
71
72        socket_rest = og_socket_server_init(ogconfig.rest.port);
73        if (socket_rest < 0) {
74                syslog(LOG_ERR, "Cannot open REST API server socket\n");
75                exit(EXIT_FAILURE);
76        }
77
78        ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ);
79        ev_io_start(og_loop, &ev_io_server_rest);
80
81        socket_agent_rest = og_socket_server_init("8889");
82        if (socket_agent_rest < 0) {
83                syslog(LOG_ERR, "Cannot open ogClient server socket\n");
84                exit(EXIT_FAILURE);
85        }
86
87        ev_io_init(&ev_io_agent_rest, og_server_accept_cb, socket_agent_rest, EV_READ);
88        ev_io_start(og_loop, &ev_io_agent_rest);
89
90        if (og_dbi_schema_update() < 0)
91                exit(EXIT_FAILURE);
92
93        if (og_dbi_schedule_get() < 0) {
94                syslog(LOG_ERR, "Cannot connect to database\n");
95                exit(EXIT_FAILURE);
96        }
97
98        og_schedule_next(og_loop);
99
100        syslog(LOG_INFO, "Waiting for connections\n");
101
102        while (1)
103                ev_loop(og_loop, 0);
104
105        exit(EXIT_SUCCESS);
106}
Note: See TracBrowser for help on using the repository browser.