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 | ||
e0080b78 BS |
12 | #include "serviceholder.h" |
13 | #include "dhs.h" | |
14 | #include "ods.h" | |
15 | ||
88a594e8 BS |
16 | #include <boost/foreach.hpp> |
17 | ||
e0080b78 BS |
18 | |
19 | // Following boost macros are needed for serialization of derived classes through a base class pointer (Service *). | |
20 | BOOST_CLASS_EXPORT_GUID(ODS, "ODS") | |
21 | BOOST_CLASS_EXPORT_GUID(DHS, "DHS") | |
22 | ||
23 | namespace fs = boost::filesystem; | |
24 | ||
8bca3c5d | 25 | using namespace std; |
527536b3 | 26 | |
b1be615b BS |
27 | /** |
28 | * Default constructor which initializes the member Conf. | |
29 | */ | |
4545a371 | 30 | Updater::Updater() |
019dc0d9 | 31 | : IPHelp(new IPHelper) |
4545a371 | 32 | { |
2e956a36 | 33 | // Initialize program wide Logger, at this point we don't know where to log and with which loglevel. |
88a594e8 | 34 | Logger::Ptr _log(new Logger); |
254bbf53 | 35 | Log = _log; |
0665b239 | 36 | _log.reset(); |
4545a371 | 37 | |
e0080b78 BS |
38 | // initialize Serviceholder |
39 | Serviceholder::Ptr _serviceholder(new Serviceholder(Log)); | |
40 | ServiceHolder = _serviceholder; | |
41 | _serviceholder.reset(); | |
42 | ||
254bbf53 | 43 | // initialize Config |
e0080b78 | 44 | Config::Ptr _config(new Config(Log,ServiceHolder)); |
254bbf53 | 45 | Conf = _config; |
0665b239 | 46 | _config.reset(); |
4545a371 BS |
47 | } |
48 | ||
527536b3 | 49 | |
b1be615b BS |
50 | /** |
51 | * Default destructor. | |
52 | */ | |
4545a371 BS |
53 | Updater::~Updater() |
54 | { | |
55 | } | |
56 | ||
527536b3 | 57 | |
b1be615b | 58 | /** |
254bbf53 BS |
59 | * Parse the command line arguments and initialize corresponding options. |
60 | * @param argc Number command line arguments. | |
61 | * @param argv[] Array with arguments. | |
62 | * @return 0 if cmd options successfully parsed, 1 if usage or version. | |
38060291 | 63 | */ |
254bbf53 | 64 | int Updater::init_config_from_cmd(int argc, char *argv[]) |
38060291 BS |
65 | { |
66 | // Load the command line parameters | |
e0080b78 | 67 | if( Conf->parse_cmd_line( argc, argv) != 0) |
667c672c | 68 | return -1; |
38060291 | 69 | |
59c8d63c BS |
70 | // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd. |
71 | init_log_facility(); | |
72 | ||
254bbf53 BS |
73 | // successful parsed |
74 | Log->print_cmd_parsed(); | |
38060291 BS |
75 | return 0; |
76 | } | |
77 | ||
78 | ||
79 | /** | |
254bbf53 BS |
80 | * Load the main config and the service definition files in config path. |
81 | * @return 0 if all is fine, | |
b1be615b | 82 | */ |
254bbf53 | 83 | int Updater::init_config_from_files() |
4545a371 | 84 | { |
254bbf53 | 85 | // Load the main and service config files in config path |
27baf279 | 86 | if ( Conf->load_config_from_files() != 0 ) |
667c672c | 87 | return -1; |
4545a371 | 88 | |
59c8d63c BS |
89 | // Re-init log facility, perhaps new config file options for logger are set. |
90 | // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught. | |
91 | init_log_facility(); | |
92 | ||
27baf279 | 93 | // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them. |
e0080b78 | 94 | if ( ServiceHolder->deserialize_services() != 0 ) |
667c672c | 95 | return -1; |
27baf279 | 96 | |
254bbf53 | 97 | // successful loaded |
667c672c | 98 | Log->print_conf_files_parsed(); |
254bbf53 | 99 | return 0; |
4545a371 BS |
100 | } |
101 | ||
527536b3 | 102 | |
b1be615b | 103 | /** |
3434b35f BS |
104 | * Getter for member Config. |
105 | * @return Member Config. | |
106 | */ | |
b38684ce | 107 | Config::Ptr Updater::get_config() const |
3434b35f BS |
108 | { |
109 | return Conf; | |
110 | } | |
111 | ||
112 | ||
113 | /** | |
114 | * Getter for member Logger. | |
115 | * @return Member Logger. | |
116 | */ | |
b38684ce | 117 | Logger::Ptr Updater::get_logger() const |
3434b35f BS |
118 | { |
119 | return Log; | |
120 | } | |
121 | ||
122 | ||
123 | /** | |
0665b239 BS |
124 | * Init the IPHelp member with needed values. |
125 | * @return 0 if all is fine, -1 otherwise. | |
126 | */ | |
127 | int Updater::init_ip_helper() | |
128 | { | |
019dc0d9 BS |
129 | // initialize IPHelper |
130 | IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6())); | |
131 | IPHelp = _iphelp; | |
132 | _iphelp.reset(); | |
0665b239 BS |
133 | |
134 | return 0; | |
135 | } | |
136 | ||
137 | ||
138 | /** | |
c5675c01 BS |
139 | * Reloading the config. Delete all Service objects and then init new Service objects from config files. |
140 | */ | |
667c672c | 141 | int Updater::reload_config() |
c5675c01 | 142 | { |
27baf279 | 143 | // serialize all service objects |
e0080b78 | 144 | if ( ServiceHolder->serialize_services() != 0 ) |
667c672c | 145 | return -1; |
27baf279 | 146 | |
8bca3c5d | 147 | // delete all service objects |
e0080b78 | 148 | ServiceHolder->delete_services(); |
8bca3c5d BS |
149 | |
150 | // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options. | |
151 | Conf->delete_variables_map(); | |
152 | ||
153 | // load only config files | |
667c672c BS |
154 | if ( init_config_from_files() != 0 ) |
155 | return -1; | |
156 | ||
157 | return 0; | |
8bca3c5d BS |
158 | } |
159 | ||
160 | ||
2bc1878a BS |
161 | /** |
162 | * Initialize the logging facility with loglevel and syslog. | |
163 | */ | |
8bca3c5d BS |
164 | void Updater::init_log_facility() |
165 | { | |
166 | Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog()); | |
167 | Log->print_init_log_facility(); | |
c5675c01 BS |
168 | } |
169 | ||
170 | ||
171 | /** | |
b1be615b BS |
172 | * Update all configured services. |
173 | */ | |
4545a371 BS |
174 | void Updater::update_services() |
175 | { | |
e0080b78 | 176 | list<Service::Ptr> services = ServiceHolder->get_services(); |
4545a371 | 177 | |
0665b239 | 178 | string ip = IPHelp->get_actual_ip(); |
4545a371 | 179 | |
3c0cd271 | 180 | if ( !ip.empty() ) |
4545a371 | 181 | { |
3c0cd271 BS |
182 | BOOST_FOREACH(Service::Ptr &service, services ) |
183 | { | |
184 | service->update(ip); | |
185 | } | |
4545a371 | 186 | } |
b1be615b | 187 | } |
e0080b78 BS |
188 | |
189 | ||
190 | /** | |
191 | * Getter for member ServiceHolder. | |
192 | * @return ServiceHolder | |
193 | */ | |
194 | Serviceholder::Ptr Updater::get_service_holder() const | |
195 | { | |
196 | return ServiceHolder; | |
197 | } |