0f95ae51e00f22cbca9d11057eaf489b2282e9da
1 /*
2 * SysDB - src/backend/puppet/store-configs.c
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/dbi.h"
32 #include "utils/error.h"
34 #include "liboconfig/utils.h"
36 #include <dbi/dbi.h>
38 #include <assert.h>
39 #include <errno.h>
41 #include <string.h>
42 #include <strings.h>
44 SDB_PLUGIN_MAGIC;
46 /*
47 * private helper functions
48 */
50 static int
51 sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
52 size_t n, sdb_data_t *data,
53 sdb_object_t __attribute__((unused)) *user_data)
54 {
55 const char *hostname;
56 sdb_time_t timestamp;
58 int status;
60 assert(n == 2);
61 assert((data[0].type == SDB_TYPE_STRING)
62 && (data[1].type == SDB_TYPE_DATETIME));
64 hostname = data[0].data.string;
65 timestamp = data[1].data.datetime;
67 status = sdb_store_host(hostname, timestamp);
69 if (status < 0) {
70 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
71 "store/update host '%s'.", hostname);
72 return -1;
73 }
74 else if (! status)
75 sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
76 "Added/updated host '%s' (last update timestamp = "
77 "%"PRIscTIME").", hostname, timestamp);
78 return 0;
79 } /* sdb_puppet_stcfg_get_hosts */
81 static int
82 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
83 size_t n, sdb_data_t *data,
84 sdb_object_t __attribute__((unused)) *user_data)
85 {
86 int status;
88 const char *hostname;
89 const char *key;
90 sdb_data_t value;
91 sdb_time_t last_update;
93 assert(n == 4);
94 assert((data[0].type == SDB_TYPE_STRING)
95 && (data[1].type == SDB_TYPE_STRING)
96 && (data[2].type == SDB_TYPE_STRING)
97 && (data[3].type == SDB_TYPE_DATETIME));
99 hostname = data[0].data.string;
100 key = data[1].data.string;
101 value.type = SDB_TYPE_STRING;
102 value.data.string = data[2].data.string;
103 last_update = data[3].data.datetime;
105 status = sdb_store_attribute(hostname, key, &value, last_update);
107 if (status < 0) {
108 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
109 "store/update host attribute '%s' for host '%s'.",
110 key, hostname);
111 return -1;
112 }
114 return 0;
115 } /* sdb_puppet_stcfg_get_attrs */
117 /*
118 * plugin API
119 */
121 static int
122 sdb_puppet_stcfg_init(sdb_object_t *user_data)
123 {
124 sdb_dbi_client_t *client;
126 if (! user_data)
127 return -1;
129 client = SDB_OBJ_WRAPPER(user_data)->data;
130 if (sdb_dbi_client_connect(client)) {
131 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
132 "Failed to connect to the storeconfigs DB.");
133 return -1;
134 }
136 sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
137 "connected to the storeconfigs DB.");
138 return 0;
139 } /* sdb_puppet_stcfg_init */
141 static int
142 sdb_puppet_stcfg_shutdown(sdb_object_t *user_data)
143 {
144 if (! user_data)
145 return -1;
147 sdb_dbi_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
148 SDB_OBJ_WRAPPER(user_data)->data = NULL;
149 return 0;
150 } /* sdb_puppet_stcfg_shutdown */
152 static int
153 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
154 {
155 sdb_dbi_client_t *client;
157 if (! user_data)
158 return -1;
160 client = SDB_OBJ_WRAPPER(user_data)->data;
161 if (sdb_dbi_client_check_conn(client)) {
162 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
163 "Connection to storeconfigs DB failed.");
164 return -1;
165 }
167 if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
168 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
169 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
170 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
171 "retrieve hosts from the storeconfigs DB.");
172 return -1;
173 }
175 if (sdb_dbi_exec_query(client, "SELECT "
176 "hosts.name AS hostname, "
177 "fact_names.name AS name, "
178 "fact_values.value AS value, "
179 "fact_values.updated_at AS updated_at "
180 "FROM fact_values "
181 "INNER JOIN hosts "
182 "ON fact_values.host_id = hosts.id "
183 "INNER JOIN fact_names "
184 "ON fact_values.fact_name_id = fact_names.id;",
185 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
186 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
187 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
188 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
189 "retrieve host attributes from the storeconfigs DB.");
190 return -1;
191 }
192 return 0;
193 } /* sdb_puppet_stcfg_collect */
195 static int
196 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
197 {
198 char *name = NULL;
199 char cb_name[1024];
201 sdb_object_t *user_data;
202 sdb_dbi_client_t *client;
203 sdb_dbi_options_t *options = NULL;
205 char *driver = NULL;
206 char *database = NULL;
208 int i;
210 if (oconfig_get_string(ci, &name)) {
211 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
212 "requires a single string argument\n"
213 "\tUsage: <Connection NAME>");
214 return -1;
215 }
217 for (i = 0; i < ci->children_num; ++i) {
218 oconfig_item_t *child = ci->children + i;
219 char *key = NULL, *value = NULL;
221 int status = 0;
223 if (! strcasecmp(child->key, "DBAdapter")) {
224 if (oconfig_get_string(child, &driver)) {
225 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
226 "DBAdapter requires a single string argument inside "
227 "<Connection %s>\n\tUsage: DBAdapter NAME",
228 name);
229 }
230 continue;
231 }
232 else if (! strcasecmp(child->key, "DBName")) {
233 if (oconfig_get_string(child, &database)) {
234 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
235 "DBName requires a single string argument inside "
236 "<Connection %s>\n\tUsage: DBName NAME",
237 name);
238 }
239 continue;
240 }
241 else if (! strcasecmp(child->key, "DBServer")) {
242 status = oconfig_get_string(child, &value);
243 key = "host";
244 }
245 else if (! strcasecmp(child->key, "DBPort")) {
246 status = oconfig_get_string(child, &value);
247 key = "port";
248 }
249 else if (! strcasecmp(child->key, "DBUser")) {
250 status = oconfig_get_string(child, &value);
251 key = "username";
252 }
253 else if (! strcasecmp(child->key, "DBPassword")) {
254 status = oconfig_get_string(child, &value);
255 key = "password";
256 }
257 else if (! strcasecmp(child->key, "DBIOption")) {
258 if ((child->values_num != 2)
259 || (child->values[0].type != OCONFIG_TYPE_STRING)
260 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
261 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
262 "DBIOption requires exactly two string arguments "
263 "inside <Connection %s>\n"
264 "\tUsage: DBIOption KEY VALUE", name);
265 continue;
266 }
268 status = 0;
269 key = child->values[0].value.string;
270 value = child->values[1].value.string;
271 }
272 else {
273 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
274 "Ignoring unknown config option '%s' inside "
275 "<Connection %s>.", child->key, name);
276 continue;
277 }
279 if (status) {
280 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
281 "'%s' requires a single string argument inside "
282 "<Connection %s>\n\tUsage: DBAdapter NAME",
283 child->key, name);
284 continue;
285 }
287 assert(key && value);
289 if (! options) {
290 if (! (options = sdb_dbi_options_create())) {
291 char errmsg[1024];
292 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
293 "Failed to create DBI options object: %s",
294 sdb_strerror(errno, errmsg, sizeof(errmsg)));
295 continue;
296 }
297 }
299 if (sdb_dbi_options_add(options, key, value)) {
300 char errmsg[1024];
301 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
302 "Failed to add option '%s': %s", key,
303 sdb_strerror(errno, errmsg, sizeof(errmsg)));
304 continue;
305 }
306 }
308 if (! driver) {
309 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
310 "Connection '%s' " "missing the 'DBAdapter' option.",
311 name);
312 return -1;
313 }
314 if (! database) {
315 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
316 "Connection '%s' missing the 'DBName' option.", name);
317 return -1;
318 }
320 snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
321 cb_name[sizeof(cb_name) - 1] = '\0';
323 client = sdb_dbi_client_create(driver, database);
324 if (! client) {
325 char errbuf[1024];
326 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
327 "Failed to create DBI client: %s",
328 sdb_strerror(errno, errbuf, sizeof(errbuf)));
329 return -1;
330 }
332 sdb_dbi_client_set_options(client, options);
334 user_data = sdb_object_create_wrapper("dbi-client", client,
335 (void (*)(void *))sdb_dbi_client_destroy);
336 if (! user_data) {
337 sdb_dbi_client_destroy(client);
338 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
339 "Failed to allocate sdb_object_t");
340 return -1;
341 }
343 sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
344 sdb_plugin_register_shutdown(cb_name, sdb_puppet_stcfg_shutdown,
345 user_data);
346 sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
347 /* interval */ NULL, user_data);
349 /* pass control to the list */
350 sdb_object_deref(user_data);
351 return 0;
352 } /* sdb_puppet_stcfg_config_conn */
354 static int
355 sdb_puppet_stcfg_config(oconfig_item_t *ci)
356 {
357 int i;
359 if (! ci) /* nothing to do to deconfigure this plugin */
360 return 0;
362 for (i = 0; i < ci->children_num; ++i) {
363 oconfig_item_t *child = ci->children + i;
365 if (! strcasecmp(child->key, "Connection"))
366 sdb_puppet_stcfg_config_conn(child);
367 else
368 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
369 "Ignoring unknown config option '%s'.", child->key);
370 }
371 return 0;
372 } /* sdb_puppet_stcfg_config */
374 int
375 sdb_module_init(sdb_plugin_info_t *info)
376 {
377 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
378 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
379 "backend accessing the Puppet stored configuration database");
380 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
381 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
382 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
383 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
384 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
386 /* don't reinitialize dbi when reinitializing the plugin */
387 if (info && (dbi_initialize(/* driver dir = */ NULL) < 0)) {
388 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
389 "initialize DBI; possibly you don't have any drivers "
390 "installed.");
391 return -1;
392 }
394 sdb_plugin_register_config("puppet::store-configs",
395 sdb_puppet_stcfg_config);
396 return 0;
397 } /* sdb_version_extra */
399 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */