Merge "fix convert failed caused by NoneType 'createopts'"
[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                     "arch": None, # None means auto-detect
36                     "pkgmgr": "yum",
37                     "name": "output",
38                     "ksfile": None,
39                     "ks": None,
40                     "repomd": None,
41                     "local_pkgs_path": None,
42                     "release": None,
43                     "logfile": None,
44                     "record_pkgs": [],
45                     "compress_disk_image": None,
46                     "name_prefix": None,
47                     "proxy": None,
48                     "no_proxy": None,
49                 },
50                 'chroot': {},
51                 'convert': {},
52                }
53
54     # make the manager class as singleton
55     _instance = None
56     def __new__(cls, *args, **kwargs):
57         if not cls._instance:
58             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
59
60         return cls._instance
61
62     def __init__(self, ksconf=None, siteconf=None):
63         # reset config options
64         self.reset()
65
66         # initial options from siteconf
67         if siteconf:
68             self._siteconf = siteconf
69         else:
70             # use default site config
71             self._siteconf = DEFAULT_GSITECONF
72
73         if ksconf:
74             self._ksconf = ksconf
75
76     def reset(self):
77         self.__ksconf = None
78         self.__siteconf = None
79
80         # initialize the values with defaults
81         for sec, vals in self.DEFAULTS.iteritems():
82             setattr(self, sec, vals)
83
84     def __set_siteconf(self, siteconf):
85         try:
86             self.__siteconf = siteconf
87             self._parse_siteconf(siteconf)
88         except ConfigParser.Error, error:
89             raise errors.ConfigError("%s" % error)
90     def __get_siteconf(self):
91         return self.__siteconf
92     _siteconf = property(__get_siteconf, __set_siteconf)
93
94     def __set_ksconf(self, ksconf):
95         if not os.path.isfile(ksconf):
96             msger.error('Cannot find ks file: %s' % ksconf)
97
98         self.__ksconf = ksconf
99         self._parse_kickstart(ksconf)
100     def __get_ksconf(self):
101         return self.__ksconf
102     _ksconf = property(__get_ksconf, __set_ksconf)
103
104     def _parse_siteconf(self, siteconf):
105         if not siteconf:
106             return
107
108         if not os.path.exists(siteconf):
109             raise errors.ConfigError("Failed to find config file: %s" \
110                                      % siteconf)
111
112         parser = ConfigParser.SafeConfigParser()
113         parser.read(siteconf)
114
115         # append common section items to other sections
116         for section in self.DEFAULTS.keys():
117             if section != "common":
118                 getattr(self, section).update(self.common)
119
120         for section in parser.sections():
121             if section in self.DEFAULTS.keys():
122                 getattr(self, section).update(dict(parser.items(section)))
123
124         proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
125
126     def _selinux_check(self, arch, ks):
127         """If a user needs to use btrfs or creates ARM image,
128         selinux must be disabled at start.
129         """
130
131         for path in ["/usr/sbin/getenforce",
132                      "/usr/bin/getenforce",
133                      "/sbin/getenforce",
134                      "/bin/getenforce",
135                      "/usr/local/sbin/getenforce",
136                      "/usr/locla/bin/getenforce"
137                      ]:
138             if os.path.exists(path):
139                 selinux_status = runner.outs([path])
140                 if arch and arch.startswith("arm") \
141                         and selinux_status == "Enforcing":
142                     raise errors.ConfigError("Can't create arm image if "
143                           "selinux is enabled, please disable it and try again")
144
145                 use_btrfs = False
146                 for part in ks.handler.partition.partitions:
147                     if part.fstype == "btrfs":
148                         use_btrfs = True
149                         break
150
151                 if use_btrfs and selinux_status == "Enforcing":
152                     raise errors.ConfigError("Can't create image using btrfs "
153                                            "filesystem if selinux is enabled, "
154                                            "please disable it and try again.")
155                 break
156
157     def _parse_kickstart(self, ksconf=None):
158         if not ksconf:
159             return
160
161         ks = kickstart.read_kickstart(ksconf)
162
163         self.create['ks'] = ks
164         self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
165
166         if self.create['name_prefix']:
167             self.create['name'] = "%s-%s" % (self.create['name_prefix'],
168                                              self.create['name'])
169
170         self._selinux_check (self.create['arch'], ks)
171
172         msger.info("Retrieving repo metadata:")
173         ksrepos = misc.get_repostrs_from_ks(ks)
174         self.create['repomd'] = misc.get_metadata_from_repos(
175                                                     ksrepos,
176                                                     self.create['cachedir'])
177         msger.raw(" DONE")
178
179         target_archlist, archlist = misc.get_arch(self.create['repomd'])
180         if self.create['arch']:
181             if self.create['arch'] not in archlist:
182                 raise errors.ConfigError("Invalid arch %s for repository. "
183                                   "Valid arches: %s" \
184                                   % (self.create['arch'], ', '.join(archlist)))
185         else:
186             if len(target_archlist) == 1:
187                 self.create['arch'] = str(target_archlist[0])
188                 msger.info("\nUse detected arch %s." % target_archlist[0])
189             else:
190                 raise errors.ConfigError("Please specify a valid arch, "
191                                          "your choise can be: %s" \
192                                          % ', '.join(archlist))
193
194         kickstart.resolve_groups(self.create, self.create['repomd'])
195
196 configmgr = ConfigMgr()