diff options
-rw-r--r-- | mic/conf.py (renamed from mic/configmgr.py) | 126 | ||||
-rw-r--r-- | mic/creator.py | 33 | ||||
-rw-r--r-- | plugins/backend/zypppkgmgr.py | 2 | ||||
-rw-r--r-- | plugins/imager/fs_plugin.py | 8 | ||||
-rw-r--r-- | plugins/imager/livecd_plugin.py | 8 | ||||
-rw-r--r-- | plugins/imager/liveusb_plugin.py | 8 | ||||
-rw-r--r-- | plugins/imager/loop_plugin.py | 8 | ||||
-rw-r--r-- | plugins/imager/raw_plugin.py | 8 | ||||
-rwxr-xr-x | tools/mic | 2 |
9 files changed, 93 insertions, 110 deletions
diff --git a/mic/configmgr.py b/mic/conf.py index b858f80..85759bd 100644 --- a/mic/configmgr.py +++ b/mic/conf.py @@ -18,59 +18,61 @@ import os, sys import ConfigParser -from mic import kickstart -from mic import msger -from mic.utils import misc, runner -from mic.utils import errors +import msger +import kickstart +from .utils import misc, runner, errors DEFAULT_GSITECONF = '/etc/mic/mic.conf' -DEFAULT_OUTDIR = './mic-output' -DEFAULT_TMPDIR = '/var/tmp/mic' -DEFAULT_CACHEDIR = DEFAULT_TMPDIR + '/cache' - -DEFAULT_CREATE = { - "tmpdir": DEFAULT_TMPDIR, - "cachedir": DEFAULT_CACHEDIR, - "outdir": DEFAULT_OUTDIR, - "arch": None, - "pkgmgr": "yum", - "name": "output", - "ksfile": None, - "ks": None, - "repomd": None, - "local_pkgs_path": None, - "release": None, - "logfile": None, - "record_pkgs": [], -} - class ConfigMgr(object): + DEFAULTS = {'common': {}, + 'create': { + "tmpdir": '/var/tmp/mic', + "cachedir": '/var/tmp/mic/cache', + "outdir": './mic-output', + "arch": None, # None means auto-detect + "pkgmgr": "yum", + "name": "output", + "ksfile": None, + "ks": None, + "repomd": None, + "local_pkgs_path": None, + "release": None, + "logfile": None, + "record_pkgs": [], + }, + 'chroot': {}, + 'convert': {}, + } + + # make the manager class as singleton + _instance = None + def __new__(cls, *args, **kwargs): + if not cls._instance: + cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs) + + return cls._instance + def __init__(self, ksconf=None, siteconf=None): # reset config options self.reset() - # initial options from siteconf - self._siteconf = siteconf - if not self.__siteconf: + if not siteconf: + # initial options from siteconf self._siteconf = DEFAULT_GSITECONF def reset(self): - self.common = {} - self.create = {} - self.convert = {} - self.chroot = {} self.__ksconf = None self.__siteconf = None - # initial create - for key in DEFAULT_CREATE.keys(): - self.create[key] = DEFAULT_CREATE[key] + # initialize the values with defaults + for sec, vals in self.DEFAULTS.iteritems(): + setattr(self, sec, vals) def __set_siteconf(self, siteconf): try: self.__siteconf = siteconf - self.parse_siteconf(siteconf) + self._parse_siteconf(siteconf) except ConfigParser.Error, error: raise errors.ConfigError("%s" % error) def __get_siteconf(self): @@ -82,48 +84,35 @@ class ConfigMgr(object): msger.error('Cannot find ks file: %s' % ksconf) self.__ksconf = ksconf - self.parse_kickstart(ksconf) + self._parse_kickstart(ksconf) def __get_ksconf(self): return self.__ksconf _ksconf = property(__get_ksconf, __set_ksconf) - def parse_siteconf(self, siteconf = None): + + def _parse_siteconf(self, siteconf): if not siteconf: return - from ConfigParser import SafeConfigParser - siteconf_parser = SafeConfigParser() if not os.path.exists(siteconf): raise errors.ConfigError("Failed to find config file: %s" % siteconf) - siteconf_parser.read(siteconf) - for option in siteconf_parser.options('common'): - value = siteconf_parser.get('common', option) - self.common[option] = value + parser = ConfigParser.SafeConfigParser() + parser.read(siteconf) - for option in siteconf_parser.options('create'): - value = siteconf_parser.get('create', option) - self.create[option] = value + for section in parser.sections(): + if section in self.DEFAULTS.keys(): + getattr(self, section).update(dict(parser.items(section))) - for option in siteconf_parser.options('convert'): - value = siteconf_parser.get('convert', option) - self.convert[option] = value - - for option in siteconf_parser.options('chroot'): - value = siteconf_parser.get('chroot', option) - self.chroot[option] = value - - def selinux_check(self, arch, ks): + def _selinux_check(self, arch, ks): """ If a user needs to use btrfs or creates ARM image, selinux must be disabled at start """ - paths = ["/usr/sbin/getenforce", - "/usr/bin/getenforce", - "/sbin/getenforce", - "/bin/getenforce", - "/usr/local/sbin/getenforce", - "/usr/locla/bin/getenforce" - ] - - for path in paths: + for path in ["/usr/sbin/getenforce", + "/usr/bin/getenforce", + "/sbin/getenforce", + "/bin/getenforce", + "/usr/local/sbin/getenforce", + "/usr/locla/bin/getenforce" + ]: if os.path.exists(path): selinux_status = runner.outs([path]) if arch and arch.startswith("arm") and selinux_status == "Enforcing": @@ -141,7 +130,7 @@ class ConfigMgr(object): break - def parse_kickstart(self, ksconf=None): + def _parse_kickstart(self, ksconf=None): if not ksconf: return @@ -150,7 +139,7 @@ class ConfigMgr(object): self.create['ks'] = ks self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] - self.selinux_check (self.create['arch'], ks) + self._selinux_check (self.create['arch'], ks) msger.info("Retrieving repo metadata:") ksrepos = misc.get_repostrs_from_ks(ks) @@ -167,12 +156,9 @@ class ConfigMgr(object): self.create['arch'] = str(target_archlist[0]) msger.info("\nUse detected arch %s." % target_archlist[0]) else: - raise errors.ConfigError("Please specify a valid arch, "\ + raise errors.ConfigError("Please specify a valid arch, " "your choise can be: " % ', '.join(target_archlist)) kickstart.resolve_groups(self.create, self.create['repomd']) -def getConfigMgr(): - return configmgr - configmgr = ConfigMgr() diff --git a/mic/creator.py b/mic/creator.py index 4e38033..90d14d5 100644 --- a/mic/creator.py +++ b/mic/creator.py @@ -18,8 +18,9 @@ import os, sys from optparse import SUPPRESS_HELP -from mic import configmgr, pluginmgr, msger +from mic import pluginmgr, msger from mic.utils import cmdln, errors, rpmmisc +from conf import configmgr class Creator(cmdln.Cmdln): """${name}: create an image @@ -36,12 +37,8 @@ class Creator(cmdln.Cmdln): def __init__(self, *args, **kwargs): cmdln.Cmdln.__init__(self, *args, **kwargs) - # load configmgr - self.configmgr = configmgr.getConfigMgr() - - # load pluginmgr - self.pluginmgr = pluginmgr.PluginMgr() - self.plugincmds = self.pluginmgr.get_plugins('imager') + # get cmds from pluginmgr + self.plugincmds = pluginmgr.PluginMgr().get_plugins('imager') # mix-in do_subcmd interface for subcmd, klass in self.plugincmds.iteritems(): @@ -115,41 +112,41 @@ class Creator(cmdln.Cmdln): if self.options.logfile: msger.set_interactive(False) msger.set_logfile(self.options.logfile) - self.configmgr.create['logfile'] = self.options.logfile + configmgr.create['logfile'] = self.options.logfile if self.options.config: - self.configmgr.reset() - self.configmgr._siteconf = self.options.config + configmgr.reset() + configmgr._siteconf = self.options.config if self.options.outdir is not None: - self.configmgr.create['outdir'] = self.options.outdir + configmgr.create['outdir'] = self.options.outdir if self.options.cachedir is not None: - self.configmgr.create['cachedir'] = self.options.cachedir + configmgr.create['cachedir'] = self.options.cachedir if self.options.local_pkgs_path is not None: - self.configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path + configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path if self.options.release: - self.configmgr.create['release'] = self.options.release + configmgr.create['release'] = self.options.release if self.options.record_pkgs: - self.configmgr.create['record_pkgs'] = [] + configmgr.create['record_pkgs'] = [] for infotype in self.options.record_pkgs.split(','): if infotype not in ('name', 'content', 'license'): raise errors.Usage('Invalid pkg recording: %s, valid ones: "name", "content", "license"' % infotype) - self.configmgr.create['record_pkgs'].append(infotype) + configmgr.create['record_pkgs'].append(infotype) if self.options.arch is not None: supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True) if self.options.arch in supported_arch: - self.configmgr.create['arch'] = self.options.arch + configmgr.create['arch'] = self.options.arch else: raise errors.Usage('Invalid architecture: "%s".\n' \ ' Supported architectures are: \n' \ ' %s\n' % (self.options.arch, ', '.join(supported_arch))) if self.options.pkgmgr is not None: - self.configmgr.create['pkgmgr'] = self.options.pkgmgr + configmgr.create['pkgmgr'] = self.options.pkgmgr def main(self, argv=None): if argv is None: diff --git a/plugins/backend/zypppkgmgr.py b/plugins/backend/zypppkgmgr.py index 82d33c9..4a23fb2 100644 --- a/plugins/backend/zypppkgmgr.py +++ b/plugins/backend/zypppkgmgr.py @@ -23,7 +23,7 @@ from mic.utils import runner, fs_related import zypp if not hasattr(zypp, 'PoolQuery') or not hasattr(zypp.RepoManager, 'loadSolvFile'): - raise ImportError("python-zypp in host system cannot support PoolQuery or loadSolvFile interface," + raise ImportError("python-zypp in host system cannot support PoolQuery or loadSolvFile interface, " "please update it to enhanced version which can be found in repo.meego.com/tools") from mic import msger diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index 344ef21..eb40658 100644 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -17,9 +17,10 @@ import os -from mic import configmgr, pluginmgr, chroot, msger +from mic import pluginmgr, chroot, msger from mic.utils import cmdln, misc, errors from mic.imager import fs +from mic.conf import configmgr from mic.pluginbase import ImagerPlugin class FsPlugin(ImagerPlugin): @@ -40,8 +41,7 @@ class FsPlugin(ImagerPlugin): if len(args) != 1: raise errors.Usage("Extra arguments given") - cfgmgr = configmgr.getConfigMgr() - creatoropts = cfgmgr.create + creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): @@ -56,7 +56,7 @@ class FsPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) name = os.path.splitext(os.path.basename(ksconf))[0] creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name) - cfgmgr._ksconf = ksconf + configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index ef5590f..e7d9357 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -19,8 +19,9 @@ import os import shutil import tempfile -from mic import configmgr, pluginmgr, chroot, msger +from mic import pluginmgr, chroot, msger from mic.utils import misc, fs_related, errors +from mic.conf import configmgr import mic.imager.livecd as livecd from mic.pluginbase import ImagerPlugin @@ -41,8 +42,7 @@ class LiveCDPlugin(ImagerPlugin): if len(args) != 1: raise errors.Usage("Extra arguments given") - cfgmgr = configmgr.getConfigMgr() - creatoropts = cfgmgr.create + creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): @@ -61,7 +61,7 @@ class LiveCDPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) name = os.path.splitext(os.path.basename(ksconf))[0] creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name) - cfgmgr._ksconf = ksconf + configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None diff --git a/plugins/imager/liveusb_plugin.py b/plugins/imager/liveusb_plugin.py index 6961b59..5beeceb 100644 --- a/plugins/imager/liveusb_plugin.py +++ b/plugins/imager/liveusb_plugin.py @@ -19,9 +19,10 @@ import os import shutil import tempfile -from mic import configmgr, pluginmgr, chroot, msger +from mic import pluginmgr, chroot, msger from mic.utils import misc, fs_related, errors from mic.utils.partitionedfs import PartitionedMount +from mic.conf import configmgr import mic.imager.liveusb as liveusb @@ -43,8 +44,7 @@ class LiveUSBPlugin(ImagerPlugin): if len(args) != 1: raise errors.Usage("Extra arguments given") - cfgmgr = configmgr.getConfigMgr() - creatoropts = cfgmgr.create + creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): @@ -63,7 +63,7 @@ class LiveUSBPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) name = os.path.splitext(os.path.basename(ksconf))[0] creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name) - cfgmgr._ksconf = ksconf + configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index 4dc2236..5341baf 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -19,8 +19,9 @@ import os import shutil import tempfile -from mic import configmgr, pluginmgr, chroot, msger +from mic import pluginmgr, chroot, msger from mic.utils import misc, fs_related, errors, cmdln +from mic.conf import configmgr import mic.imager.loop as loop from mic.pluginbase import ImagerPlugin @@ -42,8 +43,7 @@ class LoopPlugin(ImagerPlugin): if len(args) != 1: raise errors.Usage("Extra arguments given") - cfgmgr = configmgr.getConfigMgr() - creatoropts = cfgmgr.create + creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): @@ -58,7 +58,7 @@ class LoopPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) name = os.path.splitext(os.path.basename(ksconf))[0] creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name) - cfgmgr._ksconf = ksconf + configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 9e2040e..adf9ea1 100644 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -20,8 +20,9 @@ import shutil import re import tempfile -from mic import configmgr, pluginmgr, chroot, msger +from mic import pluginmgr, chroot, msger from mic.utils import misc, fs_related, errors, runner +from mic.conf import configmgr from mic.utils.partitionedfs import PartitionedMount import mic.imager.raw as raw @@ -44,8 +45,7 @@ class RawPlugin(ImagerPlugin): if len(args) != 1: raise errors.Usage("Extra arguments given") - cfgmgr = configmgr.getConfigMgr() - creatoropts = cfgmgr.create + creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): @@ -60,7 +60,7 @@ class RawPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) name = os.path.splitext(os.path.basename(ksconf))[0] creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], name) - cfgmgr._ksconf = ksconf + configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None @@ -16,7 +16,7 @@ # Temple Place - Suite 330, Boston, MA 02111-1307, USA. import os, sys -from mic import msger, creator, configmgr, pluginmgr +from mic import msger, creator, pluginmgr from mic.utils import cmdln, misc, errors from mic.__version__ import VERSION |