Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | from wic.ksparser import KickStart, KickStartError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | from wic import msger |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | from wic.utils import misc |
| 23 | |
| 24 | |
| 25 | def get_siteconf(): |
| 26 | wic_path = os.path.dirname(__file__) |
| 27 | eos = wic_path.find('scripts') + len('scripts') |
| 28 | scripts_path = wic_path[:eos] |
| 29 | |
| 30 | return scripts_path + "/lib/image/config/wic.conf" |
| 31 | |
| 32 | class ConfigMgr(object): |
| 33 | DEFAULTS = { |
| 34 | 'common': { |
| 35 | "distro_name": "Default Distribution", |
| 36 | "plugin_dir": "/usr/lib/wic/plugins"}, # TODO use prefix also? |
| 37 | 'create': { |
| 38 | "tmpdir": '/var/tmp/wic', |
| 39 | "outdir": './wic-output', |
| 40 | "release": None, |
| 41 | "logfile": None, |
| 42 | "name_prefix": None, |
| 43 | "name_suffix": None} |
| 44 | } |
| 45 | |
| 46 | # make the manager class as singleton |
| 47 | _instance = None |
| 48 | def __new__(cls, *args, **kwargs): |
| 49 | if not cls._instance: |
| 50 | cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs) |
| 51 | |
| 52 | return cls._instance |
| 53 | |
| 54 | def __init__(self, ksconf=None, siteconf=None): |
| 55 | # reset config options |
| 56 | self.reset() |
| 57 | |
| 58 | if not siteconf: |
| 59 | siteconf = get_siteconf() |
| 60 | |
| 61 | # initial options from siteconf |
| 62 | self._siteconf = siteconf |
| 63 | |
| 64 | if ksconf: |
| 65 | self._ksconf = ksconf |
| 66 | |
| 67 | def reset(self): |
| 68 | self.__ksconf = None |
| 69 | self.__siteconf = None |
| 70 | self.create = {} |
| 71 | |
| 72 | # initialize the values with defaults |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 73 | for sec, vals in self.DEFAULTS.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | setattr(self, sec, vals) |
| 75 | |
| 76 | def __set_ksconf(self, ksconf): |
| 77 | if not os.path.isfile(ksconf): |
| 78 | msger.error('Cannot find ks file: %s' % ksconf) |
| 79 | |
| 80 | self.__ksconf = ksconf |
| 81 | self._parse_kickstart(ksconf) |
| 82 | def __get_ksconf(self): |
| 83 | return self.__ksconf |
| 84 | _ksconf = property(__get_ksconf, __set_ksconf) |
| 85 | |
| 86 | def _parse_kickstart(self, ksconf=None): |
| 87 | if not ksconf: |
| 88 | return |
| 89 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | try: |
| 91 | ksobj = KickStart(ksconf) |
| 92 | except KickStartError as err: |
| 93 | msger.error(str(err)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | |
| 95 | self.create['ks'] = ksobj |
| 96 | self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] |
| 97 | |
| 98 | self.create['name'] = misc.build_name(ksconf, |
| 99 | self.create['release'], |
| 100 | self.create['name_prefix'], |
| 101 | self.create['name_suffix']) |
| 102 | |
| 103 | configmgr = ConfigMgr() |