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.
22 from mic import kickstart
23 from mic.utils import misc, runner, proxy, errors
26 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
30 if hasattr(sys, 'real_prefix'):
31 return os.path.join(sys.prefix, "etc/mic/mic.conf")
33 return DEFAULT_GSITECONF
36 if os.path.exists(os.path.join("/", ".chroot.lock")):
38 return (os.stat("/").st_ino != 2)
40 class ConfigMgr(object):
41 prefer_backends = ["zypp", "yum"]
43 DEFAULTS = {'common': {
44 "distro_name": "Default Distribution",
45 "plugin_dir": "/usr/lib/mic/plugins", # TODO use prefix also?
48 "tmpdir": '/var/tmp/mic',
49 "cachedir": '/var/tmp/mic/cache',
50 "outdir": './mic-output',
52 "arch": None, # None means auto-detect
58 "local_pkgs_path": None,
73 "localrepos": [], # save localrepos
75 "runtime": "bootstrap",
77 "ignore_ksrepo": False,
81 "use_mic_in_bootstrap": False,
82 "skip_set_hosts": False,
83 "postscripts_maxruntime": 120,
84 "block_recommends": False,
93 "rootdir": '/var/tmp/mic-bootstrap',
99 # make the manager class as singleton
101 def __new__(cls, *args, **kwargs):
102 if not cls._instance:
103 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
107 def __init__(self, ksconf=None, siteconf=None):
108 # reset config options
112 siteconf = get_siteconf()
114 # initial options from siteconf
115 self._siteconf = siteconf
118 self._ksconf = ksconf
122 self.__siteconf = None
124 # initialize the values with defaults
125 for sec, vals in self.DEFAULTS.iteritems():
126 setattr(self, sec, vals)
128 def __set_siteconf(self, siteconf):
130 self.__siteconf = siteconf
131 self._parse_siteconf(siteconf)
132 except ConfigParser.Error as error:
133 raise errors.ConfigError("%s" % error)
134 def __get_siteconf(self):
135 return self.__siteconf
136 _siteconf = property(__get_siteconf, __set_siteconf)
138 def __set_ksconf(self, ksconf):
139 if not os.path.isfile(ksconf):
140 raise errors.KsError('Cannot find ks file: %s' % ksconf)
142 self.__ksconf = ksconf
143 self._parse_kickstart(ksconf)
144 def __get_ksconf(self):
146 _ksconf = property(__get_ksconf, __set_ksconf)
148 def _parse_siteconf(self, siteconf):
150 if os.getenv("MIC_PLUGIN_DIR"):
151 self.common["plugin_dir"] = os.environ["MIC_PLUGIN_DIR"]
153 if siteconf and not os.path.exists(siteconf):
154 msger.warning("cannot find config file: %s" % siteconf)
158 self.common["distro_name"] = "Tizen"
159 # append common section items to other sections
160 for section in self.DEFAULTS.keys():
161 if section != "common":
162 getattr(self, section).update(self.common)
166 parser = ConfigParser.SafeConfigParser()
167 parser.read(siteconf)
169 for section in parser.sections():
170 if section in self.DEFAULTS:
171 getattr(self, section).update(dict(parser.items(section)))
173 # append common section items to other sections
174 for section in self.DEFAULTS.keys():
175 if section != "common":
176 getattr(self, section).update(self.common)
178 # check and normalize the scheme of proxy url
179 if self.create['proxy']:
180 m = re.match('^(\w+)://.*', self.create['proxy'])
183 if scheme not in ('http', 'https', 'ftp', 'socks'):
184 raise errors.ConfigError("%s: proxy scheme is incorrect" % siteconf)
186 msger.warning("%s: proxy url w/o scheme, use http as default"
188 self.create['proxy'] = "http://" + self.create['proxy']
190 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
192 # bootstrap option handling
193 self.set_runtime(self.create['runtime'])
194 if isinstance(self.bootstrap['packages'], basestring):
195 packages = self.bootstrap['packages'].replace('\n', ' ')
196 if packages.find(',') != -1:
197 packages = packages.split(',')
199 packages = packages.split()
200 self.bootstrap['packages'] = packages
202 if type(self.create['use_mic_in_bootstrap']) != 'bool':
203 use_mic_in_bootstrap = str(self.create['use_mic_in_bootstrap'])
204 if use_mic_in_bootstrap.lower() in ('on', 'yes', 'true', '1'):
205 self.create['use_mic_in_bootstrap'] = True
207 self.create['use_mic_in_bootstrap'] = False
209 def _parse_kickstart(self, ksconf=None):
213 ksconf = misc.normalize_ksfile(ksconf,
214 self.create['release'],
217 ks = kickstart.read_kickstart(ksconf)
219 self.create['ks'] = ks
220 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
222 self.create['name'] = misc.build_name(ksconf,
223 self.create['release'],
224 self.create['name_prefix'],
225 self.create['name_suffix'])
227 self.create['destdir'] = self.create['outdir']
228 if self.create['release'] is not None:
229 self.create['destdir'] = "%s/%s/images/%s/" % (self.create['outdir'],
230 self.create['release'],
232 self.create['name'] = self.create['release'] + '_' + self.create['name']
233 if self.create['pack_to'] is not None:
234 if '@NAME@' in self.create['pack_to']:
235 self.create['pack_to'] = self.create['pack_to'].replace('@NAME@', self.create['name'])
236 self.create['name'] = misc.strip_archive_suffix(self.create['pack_to'])
237 if self.create['name'] is None:
238 raise errors.CreatorError("Not supported archive file format: %s" % self.create['pack_to'])
240 if not self.create['logfile']:
241 self.create['logfile'] = os.path.join(self.create['destdir'],
242 self.create['name'] + ".log")
243 self.create['releaselog'] = True
246 elif self.create['pack_to'] is not None:
247 if '@NAME@' in self.create['pack_to']:
248 self.create['pack_to'] = self.create['pack_to'].replace('@NAME@', self.create['name'])
249 self.create['name'] = misc.strip_archive_suffix(self.create['pack_to'])
250 if self.create['name'] is None:
251 raise errors.CreatorError("Not supported archive file format: %s" % self.create['pack_to'])
253 msger.info("Retrieving repo metadata:")
254 ksrepos = kickstart.get_repos(ks,
255 self.create['extrarepos'],
256 self.create['ignore_ksrepo'])
258 raise errors.KsError('no valid repos found in ks file')
261 if hasattr(repo, 'baseurl') and repo.baseurl.startswith("file:"):
262 repourl = repo.baseurl.replace('file:', '')
263 repourl = "/%s" % repourl.lstrip('/')
264 self.create['localrepos'].append(repourl)
266 self.create['repomd'] = misc.get_metadata_from_repos(
268 self.create['cachedir'])
269 kstpkrepos = kickstart.get_tpkrepos(ks)
271 for tpk_repo in kstpkrepos:
272 if hasattr(tpk_repo,'baseurl') and tpk_repo.baseurl.startswith("file:"):
273 tpk_repourl = tpk_repo.baseurl.replace('file:','')
274 tpk_repourl = "/%s" % tpk_repourl.lstrip('/')
275 self.create['localtpkrepos'].append(tpk_repourl)
279 target_archlist, archlist = misc.get_arch(self.create['repomd'])
280 if self.create['arch']:
281 if self.create['arch'] not in archlist:
282 raise errors.ConfigError("Invalid arch %s for repository. "
284 % (self.create['arch'], ', '.join(archlist)))
286 if len(target_archlist) == 1:
287 self.create['arch'] = str(target_archlist[0])
288 msger.info("Use detected arch %s." % target_archlist[0])
290 raise errors.ConfigError("Please specify a valid arch, "
291 "the choice can be: %s" \
292 % ', '.join(archlist))
294 kickstart.resolve_groups(self.create, self.create['repomd'])
296 # check selinux, it will block arm and btrfs image creation
297 misc.selinux_check(self.create['arch'],
298 [p.fstype for p in ks.handler.partition.partitions])
300 def set_logfile(self, logfile = None):
302 logfile = self.create['logfile']
304 logfile_dir = os.path.dirname(self.create['logfile'])
305 if not os.path.exists(logfile_dir):
306 os.makedirs(logfile_dir)
307 msger.set_interactive(False)
312 msger.set_logfile(self.create['logfile'], mode)
314 def set_runtime(self, runtime):
315 if runtime != "bootstrap":
316 raise errors.CreatorError("Invalid runtime mode: %s, only 'bootstrap' mode is allowed." % runtime)
318 if misc.get_distro()[0] in ("tizen", "Tizen"):
320 self.create['runtime'] = runtime
322 configmgr = ConfigMgr()