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