Commit | Line | Data |
---|---|---|
b1be615b BS |
1 | /** @file |
2 | * @brief The updater class implementation. This class implements the updater logic. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
4545a371 BS |
10 | #include "updater.h" |
11 | ||
88a594e8 BS |
12 | #include <boost/foreach.hpp> |
13 | ||
8bca3c5d | 14 | using namespace std; |
527536b3 | 15 | |
b1be615b BS |
16 | /** |
17 | * Default constructor which initializes the member Conf. | |
18 | */ | |
4545a371 BS |
19 | Updater::Updater() |
20 | { | |
2e956a36 | 21 | // Initialize program wide Logger, at this point we don't know where to log and with which loglevel. |
88a594e8 | 22 | Logger::Ptr _log(new Logger); |
254bbf53 | 23 | Log = _log; |
4545a371 | 24 | |
254bbf53 | 25 | // initialize Config |
88a594e8 | 26 | Config::Ptr _config(new Config(Log)); |
254bbf53 | 27 | Conf = _config; |
4545a371 BS |
28 | } |
29 | ||
527536b3 | 30 | |
b1be615b BS |
31 | /** |
32 | * Default destructor. | |
33 | */ | |
4545a371 BS |
34 | Updater::~Updater() |
35 | { | |
36 | } | |
37 | ||
527536b3 | 38 | |
b1be615b | 39 | /** |
254bbf53 BS |
40 | * Parse the command line arguments and initialize corresponding options. |
41 | * @param argc Number command line arguments. | |
42 | * @param argv[] Array with arguments. | |
43 | * @return 0 if cmd options successfully parsed, 1 if usage or version. | |
38060291 | 44 | */ |
254bbf53 | 45 | int Updater::init_config_from_cmd(int argc, char *argv[]) |
38060291 BS |
46 | { |
47 | // Load the command line parameters | |
254bbf53 | 48 | if( Conf->parse_cmd_line( argc, argv ) != 0) |
667c672c | 49 | return -1; |
38060291 | 50 | |
59c8d63c BS |
51 | // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd. |
52 | init_log_facility(); | |
53 | ||
254bbf53 BS |
54 | // successful parsed |
55 | Log->print_cmd_parsed(); | |
38060291 BS |
56 | return 0; |
57 | } | |
58 | ||
59 | ||
60 | /** | |
254bbf53 BS |
61 | * Load the main config and the service definition files in config path. |
62 | * @return 0 if all is fine, | |
b1be615b | 63 | */ |
254bbf53 | 64 | int Updater::init_config_from_files() |
4545a371 | 65 | { |
254bbf53 | 66 | // Load the main and service config files in config path |
27baf279 | 67 | if ( Conf->load_config_from_files() != 0 ) |
667c672c | 68 | return -1; |
4545a371 | 69 | |
59c8d63c BS |
70 | // Re-init log facility, perhaps new config file options for logger are set. |
71 | // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught. | |
72 | init_log_facility(); | |
73 | ||
27baf279 BS |
74 | // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them. |
75 | if ( Conf->deserialize_services() != 0 ) | |
667c672c | 76 | return -1; |
27baf279 | 77 | |
254bbf53 | 78 | // successful loaded |
667c672c | 79 | Log->print_conf_files_parsed(); |
254bbf53 | 80 | return 0; |
4545a371 BS |
81 | } |
82 | ||
527536b3 | 83 | |
b1be615b | 84 | /** |
3434b35f BS |
85 | * Getter for member Config. |
86 | * @return Member Config. | |
87 | */ | |
b38684ce | 88 | Config::Ptr Updater::get_config() const |
3434b35f BS |
89 | { |
90 | return Conf; | |
91 | } | |
92 | ||
93 | ||
94 | /** | |
95 | * Getter for member Logger. | |
96 | * @return Member Logger. | |
97 | */ | |
b38684ce | 98 | Logger::Ptr Updater::get_logger() const |
3434b35f BS |
99 | { |
100 | return Log; | |
101 | } | |
102 | ||
103 | ||
104 | /** | |
c5675c01 BS |
105 | * Reloading the config. Delete all Service objects and then init new Service objects from config files. |
106 | */ | |
667c672c | 107 | int Updater::reload_config() |
c5675c01 | 108 | { |
27baf279 | 109 | // serialize all service objects |
667c672c BS |
110 | if ( Conf->serialize_services() != 0 ) |
111 | return -1; | |
27baf279 | 112 | |
8bca3c5d | 113 | // delete all service objects |
c5675c01 | 114 | Conf->delete_services(); |
8bca3c5d BS |
115 | |
116 | // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options. | |
117 | Conf->delete_variables_map(); | |
118 | ||
119 | // load only config files | |
667c672c BS |
120 | if ( init_config_from_files() != 0 ) |
121 | return -1; | |
122 | ||
123 | return 0; | |
8bca3c5d BS |
124 | } |
125 | ||
126 | ||
2bc1878a BS |
127 | /** |
128 | * Initialize the logging facility with loglevel and syslog. | |
129 | */ | |
8bca3c5d BS |
130 | void Updater::init_log_facility() |
131 | { | |
132 | Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog()); | |
133 | Log->print_init_log_facility(); | |
c5675c01 BS |
134 | } |
135 | ||
136 | ||
137 | /** | |
b1be615b BS |
138 | * Update all configured services. |
139 | */ | |
4545a371 BS |
140 | void Updater::update_services() |
141 | { | |
88a594e8 | 142 | list<Service::Ptr> services = this->Conf->get_services(); |
4545a371 BS |
143 | |
144 | string ip = "192.168.1.1"; | |
145 | ||
88a594e8 | 146 | BOOST_FOREACH(Service::Ptr &service, services ) |
4545a371 | 147 | { |
50d63110 | 148 | // TODO: only update if IP differs. |
4545a371 BS |
149 | service->update(ip); |
150 | } | |
b1be615b | 151 | } |