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
21 from mic import msger, rt_util
22 from mic.utils import cmdln, errors, rpmmisc
23 from mic.conf import configmgr
24 from mic.plugin import pluginmgr
27 class Creator(cmdln.Cmdln):
28 """${name}: create an image
31 ${name} SUBCOMMAND <ksfile> [OPTS]
37 name = 'mic create(cr)'
39 def __init__(self, *args, **kwargs):
40 cmdln.Cmdln.__init__(self, *args, **kwargs)
43 # get cmds from pluginmgr
44 # mix-in do_subcmd interface
45 for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
46 if not hasattr(klass, 'do_create'):
47 msger.warning("Unsurpport subcmd: %s" % subcmd)
50 func = getattr(klass, 'do_create')
51 setattr(self.__class__, "do_"+subcmd, func)
52 self._subcmds.append(subcmd)
54 def get_optparser(self):
55 optparser = cmdln.CmdlnOptionParser(self)
56 optparser.add_option('-d', '--debug', action='store_true',
59 optparser.add_option('-v', '--verbose', action='store_true',
62 optparser.add_option('', '--logfile', type='string', dest='logfile',
64 help='Path of logfile')
65 optparser.add_option('-c', '--config', type='string', dest='config',
67 help='Specify config file for mic')
68 optparser.add_option('-k', '--cachedir', type='string', action='store',
69 dest='cachedir', default=None,
70 help='Cache directory to store the downloaded')
71 optparser.add_option('-o', '--outdir', type='string', action='store',
72 dest='outdir', default=None,
73 help='Output directory')
74 optparser.add_option('-A', '--arch', type='string', dest='arch',
76 help='Specify repo architecture')
77 optparser.add_option('', '--release', type='string', dest='release',
78 default=None, metavar='RID',
79 help='Generate a release of RID with all necessary'
80 ' files, when @BUILD_ID@ is contained in '
81 'kickstart file, it will be replaced by RID')
82 optparser.add_option("", "--record-pkgs", type="string",
83 dest="record_pkgs", default=None,
84 help='Record the info of installed packages, '
85 'multiple values can be specified which '
86 'joined by ",", valid values: "name", '
87 '"content", "license", "vcs"')
88 optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr',
90 help='Specify backend package manager')
91 optparser.add_option('', '--local-pkgs-path', type='string',
92 dest='local_pkgs_path', default=None,
93 help='Path for local pkgs(rpms) to be installed')
94 optparser.add_option('', '--runtime', type='string',
95 dest='runtime', default=None,
96 help='Specify runtime mode, avaiable: bootstrap, native')
97 # --taring-to is alias to --pack-to
98 optparser.add_option('', '--taring-to', type='string',
99 dest='pack_to', default=None,
101 optparser.add_option('', '--pack-to', type='string',
102 dest='pack_to', default=None,
103 help='Pack the images together into the specified'
104 ' achive, extension supported: .zip, .tar, '
105 '.tar.gz, .tar.bz2, etc. by default, .tar '
107 optparser.add_option('', '--copy-kernel', action='store_true',
109 help='Copy kernel files from image /boot directory'
110 ' to the image output directory.')
111 optparser.add_option('', '--install-pkgs', type='string', action='store',
112 dest='install_pkgs', default=None,
113 help='Specify what type of packages to be installed,'
114 ' valid: source, debuginfo, debugsource')
115 optparser.add_option('', '--repourl', action='append',
116 dest='repourl', default=[],
120 def preoptparse(self, argv):
121 optparser = self.get_optparser()
128 if arg in ('-h', '--help'):
131 elif optparser.has_option(arg):
134 if optparser.get_option(arg).takes_value():
136 largs.append(argv.pop(0))
138 raise errors.Usage("option %s requires arguments" % arg)
141 if arg.startswith("--"):
143 opt = arg.split("=")[0]
146 elif arg.startswith("-") and len(arg) > 2:
151 if opt and optparser.has_option(opt):
158 def postoptparse(self):
159 abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
161 if self.options.verbose:
162 msger.set_loglevel('verbose')
163 if self.options.debug:
164 msger.set_loglevel('debug')
166 if self.options.logfile:
167 logfile_abs_path = abspath(self.options.logfile)
168 if os.path.isdir(logfile_abs_path):
169 raise errors.Usage("logfile's path %s should be file"
170 % self.options.logfile)
171 if not os.path.exists(os.path.dirname(logfile_abs_path)):
172 os.makedirs(os.path.dirname(logfile_abs_path))
173 msger.set_interactive(False)
174 msger.set_logfile(logfile_abs_path)
175 configmgr.create['logfile'] = self.options.logfile
177 if self.options.config:
179 configmgr._siteconf = self.options.config
181 if self.options.outdir is not None:
182 configmgr.create['outdir'] = abspath(self.options.outdir)
183 if self.options.cachedir is not None:
184 configmgr.create['cachedir'] = abspath(self.options.cachedir)
185 os.environ['ZYPP_LOCKFILE_ROOT'] = configmgr.create['cachedir']
187 for cdir in ('outdir', 'cachedir'):
188 if os.path.exists(configmgr.create[cdir]) \
189 and not os.path.isdir(configmgr.create[cdir]):
190 msger.error('Invalid directory specified: %s' \
191 % configmgr.create[cdir])
193 if self.options.local_pkgs_path is not None:
194 if not os.path.exists(self.options.local_pkgs_path):
195 msger.error('Local pkgs directory: \'%s\' not exist' \
196 % self.options.local_pkgs_path)
197 configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
199 if self.options.release:
200 configmgr.create['release'] = self.options.release
202 if self.options.record_pkgs:
203 configmgr.create['record_pkgs'] = []
204 for infotype in self.options.record_pkgs.split(','):
205 if infotype not in ('name', 'content', 'license', 'vcs'):
206 raise errors.Usage('Invalid pkg recording: %s, valid ones:'
207 ' "name", "content", "license", "vcs"' \
210 configmgr.create['record_pkgs'].append(infotype)
212 if self.options.arch is not None:
213 supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True)
214 if self.options.arch in supported_arch:
215 configmgr.create['arch'] = self.options.arch
217 raise errors.Usage('Invalid architecture: "%s".\n'
218 ' Supported architectures are: \n'
219 ' %s' % (self.options.arch,
220 ', '.join(supported_arch)))
222 if self.options.pkgmgr is not None:
223 configmgr.create['pkgmgr'] = self.options.pkgmgr
225 if self.options.runtime:
226 configmgr.set_runtime(self.options.runtime)
228 if self.options.pack_to is not None:
229 configmgr.create['pack_to'] = self.options.pack_to
231 if self.options.copy_kernel:
232 configmgr.create['copy_kernel'] = self.options.copy_kernel
234 if self.options.install_pkgs:
235 configmgr.create['install_pkgs'] = []
236 for pkgtype in self.options.install_pkgs.split(','):
237 if pkgtype not in ('source', 'debuginfo', 'debugsource'):
238 raise errors.Usage('Invalid parameter specified: "%s", '
239 'valid values: source, debuginfo, '
240 'debusource' % pkgtype)
242 configmgr.create['install_pkgs'].append(pkgtype)
244 if self.options.repourl:
245 for item in self.options.repourl:
247 key, val = item.split('=')
250 configmgr.create['repourl'][key] = val
252 def main(self, argv=None):
256 argv = argv[:] # don't modify caller's list
258 self.optparser = self.get_optparser()
261 argv = self.preoptparse(argv)
262 self.options, args = self.optparser.parse_args(argv)
264 except cmdln.CmdlnUserError, ex:
265 msg = "%s: %s\nTry '%s help' for info.\n"\
266 % (self.name, ex, self.name)
269 except cmdln.StopOptionProcessing, ex:
272 # optparser=None means no process for opts
273 self.options, args = None, argv[1:]
276 return self.emptyline()
280 return self.cmd(args)
282 def precmd(self, argv): # check help before cmd
284 if '-h' in argv or '?' in argv or '--help' in argv or 'help' in argv:
288 return ['help', argv[0]]
290 if os.geteuid() != 0:
291 raise msger.error("Root permission is required, abort")
295 def do_auto(self, subcmd, opts, *args):
296 """${cmd_name}: auto detect image type from magic header
299 ${name} ${cmd_name} <ksfile>
303 def parse_magic_line(re_str, pstr, ptype='mic'):
304 ptn = re.compile(re_str)
306 if not m or not m.groups():
309 inline_argv = m.group(1).strip()
311 m2 = re.search('(?P<format>\w+)', inline_argv)
312 elif ptype == 'mic2':
313 m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)',
319 cmdname = m2.group('format')
320 inline_argv = inline_argv.replace(m2.group(0), '')
321 return (cmdname, inline_argv)
326 raise errors.Usage("Extra arguments given")
328 if not os.path.exists(args[0]):
329 raise errors.CreatorError("Can't find the file: %s" % args[0])
331 with open(args[0], 'r') as rf:
332 first_line = rf.readline()
334 mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-'
335 mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-'
337 result = parse_magic_line(mic_re, first_line, 'mic') \
338 or parse_magic_line(mic2_re, first_line, 'mic2')
340 raise errors.KsError("Invalid magic line in file: %s" % args[0])
342 if result[0] not in self._subcmds:
343 raise errors.KsError("Unsupport format '%s' in %s"
344 % (result[0], args[0]))
346 argv = ' '.join(result + args).split()