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 | |
---|
22 | static 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 | |
---|
30 | struct og_server_cfg ogconfig = { |
---|
31 | .repo = { |
---|
32 | .dir = OG_SERVER_REPO_PATH, |
---|
33 | }, |
---|
34 | }; |
---|
35 | |
---|
36 | int 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_schedule_get() < 0) { |
---|
91 | syslog(LOG_ERR, "Cannot connect to database\n"); |
---|
92 | exit(EXIT_FAILURE); |
---|
93 | } |
---|
94 | |
---|
95 | og_schedule_next(og_loop); |
---|
96 | |
---|
97 | syslog(LOG_INFO, "Waiting for connections\n"); |
---|
98 | |
---|
99 | while (1) |
---|
100 | ev_loop(og_loop, 0); |
---|
101 | |
---|
102 | exit(EXIT_SUCCESS); |
---|
103 | } |
---|