Code

dbi utils: Use the new _r interface.
[sysdb.git] / src / backend / puppet / store-configs.c
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 <assert.h>
37 #include <errno.h>
39 #include <string.h>
40 #include <strings.h>
42 SDB_PLUGIN_MAGIC;
44 /*
45  * private helper functions
46  */
48 static int
49 sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
50                 size_t n, sdb_data_t *data,
51                 sdb_object_t __attribute__((unused)) *user_data)
52 {
53         const char *hostname;
54         sdb_time_t timestamp;
56         int status;
58         assert(n == 2);
59         assert((data[0].type == SDB_TYPE_STRING)
60                         && (data[1].type == SDB_TYPE_DATETIME));
62         hostname = data[0].data.string;
63         timestamp = data[1].data.datetime;
65         status = sdb_store_host(hostname, timestamp);
67         if (status < 0) {
68                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
69                                 "store/update host '%s'.", hostname);
70                 return -1;
71         }
72         else if (! status)
73                 sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
74                                 "Added/updated host '%s' (last update timestamp = "
75                                 "%"PRIscTIME").", hostname, timestamp);
76         return 0;
77 } /* sdb_puppet_stcfg_get_hosts */
79 static int
80 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
81                 size_t n, sdb_data_t *data,
82                 sdb_object_t __attribute__((unused)) *user_data)
83 {
84         int status;
86         const char *hostname;
87         const char *key;
88         sdb_data_t  value;
89         sdb_time_t  last_update;
91         assert(n == 4);
92         assert((data[0].type == SDB_TYPE_STRING)
93                         && (data[1].type == SDB_TYPE_STRING)
94                         && (data[2].type == SDB_TYPE_STRING)
95                         && (data[3].type == SDB_TYPE_DATETIME));
97         hostname = data[0].data.string;
98         key = data[1].data.string;
99         value.type = SDB_TYPE_STRING;
100         value.data.string = data[2].data.string;
101         last_update = data[3].data.datetime;
103         status = sdb_store_attribute(hostname, key, &value, last_update);
105         if (status < 0) {
106                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
107                                 "store/update host attribute '%s' for host '%s'.",
108                                 key, hostname);
109                 return -1;
110         }
112         return 0;
113 } /* sdb_puppet_stcfg_get_attrs */
115 /*
116  * plugin API
117  */
119 static int
120 sdb_puppet_stcfg_init(sdb_object_t *user_data)
122         sdb_dbi_client_t *client;
124         if (! user_data)
125                 return -1;
127         client = SDB_OBJ_WRAPPER(user_data)->data;
128         if (sdb_dbi_client_connect(client)) {
129                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
130                                 "Failed to connect to the storeconfigs DB.");
131                 return -1;
132         }
134         sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
135                         "connected to the storeconfigs DB.");
136         return 0;
137 } /* sdb_puppet_stcfg_init */
139 static int
140 sdb_puppet_stcfg_shutdown(sdb_object_t *user_data)
142         if (! user_data)
143                 return -1;
145         sdb_dbi_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
146         SDB_OBJ_WRAPPER(user_data)->data = NULL;
147         return 0;
148 } /* sdb_puppet_stcfg_shutdown */
150 static int
151 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
153         sdb_dbi_client_t *client;
155         if (! user_data)
156                 return -1;
158         client = SDB_OBJ_WRAPPER(user_data)->data;
159         if (sdb_dbi_client_check_conn(client)) {
160                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
161                                 "Connection to storeconfigs DB failed.");
162                 return -1;
163         }
165         if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
166                                 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
167                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
168                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
169                                 "retrieve hosts from the storeconfigs DB.");
170                 return -1;
171         }
173         if (sdb_dbi_exec_query(client, "SELECT "
174                                         "hosts.name AS hostname, "
175                                         "fact_names.name AS name, "
176                                         "fact_values.value AS value, "
177                                         "fact_values.updated_at AS updated_at "
178                                 "FROM fact_values "
179                                 "INNER JOIN hosts "
180                                         "ON fact_values.host_id = hosts.id "
181                                 "INNER JOIN fact_names "
182                                         "ON fact_values.fact_name_id = fact_names.id;",
183                                 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
184                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
185                                 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
186                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
187                                 "retrieve host attributes from the storeconfigs DB.");
188                 return -1;
189         }
190         return 0;
191 } /* sdb_puppet_stcfg_collect */
193 static int
194 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
196         char *name = NULL;
197         char cb_name[1024];
199         sdb_object_t *user_data;
200         sdb_dbi_client_t *client;
201         sdb_dbi_options_t *options = NULL;
203         char *driver = NULL;
204         char *database = NULL;
206         int i;
208         if (oconfig_get_string(ci, &name)) {
209                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
210                                 "requires a single string argument\n"
211                                 "\tUsage: <Connection NAME>");
212                 return -1;
213         }
215         for (i = 0; i < ci->children_num; ++i) {
216                 oconfig_item_t *child = ci->children + i;
217                 char *key = NULL, *value = NULL;
219                 int status = 0;
221                 if (! strcasecmp(child->key, "DBAdapter")) {
222                         if (oconfig_get_string(child, &driver)) {
223                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
224                                                 "DBAdapter requires a single string argument inside "
225                                                 "<Connection %s>\n\tUsage: DBAdapter NAME",
226                                                 name);
227                         }
228                         continue;
229                 }
230                 else if (! strcasecmp(child->key, "DBName")) {
231                         if (oconfig_get_string(child, &database)) {
232                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
233                                                 "DBName requires a single string argument inside "
234                                                 "<Connection %s>\n\tUsage: DBName NAME",
235                                                 name);
236                         }
237                         continue;
238                 }
239                 else if (! strcasecmp(child->key, "DBServer")) {
240                         status = oconfig_get_string(child, &value);
241                         key = "host";
242                 }
243                 else if (! strcasecmp(child->key, "DBPort")) {
244                         status = oconfig_get_string(child, &value);
245                         key = "port";
246                 }
247                 else if (! strcasecmp(child->key, "DBUser")) {
248                         status = oconfig_get_string(child, &value);
249                         key = "username";
250                 }
251                 else if (! strcasecmp(child->key, "DBPassword")) {
252                         status = oconfig_get_string(child, &value);
253                         key = "password";
254                 }
255                 else if (! strcasecmp(child->key, "DBIOption")) {
256                         if ((child->values_num != 2)
257                                         || (child->values[0].type != OCONFIG_TYPE_STRING)
258                                         || (child->values[1].type != OCONFIG_TYPE_STRING)) {
259                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
260                                                 "DBIOption requires exactly two string arguments "
261                                                 "inside <Connection %s>\n"
262                                                 "\tUsage: DBIOption KEY VALUE", name);
263                                 continue;
264                         }
266                         status = 0;
267                         key = child->values[0].value.string;
268                         value = child->values[1].value.string;
269                 }
270                 else {
271                         sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
272                                         "Ignoring unknown config option '%s' inside "
273                                         "<Connection %s>.", child->key, name);
274                         continue;
275                 }
277                 if (status) {
278                         sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
279                                         "'%s' requires a single string argument inside "
280                                         "<Connection %s>\n\tUsage: DBAdapter NAME",
281                                         child->key, name);
282                         continue;
283                 }
285                 assert(key && value);
287                 if (! options) {
288                         if (! (options = sdb_dbi_options_create())) {
289                                 char errmsg[1024];
290                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
291                                                 "Failed to create DBI options object: %s",
292                                                 sdb_strerror(errno, errmsg, sizeof(errmsg)));
293                                 continue;
294                         }
295                 }
297                 if (sdb_dbi_options_add(options, key, value)) {
298                         char errmsg[1024];
299                         sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
300                                         "Failed to add option '%s': %s", key,
301                                         sdb_strerror(errno, errmsg, sizeof(errmsg)));
302                         continue;
303                 }
304         }
306         if (! driver) {
307                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
308                                 "Connection '%s' " "missing the 'DBAdapter' option.",
309                                 name);
310                 return -1;
311         }
312         if (! database) {
313                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
314                                 "Connection '%s' missing the 'DBName' option.", name);
315                 return -1;
316         }
318         snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
319         cb_name[sizeof(cb_name) - 1] = '\0';
321         client = sdb_dbi_client_create(driver, database);
322         if (! client) {
323                 char errbuf[1024];
324                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
325                                 "Failed to create DBI client: %s",
326                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
327                 return -1;
328         }
330         sdb_dbi_client_set_options(client, options);
332         user_data = sdb_object_create_wrapper("dbi-client", client,
333                         (void (*)(void *))sdb_dbi_client_destroy);
334         if (! user_data) {
335                 sdb_dbi_client_destroy(client);
336                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
337                                 "Failed to allocate sdb_object_t");
338                 return -1;
339         }
341         sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
342         sdb_plugin_register_shutdown(cb_name, sdb_puppet_stcfg_shutdown,
343                         user_data);
344         sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
345                         /* interval */ NULL, user_data);
347         /* pass control to the list */
348         sdb_object_deref(user_data);
349         return 0;
350 } /* sdb_puppet_stcfg_config_conn */
352 static int
353 sdb_puppet_stcfg_config(oconfig_item_t *ci)
355         int i;
357         if (! ci) /* nothing to do to deconfigure this plugin */
358                 return 0;
360         for (i = 0; i < ci->children_num; ++i) {
361                 oconfig_item_t *child = ci->children + i;
363                 if (! strcasecmp(child->key, "Connection"))
364                         sdb_puppet_stcfg_config_conn(child);
365                 else
366                         sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
367                                         "Ignoring unknown config option '%s'.", child->key);
368         }
369         return 0;
370 } /* sdb_puppet_stcfg_config */
372 int
373 sdb_module_init(sdb_plugin_info_t *info)
375         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
376         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
377                         "backend accessing the Puppet stored configuration database");
378         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
379                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
380         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
381         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
382         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
384         sdb_plugin_register_config("puppet::store-configs",
385                         sdb_puppet_stcfg_config);
386         return 0;
387 } /* sdb_version_extra */
389 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */