Merge release-0.28.17 from 'tools/mic'
[platform/upstream/mic.git] / mic / conf.py
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
18 import os, sys, re
19 import ConfigParser
20
21 from mic import msger
22 from mic import kickstart
23 from mic.utils import misc, runner, proxy, errors
24
25
26 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
27
28
29 def get_siteconf():
30     if hasattr(sys, 'real_prefix'):
31         return os.path.join(sys.prefix, "etc/mic/mic.conf")
32     else:
33         return DEFAULT_GSITECONF
34
35 def inbootstrap():
36     if os.path.exists(os.path.join("/", ".chroot.lock")):
37         return True
38     return (os.stat("/").st_ino != 2)
39
40 class ConfigMgr(object):
41     prefer_backends = ["zypp", "yum"]
42
43     DEFAULTS = {'common': {
44                     "distro_name": "Default Distribution",
45                     "plugin_dir": "/usr/lib/mic/plugins", # TODO use prefix also?
46                 },
47                 'create': {
48                     "tmpdir": '/var/tmp/mic',
49                     "cachedir": '/var/tmp/mic/cache',
50                     "outdir": './mic-output',
51                     "destdir": None,
52                     "arch": None, # None means auto-detect
53                     "pkgmgr": "auto",
54                     "name": "output",
55                     "ksfile": None,
56                     "ks": None,
57                     "repomd": None,
58                     "local_pkgs_path": None,
59                     "release": None,
60                     "logfile": None,
61                     "releaselog": False,
62                     "record_pkgs": [],
63                     "pack_to": None,
64                     "name_prefix": None,
65                     "name_suffix": None,
66                     "proxy": None,
67                     "no_proxy": None,
68                     "ssl_verify": "yes",
69                     "copy_kernel": False,
70                     "install_pkgs": None,
71                     "check_pkgs": [],
72                     "repourl": {},
73                     "localrepos": [],  # save localrepos
74                     "localtpkrepos":[],
75                     "runtime": "bootstrap",
76                     "extrarepos": {},
77                     "ignore_ksrepo": False,
78                     "strict_mode": False,
79                     "run_script": None,
80                     "tpk_install": None,
81                     "use_mic_in_bootstrap": False,
82                     "skip_set_hosts": False,
83                     "postscripts_maxruntime": 120,
84                     "block_recommends": False,
85                 },
86                 'chroot': {
87                     "saveto": None,
88                 },
89                 'convert': {
90                     "shell": False,
91                 },
92                 'bootstrap': {
93                     "rootdir": '/var/tmp/mic-bootstrap',
94                     "packages": [],
95                     "distro_name": "",
96                 },
97                }
98
99     # make the manager class as singleton
100     _instance = None
101     def __new__(cls, *args, **kwargs):
102         if not cls._instance:
103             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
104
105         return cls._instance
106
107     def __init__(self, ksconf=None, siteconf=None):
108         # reset config options
109         self.reset()
110
111         if not siteconf:
112             siteconf = get_siteconf()
113
114         # initial options from siteconf
115         self._siteconf = siteconf
116
117         if ksconf:
118             self._ksconf = ksconf
119
120     def reset(self):
121         self.__ksconf = None
122         self.__siteconf = None
123
124         # initialize the values with defaults
125         for sec, vals in self.DEFAULTS.iteritems():
126             setattr(self, sec, vals)
127
128     def __set_siteconf(self, siteconf):
129         try:
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)
137
138     def __set_ksconf(self, ksconf):
139         if not os.path.isfile(ksconf):
140             raise errors.KsError('Cannot find ks file: %s' % ksconf)
141
142         self.__ksconf = ksconf
143         self._parse_kickstart(ksconf)
144     def __get_ksconf(self):
145         return self.__ksconf
146     _ksconf = property(__get_ksconf, __set_ksconf)
147
148     def _parse_siteconf(self, siteconf):
149
150         if os.getenv("MIC_PLUGIN_DIR"):
151             self.common["plugin_dir"] = os.environ["MIC_PLUGIN_DIR"]
152
153         if siteconf and not os.path.exists(siteconf):
154             msger.warning("cannot find config file: %s" % siteconf)
155             siteconf = None
156
157         if not 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)
163
164             return
165
166         parser = ConfigParser.SafeConfigParser()
167         parser.read(siteconf)
168
169         for section in parser.sections():
170             if section in self.DEFAULTS:
171                 getattr(self, section).update(dict(parser.items(section)))
172
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)
177
178         # check and normalize the scheme of proxy url
179         if self.create['proxy']:
180             m = re.match('^(\w+)://.*', self.create['proxy'])
181             if m:
182                 scheme = m.group(1)
183                 if scheme not in ('http', 'https', 'ftp', 'socks'):
184                     raise errors.ConfigError("%s: proxy scheme is incorrect" % siteconf)
185             else:
186                 msger.warning("%s: proxy url w/o scheme, use http as default"
187                               % siteconf)
188                 self.create['proxy'] = "http://" + self.create['proxy']
189
190         proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
191
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(',')
198             else:
199                 packages = packages.split()
200             self.bootstrap['packages'] = packages
201
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
206             else:
207                 self.create['use_mic_in_bootstrap'] = False
208
209     def _parse_kickstart(self, ksconf=None):
210         if not ksconf:
211             return
212
213         ksconf = misc.normalize_ksfile(ksconf,
214                                        self.create['release'],
215                                        self.create['arch'])
216
217         ks = kickstart.read_kickstart(ksconf)
218
219         self.create['ks'] = ks
220         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
221
222         self.create['name'] = misc.build_name(ksconf,
223                                               self.create['release'],
224                                               self.create['name_prefix'],
225                                               self.create['name_suffix'])
226
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'],
231                                                            self.create['name'])
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'])
239
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
244                 self.set_logfile()
245
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'])
252
253         msger.info("Retrieving repo metadata:")
254         ksrepos = kickstart.get_repos(ks,
255                                       self.create['extrarepos'],
256                                       self.create['ignore_ksrepo'])
257         if not ksrepos:
258             raise errors.KsError('no valid repos found in ks file')
259
260         for repo in ksrepos:
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)
265
266         self.create['repomd'] = misc.get_metadata_from_repos(
267                                                     ksrepos,
268                                                     self.create['cachedir'])
269         kstpkrepos = kickstart.get_tpkrepos(ks)
270         if kstpkrepos:
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)
276
277         msger.raw(" DONE")
278
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. "
283                                   "Valid arches: %s" \
284                                   % (self.create['arch'], ', '.join(archlist)))
285         else:
286             if len(target_archlist) == 1:
287                 self.create['arch'] = str(target_archlist[0])
288                 msger.info("Use detected arch %s." % target_archlist[0])
289             else:
290                 raise errors.ConfigError("Please specify a valid arch, "
291                                          "the choice can be: %s" \
292                                          % ', '.join(archlist))
293
294         kickstart.resolve_groups(self.create, self.create['repomd'])
295
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])
299
300     def set_logfile(self, logfile = None):
301         if not logfile:
302             logfile = self.create['logfile']
303
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)
308         if inbootstrap():
309             mode = 'a'
310         else:
311             mode = 'w'
312         msger.set_logfile(self.create['logfile'], mode)
313
314     def set_runtime(self, runtime):
315         if runtime != "bootstrap":
316             raise errors.CreatorError("Invalid runtime mode: %s, only 'bootstrap' mode is allowed." % runtime)
317
318         if misc.get_distro()[0] in ("tizen", "Tizen"):
319             runtime = "native"
320         self.create['runtime'] = runtime
321
322 configmgr = ConfigMgr()