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