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