3 # Copyright (c) 2011 Intel, Inc.
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
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
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.
19 from optparse import SUPPRESS_HELP
22 from mic.utils import cmdln, errors, rpmmisc
23 from conf import configmgr
24 from plugin import pluginmgr
26 class Creator(cmdln.Cmdln):
27 """${name}: create an image
30 ${name} SUBCOMMAND <ksfile> [OPTS]
36 name = 'mic create(cr)'
38 def __init__(self, *args, **kwargs):
39 cmdln.Cmdln.__init__(self, *args, **kwargs)
42 # get cmds from pluginmgr
43 # mix-in do_subcmd interface
44 for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
45 if not hasattr(klass, 'do_create'):
46 msger.warning("Unsurpport subcmd: %s" % subcmd)
49 func = getattr(klass, 'do_create')
50 setattr(self.__class__, "do_"+subcmd, func)
51 self._subcmds.append(subcmd)
53 def get_optparser(self):
54 optparser = cmdln.CmdlnOptionParser(self)
55 optparser.add_option('-d', '--debug', action='store_true', dest='debug',
57 optparser.add_option('-v', '--verbose', action='store_true',
60 optparser.add_option('', '--logfile', type='string', dest='logfile',
62 help='Path of logfile')
63 optparser.add_option('-c', '--config', type='string', dest='config',
65 help='Specify config file for mic')
66 optparser.add_option('-k', '--cachedir', type='string', action='store',
67 dest='cachedir', default=None,
68 help='Cache directory to store the downloaded')
69 optparser.add_option('-o', '--outdir', type='string', action='store',
70 dest='outdir', default=None,
71 help='Output directory')
72 optparser.add_option('-A', '--arch', type='string', dest='arch',
74 help='Specify repo architecture')
75 optparser.add_option('', '--release', type='string', dest='release',
76 default=None, metavar='RID',
77 help='Generate a release of RID with all necessary'
78 ' files, when @BUILD_ID@ is contained in '
79 'kickstart file, it will be replaced by RID')
80 optparser.add_option("", "--record-pkgs", type="string",
81 dest="record_pkgs", default=None,
82 help='Record the info of installed packages, '
83 'multiple values can be specified which '
84 'joined by ",", valid values: "name", '
85 '"content", "license"')
86 optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr',
88 help='Specify backend package manager')
89 optparser.add_option('', '--local-pkgs-path', type='string',
90 dest='local_pkgs_path', default=None,
91 help='Path for local pkgs(rpms) to be installed')
92 optparser.add_option('', '--runtime', type='string',
93 dest='runtime', default=None,
94 help='Specify runtime mode, avaiable: bootstrap')
95 # --taring-to is alias to --pack-to
96 optparser.add_option('', '--taring-to', type='string',
97 dest='pack_to', default=None,
99 optparser.add_option('', '--pack-to', type='string',
100 dest='pack_to', default=None,
101 help='Pack the images together into the specified'
102 ' achive, extension supported: .zip, .tar, '
103 '.tar.gz, .tar.bz2, etc. by default, .tar '
105 optparser.add_option('', '--copy-kernel', action='store_true',
107 help='Copy kernel files from image /boot directory'
108 ' to the image output directory.')
111 def preoptparse(self, argv):
112 optparser = self.get_optparser()
119 if arg in ('-h', '--help'):
122 elif optparser.has_option(arg):
125 if optparser.get_option(arg).takes_value():
127 largs.append(argv.pop(0))
129 raise errors.Usage("%s option requires an argument" % arg)
132 if arg.startswith("--"):
134 opt = arg.split("=")[0]
137 elif arg.startswith("-") and len(arg) > 2:
142 if opt and optparser.has_option(opt):
149 def postoptparse(self):
150 abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
152 if self.options.verbose:
153 msger.set_loglevel('verbose')
154 if self.options.debug:
155 msger.set_loglevel('debug')
157 if self.options.logfile:
158 msger.set_interactive(False)
159 msger.set_logfile(self.options.logfile)
160 configmgr.create['logfile'] = self.options.logfile
162 if self.options.config:
164 configmgr._siteconf = self.options.config
166 if self.options.outdir is not None:
167 configmgr.create['outdir'] = abspath(self.options.outdir)
168 if self.options.cachedir is not None:
169 configmgr.create['cachedir'] = abspath(self.options.cachedir)
170 os.environ['ZYPP_LOCKFILE_ROOT'] = configmgr.create['cachedir']
171 if self.options.local_pkgs_path is not None:
172 if not os.path.exists(self.options.local_pkgs_path):
173 msger.error('Local pkgs directory: \'%s\' not exist' \
174 % self.options.local_pkgs_path)
175 configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
177 if self.options.release:
178 configmgr.create['release'] = self.options.release
180 if self.options.record_pkgs:
181 configmgr.create['record_pkgs'] = []
182 for infotype in self.options.record_pkgs.split(','):
183 if infotype not in ('name', 'content', 'license'):
184 raise errors.Usage('Invalid pkg recording: %s, valid ones:'
185 ' "name", "content", "license"' \
188 configmgr.create['record_pkgs'].append(infotype)
190 if self.options.arch is not None:
191 supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True)
192 if self.options.arch in supported_arch:
193 configmgr.create['arch'] = self.options.arch
195 raise errors.Usage('Invalid architecture: "%s".\n'
196 ' Supported architectures are: \n'
197 ' %s' % (self.options.arch,
198 ', '.join(supported_arch)))
200 if self.options.pkgmgr is not None:
201 configmgr.create['pkgmgr'] = self.options.pkgmgr
203 if self.options.runtime:
204 configmgr.create['runtime'] = self.options.runtime
206 if self.options.pack_to is not None:
207 configmgr.create['pack_to'] = self.options.pack_to
209 if self.options.copy_kernel:
210 configmgr.create['copy_kernel'] = self.options.copy_kernel
212 def main(self, argv=None):
216 argv = argv[:] # don't modify caller's list
218 self.optparser = self.get_optparser()
221 argv = self.preoptparse(argv)
222 self.options, args = self.optparser.parse_args(argv)
224 except cmdln.CmdlnUserError, ex:
225 msg = "%s: %s\nTry '%s help' for info.\n"\
226 % (self.name, ex, self.name)
229 except cmdln.StopOptionProcessing, ex:
232 # optparser=None means no process for opts
233 self.options, args = None, argv[1:]
236 return self.emptyline()
240 if os.geteuid() != 0 and args[0] != 'help':
241 msger.error('root permission is required to continue, abort')
243 return self.cmd(args)
245 def do_auto(self, subcmd, opts, *args):
246 """${cmd_name}: auto detect image type from magic header
249 ${name} ${cmd_name} <ksfile>
253 def parse_magic_line(re_str, pstr, ptype='mic'):
254 ptn = re.compile(re_str)
256 if not m or not m.groups():
259 inline_argv = m.group(1).strip()
261 m2 = re.search('(?P<format>\w+)', inline_argv)
262 elif ptype == 'mic2':
263 m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)',
269 cmdname = m2.group('format')
270 inline_argv = inline_argv.replace(m2.group(0), '')
271 return (cmdname, inline_argv)
276 self.do_help(['help', subcmd])
280 raise errors.Usage("Extra arguments given")
282 if not os.path.exists(args[0]):
283 raise errors.CreatorError("Can't find the file: %s" % args[0])
285 with open(args[0], 'r') as rf:
286 first_line = rf.readline()
288 mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-'
289 mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-'
291 result = parse_magic_line(mic_re, first_line, 'mic') \
292 or parse_magic_line(mic2_re, first_line, 'mic2')
294 raise errors.KsError("Invalid magic line in file: %s" % args[0])
296 if result[0] not in self._subcmds:
297 raise errors.KsError("Unsupport format '%s' in %s"
298 % (result[0], args[0]))
300 argv = ' '.join(result + args).split()