summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/conf.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/mic/conf.py')
-rw-r--r--scripts/lib/mic/conf.py239
1 files changed, 239 insertions, 0 deletions
diff --git a/scripts/lib/mic/conf.py b/scripts/lib/mic/conf.py
new file mode 100644
index 0000000000..e37334cc7a
--- /dev/null
+++ b/scripts/lib/mic/conf.py
@@ -0,0 +1,239 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
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
8#
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
12# for more details.
13#
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.
17
18import os, sys, re
19import ConfigParser
20
21from mic import msger
22from mic import kickstart
23from mic.utils import misc, runner, proxy, errors
24
25
26DEFAULT_GSITECONF = '/etc/mic/mic.conf'
27
28
29def get_siteconf():
30 mic_path = os.path.dirname(__file__)
31
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")
35
36 return DEFAULT_GSITECONF
37
38class ConfigMgr(object):
39 prefer_backends = ["zypp", "yum"]
40
41 DEFAULTS = {'common': {
42 "distro_name": "Default Distribution",
43 "plugin_dir": "/usr/lib/mic/plugins", # TODO use prefix also?
44 },
45 'create': {
46 "tmpdir": '/var/tmp/mic',
47 "cachedir": '/var/tmp/mic/cache',
48 "outdir": './mic-output',
49
50 "arch": None, # None means auto-detect
51 "pkgmgr": "auto",
52 "name": "output",
53 "ksfile": None,
54 "ks": None,
55 "repomd": None,
56 "local_pkgs_path": None,
57 "release": None,
58 "logfile": None,
59 "record_pkgs": [],
60 "pack_to": None,
61 "name_prefix": None,
62 "name_suffix": None,
63 "proxy": None,
64 "no_proxy": None,
65 "copy_kernel": False,
66 "install_pkgs": None,
67 "repourl": {},
68 "localrepos": [], # save localrepos
69 "runtime": "bootstrap",
70 },
71 'chroot': {
72 "saveto": None,
73 },
74 'convert': {
75 "shell": False,
76 },
77 'bootstrap': {
78 "rootdir": '/var/tmp/mic-bootstrap',
79 "packages": [],
80 },
81 }
82
83 # make the manager class as singleton
84 _instance = None
85 def __new__(cls, *args, **kwargs):
86 if not cls._instance:
87 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
88
89 return cls._instance
90
91 def __init__(self, ksconf=None, siteconf=None):
92 # reset config options
93 self.reset()
94
95 if not siteconf:
96 siteconf = get_siteconf()
97
98 # initial options from siteconf
99 self._siteconf = siteconf
100
101 if ksconf:
102 self._ksconf = ksconf
103
104 def reset(self):
105 self.__ksconf = None
106 self.__siteconf = None
107
108 # initialize the values with defaults
109 for sec, vals in self.DEFAULTS.iteritems():
110 setattr(self, sec, vals)
111
112 def __set_siteconf(self, siteconf):
113 try:
114 self.__siteconf = siteconf
115 self._parse_siteconf(siteconf)
116 except ConfigParser.Error, error:
117 raise errors.ConfigError("%s" % error)
118 def __get_siteconf(self):
119 return self.__siteconf
120 _siteconf = property(__get_siteconf, __set_siteconf)
121
122 def __set_ksconf(self, ksconf):
123 if not os.path.isfile(ksconf):
124 msger.error('Cannot find ks file: %s' % ksconf)
125
126 self.__ksconf = ksconf
127 self._parse_kickstart(ksconf)
128 def __get_ksconf(self):
129 return self.__ksconf
130 _ksconf = property(__get_ksconf, __set_ksconf)
131
132 def _parse_siteconf(self, siteconf):
133 if not siteconf:
134 return
135
136 if not os.path.exists(siteconf):
137 msger.warning("cannot read config file: %s" % siteconf)
138 return
139
140 parser = ConfigParser.SafeConfigParser()
141 parser.read(siteconf)
142
143 for section in parser.sections():
144 if section in self.DEFAULTS:
145 getattr(self, section).update(dict(parser.items(section)))
146
147 # append common section items to other sections
148 for section in self.DEFAULTS.keys():
149 if section != "common":
150 getattr(self, section).update(self.common)
151
152 # check and normalize the scheme of proxy url
153 if self.create['proxy']:
154 m = re.match('^(\w+)://.*', self.create['proxy'])
155 if m:
156 scheme = m.group(1)
157 if scheme not in ('http', 'https', 'ftp', 'socks'):
158 msger.error("%s: proxy scheme is incorrect" % siteconf)
159 else:
160 msger.warning("%s: proxy url w/o scheme, use http as default"
161 % siteconf)
162 self.create['proxy'] = "http://" + self.create['proxy']
163
164 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
165
166 # bootstrap option handling
167 self.set_runtime(self.create['runtime'])
168 if isinstance(self.bootstrap['packages'], basestring):
169 packages = self.bootstrap['packages'].replace('\n', ' ')
170 if packages.find(',') != -1:
171 packages = packages.split(',')
172 else:
173 packages = packages.split()
174 self.bootstrap['packages'] = packages
175
176 def _parse_kickstart(self, ksconf=None):
177 if not ksconf:
178 return
179
180 ksconf = misc.normalize_ksfile(ksconf,
181 self.create['release'],
182 self.create['arch'])
183
184 ks = kickstart.read_kickstart(ksconf)
185
186 self.create['ks'] = ks
187 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
188
189 self.create['name'] = misc.build_name(ksconf,
190 self.create['release'],
191 self.create['name_prefix'],
192 self.create['name_suffix'])
193
194 msger.info("Retrieving repo metadata:")
195 ksrepos = misc.get_repostrs_from_ks(ks)
196 if not ksrepos:
197 raise errors.KsError('no valid repos found in ks file')
198
199 for repo in ksrepos:
200 if 'baseurl' in repo and repo['baseurl'].startswith("file:"):
201 repourl = repo['baseurl'].replace('file:', '')
202 repourl = "/%s" % repourl.lstrip('/')
203 self.create['localrepos'].append(repourl)
204
205 self.create['repomd'] = misc.get_metadata_from_repos(
206 ksrepos,
207 self.create['cachedir'])
208 msger.raw(" DONE")
209
210 target_archlist, archlist = misc.get_arch(self.create['repomd'])
211 if self.create['arch']:
212 if self.create['arch'] not in archlist:
213 raise errors.ConfigError("Invalid arch %s for repository. "
214 "Valid arches: %s" \
215 % (self.create['arch'], ', '.join(archlist)))
216 else:
217 if len(target_archlist) == 1:
218 self.create['arch'] = str(target_archlist[0])
219 msger.info("\nUse detected arch %s." % target_archlist[0])
220 else:
221 raise errors.ConfigError("Please specify a valid arch, "
222 "the choice can be: %s" \
223 % ', '.join(archlist))
224
225 kickstart.resolve_groups(self.create, self.create['repomd'])
226
227 # check selinux, it will block arm and btrfs image creation
228 misc.selinux_check(self.create['arch'],
229 [p.fstype for p in ks.handler.partition.partitions])
230
231 def set_runtime(self, runtime):
232 if runtime not in ("bootstrap", "native"):
233 msger.error("Invalid runtime mode: %s" % runtime)
234
235 if misc.get_distro()[0] in ("tizen", "Tizen"):
236 runtime = "native"
237 self.create['runtime'] = runtime
238
239configmgr = ConfigMgr()