summaryrefslogtreecommitdiffstats
path: root/cobbler/modules/cli_misc.py
blob: a4fc4708fe1392b8ddf9854daccda8c1924f63da (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Misc CLI functions.

Copyright 2007, Red Hat, Inc
Michael DeHaan <mdehaan@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 distutils.sysconfig
import sys
import time

plib = distutils.sysconfig.get_python_lib()
mod_path="%s/cobbler" % plib
sys.path.insert(0, mod_path)

from utils import _
import cobbler.commands as commands
from cexceptions import *
HELP_FORMAT = commands.HELP_FORMAT

# TO DO list
# cobbler check
# cobbler import (--name, --mirror, --available-as)
# cobbler reserialize
# cobbler --type=[profile|system|distro|repo] [--name=list]
# cobbler --type=[profile|system|distro|profile] [--name=report]
# cobbler status
# cobbler reposync --name=$name
# cobbler sync
# cobbler validateks
# elsewhere: repo auto-add

########################################################

class HardLinkFunction(commands.CobblerFunction):
    def help_me(self):
        return HELP_FORMAT % ("cobbler hardlink","")

    def command_name(self):
        return "hardlink"

    def add_options(self, p, args):
        pass

    def run(self):
        self.api.hardlink()

########################################################

class CheckFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler check","")

    def command_name(self):
        return "check"

    def add_options(self, p, args):
        pass

    def logprint(self,fd,msg,log_only=False):
        fd.write("%s\n" % msg)
        if log_only:
           return
        print msg

    def run(self):
        status = self.api.check()
        fd = open("/var/log/cobbler/check.log","w+")
        self.logprint(fd,"cobbler check log from %s" % time.asctime(),log_only=True)
        if len(status) == 0:
            self.logprint(fd,"No setup problems found")
             
            self.logprint(fd,"Manual review and editing of /etc/cobbler/settings is recommended to tailor cobbler to your particular configuration.")
            self.logprint(fd,"Good luck.")
            return True
        else:
            self.logprint(fd,"The following potential problems were detected:")
            for i,x in enumerate(status):
               self.logprint(fd,"#%(number)d: %(problem)s" % { "number" : i, "problem" : x })
            return False

########################################################

class ImportFunction(commands.CobblerFunction):
    
    def help_me(self):
        return HELP_FORMAT % ("cobbler import","[ARGS]")

    def command_name(self):
        return "import"

    def add_options(self, p, args):
        p.add_option("--arch",               dest="arch",               help="explicitly specify the architecture being imported (RECOMENDED)")
        p.add_option("--breed",              dest="breed",              help="explicitly specify the breed being imported (RECOMENDED)")
        p.add_option("--path",               dest="mirror",             help="local path or rsync location (REQUIRED)")
        p.add_option("--mirror",             dest="mirror_alt",         help="alias for --path")
        p.add_option("--name",               dest="name",               help="name, ex 'RHEL-5', (REQUIRED)")
        p.add_option("--available-as",       dest="available_as",       help="do not mirror, use this as install tree base")
        p.add_option("--kickstart",          dest="kickstart_file",     help="use the kickstart file specified as the profile's kickstart file, do not auto-assign")
        p.add_option("--rsync-flags",        dest="rsync_flags",        help="pass additional flags to rsync")

    def run(self):
        if self.options.mirror_alt and not self.options.mirror:
           self.options.mirror = self.options.mirror_alt
        if not self.options.mirror:
           raise CX(_("mirror is required"))
        if not self.options.name:
           raise CX(_("name is required"))
        return self.api.import_tree(
                self.options.mirror,
                self.options.name,
                network_root=self.options.available_as,
                kickstart_file=self.options.kickstart_file,
                rsync_flags=self.options.rsync_flags,
                arch=self.options.arch,
                breed=self.options.breed
        )


########################################################

class ReserializeFunction(commands.CobblerFunction):

    def help_me(self):
        return "" # hide

    def command_name(self):
        return "reserialize"

    def run(self):
        # already deserialized when API is instantiated
        # this just saves files in new config format (if any)
        return self.api.serialize()

########################################################

class ListFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler list","[ARGS]")

    def command_name(self):
        return "list"

    def add_options(self, p, args):
        p.add_option("--what",              dest="what",          default="all", help="all/distros/profiles/systems/repos")
     
    def run(self):
        if self.options.what not in [ "all", "distros", "profiles", "systems", "repos", "images" ]:
            raise CX(_("invalid value for --what"))
        if self.options.what in ["all"]:
            self.list_tree(self.api.distros(),0)
            self.list_tree(self.api.repos(),0)
            self.list_tree(self.api.images(),0)
        if self.options.what in ["distros"]:
            self.list_list(self.api.distros())
        if self.options.what in ["profiles"]:
            self.list_list(self.api.profiles())
        if self.options.what in ["systems" ]:
            self.list_list(self.api.systems())
        if self.options.what in ["repos"]:
            self.list_list(self.api.repos())
        if self.options.what in ["images"]:
            self.list_list(self.api.images())

########################################################

class StatusFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler status","[ARGS]")

    def command_name(self):
        return "status"

    def run(self):
        return self.api.status("text") # no other output modes supported yet

########################################################

class SyncFunction(commands.CobblerFunction):
    
    def add_options(self, p, args):
        p.add_option("--verbose", dest="verbose", action="store_true", help="run sync with more output")
     
    def help_me(self):
        return HELP_FORMAT % ("cobbler sync","")

    def command_name(self):
        return "sync"

    def run(self):
        return self.api.sync(verbose=self.options.verbose)

########################################################

class RepoSyncFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler reposync","[ARGS]")

    def command_name(self):
        return "reposync"

    def add_options(self, p, args):
        p.add_option("--only",           dest="only",             help="update only this repository name")
        p.add_option("--tries",          dest="tries",            help="try each repo this many times", default=1)
        p.add_option("--no-fail",        dest="nofail",           help="don't stop reposyncing if a failure occurs", action="store_true")

    def run(self):
        return self.api.reposync(self.options.only, tries=self.options.tries, nofail=self.options.nofail)

########################################################

class ValidateKsFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler validateks","")

    def command_name(self):
        return "validateks"
 
    def run(self):
        return self.api.validateks()

########################################################

class BuildIsoFunction(commands.CobblerFunction):

    def add_options(self,p,args): 
        p.add_option("--iso",      dest="isoname",  help="(OPTIONAL) output ISO to this path")
        p.add_option("--profiles", dest="profiles", help="(OPTIONAL) use these profiles only")
        p.add_option("--systems",  dest="systems",  help="(OPTIONAL) use these systems only")
        p.add_option("--tempdir",  dest="tempdir",  help="(OPTIONAL) working directory")
        p.add_option("--distro",   dest="distro",   help="(OPTIONAL) used with --standalone to create a distro-based ISO including all associated profiles/systems")
        p.add_option("--standalone", dest="standalone", action="store_true", help="(OPTIONAL) creates a standalone ISO with all required distro files on it")
        p.add_option("--source",   dest="source",   help="(OPTIONAL) used with --standalone to specify a source for the distribution files")

    def help_me(self):
       return HELP_FORMAT % ("cobbler buildiso","[ARGS]")
    
    def command_name(self):
       return "buildiso"

    def run(self):
       return self.api.build_iso(
           iso=self.options.isoname,
           profiles=self.options.profiles,
           systems=self.options.systems,
           tempdir=self.options.tempdir,
           distro=self.options.distro,
           standalone=self.options.standalone,
           source=self.options.source
       )

########################################################

class ReplicateFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler replicate","[ARGS]")

    def command_name(self):
        return "replicate"

    def add_options(self, p, args):
        p.add_option("--master",               dest="master",           help="Cobbler server to replicate from.")
        p.add_option("--include-systems",      dest="systems",          action="store_true", help="include systems in addition to distros, profiles, and repos")
        p.add_option("--full-data-sync",       dest="all",              action="store_true", help="rsync everything")
        p.add_option("--sync-kickstarts",      dest="kickstarts",       action="store_true", help="rsync kickstart templates")
        p.add_option("--sync-trees",           dest="trees",            action="store_true", help="rsync imported trees")
        p.add_option("--sync-triggers",        dest="triggers",         action="store_true", help="rsync trigger scripts")
        p.add_option("--sync-repos",           dest="repos",            action="store_true", help="rsync mirrored repo data")

    def run(self):
        return self.api.replicate(
             cobbler_master = self.options.master,
             sync_all = self.options.all,
             sync_kickstarts = self.options.kickstarts,
             sync_trees = self.options.trees,
             sync_repos = self.options.repos,
             sync_triggers = self.options.triggers,
             systems = self.options.systems
        )

########################################################

class AclFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler aclsetup","[ARGS]")

    def command_name(self):
        return "aclsetup"

    def add_options(self, p, args):
        p.add_option("--adduser",            dest="adduser",            help="give acls to this user")
        p.add_option("--addgroup",           dest="addgroup",           help="give acls to this group")
        p.add_option("--removeuser",         dest="removeuser",         help="remove acls from this user")
        p.add_option("--removegroup",        dest="removegroup",        help="remove acls from this group")

    def run(self):
        return self.api.acl_config(
            self.options.adduser,
            self.options.addgroup,
            self.options.removeuser,
            self.options.removegroup
        )

########################################################

class VersionFunction(commands.CobblerFunction):

    def help_me(self):
        return HELP_FORMAT % ("cobbler version","")

    def command_name(self):
        return "version"

    def add_options(self, p, args):
        pass

    def run(self):
 
        # --version output format borrowed from ls, so it must be right :)

        versions = self.api.version(extended=True)

        print "cobbler %s" % versions["version"]
        print ""

        # print extended info if available, which is useful for devel branch testing
        print "build date  : %s" % versions["builddate"]
        if versions.get("gitstamp","?") != "?":
           print "git hash    : %s" % versions["gitstamp"]
        if versions.get("gitdate", "?") != "?":
           print "git date    : %s" % versions["gitdate"]

        print ""

        print "Copyright (C) 2006-2008 Red Hat, Inc."
        print "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>"
        print "This is free software: you are free to change and redistribute it."
        print "There is NO WARRANTY, to the extent permitted by law."

        print ""
        
        print "Written by Michael DeHaan."

        return True

    
########################################################
# MODULE HOOKS

def register():
    """
    The mandatory cobbler module registration hook.
    """
    return "cli"

def cli_functions(api):
    return [
       BuildIsoFunction(api), 
       CheckFunction(api), ImportFunction(api), ReserializeFunction(api),
       ListFunction(api), StatusFunction(api),
       SyncFunction(api), RepoSyncFunction(api), ValidateKsFunction(api),
       ReplicateFunction(api), AclFunction(api),
       VersionFunction(api), HardLinkFunction(api)
    ]
    return []