update release help message
[tools/mic.git] / mic / creator.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os, sys
19 from optparse import SUPPRESS_HELP
20
21 from mic import configmgr, pluginmgr, msger
22 from mic.utils import cmdln, errors
23
24 class Creator(cmdln.Cmdln):
25     """${name}: create an image
26
27     usage:
28         ${name} SUBCOMMAND [OPTS] [ARGS..]
29
30     ${command_list}
31     ${option_list}
32     """
33
34     name = 'mic create(cr)'
35
36     def __init__(self, *args, **kwargs):
37         cmdln.Cmdln.__init__(self, *args, **kwargs)
38
39         # load configmgr
40         self.configmgr = configmgr.getConfigMgr()
41
42         # load pluginmgr
43         self.pluginmgr = pluginmgr.PluginMgr()
44         self.plugincmds = self.pluginmgr.get_plugins('imager')
45
46         # mix-in do_subcmd interface
47         for subcmd, klass in self.plugincmds.iteritems():
48             if not hasattr(klass, 'do_create'):
49                 msger.warning("Unsurpport subcmd: %s" % subcmd)
50                 continue
51
52             func = getattr(klass, 'do_create')
53             setattr(self.__class__, "do_"+subcmd, func)
54
55     def get_optparser(self):
56         optparser = cmdln.CmdlnOptionParser(self)
57         optparser.add_option('-d', '--debug', action='store_true', dest='debug', help=SUPPRESS_HELP)
58         optparser.add_option('-v', '--verbose', action='store_true', dest='verbose', help=SUPPRESS_HELP)
59         optparser.add_option('', '--logfile', type='string', dest='logfile', default=None, help='Path of logfile')
60         optparser.add_option('-c', '--config', type='string', dest='config', default=None, help='Specify config file for mic')
61         optparser.add_option('-k', '--cachedir', type='string', action='store', dest='cachedir', default=None, help='Cache directory to store the downloaded')
62         optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='Output directory')
63         optparser.add_option('-A', '--arch', type='string', dest='arch', default=None, help='Specify repo architecture')
64         optparser.add_option('', '--release', type='string', dest='release', default=None, metavar='RID', help='Generate a release of RID with all neccessary files,when @BUILD_ID is contained in kickstart file, it will be replaced by RID')
65         optparser.add_option("", "--record-pkgs", type="string", dest="record_pkgs", default=None,
66                              help='Record the info of installed packages, multiple values can be specified which joined by ",", valid values: "name", "content", "license"')
67         optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr', default=None, help='Specify backend package manager')
68         optparser.add_option('', '--local-pkgs-path', type='string', dest='local_pkgs_path', default=None, help='Path for local pkgs(rpms) to be installed')
69         return optparser
70
71     def preoptparse(self, argv):
72         optparser = self.get_optparser()
73
74         largs = []
75         rargs = []
76         while argv:
77             arg = argv.pop(0)
78
79             if arg in ('-h', '--help'):
80                 rargs.append(arg)
81
82             elif optparser.has_option(arg):
83                 largs.append(arg)
84
85                 if optparser.get_option(arg).takes_value():
86                     try:
87                         largs.append(argv.pop(0))
88                     except IndexError:
89                         raise errors.Usage("%s option requires an argument" % arg)
90
91             else:
92                 if arg.startswith("--"):
93                     if "=" in arg:
94                         opt = arg.split("=")[0]
95                     else:
96                         opt = None
97                 elif arg.startswith("-") and len(arg) > 2:
98                     opt = arg[0:2]
99                 else:
100                     opt = None
101
102                 if opt and optparser.has_option(opt):
103                     largs.append(arg)
104                 else:
105                     rargs.append(arg)
106
107         return largs + rargs
108
109     def postoptparse(self):
110         if self.options.verbose:
111             msger.set_loglevel('verbose')
112         if self.options.debug:
113             msger.set_loglevel('debug')
114
115         if self.options.config:
116             self.configmgr.reset()
117             self.configmgr._siteconf = self.options.config
118
119         if self.options.outdir is not None:
120             self.configmgr.create['outdir'] = self.options.outdir
121         if self.options.cachedir is not None:
122             self.configmgr.create['cachedir'] = self.options.cachedir
123         if self.options.local_pkgs_path is not None:
124             self.configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
125
126         if self.options.release:
127             self.configmgr.create['release'] = self.options.release
128
129         if self.options.record_pkgs:
130             self.configmgr.create['record_pkgs'] = []
131             for infotype in self.options.record_pkgs.split(','):
132                 if infotype not in ('name', 'content', 'license'):
133                     raise errors.Usage('Invalid pkg recording: %s, valid ones: "name", "content", "license"' % infotype)
134
135                 self.configmgr.create['record_pkgs'].append(infotype)
136
137         if self.options.arch is not None:
138             self.configmgr.create['arch'] = self.options.arch
139
140         if self.options.pkgmgr is not None:
141             self.configmgr.create['pkgmgr'] = self.options.pkgmgr
142
143         if self.options.logfile:
144             msger.set_interactive(False)
145             msger.set_logfile(self.options.logfile)
146
147     def main(self, argv=None):
148         if argv is None:
149             argv = sys.argv
150         else:
151             argv = argv[:] # don't modify caller's list
152
153         self.optparser = self.get_optparser()
154         if self.optparser:
155             try:
156                 argv = self.preoptparse(argv)
157                 self.options, args = self.optparser.parse_args(argv)
158
159             except cmdln.CmdlnUserError, ex:
160                 msg = "%s: %s\nTry '%s help' for info.\n"\
161                       % (self.name, ex, self.name)
162                 msger.error(msg)
163
164             except cmdln.StopOptionProcessing, ex:
165                 return 0
166         else:
167             # optparser=None means no process for opts
168             self.options, args = None, argv[1:]
169
170         self.postoptparse()
171
172         if not args:
173             return self.emptyline()
174
175         if os.geteuid() != 0:
176             msger.error('Root permission is required to continue, abort')
177
178         return self.cmd(args)
179