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