Merge pull request #13 from saukko/master
[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, 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 class ConfigMgr(object):
28     DEFAULTS = {'common': {
29                     "distro_name": "Default Distribution",
30                 },
31                 'create': {
32                     "tmpdir": '/var/tmp/mic',
33                     "cachedir": '/var/tmp/mic/cache',
34                     "outdir": './mic-output',
35                     "bootstrapdir": '/var/tmp/mic/bootstrap',
36
37                     "arch": None, # None means auto-detect
38                     "pkgmgr": "yum",
39                     "name": "output",
40                     "ksfile": None,
41                     "ks": None,
42                     "repomd": None,
43                     "local_pkgs_path": None,
44                     "release": None,
45                     "logfile": None,
46                     "record_pkgs": [],
47                     "rpmver": None,
48                     "compress_disk_image": None,
49                     "name_prefix": None,
50                     "proxy": None,
51                     "no_proxy": None,
52                     "copy_kernel": False,
53
54                     "runtime": None,
55                 },
56                 'chroot': {
57                     "saveto": None,
58                 },
59                 'convert': {
60                     "shell": False,
61                 },
62                 'bootstraps': {},
63                }
64
65     # make the manager class as singleton
66     _instance = None
67     def __new__(cls, *args, **kwargs):
68         if not cls._instance:
69             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
70
71         return cls._instance
72
73     def __init__(self, ksconf=None, siteconf=None):
74         # reset config options
75         self.reset()
76
77         # initial options from siteconf
78         if siteconf:
79             self._siteconf = siteconf
80         else:
81             # use default site config
82             self._siteconf = DEFAULT_GSITECONF
83
84         if ksconf:
85             self._ksconf = ksconf
86
87     def reset(self):
88         self.__ksconf = None
89         self.__siteconf = None
90
91         # initialize the values with defaults
92         for sec, vals in self.DEFAULTS.iteritems():
93             setattr(self, sec, vals)
94
95     def __set_siteconf(self, siteconf):
96         try:
97             self.__siteconf = siteconf
98             self._parse_siteconf(siteconf)
99         except ConfigParser.Error, error:
100             raise errors.ConfigError("%s" % error)
101     def __get_siteconf(self):
102         return self.__siteconf
103     _siteconf = property(__get_siteconf, __set_siteconf)
104
105     def __set_ksconf(self, ksconf):
106         if not os.path.isfile(ksconf):
107             msger.error('Cannot find ks file: %s' % ksconf)
108
109         self.__ksconf = ksconf
110         self._parse_kickstart(ksconf)
111     def __get_ksconf(self):
112         return self.__ksconf
113     _ksconf = property(__get_ksconf, __set_ksconf)
114
115     def _parse_siteconf(self, siteconf):
116         if not siteconf:
117             return
118
119         if not os.path.exists(siteconf):
120             raise errors.ConfigError("Failed to find config file: %s" \
121                                      % siteconf)
122
123         parser = ConfigParser.SafeConfigParser()
124         parser.read(siteconf)
125
126         for section in parser.sections():
127             if section in self.DEFAULTS:
128                 getattr(self, section).update(dict(parser.items(section)))
129
130         # append common section items to other sections
131         for section in self.DEFAULTS.keys():
132             if section != "common" and not section.startswith('bootstrap'):
133                 getattr(self, section).update(self.common)
134
135         # check and normalize the scheme of proxy url
136         if self.create['proxy']:
137             m = re.match('^(\w+)://.*', self.create['proxy'])
138             if m:
139                 scheme = m.group(1)
140                 if scheme not in ('http', 'https', 'ftp', 'socks'):
141                     msger.error("%s: proxy scheme is incorrect" % siteconf)
142             else:
143                 msger.warning("%s: proxy url w/o scheme, use http as default"
144                               % siteconf)
145                 self.create['proxy'] = "http://" + self.create['proxy']
146
147         proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
148
149         for section in parser.sections():
150             if section.startswith('bootstrap'):
151                 name = section
152                 repostr = {}
153                 for option in parser.options(section):
154                     if option == 'name':
155                         name = parser.get(section, 'name')
156                         continue
157
158                     val = parser.get(section, option)
159                     if '_' in option:
160                         (reponame, repoopt) = option.split('_')
161                         if repostr.has_key(reponame):
162                             repostr[reponame] += "%s:%s," % (repoopt, val)
163                         else:
164                             repostr[reponame] = "%s:%s," % (repoopt, val)
165                         continue
166
167                     if val.split(':')[0] in ('file', 'http', 'https', 'ftp'):
168                         if repostr.has_key(option):
169                             repostr[option] += "name:%s,baseurl:%s," % (option, val)
170                         else:
171                             repostr[option]  = "name:%s,baseurl:%s," % (option, val)
172                         continue
173
174                 self.bootstraps[name] = repostr
175
176     def _selinux_check(self, arch, ks):
177         """If a user needs to use btrfs or creates ARM image,
178         selinux must be disabled at start.
179         """
180
181         for path in ["/usr/sbin/getenforce",
182                      "/usr/bin/getenforce",
183                      "/sbin/getenforce",
184                      "/bin/getenforce",
185                      "/usr/local/sbin/getenforce",
186                      "/usr/locla/bin/getenforce"
187                      ]:
188             if os.path.exists(path):
189                 selinux_status = runner.outs([path])
190                 if arch and arch.startswith("arm") \
191                         and selinux_status == "Enforcing":
192                     raise errors.ConfigError("Can't create arm image if "
193                           "selinux is enabled, please disable it and try again")
194
195                 use_btrfs = False
196                 for part in ks.handler.partition.partitions:
197                     if part.fstype == "btrfs":
198                         use_btrfs = True
199                         break
200
201                 if use_btrfs and selinux_status == "Enforcing":
202                     raise errors.ConfigError("Can't create image using btrfs "
203                                            "filesystem if selinux is enabled, "
204                                            "please disable it and try again.")
205                 break
206
207     def _parse_kickstart(self, ksconf=None):
208         if not ksconf:
209             return
210
211         ks = kickstart.read_kickstart(ksconf)
212
213         self.create['ks'] = ks
214         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
215
216         if self.create['name_prefix']:
217             self.create['name'] = "%s-%s" % (self.create['name_prefix'],
218                                              self.create['name'])
219
220         self._selinux_check (self.create['arch'], ks)
221
222         msger.info("Retrieving repo metadata:")
223         ksrepos = misc.get_repostrs_from_ks(ks)
224         if not ksrepos:
225             raise errors.KsError('no valid repos found in ks file')
226
227         self.create['repomd'] = misc.get_metadata_from_repos(
228                                                     ksrepos,
229                                                     self.create['cachedir'])
230         msger.raw(" DONE")
231
232         self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
233
234         target_archlist, archlist = misc.get_arch(self.create['repomd'])
235         if self.create['arch']:
236             if self.create['arch'] not in archlist:
237                 raise errors.ConfigError("Invalid arch %s for repository. "
238                                   "Valid arches: %s" \
239                                   % (self.create['arch'], ', '.join(archlist)))
240         else:
241             if len(target_archlist) == 1:
242                 self.create['arch'] = str(target_archlist[0])
243                 msger.info("\nUse detected arch %s." % target_archlist[0])
244             else:
245                 raise errors.ConfigError("Please specify a valid arch, "
246                                          "the choice can be: %s" \
247                                          % ', '.join(archlist))
248
249         kickstart.resolve_groups(self.create, self.create['repomd'])
250
251 configmgr = ConfigMgr()