import jinja2
+from six.moves import configparser
+
# Own modules
-from fb_tools.common import pp, to_str, is_sequence
+from fb_tools.common import pp, to_str, is_sequence, to_bool
from fb_tools.errors import HandlerError, ExpectedHandlerError
from fb_tools.handling_obj import CompletedProcess
from fb_tools.handler import BaseHandler
from .xlate import XLATOR
-__version__ = '0.8.7'
+__version__ = '0.8.8'
LOG = logging.getLogger(__name__)
profile = self.cfg.cobbler_profile
LOG.debug(_("Checking existing profile {!r} ...").format(profile))
- remote_file = self.cfg.cobbler_profile_dir / (self.cfg.cobbler_profile + '.json')
- return
+ profile_vars = self.get_profile_vars(profile)
- fcontent = self.get_remote_filecontent(remote_file)
- if self.verbose > 2:
- LOG.debug(
- _("Got content of remote {!r}:").format(str(remote_file)) + '\n' + fcontent)
+ if self.verbose > 1:
+ LOG.debug(_("Got profile variables:") + '\n' + pp(profile_vars))
+ self._change_profile(profile_vars)
+
+ # -------------------------------------------------------------------------
+ def get_profile_vars(self, profile):
+
+ vars_out = '[main]\n'
+
+ kwargs = {
+ 'allow_no_value': True,
+ 'strict': False,
+ }
+
+ cmd = ('profile', 'dumpvars', '--name', profile)
+ proc = self.exec_cobbler(cmd, no_simulate=True)
+ vars_out += proc.stdout
+
+ parser = configparser.RawConfigParser(**kwargs)
try:
- js = json.loads(fcontent)
- except JSONDecodeError as e:
- msg = _("Error interpreting JS: {}").format(e)
+ parser.read_string(vars_out)
+ except Exception as e:
+ msg = _("Got {what} on reading and parsing of profile {p!r}:").format(
+ what=e.__class__.__name__, p=profile)
+ msg += ' ' + str(e)
raise ExpectedCobblerError(msg)
- if self.verbose > 1:
- LOG.debug(_("Got json object for profile {!r}:").format(profile) + '\n' + pp(js))
+ data = {}
- self._change_profile(js)
+ for (key, value) in parser.items('main'):
+ k = key.lower()
+ data[k] = value
+
+ return data
# -------------------------------------------------------------------------
- def _change_profile(self, js):
+ def _change_profile(self, profile_vars):
profile = self.cfg.cobbler_profile
distro = self.cfg.cobbler_distro
+ distro_info = self.cfg.current_distro
+
+ LOG.debug(_("Checking existing profile {n!r} ({d}) ...").format(
+ n=profile, d=distro_info.description))
+
+ repos = []
+ if distro_info.repos:
+ repos = distro_info.repos.as_list()
+ repos_str = ' '.join(repos)
+
+ comment = "Profile for creating a {} VM.".format(distro_info.description)
+ name_servers = '[' + ', '.join(
+ map(lambda x: "'" + x + "'", self.cfg.cobbler_nameservers)) + ']'
+ dns_search = '[' + ', '.join(
+ map(lambda x: "'" + x + "'", self.cfg.cobbler_dns_search)) + ']'
args = []
- if js['distro'] != distro:
+ if self.verbose > 1:
+ msg = _("Checking for distro:") + ' ' + distro
+ LOG.debug(msg)
+ if profile_vars['distro'] != distro:
args.append('--distro')
args.append(distro)
- if not js['enable_menu']:
+ enable_menu = to_bool(profile_vars['enable_menu'])
+ if not enable_menu:
args.append('--enable-menu')
args.append('1')
- if js['kickstart'] != str(self.cfg.cobbler_profile_ks):
- if self.cfg.cobbler_major_version == 3:
+ if self.cfg.cobbler_major_version == 3:
+ if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name):
args.append('--autoinstall')
- else:
+ args.append(str(self.cfg.cobbler_profile_ks.name))
+ else:
+ if profile_vars['kickstart'] != str(self.cfg.cobbler_profile_ks):
args.append('--kickstart')
- args.append(str(self.cfg.cobbler_profile_ks))
+ args.append(str(self.cfg.cobbler_profile_ks))
- if js['repos'] != self.cfg.cobbler_profile_repos:
+ if self.verbose > 1:
+ msg = _("Checking for repos:") + ' ' + repos_str
+ LOG.debug(msg)
+ if profile_vars['repos'] != repos_str:
args.append('--repos')
- args.append(' '.join(self.cfg.cobbler_profile_repos))
+ args.append(repos_str)
- os_id = self.cfg.os_id
- comment = "Kickstart file for creating a {} profile.".format(os_id)
- if js['comment'] != comment:
+ if profile_vars['comment'] != comment:
args.append('--comment')
args.append(comment)
- if js['name_servers'] != self.cfg.cobbler_nameservers:
+ if self.verbose > 1:
+ msg = _("Checking for nameservers:") + ' ' + name_servers
+ LOG.debug(msg)
+ if profile_vars['name_servers'] != name_servers:
args.append('--name-servers')
args.append(' '.join(self.cfg.cobbler_nameservers))
- if js['name_servers_search'] != self.cfg.cobbler_dns_search:
+ if self.verbose > 1:
+ msg = _("Checking for DNS search domains:") + ' ' + dns_search
+ LOG.debug(msg)
+ if profile_vars['name_servers_search'] != dns_search:
args.append('--name-servers-search')
args.append(' '.join(self.cfg.cobbler_dns_search))