from six.moves import configparser
-from fb_tools.common import pp, to_str, is_sequence, to_bool
-from fb_tools.xlate import format_list
+from fb_tools.common import pp, to_bool
# Own modules
from .. import print_section_start, print_section_end
-from ..errors import CobblerError, ExpectedCobblerError
+from ..errors import ExpectedCobblerError
from ..xlate import XLATOR
-__version__ = '0.1.0'
+__version__ = '0.1.1'
LOG = logging.getLogger(__name__)
A mixin class for extending the Cobbler class for profile dependend methods.
"""
+ profile_comment_tpl = "Profile for creating a {} VM."
+
# -------------------------------------------------------------------------
def get_profile_list(self):
"""Trying to get a list of all configured cobbler profiles."""
def _change_profile(self, profile_vars):
profile = self.cfg.cobbler_profile
- distro = self.cfg.cobbler_distro
distro_info = self.cfg.current_distro
- status = self.cfg.system_status
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)
+ args = self._get_profile_change_attribs(profile_vars)
+ if not args:
+ LOG.debug(_("No need for changing profile {!r}").format(profile))
+ return
+
+ args = ['profile', 'edit', '--name', profile] + args
+
+ if self.verbose > 1:
+ LOG.debug('Arguments for changing profile:\n' + pp(args))
+ return
+
+ proc = self.exec_cobbler(args)
+
+ if proc.returncode:
+ err = _('No error message')
+ if proc.stderr:
+ err = proc.stderr
+ elif proc.stdout:
+ err = proc.stdout
+ msg = _("Error editing a cobbler profile - returncode was {rc}: {err}").format(
+ rc=proc.returncode, err=err)
+ raise ExpectedCobblerError(msg)
+
+ # -------------------------------------------------------------------------
+ def _get_profile_change_attribs(self, profile_vars):
+
+ distro = self.cfg.cobbler_distro
+ distro_info = self.cfg.current_distro
+ repos_str = distro_info.repos_string
+ comment = self.profile_comment_tpl.format(distro_info.description)
+ status = self.cfg.system_status
- comment = "Profile for creating a {} VM.".format(distro_info.description)
name_servers = '[' + ', '.join(
map(lambda x: "'" + x + "'", self.cfg.cobbler_nameservers)) + ']'
dns_search = '[' + ', '.join(
args.append('--enable-menu')
args.append('1')
- if self.cfg.cobbler_major_version == 3:
- if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name):
- args.append('--autoinstall')
- 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))
+ if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name):
+ args.append('--autoinstall')
+ args.append(str(self.cfg.cobbler_profile_ks.name))
if self.verbose > 1:
msg = _("Checking for repos:") + ' ' + repos_str
args.append('--name-servers-search')
args.append(' '.join(self.cfg.cobbler_dns_search))
- ks_meta_ok = True
- ks_meta_vars = {}
- if 'autoinstall_meta' in profile_vars:
- ks_meta_vars = self.xform_ks_meta(profile_vars['autoinstall_meta'])
- if 'ROOT_PWD_HASH' not in ks_meta_vars:
- LOG.debug(_('Profile ks_meta {!r} is not ok.').format('ROOT_PWD_HASH'))
- ks_meta_ok = False
- if ('SWAP_SIZE_MB' not in ks_meta_vars or
- ks_meta_vars['SWAP_SIZE_MB'] != str(self.cfg.swap_size_mb)):
- LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SWAP_SIZE_MB'))
- ks_meta_ok = False
- if ('SYSTEM_STATUS' not in ks_meta_vars or
- ks_meta_vars['SYSTEM_STATUS'] != status):
- LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SYSTEM_STATUS'))
- ks_meta_ok = False
- if ('WS_REL_FILESDIR' not in ks_meta_vars or
- ks_meta_vars['WS_REL_FILESDIR'] != str(self.cfg.cobbler_ws_rel_filesdir)):
- LOG.debug(_('Profile ks_meta {!r} is not ok.').format('WS_REL_FILESDIR'))
- ks_meta_ok = False
- if ('COBBLER_URL' not in ks_meta_vars or
- ks_meta_vars['COBBLER_URL'] != "http://{}".format(self.cfg.cobbler_host)):
- LOG.debug(_('Profile ks_meta {!r} is not ok.').format('COBBLER_URL'))
- ks_meta_ok = False
-
- if not ks_meta_ok:
+ if not self._check_profile_ks_metavars(profile_vars):
ks_meta_list = []
ks_meta_list.append("ROOT_PWD_HASH={}".format(self.cfg.get_root_pwd_hash()))
ks_meta_list.append("SWAP_SIZE_MB={}".format(self.cfg.swap_size_mb))
args.append(ks_meta)
if self.verbose:
- LOG.debug("Args for 'profile edit:\n{}".format(pp(args)))
+ LOG.debug(_("Arguments for editing the profile:") + '\n' + pp(args))
- if not args:
- LOG.debug(_("No need for changing profile {!r}").format(profile))
- return
+ return args
- args = ['profile', 'edit', '--name', profile] + args
+ # -------------------------------------------------------------------------
+ def _check_profile_ks_metavars(self, profile_vars):
- if self.verbose > 1:
- LOG.debug('Arguments for changing profile:\n' + pp(args))
- return
+ status = self.cfg.system_status
- proc = self.exec_cobbler(args)
+ ks_meta_ok = True
+ ks_meta_vars = {}
- if proc.returncode:
- err = _('No error message')
- if proc.stderr:
- err = proc.stderr
- elif proc.stdout:
- err = proc.stdout
- msg = _("Error editing a cobbler profile - returncode was {rc}: {err}").format(
- rc=proc.returncode, err=err)
- raise ExpectedCobblerError(msg)
+ if 'autoinstall_meta' in profile_vars:
+ ks_meta_vars = self.xform_ks_meta(profile_vars['autoinstall_meta'])
+
+ if 'ROOT_PWD_HASH' not in ks_meta_vars:
+ LOG.debug(_('Profile ks_meta {!r} is not ok.').format('ROOT_PWD_HASH'))
+ ks_meta_ok = False
+
+ if 'SWAP_SIZE_MB' not in ks_meta_vars or \
+ ks_meta_vars['SWAP_SIZE_MB'] != str(self.cfg.swap_size_mb):
+ LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SWAP_SIZE_MB'))
+ ks_meta_ok = False
+
+ if 'SYSTEM_STATUS' not in ks_meta_vars or \
+ ks_meta_vars['SYSTEM_STATUS'] != status:
+ LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SYSTEM_STATUS'))
+ ks_meta_ok = False
+
+ if 'WS_REL_FILESDIR' not in ks_meta_vars or \
+ ks_meta_vars['WS_REL_FILESDIR'] != str(self.cfg.cobbler_ws_rel_filesdir):
+ LOG.debug(_('Profile ks_meta {!r} is not ok.').format('WS_REL_FILESDIR'))
+ ks_meta_ok = False
+ if 'COBBLER_URL' not in ks_meta_vars or \
+ ks_meta_vars['COBBLER_URL'] != "http://{}".format(self.cfg.cobbler_host):
+ LOG.debug(_('Profile ks_meta {!r} is not ok.').format('COBBLER_URL'))
+ ks_meta_ok = False
+
+ return ks_meta_ok
# -------------------------------------------------------------------------
def add_profile(self):
LOG.info(_("Creating new profile {!r} ...").format(profile))
distro_info = self.cfg.current_distro
- comment = "Profile for creating a {} VM.".format(distro_info.description)
+ comment = self.profile_comment_tpl.format(distro_info.description)
status = self.cfg.system_status
LOG.debug("Using kickstart file {!r}".format(self.cfg.cobbler_profile_ks))