def _gather_script(self): """get node information via a lookup script""" command = seedlib.apply_template(self.provider, self.values) command = command.split(' ') result = seedlib.run(command) if not result.retcode: return self._return(yaml.load(result.output))
def overlay(self, address, pxe_vars): """create archive with files from selected overlay and apply templates""" dir = os.path.join(sp_paths['overlays'], pxe_vars['seedbank_overlay']) if not os.path.isdir(dir): self.error = 'directory "%s" does not exist' % dir return (self.info, self.error, self.contents) destination = os.path.join('/tmp', 'seedbank_' + address) if os.path.isdir(destination): try: shutil.rmtree(destination) except (IOError, OSError): self.error = 'failed to remove the temporary directory "%s"' % destination return (self.info, self.error, self.contents) try: shutil.copytree(dir, destination) except (IOError, OSError): self.error = 'failed to copy overlay "%s"' % destination return (self.info, self.error, self.contents) templates_list = [] for root, dirs, files in os.walk(destination): for file in files: if fnmatch.fnmatch(file, '*.sptemplate'): templates_list.append(os.path.join(root, file)) for filename in templates_list: data = open(filename, 'r').read() try: data = seedlib.apply_template(data, pxe_vars) except: self.error = 'failed to apply template on "%s", it seems some variables are missing in the pxe file' % filename return (self.info, self.error, self.contents) try: file = open(filename, 'w') except (IOError, OSError): self.error = 'failed to open "%s" for writing' % filename return (self.info, self.error, self.contents) else: file.write(data) file.close() try: os.rename(filename, filename.replace('.sptemplate', '')) except (IOError, OSError): self.error = 'failed to rename "%s"' % filename return (self.info, self.error, self.contents) contents = directory_to_tar_gz(destination) if contents: self.contents = contents self.info = 'generated tar gz archive successfully' else: self.error = 'creating tar gz for "%s" failed' % destination return (self.info, self.error, self.contents)
def _gather_http(self): """get internal nodes information via http ot https""" url = seedlib.apply_template(self.provider, self.values) try: output = seedlib.read_url(url, 1) except Exception: return else: return self._return(yaml.load(output))
def run(self, hooks): """check if hooks are enabled and run a hook""" if settings['disable_hooks']: return queue = [] for value in hooks.values(): value = seedlib.apply_template(value, self.values) queue.append(value) for command in queue: print ('info: running hook "%s"' % command) seedlib.run_pipe(command)