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.
23 from .utils import misc, runner, errors
25 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
27 class ConfigMgr(object):
28 DEFAULTS = {'common': {},
30 "tmpdir": '/var/tmp/mic',
31 "cachedir": '/var/tmp/mic/cache',
32 "outdir": './mic-output',
33 "arch": None, # None means auto-detect
39 "local_pkgs_path": None,
43 "compress_disk_image": None,
44 "distro_name": "Default Distribution",
51 # make the manager class as singleton
53 def __new__(cls, *args, **kwargs):
55 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
59 def __init__(self, ksconf=None, siteconf=None):
60 # reset config options
63 # initial options from siteconf
65 self._siteconf = siteconf
67 # use default site config
68 self._siteconf = DEFAULT_GSITECONF
75 self.__siteconf = None
77 # initialize the values with defaults
78 for sec, vals in self.DEFAULTS.iteritems():
79 setattr(self, sec, vals)
81 def __set_siteconf(self, siteconf):
83 self.__siteconf = siteconf
84 self._parse_siteconf(siteconf)
85 except ConfigParser.Error, error:
86 raise errors.ConfigError("%s" % error)
87 def __get_siteconf(self):
88 return self.__siteconf
89 _siteconf = property(__get_siteconf, __set_siteconf)
91 def __set_ksconf(self, ksconf):
92 if not os.path.isfile(ksconf):
93 msger.error('Cannot find ks file: %s' % ksconf)
95 self.__ksconf = ksconf
96 self._parse_kickstart(ksconf)
97 def __get_ksconf(self):
99 _ksconf = property(__get_ksconf, __set_ksconf)
101 def _parse_siteconf(self, siteconf):
105 if not os.path.exists(siteconf):
106 raise errors.ConfigError("Failed to find config file: %s" % siteconf)
108 parser = ConfigParser.SafeConfigParser()
109 parser.read(siteconf)
111 for section in parser.sections():
112 if section in self.DEFAULTS.keys():
113 getattr(self, section).update(dict(parser.items(section)))
115 def _selinux_check(self, arch, ks):
116 """ If a user needs to use btrfs or creates ARM image, selinux must be disabled at start """
118 for path in ["/usr/sbin/getenforce",
119 "/usr/bin/getenforce",
122 "/usr/local/sbin/getenforce",
123 "/usr/locla/bin/getenforce"
125 if os.path.exists(path):
126 selinux_status = runner.outs([path])
127 if arch and arch.startswith("arm") and selinux_status == "Enforcing":
128 raise errors.ConfigError("Can't create arm image if selinux is enabled, please disable it and try again")
131 parts = ks.handler.partition.partitions
132 for part in ks.handler.partition.partitions:
133 if part.fstype == "btrfs":
137 if use_btrfs and selinux_status == "Enforcing":
138 raise errors.ConfigError("Can't create image useing btrfs filesystem if selinux is enabled, please disable it and try again")
142 def _parse_kickstart(self, ksconf=None):
146 ks = kickstart.read_kickstart(ksconf)
148 self.create['ks'] = ks
149 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
151 if self.create['name_prefix']:
152 self.create['name'] = "%s-%s" % (self.create['name_prefix'], self.create['name'])
154 self._selinux_check (self.create['arch'], ks)
156 msger.info("Retrieving repo metadata:")
157 ksrepos = misc.get_repostrs_from_ks(ks)
158 self.create['repomd'] = misc.get_metadata_from_repos(ksrepos, self.create['cachedir'])
161 target_archlist, archlist = misc.get_arch(self.create['repomd'])
162 if self.create['arch']:
163 if self.create['arch'] not in archlist:
164 raise errors.ConfigError("Invalid arch %s for repository. Valid arches: %s"\
165 % (self.create['arch'], ', '.join(archlist)))
167 if len(target_archlist) == 1:
168 self.create['arch'] = str(target_archlist[0])
169 msger.info("\nUse detected arch %s." % target_archlist[0])
171 raise errors.ConfigError("Please specify a valid arch, "\
172 "your choise can be: %s" % ', '.join(archlist))
174 kickstart.resolve_groups(self.create, self.create['repomd'])
176 configmgr = ConfigMgr()