def check_tftpd_dir(self,status): """ Check if cobbler.conf's tftpboot directory exists """ bootloc = utils.tftpboot_location() if not os.path.exists(bootloc): status.append(_("please create directory: %(dirname)s") % { "dirname" : bootloc })
def remove_single_system(self, name): bootloc = utils.tftpboot_location() system_record = self.systems.find(name=name) # delete contents of kickstarts_sys/$name in webdir system_record = self.systems.find(name=name) if self.settings.manage_dhcp: if self.settings.omapi_enabled: for (name,interface) in system_record.interfaces.iteritems(): self.sync.dhcp.remove_dhcp_lease( self.settings.omapi_port, interface["dns_name"] ) itanic = False profile = self.profiles.find(name=system_record.profile) if profile is not None: distro = self.distros.find(name=profile.distro) if distro is not None and distro in [ "ia64", "IA64"]: itanic = True for (name,interface) in system_record.interfaces.iteritems(): filename = utils.get_config_filename(system_record,interface=name) if not itanic: utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", filename)) else: utils.rmfile(os.path.join(bootloc, filename))
def __init__(self,config,verbose=True,dhcp=None,dns=None,logger=None): """ Constructor """ self.logger = logger if logger is None: self.logger = clogger.Logger() self.verbose = verbose self.config = config self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.templar = templar.Templar(config, self.logger) self.pxegen = pxegen.PXEGen(config, self.logger) self.dns = dns self.dhcp = dhcp self.bootloc = utils.tftpboot_location() self.pxegen.verbose = verbose self.dns.verbose = verbose self.dhcp.verbose = verbose
def __init__(self, config, verbose=True, dhcp=None, dns=None, logger=None, tftpd=None): """ Constructor """ self.logger = logger if logger is None: self.logger = clogger.Logger() self.verbose = verbose self.config = config self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.templar = templar.Templar(config, self.logger) self.pxegen = pxegen.PXEGen(config, self.logger) self.dns = dns self.dhcp = dhcp self.tftpd = tftpd self.bootloc = utils.tftpboot_location() self.pxegen.verbose = verbose self.dns.verbose = verbose self.dhcp.verbose = verbose self.pxelinux_dir = os.path.join(self.bootloc, "pxelinux.cfg") self.grub_dir = os.path.join(self.bootloc, "grub") self.images_dir = os.path.join(self.bootloc, "images") self.yaboot_bin_dir = os.path.join(self.bootloc, "ppc") self.yaboot_cfg_dir = os.path.join(self.bootloc, "etc") self.rendered_dir = os.path.join(self.settings.webdir, "rendered")
def write_boot_files_distro(self,distro): # collapse the object down to a rendered datastructure # the second argument set to false means we don't collapse # hashes/arrays into a flat string target = utils.blender(self.config.api, False, distro) # Create metadata for the templar function # Right now, just using img_path, but adding more # cobbler variables here would probably be good metadata = {} metadata["img_path"] = os.path.join( utils.tftpboot_location(), "images",distro.name) # Create the templar instance. Used to template the target directory templater = templar.Templar() # Loop through the hash of boot files, # executing a cp for each one for file in target["boot_files"].keys(): file_dst = templater.render(file,metadata,None) try: shutil.copyfile(target["boot_files"][file], file_dst) self.config.api.log("copied file %s to %s for %s" % ( target["boot_files"][file], file_dst, distro.name)) except: self.logger.error("failed to copy file %s to %s for %s" % ( target["boot_files"][file], file_dst, distro.name)) # Continue on to sync what you can return 0
def remove_single_distro(self, name): bootloc = utils.tftpboot_location() # delete contents of images/$name directory in webdir utils.rmtree(os.path.join(self.settings.webdir, "images", name)) # delete contents of images/$name in tftpboot utils.rmtree(os.path.join(bootloc, "images", name)) # delete potential symlink to tree in webdir/links utils.rmfile(os.path.join(self.settings.webdir, "links", name))
def remove_single_system(self, name): bootloc = utils.tftpboot_location() system_record = self.systems.find(name=name) # delete contents of kickstarts_sys/$name in webdir system_record = self.systems.find(name=name) for (name, interface) in system_record.interfaces.iteritems(): filename = utils.get_config_filename(system_record, interface=name) utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", filename)) utils.rmfile(os.path.join(bootloc, "grub", filename.upper()))
def check_ctftpd_dir(self,status): """ Check if cobbler.conf's tftpboot directory exists """ if self.checked_dist in ["debian", "ubuntu"]: return bootloc = utils.tftpboot_location() if not os.path.exists(bootloc): status.append(_("please create directory: %(dirname)s") % { "dirname" : bootloc })
def __init__(self,config,logger): """ Constructor """ self.logger = logger self.config = config self.templar = templar.Templar(config) self.settings_file = "/etc/xinetd.d/tftp" self.pxegen = pxegen.PXEGen(config, self.logger) self.systems = config.systems() self.bootloc = utils.tftpboot_location()
def check_ctftpd_dir(self, status): """ Check if cobbler.conf's tftpboot directory exists """ if self.checked_family == "debian": return bootloc = utils.tftpboot_location() if not os.path.exists(bootloc): status.append( _("please create directory: %(dirname)s") % {"dirname": bootloc})
def __init__(self, collection_mgr, logger): """ Constructor """ self.logger = logger if self.logger is None: self.logger = clogger.Logger() self.collection_mgr = collection_mgr self.templar = templar.Templar(collection_mgr) self.settings_file = "/etc/xinetd.d/tftp" self.tftpgen = tftpgen.TFTPGen(collection_mgr, self.logger) self.systems = collection_mgr.systems() self.bootloc = utils.tftpboot_location()
def __init__(self,config,logger): """ Constructor """ self.logger = logger if self.logger is None: self.logger = clogger.Logger() self.config = config self.templar = templar.Templar(config) self.settings_file = "/etc/xinetd.d/tftp" self.pxegen = pxegen.PXEGen(config, self.logger) self.systems = config.systems() self.bootloc = utils.tftpboot_location()
def __init__(self, config, logger): """ Constructor """ self.config = config self.logger = logger self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.images = config.images() self.templar = templar.Templar(config) self.bootloc = utils.tftpboot_location()
def __init__(self,config): """ Constructor """ self.config = config self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.images = config.images() self.templar = templar.Templar(config) self.bootloc = utils.tftpboot_location() self.verbose = False
def __init__(self, collection_mgr, logger): """ Constructor """ self.collection_mgr = collection_mgr self.logger = logger self.api = collection_mgr.api self.distros = collection_mgr.distros() self.profiles = collection_mgr.profiles() self.systems = collection_mgr.systems() self.settings = collection_mgr.settings() self.repos = collection_mgr.repos() self.images = collection_mgr.images() self.templar = templar.Templar(collection_mgr) self.bootloc = utils.tftpboot_location()
def modacl(self, isadd, isuser, who): webdir = self.settings.webdir snipdir = self.settings.snippetsdir tftpboot = utils.tftpboot_location() PROCESS_DIRS = { "/var/log/cobbler": "rwx", "/var/log/cobbler/tasks": "rwx", "/var/lib/cobbler": "rwx", "/etc/cobbler": "rwx", tftpboot: "rwx", "/var/lib/cobbler/triggers": "rwx" } if not snipdir.startswith("/var/lib/cobbler/"): PROCESS_DIRS[snipdir] = "r" cmd = "-R" if isadd: cmd = "%s -m" % cmd else: cmd = "%s -x" % cmd if isuser: cmd = "%s u:%s" % (cmd, who) else: cmd = "%s g:%s" % (cmd, who) for d in PROCESS_DIRS: how = PROCESS_DIRS[d] if isadd: cmd2 = "%s:%s" % (cmd, how) else: cmd2 = cmd cmd2 = "%s %s" % (cmd2, d) rc = utils.subprocess_call(self.logger, "setfacl -d %s" % cmd2, shell=True) if not rc == 0: utils.die(self.logger, "command failed") rc = utils.subprocess_call(self.logger, "setfacl %s" % cmd2, shell=True) if not rc == 0: utils.die(self.logger, "command failed")
def check_tftpd_conf(self,status): """ Check that configured tftpd boot directory matches with actual Check that tftpd is enabled to autostart """ if os.path.exists(self.settings.tftpd_conf): f = open(self.settings.tftpd_conf) re_disable = re.compile(r'disable.*=.*yes') for line in f.readlines(): if re_disable.search(line) and not line.strip().startswith("#"): status.append(_("change 'disable' to 'no' in %(file)s") % { "file" : self.settings.tftpd_conf }) else: status.append(_("file %(file)s does not exist") % { "file" : self.settings.tftpd_conf }) bootloc = utils.tftpboot_location() if not os.path.exists(bootloc): status.append(_("directory needs to be created: %s" % bootloc))
def __init__(self, config, logger): """ Constructor """ self.config = config self.logger = logger self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.images = config.images() self.templar = templar.Templar(config) self.bootloc = utils.tftpboot_location() # FIXME: not used anymore, can remove? self.verbose = False
def modacl(self,isadd,isuser,who): webdir = self.settings.webdir snipdir = self.settings.snippetsdir tftpboot = utils.tftpboot_location() PROCESS_DIRS = { webdir : "rwx", "/var/log/cobbler" : "rwx", "/var/lib/cobbler" : "rwx", "/etc/cobbler" : "rwx", tftpboot : "rwx", "/var/lib/cobbler/triggers" : "rwx" } if not snipdir.startswith("/var/lib/cobbler/"): PROCESS_DIRS[snipdir] = "r" cmd = "-R" if isadd: cmd = "%s -m" % cmd else: cmd = "%s -x" % cmd if isuser: cmd = "%s u:%s" % (cmd,who) else: cmd = "%s g:%s" % (cmd,who) for d in PROCESS_DIRS: how = PROCESS_DIRS[d] if isadd: cmd2 = "%s:%s" % (cmd,how) else: cmd2 = cmd cmd2 = "%s %s" % (cmd2,d) print "- setfacl -d %s" % cmd2 rc = sub_process.call("setfacl -d %s" % cmd2,shell=True,close_fds=True) if not rc == 0: raise CX(_("command failed")) print "- setfacl %s" % cmd2 rc = sub_process.call("setfacl %s" % cmd2,shell=True,close_fds=True) if not rc == 0: raise CX(_("command failed"))
def modacl(self, isadd, isuser, who): snipdir = self.settings.autoinstall_snippets_dir tftpboot = utils.tftpboot_location() PROCESS_DIRS = { "/var/log/cobbler": "rwx", "/var/log/cobbler/tasks": "rwx", "/var/lib/cobbler": "rwx", "/etc/cobbler": "rwx", tftpboot: "rwx", "/var/lib/cobbler/triggers": "rwx" } if not snipdir.startswith("/var/lib/cobbler/"): PROCESS_DIRS[snipdir] = "r" cmd = "-R" if isadd: cmd = "%s -m" % cmd else: cmd = "%s -x" % cmd if isuser: cmd = "%s u:%s" % (cmd, who) else: cmd = "%s g:%s" % (cmd, who) for d in PROCESS_DIRS: how = PROCESS_DIRS[d] if isadd: cmd2 = "%s:%s" % (cmd, how) else: cmd2 = cmd cmd2 = "%s %s" % (cmd2, d) rc = utils.subprocess_call(self.logger, "setfacl -d %s" % cmd2, shell=True) if not rc == 0: utils.die(self.logger, "command failed") rc = utils.subprocess_call(self.logger, "setfacl %s" % cmd2, shell=True) if not rc == 0: utils.die(self.logger, "command failed")
def remove_single_system(self, name): bootloc = utils.tftpboot_location() system_record = self.systems.find(name=name) # delete contents of kickstarts_sys/$name in webdir system_record = self.systems.find(name=name) itanic = False profile = self.profiles.find(name=system_record.profile) if profile is not None: distro = self.distros.find(name=profile.get_conceptual_parent().name) if distro is not None and distro in [ "ia64", "IA64"]: itanic = True for (name,interface) in system_record.interfaces.iteritems(): filename = utils.get_config_filename(system_record,interface=name) if not itanic: utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", filename)) else: utils.rmfile(os.path.join(bootloc, filename))
def remove_single_system(self, name): bootloc = utils.tftpboot_location() system_record = self.systems.find(name=name) # delete contents of kickstarts_sys/$name in webdir system_record = self.systems.find(name=name) itanic = False profile = self.profiles.find(name=system_record.profile) if profile is not None: distro = self.distros.find(name=profile.get_conceptual_parent().name) if distro is not None and distro in [ "ia64", "IA64"]: itanic = True for (name,interface) in system_record.interfaces.iteritems(): filename = utils.get_config_filename(system_record,interface=name) if not itanic: utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", filename)) utils.rmfile(os.path.join(bootloc, "grub", filename.upper())) else: utils.rmfile(os.path.join(bootloc, filename))
def write_boot_files_distro(self, distro): # collapse the object down to a rendered datastructure # the second argument set to false means we don't collapse # hashes/arrays into a flat string target = utils.blender(self.config.api, False, distro) # Create metadata for the templar function # Right now, just using local_img_path, but adding more # cobbler variables here would probably be good metadata = {} metadata["local_img_path"] = os.path.join(utils.tftpboot_location(), "images", distro.name) # Create the templar instance. Used to template the target directory templater = templar.Templar(self.config) # Loop through the hash of boot files, # executing a cp for each one self.logger.info("processing boot_files for distro: %s" % distro.name) for file in target["boot_files"].keys(): rendered_file = templater.render(file, metadata, None) try: for f in glob.glob(target["boot_files"][file]): if f == target["boot_files"][file]: # this wasn't really a glob, so just copy it as is filedst = rendered_file else: # this was a glob, so figure out what the destination # file path/name should be tgt_path, tgt_file = os.path.split(f) rnd_path, rnd_file = os.path.split(rendered_file) filedst = os.path.join(rnd_path, tgt_file) if not os.path.isfile(filedst): shutil.copyfile(f, filedst) self.config.api.log("copied file %s to %s for %s" % (f, filedst, distro.name)) except: self.logger.error("failed to copy file %s to %s for %s" % (f, filedst, distro.name)) return 0
def __init__(self, config, verbose=True, dhcp=None, dns=None, logger=None, tftpd=None): """ Constructor """ self.logger = logger if logger is None: self.logger = clogger.Logger() self.verbose = verbose self.config = config self.api = config.api self.distros = config.distros() self.profiles = config.profiles() self.systems = config.systems() self.settings = config.settings() self.repos = config.repos() self.templar = templar.Templar(config, self.logger) self.pxegen = pxegen.PXEGen(config, self.logger) self.dns = dns self.dhcp = dhcp self.tftpd = tftpd self.bootloc = utils.tftpboot_location() self.pxegen.verbose = verbose self.dns.verbose = verbose self.dhcp.verbose = verbose self.pxelinux_dir = os.path.join(self.bootloc, "pxelinux.cfg") self.grub_dir = os.path.join(self.bootloc, "grub") self.images_dir = os.path.join(self.bootloc, "images") self.yaboot_bin_dir = os.path.join(self.bootloc, "ppc") self.yaboot_cfg_dir = os.path.join(self.bootloc, "etc") self.s390_dir = os.path.join(self.bootloc, "s390x") self.rendered_dir = os.path.join(self.settings.webdir, "rendered")
def write_boot_files_distro(self,distro): # collapse the object down to a rendered datastructure # the second argument set to false means we don't collapse # hashes/arrays into a flat string target = utils.blender(self.config.api, False, distro) # Create metadata for the templar function # Right now, just using img_path, but adding more # cobbler variables here would probably be good metadata = {} metadata["img_path"] = os.path.join( utils.tftpboot_location(), "images",distro.name) # Create the templar instance. Used to template the target directory templater = templar.Templar(self.config) # Loop through the hash of boot files, # executing a cp for each one for file in target["boot_files"].keys(): rendered_file = templater.render(file,metadata,None) try: for f in glob.glob(target["boot_files"][file]): if f == target["boot_files"][file]: # this wasn't really a glob, so just copy it as is filedst = rendered_file else: # this was a glob, so figure out what the destination # file path/name should be tgt_path,tgt_file=os.path.split(f) rnd_path,rnd_file=os.path.split(rendered_file) filedst = os.path.join(rnd_path,tgt_file) if not os.path.isfile(filedst): shutil.copyfile(f, filedst) self.config.api.log("copied file %s to %s for %s" % (f,filedst,distro.name)) except: self.logger.error("failed to copy file %s to %s for %s" % (f,filedst,distro.name)) return 0
def check_vsftpd_bin(self,status): """ Check if vsftpd is installed """ if not os.path.exists(self.settings.vsftpd_bin): status.append(_("vsftpd is not installed (NOTE: needed for s390x support only)")) else: self.check_service(status,"vsftpd","needed for 390x support only") bootloc = utils.tftpboot_location() if not os.path.exists("/etc/vsftpd/vsftpd.conf"): status.append("missing /etc/vsftpd/vsftpd.conf") conf = open("/etc/vsftpd/vsftpd.conf") data = conf.read() lines = data.split("\n") ok = False for line in lines: if line.find("anon_root") != -1 and line.find(bootloc) != -1: ok = True break conf.close() if not ok: status.append("in /etc/vsftpd/vsftpd.conf the line 'anon_root=%s' should be added (needed for s390x support only)" % bootloc)
def write_boot_files_distro(self, distro): # collapse the object down to a rendered datastructure # the second argument set to false means we don't collapse # hashes/arrays into a flat string target = utils.blender(self.config.api, False, distro) # Create metadata for the templar function # Right now, just using img_path, but adding more # cobbler variables here would probably be good metadata = {} metadata["img_path"] = os.path.join(utils.tftpboot_location(), "images", distro.name) # Create the templar instance. Used to template the target directory templater = templar.Templar(self.config) # Loop through the hash of boot files, # executing a cp for each one for file in target["boot_files"].keys(): file_dst = templater.render(file, metadata, None) try: for f in glob.glob(target["boot_files"][file]): rawpath, rawfile = os.path.split(f) filedst = file_dst + rawfile if not os.path.isfile(filedst): shutil.copyfile(f, filedst) self.config.api.log( "copied file %s to %s for %s" % (target["boot_files"][file], filedst, distro.name)) except: self.logger.error( "failed to copy file %s to %s for %s" % (target["boot_files"][file], filedst, distro.name)) # Continue on to sync what you can return 0
def remove_single_image(self, name): bootloc = utils.tftpboot_location() utils.rmfile(os.path.join(bootloc, "images2", name))
def write_templates(self,obj,write_file=False,path=None): """ A semi-generic function that will take an object with a template_files hash {source:destiation}, and generate a rendered file. The write_file option allows for generating of the rendered output without actually creating any files. The return value is a hash of the destination file names (after variable substitution is done) and the data in the file. """ self.logger.info("Writing template files for %s" % obj.name) results = {} try: templates = obj.template_files except: return results blended = utils.blender(self.api, False, obj) ksmeta = blended.get("ks_meta",{}) try: del blended["ks_meta"] except: pass blended.update(ksmeta) # make available at top level templates = blended.get("template_files",{}) try: del blended["template_files"] except: pass blended.update(templates) # make available at top level (success, templates) = utils.input_string_or_hash(templates) if not success: return results blended['img_path'] = os.path.join("/images",blended["distro_name"]) blended['local_img_path'] = os.path.join(utils.tftpboot_location(),"images",blended["distro_name"]) for template in templates.keys(): dest = templates[template] if dest is None: continue # Run the source and destination files through # templar first to allow for variables in the path template = self.templar.render(template, blended, None).strip() dest = os.path.normpath(self.templar.render(dest, blended, None).strip()) # Get the path for the destination output dest_dir = os.path.normpath(os.path.dirname(dest)) # If we're looking for a single template, skip if this ones # destination is not it. if not path is None and path != dest: continue # If we are writing output to a file, we allow files tobe # written into the tftpboot directory, otherwise force all # templated configs into the rendered directory to ensure that # a user granted cobbler privileges via sudo can't overwrite # arbitrary system files (This also makes cleanup easier). if os.path.isabs(dest_dir) and write_file: if dest_dir.find(utils.tftpboot_location()) != 0: raise CX(" warning: template destination (%s) is outside %s, skipping." % (dest_dir,utils.tftpboot_location())) continue else: dest_dir = os.path.join(self.settings.webdir, "rendered", dest_dir) dest = os.path.join(dest_dir, os.path.basename(dest)) if not os.path.exists(dest_dir): utils.mkdir(dest_dir) # Check for problems if not os.path.exists(template): raise CX("template source %s does not exist" % template) continue elif write_file and not os.path.isdir(dest_dir): raise CX("template destination (%s) is invalid" % dest_dir) continue elif write_file and os.path.exists(dest): raise CX("template destination (%s) already exists" % dest) continue elif write_file and os.path.isdir(dest): raise CX("template destination (%s) is a directory" % dest) continue elif template == "" or dest == "": raise CX("either the template source or destination was blank (unknown variable used?)" % dest) continue template_fh = open(template) template_data = template_fh.read() template_fh.close() buffer = self.templar.render(template_data, blended, None) results[dest] = buffer if write_file: self.logger.info("generating: %s" % dest) fd = open(dest, "w") fd.write(buffer) fd.close() return results