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'
28 mic_path = os.path.dirname(__file__)
30 m = re.match(r"(?P<prefix>.*)\/lib(64)?\/.*", mic_path)
31 if m and m.group('prefix') != "/usr":
32 return os.path.join(m.group('prefix'), DEFAULT_GSITECONF)
34 return DEFAULT_GSITECONF
36 class ConfigMgr(object):
37 DEFAULTS = {'common': {
38 "distro_name": "Default Distribution",
39 "plugin_dir": "/usr/lib/mic/plugins", # TODO use prefix also?
42 "tmpdir": '/var/tmp/mic',
43 "cachedir": '/var/tmp/mic/cache',
44 "outdir": './mic-output',
45 "bootstrapdir": '/var/tmp/mic/bootstrap',
47 "arch": None, # None means auto-detect
53 "local_pkgs_path": None,
58 "compress_disk_image": None,
75 # make the manager class as singleton
77 def __new__(cls, *args, **kwargs):
79 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
83 def __init__(self, ksconf=None, siteconf=None):
84 # reset config options
88 siteconf = get_siteconf()
90 # initial options from siteconf
91 self._siteconf = siteconf
98 self.__siteconf = None
100 # initialize the values with defaults
101 for sec, vals in self.DEFAULTS.iteritems():
102 setattr(self, sec, vals)
104 def __set_siteconf(self, siteconf):
106 self.__siteconf = siteconf
107 self._parse_siteconf(siteconf)
108 except ConfigParser.Error, error:
109 raise errors.ConfigError("%s" % error)
110 def __get_siteconf(self):
111 return self.__siteconf
112 _siteconf = property(__get_siteconf, __set_siteconf)
114 def __set_ksconf(self, ksconf):
115 if not os.path.isfile(ksconf):
116 msger.error('Cannot find ks file: %s' % ksconf)
118 self.__ksconf = ksconf
119 self._parse_kickstart(ksconf)
120 def __get_ksconf(self):
122 _ksconf = property(__get_ksconf, __set_ksconf)
124 def _parse_siteconf(self, siteconf):
128 if not os.path.exists(siteconf):
129 msger.warning("cannot read config file: %s" % siteconf)
132 parser = ConfigParser.SafeConfigParser()
133 parser.read(siteconf)
135 for section in parser.sections():
136 if section in self.DEFAULTS:
137 getattr(self, section).update(dict(parser.items(section)))
139 # append common section items to other sections
140 for section in self.DEFAULTS.keys():
141 if section != "common" and not section.startswith('bootstrap'):
142 getattr(self, section).update(self.common)
144 # check and normalize the scheme of proxy url
145 if self.create['proxy']:
146 m = re.match('^(\w+)://.*', self.create['proxy'])
149 if scheme not in ('http', 'https', 'ftp', 'socks'):
150 msger.error("%s: proxy scheme is incorrect" % siteconf)
152 msger.warning("%s: proxy url w/o scheme, use http as default"
154 self.create['proxy'] = "http://" + self.create['proxy']
156 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
158 for section in parser.sections():
159 if section.startswith('bootstrap'):
162 for option in parser.options(section):
164 name = parser.get(section, 'name')
167 val = parser.get(section, option)
169 (reponame, repoopt) = option.split('_')
170 if repostr.has_key(reponame):
171 repostr[reponame] += "%s:%s," % (repoopt, val)
173 repostr[reponame] = "%s:%s," % (repoopt, val)
176 if val.split(':')[0] in ('file', 'http', 'https', 'ftp'):
177 if repostr.has_key(option):
178 repostr[option] += "name:%s,baseurl:%s," % (option, val)
180 repostr[option] = "name:%s,baseurl:%s," % (option, val)
183 self.bootstraps[name] = repostr
185 def _selinux_check(self, arch, ks):
186 """If a user needs to use btrfs or creates ARM image,
187 selinux must be disabled at start.
190 for path in ["/usr/sbin/getenforce",
191 "/usr/bin/getenforce",
194 "/usr/local/sbin/getenforce",
195 "/usr/locla/bin/getenforce"
197 if os.path.exists(path):
198 selinux_status = runner.outs([path])
199 if arch and arch.startswith("arm") \
200 and selinux_status == "Enforcing":
201 raise errors.ConfigError("Can't create arm image if "
202 "selinux is enabled, please disable it and try again")
205 for part in ks.handler.partition.partitions:
206 if part.fstype == "btrfs":
210 if use_btrfs and selinux_status == "Enforcing":
211 raise errors.ConfigError("Can't create image using btrfs "
212 "filesystem if selinux is enabled, "
213 "please disable it and try again.")
216 def _parse_kickstart(self, ksconf=None):
220 ks = kickstart.read_kickstart(ksconf)
222 self.create['ks'] = ks
223 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
225 if self.create['name_prefix']:
226 self.create['name'] = "%s-%s" % (self.create['name_prefix'],
229 self._selinux_check (self.create['arch'], ks)
231 msger.info("Retrieving repo metadata:")
232 ksrepos = misc.get_repostrs_from_ks(ks)
234 raise errors.KsError('no valid repos found in ks file')
236 self.create['repomd'] = misc.get_metadata_from_repos(
238 self.create['cachedir'])
241 self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
243 target_archlist, archlist = misc.get_arch(self.create['repomd'])
244 if self.create['arch']:
245 if self.create['arch'] not in archlist:
246 raise errors.ConfigError("Invalid arch %s for repository. "
248 % (self.create['arch'], ', '.join(archlist)))
250 if len(target_archlist) == 1:
251 self.create['arch'] = str(target_archlist[0])
252 msger.info("\nUse detected arch %s." % target_archlist[0])
254 raise errors.ConfigError("Please specify a valid arch, "
255 "the choice can be: %s" \
256 % ', '.join(archlist))
258 kickstart.resolve_groups(self.create, self.create['repomd'])
260 configmgr = ConfigMgr()