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 class ConfigMgr(object):
28 DEFAULTS = {'common': {
29 "distro_name": "Default Distribution",
32 "tmpdir": '/var/tmp/mic',
33 "cachedir": '/var/tmp/mic/cache',
34 "outdir": './mic-output',
35 "bootstrapdir": '/var/tmp/mic/bootstrap',
37 "arch": None, # None means auto-detect
43 "local_pkgs_path": None,
48 "compress_disk_image": None,
60 # make the manager class as singleton
62 def __new__(cls, *args, **kwargs):
64 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
68 def __init__(self, ksconf=None, siteconf=None):
69 # reset config options
72 # initial options from siteconf
74 self._siteconf = siteconf
76 # use default site config
77 self._siteconf = DEFAULT_GSITECONF
84 self.__siteconf = None
86 # initialize the values with defaults
87 for sec, vals in self.DEFAULTS.iteritems():
88 setattr(self, sec, vals)
90 def __set_siteconf(self, siteconf):
92 self.__siteconf = siteconf
93 self._parse_siteconf(siteconf)
94 except ConfigParser.Error, error:
95 raise errors.ConfigError("%s" % error)
96 def __get_siteconf(self):
97 return self.__siteconf
98 _siteconf = property(__get_siteconf, __set_siteconf)
100 def __set_ksconf(self, ksconf):
101 if not os.path.isfile(ksconf):
102 msger.error('Cannot find ks file: %s' % ksconf)
104 self.__ksconf = ksconf
105 self._parse_kickstart(ksconf)
106 def __get_ksconf(self):
108 _ksconf = property(__get_ksconf, __set_ksconf)
110 def _parse_siteconf(self, siteconf):
114 if not os.path.exists(siteconf):
115 raise errors.ConfigError("Failed to find config file: %s" \
118 parser = ConfigParser.SafeConfigParser()
119 parser.read(siteconf)
121 for section in parser.sections():
122 if section in self.DEFAULTS:
123 getattr(self, section).update(dict(parser.items(section)))
125 # append common section items to other sections
126 for section in self.DEFAULTS.keys():
127 if section != "common":
128 getattr(self, section).update(self.common)
130 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
132 for section in parser.sections():
133 if section.startswith('bootstrap'):
136 for option in parser.options(section):
138 name = parser.get(section, 'name')
141 val = parser.get(section, option)
143 (reponame, repoopt) = option.split('_')
144 if repostr.has_key(reponame):
145 repostr[reponame] += "%s:%s," % (repoopt, val)
147 repostr[reponame] = "%s:%s," % (repoopt, val)
150 if val.split(':')[0] in ('file', 'http', 'https', 'ftp'):
151 if repostr.has_key(option):
152 repostr[option] += "name:%s,baseurl:%s," % (option, val)
154 repostr[option] = "name:%s,baseurl:%s," % (option, val)
157 self.bootstraps[name] = repostr
159 def _selinux_check(self, arch, ks):
160 """If a user needs to use btrfs or creates ARM image,
161 selinux must be disabled at start.
164 for path in ["/usr/sbin/getenforce",
165 "/usr/bin/getenforce",
168 "/usr/local/sbin/getenforce",
169 "/usr/locla/bin/getenforce"
171 if os.path.exists(path):
172 selinux_status = runner.outs([path])
173 if arch and arch.startswith("arm") \
174 and selinux_status == "Enforcing":
175 raise errors.ConfigError("Can't create arm image if "
176 "selinux is enabled, please disable it and try again")
179 for part in ks.handler.partition.partitions:
180 if part.fstype == "btrfs":
184 if use_btrfs and selinux_status == "Enforcing":
185 raise errors.ConfigError("Can't create image using btrfs "
186 "filesystem if selinux is enabled, "
187 "please disable it and try again.")
190 def _parse_kickstart(self, ksconf=None):
194 ks = kickstart.read_kickstart(ksconf)
196 self.create['ks'] = ks
197 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
199 if self.create['name_prefix']:
200 self.create['name'] = "%s-%s" % (self.create['name_prefix'],
203 self._selinux_check (self.create['arch'], ks)
205 msger.info("Retrieving repo metadata:")
206 ksrepos = misc.get_repostrs_from_ks(ks)
208 raise errors.KsError('no valid repos found in ks file')
210 self.create['repomd'] = misc.get_metadata_from_repos(
212 self.create['cachedir'])
215 self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
217 target_archlist, archlist = misc.get_arch(self.create['repomd'])
218 if self.create['arch']:
219 if self.create['arch'] not in archlist:
220 raise errors.ConfigError("Invalid arch %s for repository. "
222 % (self.create['arch'], ', '.join(archlist)))
224 if len(target_archlist) == 1:
225 self.create['arch'] = str(target_archlist[0])
226 msger.info("\nUse detected arch %s." % target_archlist[0])
228 raise errors.ConfigError("Please specify a valid arch, "
229 "the choice can be: %s" \
230 % ', '.join(archlist))
232 kickstart.resolve_groups(self.create, self.create['repomd'])
234 configmgr = ConfigMgr()