Code

store: Added support for different data-types for attributes.
[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 <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)
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_collect(sdb_object_t *user_data)
144         sdb_dbi_client_t *client;
146         if (! user_data)
147                 return -1;
149         client = SDB_OBJ_WRAPPER(user_data)->data;
150         if (sdb_dbi_client_check_conn(client)) {
151                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
152                                 "Connection to storeconfigs DB failed.");
153                 return -1;
154         }
156         if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
157                                 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
158                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
159                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
160                                 "retrieve hosts from the storeconfigs DB.");
161                 return -1;
162         }
164         if (sdb_dbi_exec_query(client, "SELECT "
165                                         "hosts.name AS hostname, "
166                                         "fact_names.name AS name, "
167                                         "fact_values.value AS value, "
168                                         "fact_values.updated_at AS updated_at "
169                                 "FROM fact_values "
170                                 "INNER JOIN hosts "
171                                         "ON fact_values.host_id = hosts.id "
172                                 "INNER JOIN fact_names "
173                                         "ON fact_values.fact_name_id = fact_names.id;",
174                                 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
175                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
176                                 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
177                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
178                                 "retrieve host attributes from the storeconfigs DB.");
179                 return -1;
180         }
181         return 0;
182 } /* sdb_puppet_stcfg_collect */
184 static int
185 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
187         char *name = NULL;
188         char cb_name[1024];
190         sdb_object_t *user_data;
191         sdb_dbi_client_t *client;
192         sdb_dbi_options_t *options = NULL;
194         char *driver = NULL;
195         char *database = NULL;
197         int i;
199         if (oconfig_get_string(ci, &name)) {
200                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
201                                 "requires a single string argument\n"
202                                 "\tUsage: <Connection NAME>");
203                 return -1;
204         }
206         for (i = 0; i < ci->children_num; ++i) {
207                 oconfig_item_t *child = ci->children + i;
208                 char *key = NULL, *value = NULL;
210                 int status = 0;
212                 if (! strcasecmp(child->key, "DBAdapter")) {
213                         if (oconfig_get_string(child, &driver)) {
214                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
215                                                 "DBAdapter requires a single string argument inside "
216                                                 "<Connection %s>\n\tUsage: DBAdapter NAME",
217                                                 name);
218                         }
219                         continue;
220                 }
221                 else if (! strcasecmp(child->key, "DBName")) {
222                         if (oconfig_get_string(child, &database)) {
223                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
224                                                 "DBName requires a single string argument inside "
225                                                 "<Connection %s>\n\tUsage: DBName NAME",
226                                                 name);
227                         }
228                         continue;
229                 }
230                 else if (! strcasecmp(child->key, "DBServer")) {
231                         status = oconfig_get_string(child, &value);
232                         key = "host";
233                 }
234                 else if (! strcasecmp(child->key, "DBPort")) {
235                         status = oconfig_get_string(child, &value);
236                         key = "port";
237                 }
238                 else if (! strcasecmp(child->key, "DBUser")) {
239                         status = oconfig_get_string(child, &value);
240                         key = "username";
241                 }
242                 else if (! strcasecmp(child->key, "DBPassword")) {
243                         status = oconfig_get_string(child, &value);
244                         key = "password";
245                 }
246                 else if (! strcasecmp(child->key, "DBIOption")) {
247                         if ((child->values_num != 2)
248                                         || (child->values[0].type != OCONFIG_TYPE_STRING)
249                                         || (child->values[1].type != OCONFIG_TYPE_STRING)) {
250                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
251                                                 "DBIOption requires exactly two string arguments "
252                                                 "inside <Connection %s>\n"
253                                                 "\tUsage: DBIOption KEY VALUE", name);
254                                 continue;
255                         }
257                         status = 0;
258                         key = child->values[0].value.string;
259                         value = child->values[1].value.string;
260                 }
261                 else {
262                         sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
263                                         "Ignoring unknown config option '%s' inside "
264                                         "<Connection %s>.", child->key, name);
265                         continue;
266                 }
268                 if (status) {
269                         sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
270                                         "'%s' requires a single string argument inside "
271                                         "<Connection %s>\n\tUsage: DBAdapter NAME",
272                                         child->key, name);
273                         continue;
274                 }
276                 assert(key && value);
278                 if (! options) {
279                         if (! (options = sdb_dbi_options_create())) {
280                                 char errmsg[1024];
281                                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
282                                                 "Failed to create DBI options object: %s",
283                                                 sdb_strerror(errno, errmsg, sizeof(errmsg)));
284                                 continue;
285                         }
286                 }
288                 if (sdb_dbi_options_add(options, key, value)) {
289                         char errmsg[1024];
290                         sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
291                                         "Failed to add option '%s': %s", key,
292                                         sdb_strerror(errno, errmsg, sizeof(errmsg)));
293                         continue;
294                 }
295         }
297         if (! driver) {
298                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
299                                 "Connection '%s' " "missing the 'DBAdapter' option.",
300                                 name);
301                 return -1;
302         }
303         if (! database) {
304                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
305                                 "Connection '%s' missing the 'DBName' option.", name);
306                 return -1;
307         }
309         snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
310         cb_name[sizeof(cb_name) - 1] = '\0';
312         client = sdb_dbi_client_create(driver, database);
313         if (! client) {
314                 char errbuf[1024];
315                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
316                                 "Failed to create DBI client: %s",
317                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
318                 return -1;
319         }
321         sdb_dbi_client_set_options(client, options);
323         user_data = sdb_object_create_wrapper("dbi-client", client,
324                         (void (*)(void *))sdb_dbi_client_destroy);
325         if (! user_data) {
326                 sdb_dbi_client_destroy(client);
327                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
328                                 "Failed to allocate sdb_object_t");
329                 return -1;
330         }
332         sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
333         sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
334                         /* interval */ NULL, user_data);
336         /* pass control to the list */
337         sdb_object_deref(user_data);
338         return 0;
339 } /* sdb_puppet_stcfg_config_conn */
341 static int
342 sdb_puppet_stcfg_config(oconfig_item_t *ci)
344         int i;
346         for (i = 0; i < ci->children_num; ++i) {
347                 oconfig_item_t *child = ci->children + i;
349                 if (! strcasecmp(child->key, "Connection"))
350                         sdb_puppet_stcfg_config_conn(child);
351                 else
352                         sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
353                                         "Ignoring unknown config option '%s'.", child->key);
354         }
355         return 0;
356 } /* sdb_puppet_stcfg_config */
358 int
359 sdb_module_init(sdb_plugin_info_t *info)
361         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
362         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
363                         "backend accessing the Puppet stored configuration database");
364         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
365                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
366         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
367         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
368         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
370         if (dbi_initialize(/* driver dir = */ NULL) < 0) {
371                 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
372                                 "initialize DBI; possibly you don't have any drivers "
373                                 "installed.");
374                 return -1;
375         }
377         sdb_plugin_register_config("puppet::store-configs",
378                         sdb_puppet_stcfg_config);
379         return 0;
380 } /* sdb_version_extra */
382 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */