summaryrefslogtreecommitdiffstats
path: root/__root__/ccs-flatten/flatten.c
blob: e2f0662898041c4327a092f2592adaedaec0554a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
  Copyright 2014 Red Hat, Inc.

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301 USA
*/
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/xpath.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <libgen.h>
#include "list.h"
#include "resgroup.h"
#include "reslist.h"
#include "xmlconf.h"

#define shift() {++argv; --argc;}

const char *agentpath = RESOURCE_ROOTDIR;

static xmlNode *
get_rm_node(xmlDocPtr doc)
{
    xmlNodePtr n, o;

    if (!doc)
        return NULL;
    n = xmlDocGetRootElement(doc);
    if (!n)
        return NULL;
    if (strcmp((char *)n->name, "cluster")) {
        fprintf(stderr, "Expected cluster tag, got %s\n", (char *)n->name);
        return NULL;
    }

    for (o = n->xmlChildrenNode; o; o = o->next) {
        if (o->type != XML_ELEMENT_NODE)
            continue;
        if (!strcmp((char *)o->name, "rm"))
            return o;
    }

    return NULL;
}

static void
remove_resources_block(xmlNodePtr rm)
{
    xmlNodePtr o, r = NULL;

    for (o = rm->xmlChildrenNode; o; o = o->next) {
        if (o->type != XML_ELEMENT_NODE)
            continue;
        if (!strcmp((char *)o->name, "resources")) {
            r = o;
            break;
        }
    }

    if (!r)
        return;

    xmlUnlinkNode(r);
    xmlFreeNode(r);
}

static int
replace_resource(xmlNodePtr rm, char *restype, char *primattr, char *ident, xmlNodePtr n)
{
    xmlNodePtr o, r = NULL;
    char *p;

    for (o = rm->xmlChildrenNode; o; o = o->next) {
        if (o->type != XML_ELEMENT_NODE)
            continue;
        if (!strcmp((char *)o->name, restype)) {
            p = (char *)xmlGetProp(o, (xmlChar *) primattr);
            if (!strcmp(p, ident)) {
                r = o;
                break;
            }
        }
    }

    if (!r)
        return -1;

    xmlUnlinkNode(r);
    xmlFreeNode(r);

    xmlAddChild(rm, n);

    return 0;
}

static int
flatten(int argc, char **argv, xmlDocPtr * doc)
{
    xmlDocPtr d = NULL;
    xmlNode *n = NULL, *rm = NULL, *new_rb = NULL;
    resource_rule_t *rulelist = NULL;
    resource_t *reslist = NULL, *curres;
    resource_node_t *tree = NULL, *rn;
    FILE *f = stdout;
    int rawmetadata = 0, ret = 0;

    conf_setconfig(argv[0]);
    if (conf_open() < 0)
        goto out;

    while (argc >= 2) {
        shift();
        if (!strcmp(argv[0], "-r")) {
            if (!new_rb)
                new_rb = xmlNewNode(NULL, (xmlChar *) "rm");
#ifdef RAWMETADATA_EXT
        } else if (!strcmp(argv[0], "-m")) {
            rawmetadata = 1;
#endif
        } else {
            if (f == stdout)
                f = fopen(argv[0], "w+");
        }
    }

    d = conf_get_doc();
    rm = get_rm_node(d);

    /* allow fallback to rawmetadata, but not the other way around */
    do {
	fprintf(stderr, "%s\n", rawmetadata ? "raw":"noraw");
        load_resource_rules(agentpath, &rulelist, rawmetadata);
        if (rulelist)
            break;
        rawmetadata = !rawmetadata;
    } while (rawmetadata);
    if (!rulelist) {
        fprintf(stderr, "No resource rules available\n");
        goto out;
    }

    load_resources(&reslist, &rulelist);
    build_resource_tree(&tree, &rulelist, &reslist);
    if (!tree) {
        fprintf(stderr, "No resource trees defined; nothing to do\n");
        goto out;
    }
#ifdef DEBUG
    fprintf(stderr, "Resources %p tree %p\n", reslist, tree);
#endif

    shift();

    list_do(&tree, rn) {
        n = NULL;

        curres = rn->rn_resource;

#ifdef DEBUG
        fprintf(stderr, "Flatten %s:%s ... \n", curres->r_rule->rr_type,
                curres->r_attrs[0].ra_value);
#endif
        if (res_flatten(&n, new_rb, &tree, curres)) {
            fprintf(stderr, "FAIL 1\n");
            ret = -1;
            goto out;
        }

        if (replace_resource(rm, curres->r_rule->rr_type,
                             curres->r_attrs[0].ra_name, curres->r_attrs[0].ra_value, n) != 0) {
            fprintf(stderr, "FAIL 2\n");
            ret = -1;
            goto out;
        }

    }
    while (!list_done(&tree, rn)) ;

    remove_resources_block(rm);
    if (new_rb) {
        xmlAddChild(rm, new_rb);
    }

    xmlDocFormatDump(f, d, 1);
    if (f != stdout)
        fclose(f);

  out:
    if (ret < 0) {
        xmlFreeDoc(d);
    } else {
        *doc = d;
    }
    conf_close();
    destroy_resource_tree(&tree);
    destroy_resources(&reslist);
    destroy_resource_rules(&rulelist);

    return ret;
}

static void
usage(const char *arg0, int ret)
{
#ifdef RAWMETADATA_EXT
    fprintf(stderr, "usage: %s <input.conf> [-m] [-r] [output.conf] \n\n", arg0);
    fprintf(stderr, "-m\tskip evaluation of RA executables, immediately use *.%s"
                    " files\n", RAWMETADATA_EXT);
#else
    fprintf(stderr, "usage: %s <input.conf> [-r] [output.conf] \n\n", arg0);
#endif
    fprintf(stderr, "-r\t?\n");
    exit(ret);
}

int
main(int argc, char **argv)
{
    char *arg0 = basename(argv[0]);
    int ret = 0;
    xmlDocPtr doc = NULL;

    if (argc < 2) {
        usage(arg0, 1);
    }

    if (!strcasecmp(argv[1], "-h") || !strcasecmp(argv[1], "-?")) {
        usage(arg0, 0);
    }

    xmlInitParser();
    xmlIndentTreeOutput = 1;
    xmlKeepBlanksDefault(0);

    shift();
    ret = flatten(argc, argv, &doc);

    //if (doc)
    //xmlFreeDoc(doc);
    xmlCleanupParser();
    return ret;
}