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 mic_path = os.path.dirname(__file__)
32 m = re.match(r"(?P<prefix>.*)\/lib(64)?\/.*", mic_path)
33 if m and m.group('prefix') != "/usr":
34 return os.path.join(m.group('prefix'), "etc/mic/mic.conf")
36 return DEFAULT_GSITECONF
38 class ConfigMgr(object):
39 prefer_backends = ["zypp", "yum"]
41 DEFAULTS = {'common': {
42 "distro_name": "Default Distribution",
43 "plugin_dir": "/usr/lib/mic/plugins", # TODO use prefix also?
46 "tmpdir": '/var/tmp/mic',
47 "cachedir": '/var/tmp/mic/cache',
48 "outdir": './mic-output',
50 "plugin_dir": "/usr/lib/mic/plugins",
51 "arch": None, # None means auto-detect
57 "local_pkgs_path": None,
70 "localrepos": [], # save localrepos
71 "runtime": "bootstrap",
80 "rootdir": '/var/tmp/mic-bootstrap',
86 # make the manager class as singleton
88 def __new__(cls, *args, **kwargs):
90 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
94 def __init__(self, ksconf=None, siteconf=None):
95 # reset config options
99 siteconf = get_siteconf()
101 # initial options from siteconf
102 self._siteconf = siteconf
105 self._ksconf = ksconf
109 self.__siteconf = None
111 # initialize the values with defaults
112 for sec, vals in self.DEFAULTS.iteritems():
113 setattr(self, sec, vals)
115 def __set_siteconf(self, siteconf):
117 self.__siteconf = siteconf
118 self._parse_siteconf(siteconf)
119 except ConfigParser.Error, error:
120 raise errors.ConfigError("%s" % error)
121 def __get_siteconf(self):
122 return self.__siteconf
123 _siteconf = property(__get_siteconf, __set_siteconf)
125 def __set_ksconf(self, ksconf):
126 if not os.path.isfile(ksconf):
127 msger.error('Cannot find ks file: %s' % ksconf)
129 self.__ksconf = ksconf
130 self._parse_kickstart(ksconf)
131 def __get_ksconf(self):
133 _ksconf = property(__get_ksconf, __set_ksconf)
135 def _parse_siteconf(self, siteconf):
139 if not os.path.exists(siteconf):
140 msger.warning("cannot read config file: %s" % siteconf)
143 parser = ConfigParser.SafeConfigParser()
144 parser.read(siteconf)
146 for section in parser.sections():
147 if section in self.DEFAULTS:
148 getattr(self, section).update(dict(parser.items(section)))
150 # append common section items to other sections
151 for section in self.DEFAULTS.keys():
152 if section != "common":
153 getattr(self, section).update(self.common)
155 # check and normalize the scheme of proxy url
156 if self.create['proxy']:
157 m = re.match('^(\w+)://.*', self.create['proxy'])
160 if scheme not in ('http', 'https', 'ftp', 'socks'):
161 msger.error("%s: proxy scheme is incorrect" % siteconf)
163 msger.warning("%s: proxy url w/o scheme, use http as default"
165 self.create['proxy'] = "http://" + self.create['proxy']
167 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
169 # bootstrap option handling
170 self.set_runtime(self.create['runtime'])
171 if isinstance(self.bootstrap['packages'], basestring):
172 packages = self.bootstrap['packages'].replace('\n', ' ')
173 if packages.find(',') != -1:
174 packages = packages.split(',')
176 packages = packages.split()
177 self.bootstrap['packages'] = packages
179 def _parse_kickstart(self, ksconf=None):
183 ksconf = misc.normalize_ksfile(ksconf,
184 self.create['release'],
187 ks = kickstart.read_kickstart(ksconf)
189 self.create['ks'] = ks
190 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
192 self.create['name'] = misc.build_name(ksconf,
193 self.create['release'],
194 self.create['name_prefix'],
195 self.create['name_suffix'])
197 msger.info("Retrieving repo metadata:")
198 ksrepos = misc.get_repostrs_from_ks(ks)
200 raise errors.KsError('no valid repos found in ks file')
203 if 'baseurl' in repo and repo['baseurl'].startswith("file:"):
204 repourl = repo['baseurl'].replace('file:', '')
205 repourl = "/%s" % repourl.lstrip('/')
206 self.create['localrepos'].append(repourl)
208 self.create['repomd'] = misc.get_metadata_from_repos(
210 self.create['cachedir'])
213 target_archlist, archlist = misc.get_arch(self.create['repomd'])
214 if self.create['arch']:
215 if self.create['arch'] not in archlist:
216 raise errors.ConfigError("Invalid arch %s for repository. "
218 % (self.create['arch'], ', '.join(archlist)))
220 if len(target_archlist) == 1:
221 self.create['arch'] = str(target_archlist[0])
222 msger.info("\nUse detected arch %s." % target_archlist[0])
224 raise errors.ConfigError("Please specify a valid arch, "
225 "the choice can be: %s" \
226 % ', '.join(archlist))
228 kickstart.resolve_groups(self.create, self.create['repomd'])
230 # check selinux, it will block arm and btrfs image creation
231 misc.selinux_check(self.create['arch'],
232 [p.fstype for p in ks.handler.partition.partitions])
234 def set_runtime(self, runtime):
235 if runtime not in ("bootstrap", "native"):
236 msger.error("Invalid runtime mode: %s" % runtime)
238 if misc.get_distro()[0] in ("tizen", "Tizen"):
240 self.create['runtime'] = runtime
242 configmgr = ConfigMgr()