e29bd3bcf6fd77c5b26b044970d86f02f5e156a8
[tools/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     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
38 class 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                     "plugin_dir": "/usr/lib/mic/plugins",
51                     "arch": None, # None means auto-detect
52                     "pkgmgr": "auto",
53                     "name": "output",
54                     "ksfile": None,
55                     "ks": None,
56                     "repomd": None,
57                     "local_pkgs_path": None,
58                     "release": None,
59                     "logfile": None,
60                     "record_pkgs": [],
61                     "pack_to": None,
62                     "name_prefix": None,
63                     "name_suffix": None,
64                     "proxy": None,
65                     "no_proxy": None,
66                     "copy_kernel": False,
67                     "install_pkgs": None,
68                     "check_pkgs": [],
69                     "repourl": {},
70                     "localrepos": [],  # save localrepos
71                     "runtime": "bootstrap",
72                 },
73                 'chroot': {
74                     "saveto": None,
75                 },
76                 'convert': {
77                     "shell": False,
78                 },
79                 'bootstrap': {
80                     "rootdir": '/var/tmp/mic-bootstrap',
81                     "packages": [],
82                     "distro_name": "",
83                 },
84                }
85
86     # make the manager class as singleton
87     _instance = None
88     def __new__(cls, *args, **kwargs):
89         if not cls._instance:
90             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
91
92         return cls._instance
93
94     def __init__(self, ksconf=None, siteconf=None):
95         # reset config options
96         self.reset()
97
98         if not siteconf:
99             siteconf = get_siteconf()
100
101         # initial options from siteconf
102         self._siteconf = siteconf
103
104         if ksconf:
105             self._ksconf = ksconf
106
107     def reset(self):
108         self.__ksconf = None
109         self.__siteconf = None
110
111         # initialize the values with defaults
112         for sec, vals in self.DEFAULTS.iteritems():
113             setattr(self, sec, vals)
114
115     def __set_siteconf(self, siteconf):
116         try:
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)
124
125     def __set_ksconf(self, ksconf):
126         if not os.path.isfile(ksconf):
127             msger.error('Cannot find ks file: %s' % ksconf)
128
129         self.__ksconf = ksconf
130         self._parse_kickstart(ksconf)
131     def __get_ksconf(self):
132         return self.__ksconf
133     _ksconf = property(__get_ksconf, __set_ksconf)
134
135     def _parse_siteconf(self, siteconf):
136         if not siteconf:
137             return
138
139         if not os.path.exists(siteconf):
140             msger.warning("cannot read config file: %s" % siteconf)
141             return
142
143         parser = ConfigParser.SafeConfigParser()
144         parser.read(siteconf)
145
146         for section in parser.sections():
147             if section in self.DEFAULTS:
148                 getattr(self, section).update(dict(parser.items(section)))
149
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)
154
155         # check and normalize the scheme of proxy url
156         if self.create['proxy']:
157             m = re.match('^(\w+)://.*', self.create['proxy'])
158             if m:
159                 scheme = m.group(1)
160                 if scheme not in ('http', 'https', 'ftp', 'socks'):
161                     msger.error("%s: proxy scheme is incorrect" % siteconf)
162             else:
163                 msger.warning("%s: proxy url w/o scheme, use http as default"
164                               % siteconf)
165                 self.create['proxy'] = "http://" + self.create['proxy']
166
167         proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
168
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(',')
175             else:
176                 packages = packages.split()
177             self.bootstrap['packages'] = packages
178
179     def _parse_kickstart(self, ksconf=None):
180         if not ksconf:
181             return
182
183         ksconf = misc.normalize_ksfile(ksconf,
184                                        self.create['release'],
185                                        self.create['arch'])
186
187         ks = kickstart.read_kickstart(ksconf)
188
189         self.create['ks'] = ks
190         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
191
192         self.create['name'] = misc.build_name(ksconf,
193                                               self.create['release'],
194                                               self.create['name_prefix'],
195                                               self.create['name_suffix'])
196
197         msger.info("Retrieving repo metadata:")
198         ksrepos = misc.get_repostrs_from_ks(ks)
199         if not ksrepos:
200             raise errors.KsError('no valid repos found in ks file')
201
202         for repo in ksrepos:
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)
207
208         self.create['repomd'] = misc.get_metadata_from_repos(
209                                                     ksrepos,
210                                                     self.create['cachedir'])
211         msger.raw(" DONE")
212
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. "
217                                   "Valid arches: %s" \
218                                   % (self.create['arch'], ', '.join(archlist)))
219         else:
220             if len(target_archlist) == 1:
221                 self.create['arch'] = str(target_archlist[0])
222                 msger.info("\nUse detected arch %s." % target_archlist[0])
223             else:
224                 raise errors.ConfigError("Please specify a valid arch, "
225                                          "the choice can be: %s" \
226                                          % ', '.join(archlist))
227
228         kickstart.resolve_groups(self.create, self.create['repomd'])
229
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])
233
234     def set_runtime(self, runtime):
235         if runtime not in ("bootstrap", "native"):
236             msger.error("Invalid runtime mode: %s" % runtime)
237
238         if misc.get_distro()[0] in ("tizen", "Tizen"):
239             runtime = "native"
240         self.create['runtime'] = runtime
241
242 configmgr = ConfigMgr()