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