3 # Copyright (c) 2011 Intel, Inc.
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
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
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.
23 from .utils import misc, runner, proxy, errors
25 DEFAULT_GSITECONF = '/etc/mic/mic.conf'
27 class ConfigMgr(object):
28 DEFAULTS = {'common': {
29 "distro_name": "Default Distribution",
32 "tmpdir": '/var/tmp/mic',
33 "cachedir": '/var/tmp/mic/cache',
34 "outdir": './mic-output',
35 "arch": None, # None means auto-detect
41 "local_pkgs_path": None,
45 "compress_disk_image": None,
54 # make the manager class as singleton
56 def __new__(cls, *args, **kwargs):
58 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
62 def __init__(self, ksconf=None, siteconf=None):
63 # reset config options
66 # initial options from siteconf
68 self._siteconf = siteconf
70 # use default site config
71 self._siteconf = DEFAULT_GSITECONF
78 self.__siteconf = None
80 # initialize the values with defaults
81 for sec, vals in self.DEFAULTS.iteritems():
82 setattr(self, sec, vals)
84 def __set_siteconf(self, siteconf):
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)
94 def __set_ksconf(self, ksconf):
95 if not os.path.isfile(ksconf):
96 msger.error('Cannot find ks file: %s' % ksconf)
98 self.__ksconf = ksconf
99 self._parse_kickstart(ksconf)
100 def __get_ksconf(self):
102 _ksconf = property(__get_ksconf, __set_ksconf)
104 def _parse_siteconf(self, siteconf):
108 if not os.path.exists(siteconf):
109 raise errors.ConfigError("Failed to find config file: %s" \
112 parser = ConfigParser.SafeConfigParser()
113 parser.read(siteconf)
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)
120 for section in parser.sections():
121 if section in self.DEFAULTS.keys():
122 getattr(self, section).update(dict(parser.items(section)))
124 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
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.
131 for path in ["/usr/sbin/getenforce",
132 "/usr/bin/getenforce",
135 "/usr/local/sbin/getenforce",
136 "/usr/locla/bin/getenforce"
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")
146 for part in ks.handler.partition.partitions:
147 if part.fstype == "btrfs":
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.")
157 def _parse_kickstart(self, ksconf=None):
161 ks = kickstart.read_kickstart(ksconf)
163 self.create['ks'] = ks
164 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
166 if self.create['name_prefix']:
167 self.create['name'] = "%s-%s" % (self.create['name_prefix'],
170 self._selinux_check (self.create['arch'], ks)
172 msger.info("Retrieving repo metadata:")
173 ksrepos = misc.get_repostrs_from_ks(ks)
174 self.create['repomd'] = misc.get_metadata_from_repos(
176 self.create['cachedir'])
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. "
184 % (self.create['arch'], ', '.join(archlist)))
186 if len(target_archlist) == 1:
187 self.create['arch'] = str(target_archlist[0])
188 msger.info("\nUse detected arch %s." % target_archlist[0])
190 raise errors.ConfigError("Please specify a valid arch, "
191 "your choise can be: %s" \
192 % ', '.join(archlist))
194 kickstart.resolve_groups(self.create, self.create['repomd'])
196 configmgr = ConfigMgr()