ec2da56c6446425a694b2e145af29b96e5050ffe
[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                     "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                     "pack_to": 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," % \
179                                                 (option, val)
180                         else:
181                             repostr[option]  = "name:%s,baseurl:%s," % \
182                                                 (option, val)
183                         continue
184
185                 self.bootstraps[name] = repostr
186
187     def _parse_kickstart(self, ksconf=None):
188         if not ksconf:
189             return
190
191         ks = kickstart.read_kickstart(ksconf)
192
193         self.create['ks'] = ks
194         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
195
196         if self.create['name_prefix']:
197             self.create['name'] = "%s-%s" % (self.create['name_prefix'],
198                                              self.create['name'])
199
200         msger.info("Retrieving repo metadata:")
201         ksrepos = misc.get_repostrs_from_ks(ks)
202         if not ksrepos:
203             raise errors.KsError('no valid repos found in ks file')
204
205         self.create['repomd'] = misc.get_metadata_from_repos(
206                                                     ksrepos,
207                                                     self.create['cachedir'])
208         msger.raw(" DONE")
209
210         self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
211
212         target_archlist, archlist = misc.get_arch(self.create['repomd'])
213         if self.create['arch']:
214             if self.create['arch'] not in archlist:
215                 raise errors.ConfigError("Invalid arch %s for repository. "
216                                   "Valid arches: %s" \
217                                   % (self.create['arch'], ', '.join(archlist)))
218         else:
219             if len(target_archlist) == 1:
220                 self.create['arch'] = str(target_archlist[0])
221                 msger.info("\nUse detected arch %s." % target_archlist[0])
222             else:
223                 raise errors.ConfigError("Please specify a valid arch, "
224                                          "the choice can be: %s" \
225                                          % ', '.join(archlist))
226
227         kickstart.resolve_groups(self.create, self.create['repomd'])
228
229         # check selinux, it will block arm and btrfs image creation
230         misc.selinux_check(self.create['arch'],
231                            [p.fstype for p in ks.handler.partition.partitions])
232
233 configmgr = ConfigMgr()