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.
20 from optparse import SUPPRESS_HELP
23 from mic.utils import cmdln, errors, rpmmisc
24 from mic.conf import configmgr
25 from mic.plugin import pluginmgr
28 class Creator(cmdln.Cmdln):
29 """${name}: create an image
32 ${name} SUBCOMMAND <ksfile> [OPTS]
38 name = 'mic create(cr)'
40 def __init__(self, *args, **kwargs):
41 cmdln.Cmdln.__init__(self, *args, **kwargs)
44 # get cmds from pluginmgr
45 # mix-in do_subcmd interface
46 for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
47 if not hasattr(klass, 'do_create'):
48 msger.warning("Unsurpport subcmd: %s" % subcmd)
51 func = getattr(klass, 'do_create')
52 setattr(self.__class__, "do_"+subcmd, func)
53 self._subcmds.append(subcmd)
55 def get_optparser(self):
56 optparser = cmdln.CmdlnOptionParser(self)
57 optparser.add_option('-d', '--debug', action='store_true',
60 optparser.add_option('-v', '--verbose', action='store_true',
63 optparser.add_option('', '--logfile', type='string', dest='logfile',
65 help='Path of logfile')
66 optparser.add_option('-c', '--config', type='string', dest='config',
68 help='Specify config file for mic')
69 optparser.add_option('-k', '--cachedir', type='string', action='store',
70 dest='cachedir', default=None,
71 help='Cache directory to store the downloaded')
72 optparser.add_option('-o', '--outdir', type='string', action='store',
73 dest='outdir', default=None,
74 help='Output directory')
75 optparser.add_option('-A', '--arch', type='string', dest='arch',
77 help='Specify repo architecture')
78 optparser.add_option('', '--release', type='string', dest='release',
79 default=None, metavar='RID',
80 help='Generate a release of RID with all necessary'
81 ' files, when @BUILD_ID@ is contained in '
82 'kickstart file, it will be replaced by RID')
83 optparser.add_option("", "--record-pkgs", type="string",
84 dest="record_pkgs", default=None,
85 help='Record the info of installed packages, '
86 'multiple values can be specified which '
87 'joined by ",", valid values: "name", '
88 '"content", "license", "vcs"')
89 optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr',
91 help='Specify backend package manager')
92 optparser.add_option('', '--local-pkgs-path', type='string',
93 dest='local_pkgs_path', default=None,
94 help='Path for local pkgs(rpms) to be installed')
95 optparser.add_option('', '--runtime', type='string',
96 dest='runtime', default=None,
97 help='Specify runtime mode, avaiable: bootstrap, native')
98 # --taring-to is alias to --pack-to
99 optparser.add_option('', '--taring-to', type='string',
100 dest='pack_to', default=None,
102 optparser.add_option('', '--pack-to', type='string',
103 dest='pack_to', default=None,
104 help='Pack the images together into the specified'
105 ' achive, extension supported: .zip, .tar, '
106 '.tar.gz, .tar.bz2, etc. by default, .tar '
108 optparser.add_option('', '--copy-kernel', action='store_true',
110 help='Copy kernel files from image /boot directory'
111 ' to the image output directory.')
112 optparser.add_option('', '--install-pkgs', type='string', action='store',
113 dest='install_pkgs', default=None,
114 help='Specify what type of packages to be installed,'
115 ' valid: source, debuginfo, debugsource')
116 optparser.add_option('', '--check-pkgs', type='string', action='store',
117 dest='check_pkgs', default=[],
118 help='Check if given packages would be installed, '
119 'packages should be separated by comma')
120 optparser.add_option('', '--tmpfs', action='store_true', dest='enabletmpfs',
121 help='Setup tmpdir as tmpfs to accelerate, experimental'
122 ' feature, use it if you have more than 4G memory')
123 optparser.add_option('', '--repourl', action='append',
124 dest='repourl', default=[],
126 optparser.add_option('-R', '--repo', action='append',
127 dest='repo', default=[],
129 optparser.add_option('', '--ignore-ksrepo', action='store_true',
130 dest='ignore_ksrepo', default=False,
132 optparser.add_option('', '--strict_mode', action='store_true',
133 dest='strict_mode', default=False,
134 help='Abort creation of image, if there are some errors'
135 ' during rpm installation. ')
138 def preoptparse(self, argv):
139 optparser = self.get_optparser()
146 if arg in ('-h', '--help'):
149 elif optparser.has_option(arg):
152 if optparser.get_option(arg).takes_value():
154 largs.append(argv.pop(0))
156 raise errors.Usage("option %s requires arguments" % arg)
159 if arg.startswith("--"):
161 opt = arg.split("=")[0]
164 elif arg.startswith("-") and len(arg) > 2:
169 if opt and optparser.has_option(opt):
176 def postoptparse(self):
177 abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
179 if self.options.verbose:
180 msger.set_loglevel('VERBOSE')
181 if self.options.debug:
182 msger.set_loglevel('DEBUG')
184 if self.options.logfile:
185 logfile_abs_path = abspath(self.options.logfile)
186 if os.path.isdir(logfile_abs_path):
187 raise errors.Usage("logfile's path %s should be file"
188 % self.options.logfile)
189 configmgr.create['logfile'] = logfile_abs_path
190 configmgr.set_logfile()
192 if self.options.config:
194 configmgr._siteconf = self.options.config
196 if self.options.outdir is not None:
197 configmgr.create['outdir'] = abspath(self.options.outdir)
198 if self.options.cachedir is not None:
199 configmgr.create['cachedir'] = abspath(self.options.cachedir)
200 os.environ['ZYPP_LOCKFILE_ROOT'] = configmgr.create['cachedir']
202 for cdir in ('outdir', 'cachedir'):
203 if os.path.exists(configmgr.create[cdir]) \
204 and not os.path.isdir(configmgr.create[cdir]):
205 raise errors.Usage('Invalid directory specified: %s' \
206 % configmgr.create[cdir])
207 if not os.path.exists(configmgr.create[cdir]):
208 os.makedirs(configmgr.create[cdir])
209 if os.getenv('SUDO_UID', '') and os.getenv('SUDO_GID', ''):
210 os.chown(configmgr.create[cdir],
211 int(os.getenv('SUDO_UID')),
212 int(os.getenv('SUDO_GID')))
214 if self.options.local_pkgs_path is not None:
215 if not os.path.exists(self.options.local_pkgs_path):
216 raise errors.Usage('Local pkgs directory: \'%s\' not exist' \
217 % self.options.local_pkgs_path)
218 configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
220 if self.options.release:
221 configmgr.create['release'] = self.options.release.rstrip('/')
223 if self.options.record_pkgs:
224 configmgr.create['record_pkgs'] = []
225 for infotype in self.options.record_pkgs.split(','):
226 if infotype not in ('name', 'content', 'license', 'vcs'):
227 raise errors.Usage('Invalid pkg recording: %s, valid ones:'
228 ' "name", "content", "license", "vcs"' \
231 configmgr.create['record_pkgs'].append(infotype)
233 if self.options.strict_mode:
234 configmgr.create['strict_mode'] = self.options.strict_mode
235 if self.options.arch is not None:
236 supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True)
237 if self.options.arch in supported_arch:
238 configmgr.create['arch'] = self.options.arch
240 raise errors.Usage('Invalid architecture: "%s".\n'
241 ' Supported architectures are: \n'
242 ' %s' % (self.options.arch,
243 ', '.join(supported_arch)))
245 if self.options.pkgmgr is not None:
246 configmgr.create['pkgmgr'] = self.options.pkgmgr
248 if self.options.runtime:
249 configmgr.set_runtime(self.options.runtime)
251 if self.options.pack_to is not None:
252 configmgr.create['pack_to'] = self.options.pack_to
254 if self.options.copy_kernel:
255 configmgr.create['copy_kernel'] = self.options.copy_kernel
257 if self.options.install_pkgs:
258 configmgr.create['install_pkgs'] = []
259 for pkgtype in self.options.install_pkgs.split(','):
260 if pkgtype not in ('source', 'debuginfo', 'debugsource'):
261 raise errors.Usage('Invalid parameter specified: "%s", '
262 'valid values: source, debuginfo, '
263 'debusource' % pkgtype)
265 configmgr.create['install_pkgs'].append(pkgtype)
267 if self.options.check_pkgs:
268 for pkg in self.options.check_pkgs.split(','):
269 configmgr.create['check_pkgs'].append(pkg)
271 if self.options.enabletmpfs:
272 configmgr.create['enabletmpfs'] = self.options.enabletmpfs
274 if self.options.repourl:
275 for item in self.options.repourl:
277 key, val = item.split('=')
280 configmgr.create['repourl'][key] = val
282 if self.options.repo:
283 for optvalue in self.options.repo:
285 for item in optvalue.split(';'):
287 key, val = item.split('=')
290 repo[key.strip()] = val.strip()
292 configmgr.create['extrarepos'][repo['name']] = repo
294 if self.options.ignore_ksrepo:
295 configmgr.create['ignore_ksrepo'] = self.options.ignore_ksrepo
297 def main(self, argv=None):
301 argv = argv[:] # don't modify caller's list
303 self.optparser = self.get_optparser()
306 argv = self.preoptparse(argv)
307 self.options, args = self.optparser.parse_args(argv)
309 except cmdln.CmdlnUserError, ex:
310 msg = "%s: %s\nTry '%s help' for info.\n"\
311 % (self.name, ex, self.name)
312 raise errors.Usage(msg)
314 except cmdln.StopOptionProcessing, ex:
317 # optparser=None means no process for opts
318 self.options, args = None, argv[1:]
321 return self.emptyline()
325 return self.cmd(args)
327 def precmd(self, argv): # check help before cmd
329 if '-h' in argv or '?' in argv or '--help' in argv or 'help' in argv:
333 return ['help', argv[0]]
335 if os.geteuid() != 0:
336 msger.error("Root permission is required, abort")
339 w = pwd.getpwuid(os.geteuid())
341 msger.warning("Might fail in compressing stage for undetermined user")
345 def do_auto(self, subcmd, opts, *args):
346 """${cmd_name}: auto detect image type from magic header
349 ${name} ${cmd_name} <ksfile>
353 def parse_magic_line(re_str, pstr, ptype='mic'):
354 ptn = re.compile(re_str)
356 if not m or not m.groups():
359 inline_argv = m.group(1).strip()
361 m2 = re.search('(?P<format>\w+)', inline_argv)
362 elif ptype == 'mic2':
363 m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)',
369 cmdname = m2.group('format')
370 inline_argv = inline_argv.replace(m2.group(0), '')
371 return (cmdname, inline_argv)
376 raise errors.Usage("Extra arguments given")
378 if not os.path.exists(args[0]):
379 raise errors.CreatorError("Can't find the file: %s" % args[0])
381 with open(args[0], 'r') as rf:
382 first_line = rf.readline()
384 mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-'
385 mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-'
387 result = parse_magic_line(mic_re, first_line, 'mic') \
388 or parse_magic_line(mic2_re, first_line, 'mic2')
390 raise errors.KsError("Invalid magic line in file: %s" % args[0])
392 if result[0] not in self._subcmds:
393 raise errors.KsError("Unsupport format '%s' in %s"
394 % (result[0], args[0]))
396 argv = ' '.join(result + args).split()