summaryrefslogtreecommitdiffstats
path: root/cobbler/action_report.py
blob: 2288433e903d4d562d8926cce9a3adc9a879b6ca (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Report from a cobbler master.

Copyright 2007-2008, Red Hat, Inc
Anderson Silva <ansilva@redhat.com>


This software may be freely redistributed under the terms of the GNU
general public license.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

import os
import os.path
import xmlrpclib
import api as cobbler_api
from cexceptions import *
from utils import _


class Report:

    def __init__(self, config):
        """
        Constructor
        """
        self.config = config
        self.settings = config.settings()
        self.api = config.api
        self.report_type = None
        self.report_what = None
        self.report_name = None
        self.report_fields = None
        self.report_noheaders = None

    def reporting_csv(self, info, order, noheaders):
        """
        Formats data on 'info' for csv output
        """
        outputheaders = ''
        outputbody = ''
        sep = ','

        info_count = 0
        for item in info:

            item_count = 0
            for key in order:

                if info_count == 0:
                    outputheaders += str(key) + sep

                outputbody += str(item[key]) + sep

                item_count = item_count + 1

            info_count = info_count + 1
            outputbody += '\n'

        outputheaders += '\n'

        if noheaders:
            outputheaders = '';

        return outputheaders + outputbody
 
    def reporting_trac(self, info, order, noheaders):
        """
        Formats data on 'info' for trac wiki table output
        """        
        outputheaders = ''
        outputbody = ''
        sep = '||'

        info_count = 0
        for item in info:
            
            item_count = 0
            for key in order:


                if info_count == 0:
                    outputheaders += sep + str(key)

                outputbody += sep + str(item[key])

                item_count = item_count + 1

            info_count = info_count + 1
            outputbody += '||\n'

        outputheaders += '||\n'
        
        if noheaders:
            outputheaders = '';
        
        return outputheaders + outputbody

    def reporting_doku(self, info, order, noheaders):
        """
        Formats data on 'info' for doku wiki table output
        """      
        outputheaders = ''
        outputbody = ''
        sep1 = '^'
        sep2 = '|'


        info_count = 0
        for item in info:
            
            item_count = 0
            for key in order:

                if info_count == 0:
                    outputheaders += sep1 + key

                outputbody += sep2 + item[key]

                item_count = item_count + 1

            info_count = info_count + 1
            outputbody += sep2 + '\n'

        outputheaders += sep1 + '\n'
        
        if noheaders:
            outputheaders = '';
        
        return outputheaders + outputbody

    def reporting_mediawiki(self, info, order, noheaders):
        """
        Formats data on 'info' for mediawiki table output
        """
        outputheaders = ''
        outputbody = ''
        opentable = '{| border="1"\n'
        closetable = '|}\n'
        sep1 = '||'
        sep2 = '|'
        sep3 = '|-'


        info_count = 0
        for item in info:
            
            item_count = 0
            for key in order:

                if info_count == 0 and item_count == 0:
                    outputheaders += sep2 + key
                elif info_count == 0:
                    outputheaders += sep1 + key

                if item_count == 0:
                    outputbody += sep2 + str(item[key])
                else:
                    outputbody += sep1 + str(item[key])

                item_count = item_count + 1

            info_count = info_count + 1
            outputbody += '\n' + sep3 + '\n'

        outputheaders += '\n' + sep3 + '\n'

        if noheaders:
            outputheaders = '';

        return opentable + outputheaders + outputbody + closetable
    
    def print_formatted_data(self, data, order, report_type, noheaders):
        """
        Used for picking the correct format to output data as
        """
        if report_type == "csv":
            print self.reporting_csv(data, order, noheaders)
        if report_type == "mediawiki":
            print self.reporting_mediawiki(data, order, noheaders)
        if report_type == "trac":
            print self.reporting_trac(data, order, noheaders)
        if report_type == "doku":
            print self.reporting_doku(data, order, noheaders)

        return True

    def reporting_sorter(self, a, b):
        """
        Used for sorting cobbler objects for report commands
        """
        return cmp(a.name, b.name)

    def reporting_print_sorted(self, collection):
        """
        Prints all objects in a collection sorted by name
        """
        collection = [x for x in collection]
        collection.sort(self.reporting_sorter)
        for x in collection:
            print x.printable()
        return True

    def reporting_list_names2(self, collection, name):
        """
        Prints a specific object in a collection.
        """
        obj = collection.find(name=name)
        if obj is not None:
            print obj.printable()
        return True
    
    def reporting_print_all_fields(self, collection, report_type, report_noheaders):
        """
        Prints all fields in a collection as a table given the report type
        """
        collection = [x for x in collection]
        collection.sort(self.reporting_sorter)
        data = []
        out_order = []
        count = 0
        for x in collection:
            item = {}
            structure = x.to_datastruct()
           
            for (key, value) in structure.iteritems():

                # exception for systems which could have > 1 interface
                if key == "interfaces":
                    for (device, info) in value.iteritems():
                        for (info_header, info_value) in info.iteritems():
                            item[info_header] = str(device) + ': ' + str(info_value)
                            # needs to create order list for print_formatted_fields
                            if count == 0:
                                out_order.append(info_header)
                else: 
                    item[key] = value
                    # needs to create order list for print_formatted_fields
                    if count == 0:
                        out_order.append(key)                  

            count = count + 1
  
            data.append(item) 

        self.print_formatted_data(data = data, order = out_order, report_type = report_type, noheaders = report_noheaders)
        
        return True
    
    def reporting_print_x_fields(self, collection, report_type, report_fields, report_noheaders):
        """
        Prints specific fields in a collection as a table given the report type
        """
        collection = [x for x in collection]
        collection.sort(self.reporting_sorter)
        data = []
        fields_list = report_fields.replace(' ', '').split(',')
        
        for x in collection:
            structure = x.to_datastruct()
            item = {}
            for field in fields_list:

                if field in structure.keys():
                    item[field] = structure[field]
 
                # exception for systems which could have > 1 interface
                elif "interfaces" in structure.keys():
                    for device in structure['interfaces'].keys():
                        if field in structure['interfaces'][device]:
                            item[field] = device + ': ' + structure['interfaces'][device][field]                    
                else: 
                    raise CX(_("The field %s does not exist, see cobbler dumpvars for available fields.") % field)

            data.append(item)
         
        self.print_formatted_data(data = data, order = fields_list, report_type = report_type, noheaders = report_noheaders)
                        
        return True
        
    # -------------------------------------------------------

    def run(self, report_what = None, report_name = None, report_type = None, report_fields = None, report_noheaders = None):
        """
        Get remote profiles and distros and sync them locally
        """
               
        """
        1. Handles original report output
        2. Handles all fields of report outputs as table given a format
        3. Handles specific fields of report outputs as table given a format
        """        
        

        if report_type == 'text' and report_fields == 'all':

            if report_what in [ "all", "distros", "distro" ]:
                if report_name:
                    self.reporting_list_names2(self.api.distros(), report_name)
                else:
                    self.reporting_print_sorted(self.api.distros())

            if report_what in [ "all", "profiles", "profile" ]:
                if report_name:
                    self.reporting_list_names2(self.api.profiles(), report_name)
                else:
                    self.reporting_print_sorted(self.api.profiles())

            if report_what in [ "all", "systems", "system" ]:
                if report_name:
                    self.reporting_list_names2(self.api.systems(), report_name)
                else:
                    self.reporting_print_sorted(self.api.systems())

            if report_what in [ "all", "repos", "repo" ]:
                if report_name is not None:
                    self.reporting_list_names2(self.api.repos(), report_name)
                else:
                    self.reporting_print_sorted(self.api.repos())

            if report_what in [ "all", "images", "image" ]:
                if report_name is not None:
                    self.reporting_list_names2(self.api.images(), report_name)
                else:
                    self.reporting_print_sorted(self.api.images())
                   
        elif report_type == 'text' and report_fields != 'all':
            raise CX(_("The 'text' type can only be used with field set to 'all'"))
 
        elif report_type != 'text' and report_fields == 'all':
            
            if report_what in [ "all", "distros", "distro" ]:
                self.reporting_print_all_fields(self.api.distros(), report_type, report_noheaders)

            if report_what in [ "all", "profiles", "profile" ]:
                self.reporting_print_all_fields(self.api.profiles(), report_type, report_noheaders)

            if report_what in [ "all", "systems", "system" ]:
                self.reporting_print_all_fields(self.api.systems(), report_type, report_noheaders)

            if report_what in [ "all", "repos", "repo" ]:
                self.reporting_print_all_fields(self.api.repos(), report_type, report_noheaders) 

            if report_what in [ "all", "images", "image" ]:
                self.reporting_print_all_fields(self.api.images(), report_type, report_noheaders) 
        
        else:
            
            if report_what in [ "all", "distros", "distro" ]:
                self.reporting_print_x_fields(self.api.distros(), report_type, report_fields, report_noheaders)

            if report_what in [ "all", "profiles", "profile" ]:
                self.reporting_print_x_fields(self.api.profiles(), report_type, report_fields, report_noheaders)

            if report_what in [ "all", "systems", "system" ]:
                self.reporting_print_x_fields(self.api.systems(), report_type, report_fields, report_noheaders)

            if report_what in [ "all", "repos", "repo" ]:
                self.reporting_print_x_fields(self.api.repos(), report_type, report_fields, report_noheaders)
            if report_what in [ "all", "images", "image" ]:
                self.reporting_print_x_fields(self.api.images(), report_type, report_fields, report_noheaders)