import re
import datetime
import pipes
+import json
# Third party modules
import paramiko
from .xlate import XLATOR
-__version__ = '0.2.4'
+__version__ = '0.3.0'
LOG = logging.getLogger(__name__)
self.private_ssh_key = None
self.ssh = None
self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout
+ self.root_dir = CrTplConfiguration.default_cobbler_rootdir
super(Cobbler, self).__init__(
appname=appname, verbose=verbose, version=version, base_dir=base_dir,
self.ssh_port = config.cobbler_ssh_port
self.ssh_user = config.cobbler_ssh_user
self.ssh_timeout = config.cobbler_ssh_timeout
+ self.root_dir = config.cobbler_rootdir
if initialized:
self.initialized = True
LOG.debug(_("Sorted list of found distros:") + "\n{}".format(pp(distro_list)))
return distro_list
+ # -------------------------------------------------------------------------
+ def get_profile_list(self):
+ """Trying to get a list of all configured cobbler profiles."""
+
+ profile_list = []
+ proc = self.exec_cobbler(('profile', 'list'), no_simulate=True)
+ lines = proc.stdout.splitlines()
+ for line in proc.stdout.splitlines():
+ profile = line.strip()
+ if profile:
+ profile_list.append(profile)
+ profile_list.sort(key=str.lower)
+ if self.verbose > 1:
+ LOG.debug(_("Sorted list of found profiles:") + "\n{}".format(pp(profile_list)))
+ return profile_list
+
+ # -------------------------------------------------------------------------
+ def ensure_profile(self):
+ """Ensure the existence and the correctnes of the given profile."""
+
+ profile = self.config.cobbler_profile
+
+ LOG.info(_("Ensuring profile {!r} ...").format(profile))
+
+ profile_list = self.get_profile_list()
+
+ if profile in profile_list:
+ self.change_profile()
+ else:
+ self.add_profile()
+
+ # -------------------------------------------------------------------------
+ def change_profile(self):
+ """Ensure correctnes of an existing profile."""
+
+ profile = self.config.cobbler_profile
+ distro = self.config.cobbler_distro
+
+ LOG.debug(_("Checking existing profile {!r} ...").format(profile))
+
+ # -------------------------------------------------------------------------
+ def add_profile(self):
+ """Creating a new profile."""
+
+ profile = self.config.cobbler_profile
+ distro = self.config.cobbler_distro
+
+ LOG.info(_("Creating new profile {!r} ...").format(profile))
+
# =============================================================================
if __name__ == "__main__":
import re
# Third party modules
+from pathlib import Path
# Own modules
from fb_tools.config import ConfigError, BaseConfiguration
from .xlate import XLATOR
-__version__ = '1.4.1'
+__version__ = '1.4.2'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
default_cobbler_ssh_user = 'root'
default_cobbler_ssh_timeout = 30
default_cobbler_distro = 'CentOS-8.1-x86_64'
+ default_cobbler_rootdir = Path('/var/lib/cobbler')
+ default_cobbler_profile = 'vmware-template_centos8'
ssh_privkey = 'id_rsa_cr_vmw_tpl'
self.cobbler_ssh_port = self.default_cobbler_ssh_port
self.cobbler_ssh_user = self.default_cobbler_ssh_user
self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout
+ self.cobbler_rootdir = self.default_cobbler_rootdir
+ self.cobbler_profile = self.default_cobbler_profile
self.excluded_datastores = []
re_port_key = re.compile(r'^\s*ssh[-_]?port\s*$', re.IGNORECASE)
re_user_key = re.compile(r'^\s*ssh[-_]?user\s*$', re.IGNORECASE)
- re_timeout_key = re.compile(r'^\s*ssh-_]?timeout\s*$', re.IGNORECASE)
+ re_timeout_key = re.compile(r'^\s*ssh[-_]?timeout\s*$', re.IGNORECASE)
+ re_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE)
for (key, value) in config.items(section_name):
if key.lower() == 'distro' and value.strip() != '':
continue
if re_timeout_key.match(key):
self.cobbler_ssh_timeout = int(value)
+ continue
+ if re_rootdir_key.match(key):
+ dpath = Path(value)
+ if dpath.is_absolute():
+ self.cobbler_rootdir = Path(value)
+ else:
+ msg = _("Path for Cobbler root directory {!r} is not absolute.").format(
+ str(dpath))
+ LOG.error(msg)
+ continue
+ if key.lower() == 'profile' and value.strip() != '':
+ self.cobbler_profile = value.strip().lower()
+ continue
# =============================================================================
if __name__ == "__main__":