def _validate_template(self, template): template = common.validate_filename(template) template_full_path = self.tftp_dir + template if not os.path.isfile(template_full_path): msg = 'Template %s does not exist' % template raise error.NotFound, msg if not '.template' in template: msg = 'Template %s must have .template extension' % template raise error.InputError, msg return template
def __init__(self, tftp_root=None): self.config = config.setup() self.log = logging.getLogger(__name__) self.type = 'TFTP config' if not tftp_root: tftp_root = self.config.get('TFTP', 'tftp_root') self.tftp_root = common.validate_filename(tftp_root) self.tftp_conf_dir = self.config.get('TFTP', 'tftp_conf_dir', 'pxelinux.cfg') self.tftp_mac_prefix = self.config.get('TFTP', 'tftp_mac_prefix', '01') # Add the delimiter (makes life easier when concating strings self.tftp_prefix = self.tftp_mac_prefix + '-' #check file exists in the TFTP directory self.tftp_dir = self.tftp_root + "/" + self.tftp_conf_dir + "/" if not os.path.isdir(self.tftp_dir): msg = "TFTP config directory %s not found" % self.tftp_dir raise error.NotFound, msg
def __init__(self, tftp_root=None): self.config = config.setup() self.log = logger.setup(__name__) self.type = 'TFTP config' if not tftp_root: tftp_root = self.config.get('TFTP', 'tftp_root') self.tftp_root = common.validate_filename(tftp_root) self.tftp_conf_dir = self.config.get('TFTP', 'tftp_conf_dir', 'pxelinux.cfg') self.tftp_mac_prefix = self.config.get('TFTP', 'tftp_mac_prefix', '01') # Add the delimiter (makes life easier when concating strings self.tftp_prefix = self.tftp_mac_prefix + '-' #check file exists in the TFTP directory self.tftp_dir = self.tftp_root + "/" + self.tftp_conf_dir + "/" if not os.path.isdir(self.tftp_dir): msg = "TFTP config directory %s not found" % self.tftp_dir raise error.NotFound, msg
def search(self, mac=None, template=None): data = [] if mac is None and template is None: # Give me all macs and all templates #read the file system self.log.debug('Searching for all templates and macs under %s' % \ self.tftp_dir) file_list = os.listdir(self.tftp_dir) if len(file_list) == 0: result = common.process_results(data, self.type) self.log.debug('Result: %s' % result) return result item = {} for file in file_list: valid_template = False item_path = self.tftp_dir + file file_name = os.path.basename(item_path) if ".template" in file_name: valid_template = True file_name = file_name[3:] #use a regex to see if the file is a mac config pattern = re.compile('^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$') valid_mac = pattern.match(file_name) #if it's a mac add to list of macs if valid_mac and os.path.isfile(item_path): macs = file_name try: item["configs"] except KeyError: item["configs"] = [] # Initialise dict if not exist item["configs"].append(macs) elif valid_template and os.path.isfile(item_path): try: item["templates"] except KeyError: item["templates"] = [] # Initialise dict if not exist item["templates"].append(file) else: self.log.debug('Unknown file type %s, skipping' % file) elif mac is not None and template is None: # We're looking for a mac address mac = common.validate_mac(mac) mac_file = string.replace(mac, ":", "-") #Format for use on filesystem mac_file_name = self.tftp_prefix + mac_file mac_link = self.tftp_dir + mac_file_name if not os.path.isfile(mac_link): result = common.process_results(data, self.type) self.log.debug('Result: %s' % result) return result else: item = [mac_file_name] #item[mac_file_name] = "Found" elif template is not None and mac is None: #Now we just want to check if template exists, no longer maintaining list of links template = common.validate_filename(template) template = self._validate_template(template) item = [template] else: msg = "please specify nothing, mac or target (not mac and target)." raise error.InputError, msg data.append(item) result = common.process_results(data, self.type) self.log.debug('Result: %s' % result) return result