add strict_mode(MHA-1115)
[platform/upstream/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, re
19 import pwd
20 from optparse import SUPPRESS_HELP
21
22 from mic import msger
23 from mic.utils import cmdln, errors, rpmmisc
24 from mic.conf import configmgr
25 from mic.plugin import pluginmgr
26
27
28 class Creator(cmdln.Cmdln):
29     """${name}: create an image
30
31     Usage:
32         ${name} SUBCOMMAND <ksfile> [OPTS]
33
34     ${command_list}
35     ${option_list}
36     """
37
38     name = 'mic create(cr)'
39
40     def __init__(self, *args, **kwargs):
41         cmdln.Cmdln.__init__(self, *args, **kwargs)
42         self._subcmds = []
43
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)
49                 continue
50
51             func = getattr(klass, 'do_create')
52             setattr(self.__class__, "do_"+subcmd, func)
53             self._subcmds.append(subcmd)
54
55     def get_optparser(self):
56         optparser = cmdln.CmdlnOptionParser(self)
57         optparser.add_option('-d', '--debug', action='store_true',
58                              dest='debug',
59                              help=SUPPRESS_HELP)
60         optparser.add_option('-v', '--verbose', action='store_true',
61                              dest='verbose',
62                              help=SUPPRESS_HELP)
63         optparser.add_option('', '--logfile', type='string', dest='logfile',
64                              default=None,
65                              help='Path of logfile')
66         optparser.add_option('-c', '--config', type='string', dest='config',
67                              default=None,
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',
76                              default=None,
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',
90                              default=None,
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,
101                              help=SUPPRESS_HELP)
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 '
107                                   'will be used')
108         optparser.add_option('', '--copy-kernel', action='store_true',
109                              dest='copy_kernel',
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=[],
125                              help=SUPPRESS_HELP)
126         optparser.add_option('-R', '--repo', action='append',
127                              dest='repo', default=[],
128                              help=SUPPRESS_HELP)
129         optparser.add_option('', '--ignore-ksrepo', action='store_true',
130                              dest='ignore_ksrepo', default=False,
131                              help=SUPPRESS_HELP)
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. ')
136         return optparser
137
138     def preoptparse(self, argv):
139         optparser = self.get_optparser()
140
141         largs = []
142         rargs = []
143         while argv:
144             arg = argv.pop(0)
145
146             if arg in ('-h', '--help'):
147                 rargs.append(arg)
148
149             elif optparser.has_option(arg):
150                 largs.append(arg)
151
152                 if optparser.get_option(arg).takes_value():
153                     try:
154                         largs.append(argv.pop(0))
155                     except IndexError:
156                         raise errors.Usage("option %s requires arguments" % arg)
157
158             else:
159                 if arg.startswith("--"):
160                     if "=" in arg:
161                         opt = arg.split("=")[0]
162                     else:
163                         opt = None
164                 elif arg.startswith("-") and len(arg) > 2:
165                     opt = arg[0:2]
166                 else:
167                     opt = None
168
169                 if opt and optparser.has_option(opt):
170                     largs.append(arg)
171                 else:
172                     rargs.append(arg)
173
174         return largs + rargs
175
176     def postoptparse(self):
177         abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
178
179         if self.options.verbose:
180             msger.set_loglevel('VERBOSE')
181         if self.options.debug:
182             msger.set_loglevel('DEBUG')
183
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()
191
192         if self.options.config:
193             configmgr.reset()
194             configmgr._siteconf = self.options.config
195
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']
201
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')))
213
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
219
220         if self.options.release:
221             configmgr.create['release'] = self.options.release.rstrip('/')
222
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"' \
229                                        % infotype)
230
231                 configmgr.create['record_pkgs'].append(infotype)
232
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
239             else:
240                 raise errors.Usage('Invalid architecture: "%s".\n'
241                                    '  Supported architectures are: \n'
242                                    '  %s' % (self.options.arch,
243                                                ', '.join(supported_arch)))
244
245         if self.options.pkgmgr is not None:
246             configmgr.create['pkgmgr'] = self.options.pkgmgr
247
248         if self.options.runtime:
249             configmgr.set_runtime(self.options.runtime)
250
251         if self.options.pack_to is not None:
252             configmgr.create['pack_to'] = self.options.pack_to
253
254         if self.options.copy_kernel:
255             configmgr.create['copy_kernel'] = self.options.copy_kernel
256
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)
264
265                 configmgr.create['install_pkgs'].append(pkgtype)
266
267         if self.options.check_pkgs:
268             for pkg in self.options.check_pkgs.split(','):
269                 configmgr.create['check_pkgs'].append(pkg)
270
271         if self.options.enabletmpfs:
272             configmgr.create['enabletmpfs'] = self.options.enabletmpfs
273
274         if self.options.repourl:
275             for item in self.options.repourl:
276                 try:
277                     key, val = item.split('=')
278                 except:
279                     continue
280                 configmgr.create['repourl'][key] = val
281
282         if self.options.repo:
283             for optvalue in self.options.repo:
284                 repo = {}
285                 for item in optvalue.split(';'):
286                     try:
287                         key, val = item.split('=')
288                     except:
289                         continue
290                     repo[key.strip()] = val.strip()
291                 if 'name' in repo:
292                     configmgr.create['extrarepos'][repo['name']] = repo
293
294         if self.options.ignore_ksrepo:
295             configmgr.create['ignore_ksrepo'] = self.options.ignore_ksrepo
296
297     def main(self, argv=None):
298         if argv is None:
299             argv = sys.argv
300         else:
301             argv = argv[:] # don't modify caller's list
302
303         self.optparser = self.get_optparser()
304         if self.optparser:
305             try:
306                 argv = self.preoptparse(argv)
307                 self.options, args = self.optparser.parse_args(argv)
308
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)
313
314             except cmdln.StopOptionProcessing, ex:
315                 return 0
316         else:
317             # optparser=None means no process for opts
318             self.options, args = None, argv[1:]
319
320         if not args:
321             return self.emptyline()
322
323         self.postoptparse()
324
325         return self.cmd(args)
326
327     def precmd(self, argv): # check help before cmd
328
329         if '-h' in argv or '?' in argv or '--help' in argv or 'help' in argv:
330             return argv
331
332         if len(argv) == 1:
333             return ['help', argv[0]]
334
335         if os.geteuid() != 0:
336             msger.error("Root permission is required, abort")
337
338         try:
339             w = pwd.getpwuid(os.geteuid())
340         except KeyError:
341             msger.warning("Might fail in compressing stage for undetermined user")
342
343         return argv
344
345     def do_auto(self, subcmd, opts, *args):
346         """${cmd_name}: auto detect image type from magic header
347
348         Usage:
349             ${name} ${cmd_name} <ksfile>
350
351         ${cmd_option_list}
352         """
353         def parse_magic_line(re_str, pstr, ptype='mic'):
354             ptn = re.compile(re_str)
355             m = ptn.match(pstr)
356             if not m or not m.groups():
357                 return None
358
359             inline_argv = m.group(1).strip()
360             if ptype == 'mic':
361                 m2 = re.search('(?P<format>\w+)', inline_argv)
362             elif ptype == 'mic2':
363                 m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)',
364                                inline_argv)
365             else:
366                 return None
367
368             if m2:
369                 cmdname = m2.group('format')
370                 inline_argv = inline_argv.replace(m2.group(0), '')
371                 return (cmdname, inline_argv)
372
373             return None
374
375         if len(args) != 1:
376             raise errors.Usage("Extra arguments given")
377
378         if not os.path.exists(args[0]):
379             raise errors.CreatorError("Can't find the file: %s" % args[0])
380
381         with open(args[0], 'r') as rf:
382             first_line = rf.readline()
383
384         mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-'
385         mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-'
386
387         result = parse_magic_line(mic_re, first_line, 'mic') \
388                  or parse_magic_line(mic2_re, first_line, 'mic2')
389         if not result:
390             raise errors.KsError("Invalid magic line in file: %s" % args[0])
391
392         if result[0] not in self._subcmds:
393             raise errors.KsError("Unsupport format '%s' in %s"
394                                  % (result[0], args[0]))
395
396         argv = ' '.join(result + args).split()
397         self.main(argv)
398