[48de515] | 1 | /* |
---|
[a7cce8d] | 2 | * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
[48de515] | 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 |
---|
[a7cce8d] | 6 | * Free Software Foundation; either version 3 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
[48de515] | 8 | */ |
---|
| 9 | |
---|
| 10 | #include "ogAdmServer.h" |
---|
| 11 | #include "dbi.h" |
---|
| 12 | #include "utils.h" |
---|
| 13 | #include "list.h" |
---|
| 14 | #include "rest.h" |
---|
| 15 | #include "client.h" |
---|
| 16 | #include "json.h" |
---|
| 17 | #include "schedule.h" |
---|
| 18 | #include "core.h" |
---|
[866b6c5] | 19 | #include "cfg.h" |
---|
[48de515] | 20 | #include <syslog.h> |
---|
[7500700] | 21 | #include <getopt.h> |
---|
| 22 | |
---|
| 23 | static struct option og_server_opts[] = { |
---|
| 24 | { "config-file", 1, 0, 'f' }, |
---|
| 25 | { NULL }, |
---|
| 26 | }; |
---|
[48de515] | 27 | |
---|
[96090fd] | 28 | #define OG_SERVER_CFG_JSON "/opt/opengnsys/cfg/ogserver.json" |
---|
[d89d6c6] | 29 | #define OG_SERVER_REPO_PATH "/opt/opengnsys/images" |
---|
[866b6c5] | 30 | |
---|
[3cea4bb] | 31 | struct og_server_cfg ogconfig = { |
---|
[d89d6c6] | 32 | .repo = { |
---|
| 33 | .dir = OG_SERVER_REPO_PATH, |
---|
| 34 | }, |
---|
| 35 | }; |
---|
[f068a79] | 36 | |
---|
[f3422f6] | 37 | time_t start_time; |
---|
| 38 | |
---|
[48de515] | 39 | int main(int argc, char *argv[]) |
---|
| 40 | { |
---|
[7500700] | 41 | char config_file[PATH_MAX + 1] = OG_SERVER_CFG_JSON; |
---|
[48de515] | 42 | struct ev_io ev_io_server_rest, ev_io_agent_rest; |
---|
[7500700] | 43 | int val; |
---|
[48de515] | 44 | |
---|
[f3422f6] | 45 | start_time = time(NULL); |
---|
| 46 | |
---|
[48de515] | 47 | og_loop = ev_default_loop(0); |
---|
| 48 | |
---|
| 49 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
---|
| 50 | exit(EXIT_FAILURE); |
---|
| 51 | |
---|
[1a406b2] | 52 | openlog("ogserver", LOG_PID, LOG_DAEMON); |
---|
[48de515] | 53 | |
---|
[7500700] | 54 | while (1) { |
---|
| 55 | val = getopt_long(argc, argv, "f:l:d:", og_server_opts, NULL); |
---|
| 56 | if (val < 0) |
---|
| 57 | break; |
---|
| 58 | |
---|
| 59 | switch (val) { |
---|
| 60 | case 'f': |
---|
| 61 | snprintf(config_file, sizeof(config_file), "%s", optarg); |
---|
| 62 | break; |
---|
| 63 | case 'l': |
---|
| 64 | case 'd': |
---|
| 65 | /* ignore, legacy options */ |
---|
| 66 | break; |
---|
| 67 | case '?': |
---|
| 68 | return EXIT_FAILURE; |
---|
| 69 | default: |
---|
| 70 | break; |
---|
| 71 | } |
---|
[48de515] | 72 | } |
---|
| 73 | |
---|
[3cea4bb] | 74 | if (parse_json_config(config_file, &ogconfig) < 0) |
---|
[7500700] | 75 | return EXIT_FAILURE; |
---|
| 76 | |
---|
[3cea4bb] | 77 | socket_rest = og_socket_server_init(ogconfig.rest.port); |
---|
[f6f1f18] | 78 | if (socket_rest < 0) { |
---|
| 79 | syslog(LOG_ERR, "Cannot open REST API server socket\n"); |
---|
[48de515] | 80 | exit(EXIT_FAILURE); |
---|
[f6f1f18] | 81 | } |
---|
[48de515] | 82 | |
---|
| 83 | ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ); |
---|
| 84 | ev_io_start(og_loop, &ev_io_server_rest); |
---|
| 85 | |
---|
| 86 | socket_agent_rest = og_socket_server_init("8889"); |
---|
[f6f1f18] | 87 | if (socket_agent_rest < 0) { |
---|
| 88 | syslog(LOG_ERR, "Cannot open ogClient server socket\n"); |
---|
[48de515] | 89 | exit(EXIT_FAILURE); |
---|
[f6f1f18] | 90 | } |
---|
[48de515] | 91 | |
---|
| 92 | ev_io_init(&ev_io_agent_rest, og_server_accept_cb, socket_agent_rest, EV_READ); |
---|
| 93 | ev_io_start(og_loop, &ev_io_agent_rest); |
---|
| 94 | |
---|
[7d74d42] | 95 | if (og_dbi_schema_update() < 0) |
---|
| 96 | exit(EXIT_FAILURE); |
---|
| 97 | |
---|
[f6f1f18] | 98 | if (og_dbi_schedule_get() < 0) { |
---|
| 99 | syslog(LOG_ERR, "Cannot connect to database\n"); |
---|
[48de515] | 100 | exit(EXIT_FAILURE); |
---|
[f6f1f18] | 101 | } |
---|
[48de515] | 102 | |
---|
| 103 | og_schedule_next(og_loop); |
---|
| 104 | |
---|
| 105 | syslog(LOG_INFO, "Waiting for connections\n"); |
---|
| 106 | |
---|
| 107 | while (1) |
---|
| 108 | ev_loop(og_loop, 0); |
---|
| 109 | |
---|
| 110 | exit(EXIT_SUCCESS); |
---|
| 111 | } |
---|