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, proxy, errors
25 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
27 def get_siteconf(siteconf="etc/mic/mic.conf"):
28 mic_path = os.path.dirname(__file__)
29 path_ptn = re.compile(r"(?P<prefix>.*)\/lib(64)?\/.*")
30 m = path_ptn.match(mic_path)
32 if m.group('prefix') == "/usr":
33 return DEFAULT_GSITECONF
35 return os.path.join(m.group('prefix'), siteconf)
38 class ConfigMgr(object):
39 DEFAULTS = {'common': {
40 "distro_name": "Default Distribution",
43 "tmpdir": '/var/tmp/mic',
44 "cachedir": '/var/tmp/mic/cache',
45 "outdir": './mic-output',
46 "bootstrapdir": '/var/tmp/mic/bootstrap',
48 "arch": None, # None means auto-detect
54 "local_pkgs_path": None,
59 "compress_disk_image": None,
76 # make the manager class as singleton
78 def __new__(cls, *args, **kwargs):
80 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
84 def __init__(self, ksconf=None, siteconf=None):
85 # reset config options
89 siteconf = get_siteconf()
90 if not siteconf or not os.path.exists(siteconf):
91 siteconf = DEFAULT_GSITECONF
93 # initial options from siteconf
94 self._siteconf = siteconf
101 self.__siteconf = None
103 # initialize the values with defaults
104 for sec, vals in self.DEFAULTS.iteritems():
105 setattr(self, sec, vals)
107 def __set_siteconf(self, siteconf):
109 self.__siteconf = siteconf
110 self._parse_siteconf(siteconf)
111 except ConfigParser.Error, error:
112 raise errors.ConfigError("%s" % error)
113 def __get_siteconf(self):
114 return self.__siteconf
115 _siteconf = property(__get_siteconf, __set_siteconf)
117 def __set_ksconf(self, ksconf):
118 if not os.path.isfile(ksconf):
119 msger.error('Cannot find ks file: %s' % ksconf)
121 self.__ksconf = ksconf
122 self._parse_kickstart(ksconf)
123 def __get_ksconf(self):
125 _ksconf = property(__get_ksconf, __set_ksconf)
127 def _parse_siteconf(self, siteconf):
131 if not os.path.exists(siteconf):
132 raise errors.ConfigError("Failed to find config file: %s" \
135 parser = ConfigParser.SafeConfigParser()
136 parser.read(siteconf)
138 for section in parser.sections():
139 if section in self.DEFAULTS:
140 getattr(self, section).update(dict(parser.items(section)))
142 # append common section items to other sections
143 for section in self.DEFAULTS.keys():
144 if section != "common" and not section.startswith('bootstrap'):
145 getattr(self, section).update(self.common)
147 # check and normalize the scheme of proxy url
148 if self.create['proxy']:
149 m = re.match('^(\w+)://.*', self.create['proxy'])
152 if scheme not in ('http', 'https', 'ftp', 'socks'):
153 msger.error("%s: proxy scheme is incorrect" % siteconf)
155 msger.warning("%s: proxy url w/o scheme, use http as default"
157 self.create['proxy'] = "http://" + self.create['proxy']
159 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
161 for section in parser.sections():
162 if section.startswith('bootstrap'):
165 for option in parser.options(section):
167 name = parser.get(section, 'name')
170 val = parser.get(section, option)
172 (reponame, repoopt) = option.split('_')
173 if repostr.has_key(reponame):
174 repostr[reponame] += "%s:%s," % (repoopt, val)
176 repostr[reponame] = "%s:%s," % (repoopt, val)
179 if val.split(':')[0] in ('file', 'http', 'https', 'ftp'):
180 if repostr.has_key(option):
181 repostr[option] += "name:%s,baseurl:%s," % (option, val)
183 repostr[option] = "name:%s,baseurl:%s," % (option, val)
186 self.bootstraps[name] = repostr
188 def _selinux_check(self, arch, ks):
189 """If a user needs to use btrfs or creates ARM image,
190 selinux must be disabled at start.
193 for path in ["/usr/sbin/getenforce",
194 "/usr/bin/getenforce",
197 "/usr/local/sbin/getenforce",
198 "/usr/locla/bin/getenforce"
200 if os.path.exists(path):
201 selinux_status = runner.outs([path])
202 if arch and arch.startswith("arm") \
203 and selinux_status == "Enforcing":
204 raise errors.ConfigError("Can't create arm image if "
205 "selinux is enabled, please disable it and try again")
208 for part in ks.handler.partition.partitions:
209 if part.fstype == "btrfs":
213 if use_btrfs and selinux_status == "Enforcing":
214 raise errors.ConfigError("Can't create image using btrfs "
215 "filesystem if selinux is enabled, "
216 "please disable it and try again.")
219 def _parse_kickstart(self, ksconf=None):
223 ks = kickstart.read_kickstart(ksconf)
225 self.create['ks'] = ks
226 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
228 if self.create['name_prefix']:
229 self.create['name'] = "%s-%s" % (self.create['name_prefix'],
232 self._selinux_check (self.create['arch'], ks)
234 msger.info("Retrieving repo metadata:")
235 ksrepos = misc.get_repostrs_from_ks(ks)
237 raise errors.KsError('no valid repos found in ks file')
239 self.create['repomd'] = misc.get_metadata_from_repos(
241 self.create['cachedir'])
244 self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
246 target_archlist, archlist = misc.get_arch(self.create['repomd'])
247 if self.create['arch']:
248 if self.create['arch'] not in archlist:
249 raise errors.ConfigError("Invalid arch %s for repository. "
251 % (self.create['arch'], ', '.join(archlist)))
253 if len(target_archlist) == 1:
254 self.create['arch'] = str(target_archlist[0])
255 msger.info("\nUse detected arch %s." % target_archlist[0])
257 raise errors.ConfigError("Please specify a valid arch, "
258 "the choice can be: %s" \
259 % ', '.join(archlist))
261 kickstart.resolve_groups(self.create, self.create['repomd'])
263 configmgr = ConfigMgr()