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,
46 "compress_disk_image": None,
55 # make the manager class as singleton
57 def __new__(cls, *args, **kwargs):
59 cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
63 def __init__(self, ksconf=None, siteconf=None):
64 # reset config options
67 # initial options from siteconf
69 self._siteconf = siteconf
71 # use default site config
72 self._siteconf = DEFAULT_GSITECONF
79 self.__siteconf = None
81 # initialize the values with defaults
82 for sec, vals in self.DEFAULTS.iteritems():
83 setattr(self, sec, vals)
85 def __set_siteconf(self, siteconf):
87 self.__siteconf = siteconf
88 self._parse_siteconf(siteconf)
89 except ConfigParser.Error, error:
90 raise errors.ConfigError("%s" % error)
91 def __get_siteconf(self):
92 return self.__siteconf
93 _siteconf = property(__get_siteconf, __set_siteconf)
95 def __set_ksconf(self, ksconf):
96 if not os.path.isfile(ksconf):
97 msger.error('Cannot find ks file: %s' % ksconf)
99 self.__ksconf = ksconf
100 self._parse_kickstart(ksconf)
101 def __get_ksconf(self):
103 _ksconf = property(__get_ksconf, __set_ksconf)
105 def _parse_siteconf(self, siteconf):
109 if not os.path.exists(siteconf):
110 raise errors.ConfigError("Failed to find config file: %s" \
113 parser = ConfigParser.SafeConfigParser()
114 parser.read(siteconf)
116 for section in parser.sections():
117 if section in self.DEFAULTS:
118 getattr(self, section).update(dict(parser.items(section)))
120 # append common section items to other sections
121 for section in self.DEFAULTS.keys():
122 if section != "common":
123 getattr(self, section).update(self.common)
125 proxy.set_proxies(self.create['proxy'], self.create['no_proxy'])
127 def _selinux_check(self, arch, ks):
128 """If a user needs to use btrfs or creates ARM image,
129 selinux must be disabled at start.
132 for path in ["/usr/sbin/getenforce",
133 "/usr/bin/getenforce",
136 "/usr/local/sbin/getenforce",
137 "/usr/locla/bin/getenforce"
139 if os.path.exists(path):
140 selinux_status = runner.outs([path])
141 if arch and arch.startswith("arm") \
142 and selinux_status == "Enforcing":
143 raise errors.ConfigError("Can't create arm image if "
144 "selinux is enabled, please disable it and try again")
147 for part in ks.handler.partition.partitions:
148 if part.fstype == "btrfs":
152 if use_btrfs and selinux_status == "Enforcing":
153 raise errors.ConfigError("Can't create image using btrfs "
154 "filesystem if selinux is enabled, "
155 "please disable it and try again.")
158 def _parse_kickstart(self, ksconf=None):
162 ks = kickstart.read_kickstart(ksconf)
164 self.create['ks'] = ks
165 self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0]
167 if self.create['name_prefix']:
168 self.create['name'] = "%s-%s" % (self.create['name_prefix'],
171 self._selinux_check (self.create['arch'], ks)
173 msger.info("Retrieving repo metadata:")
174 ksrepos = misc.get_repostrs_from_ks(ks)
175 self.create['repomd'] = misc.get_metadata_from_repos(
177 self.create['cachedir'])
180 self.create['rpmver'] = misc.get_rpmver_in_repo(self.create['repomd'])
182 target_archlist, archlist = misc.get_arch(self.create['repomd'])
183 if self.create['arch']:
184 if self.create['arch'] not in archlist:
185 raise errors.ConfigError("Invalid arch %s for repository. "
187 % (self.create['arch'], ', '.join(archlist)))
189 if len(target_archlist) == 1:
190 self.create['arch'] = str(target_archlist[0])
191 msger.info("\nUse detected arch %s." % target_archlist[0])
193 raise errors.ConfigError("Please specify a valid arch, "
194 "your choise can be: %s" \
195 % ', '.join(archlist))
197 kickstart.resolve_groups(self.create, self.create['repomd'])
199 configmgr = ConfigMgr()