summaryrefslogtreecommitdiffstats
path: root/bootconf
blob: 1b7c8870762b29ce85be4422f5b7432e45ed492b (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
#!/usr/bin/env python

# BootConf.py
# 
# The command line interface for BootConf, a network boot configuration
# library ...
#
# Michael DeHaan <mdehaan@redhat.com>

import os
import sys
import getopt
import traceback

import api
import util

class BootCLI:

    """
    Build the command line parser and API instances, etc.
    """
    def __init__(self,args):
        self.args = args
        self.api = api.BootAPI()
        self.distro_commands = { 
            'add'     :  self.distro_edit,
            'edit'    :  self.distro_edit,
            'delete'  :  self.distro_remove,
            'remove'  :  self.distro_remove,
            'list'    :  self.distro_list
        }
        self.group_commands = {
            'add'     :  self.group_edit,
            'edit'    :  self.group_edit,
            'delete'  :  self.group_remove,
            'remove'  :  self.group_remove,
            'list'    :  self.group_list
        }
        self.system_commands = {
            'add'     :  self.system_edit,
            'edit'    :  self.system_edit,
            'delete'  :  self.system_remove,
            'remove'  :  self.system_remove,
            'list'    :  self.system_list
        }
        self.toplevel_commands = {
            'check'   : self.check,
            'distros' : self.distro,
            'distro'  : self.distro,
            'groups'  : self.group, 
            'group'   : self.group, 
            'systems' : self.system,
            'system'  : self.system,
            'sync'    : self.sync,
            'help'    : self.help
        }

    """
    Run the command line
    """
    def run(self):
        return self.curry_args(self.args[1:], self.toplevel_commands)

    """
    Print out abbreviated help if user gives bad syntax
    """
    def usage(self):
        print "for help, run 'bootconf help'"
        return False

    """
    Print out tediously wrong help:  'bootconf help'
    """
    def help(self,args):
        print open("help.txt").read()
        return False

    """
    Print out the list of systems:  'bootconf system list'
    """
    def system_list(self,args):
        print str(self.api.get_systems())

    """
    Print out the list of groups:  'bootconf group list'
    """
    def group_list(self,args):
        print str(self.api.get_groups())
    
    """
    Print out the list of distros: 'bootconf distro list'
    """
    def distro_list(self,args):
        print str(self.api.get_distros())

    """
    Delete a system:  'bootconf system remove --name=foo'
    """
    def system_remove(self,args):
        sys = self.api.new_system()
        commands = {
           '--name'       : lambda(a):  sys.set_name(a)
        }
        results = {
           True         : lambda:  self.api.get_systems().remove(sys),
           False        : lambda:  self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc

    """
    Delete a group:   'bootconf group remove --name=foo'
    """
    def group_remove(self,args):
        group = self.api.new_group()
        commands = {
           '--name'       : lambda(a):  group.set_name(a)
        }
        results = {
           True         : lambda:  self.api.get_groups().remove(group),
           False        : lambda:  self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc

    """
    Delete a distro:  'bootconf distro remove --name='foo'
    """
    def distro_remove(self,args):
        distro = self.api.new_distro()
        commands = {
           '--name'       : lambda(a):  distro.set_name(a)
        }
        results = {
           True         : lambda:  self.api.get_distros().remove(distro),
           False        : lambda:  self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc

    """
    Create/Edit a system:  'bootconf system edit --name='foo' ...
    """
    def system_edit(self,args):
        sys = self.api.new_system()
        commands = {
           '--name'     :  lambda(a) : sys.set_name(a),
           '--group'    :  lambda(a) : sys.set_group(a)           
        }
        results = {
            True        :  lambda : self.api.get_systems().add(sys),
            False       :  lambda : self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc

    """
    Create/Edit a group:  'bootconf group edit --name='foo' ...
    """
    def group_edit(self,args):
        group = self.api.new_group()
        commands = {
            '--name'      :  lambda(a) : group.set_name(a),
            '--distro'    :  lambda(a) : group.set_distro(a),
            '--kickstart' :  lambda(a) : group.set_kickstart(a)
        }
        results = {
            True        :  lambda : self.api.get_groups().add(group),
            False       :  lambda : self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc
    
    """
    Create/Edit a distro:  'bootconf distro edit --name='foo' ...
    """
    def distro_edit(self,args):
        distro = self.api.new_distro()
        commands = {
            '--name'      :  lambda(a) : distro.set_name(a),
            '--kernel'    :  lambda(a) : distro.set_kernel(a),
            '--initrd'    :  lambda(a) : distro.set_initrd(a)
        }
        results = { 
             True       :  lambda : self.api.get_distros().add(distro),
             False      :  lambda : self.api.show_error()
        }
        rc = self.apply_args(args,commands,results)
        if rc: self.api.serialize()
        return rc

    """
    Instead of getopt...
    Parses arguments of the form --foo=bar, see group_edit for example
    """
    def apply_args(self,args,input_routines,results):
        for x in args:
            try:
                key, value = x.split("=")
            except:
                print "Syntax error in argument"
                return results[False]()
            if key in input_routines:
                if not input_routines[key](value):
                   print "Argument value rejected: %s" % key
                   return results[False]()
            else:
                print "Unmatched argument: %s" % key
                return results[False]()
        return results[True]()

    """
    Helper function to make subcommands a bit more friendly.
    See group(), system(), or distro() for examples
    """
    def curry_args(self, args, commands):
        if args is None or len(args) == 0:
            return self.usage()
        if args[0] in commands:
            rc = commands[args[0]](args[1:])
        else:
            rc = self.usage()
        return rc

    """
    Sync the config file with the system config: 'bootconf sync [--dryrun]'
    """
    def sync(self, args):
        status = None 
        if args is not None and len(args) > 0 and args[0] == "--dryrun":
            status = self.api.sync(dry_run=True)
        else:
            status = self.api.sync(dry_run=False)
        if not status:
            print self.api.last_error
        return status
       
    """
    Check system for network boot decency/prereqs: 'bootconf check'
    """
    def check(self,args):
        status = self.api.check()
        if status is None:
            print self.api.last_error
            return False
        elif len(status) == 0:
            # FIXME: rewrite this text.
            print "\nNo setup problems found, though we can't tell if /etc/dhcpd.conf is totally correct, so that's left as an exercise for the reader.  Network boot infrastructure configuration via other 'bootconf' commands should be good to go, though you should correct any errors found as a result of running 'bootconf sync' as well.  Next look over /etc/bootconf.conf and edit any global settings that are 'wrong'.  Ensure that dhcpd and tftpd are started after you are done, but they do not need to be started now.  Note: making changes to /etc/dhcpd.conf always requires a restart of dhcpd. Good luck!\n"
            return True
        else:
            print "The following items need to be corrected:"
            for i,x in enumerate(status): 
               print "#%d: %s" % (i,x)
            return False
  
    """
    Handles any of the 'bootconf distro' subcommands
    """
    def distro(self,args):
        return self.curry_args(args, self.distro_commands) 

    """
    Handles any of the 'bootconf group' subcommands
    """
    def group(self,args):
        return self.curry_args(args, self.group_commands)

    """
    Handles any of the 'bootconf system' subcommands
    """
    def system(self,args):
        return self.curry_args(args, self.system_commands)

  
if __name__ == "__main__":
    if os.getuid() != 0:
       print "bootconf must be run as root"
       sys.exit(1)
    if BootCLI(sys.argv).run():
       sys.exit(0)
    else:
       sys.exit(1)