def post_run(self): self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("executing post_run"), ) ) # run outer chroot script after exec_script = self.metadata.get('outer_source_chroot_script_after') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env = env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("outer chroot hook (after inner) failed"), rc, ) ) return rc return 0
def post_run(self): self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("executing post_run"), ) ) # run outer chroot script after exec_script = self.metadata.get('outer_source_chroot_script_after') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("outer chroot hook (after inner) failed"), rc, ) ) return rc return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring running"), ) ) # running sync args = [self._mirror_syncer] args.extend(self._mirror_syncer_builtin_args) args.extend(self.metadata.get('extra_rsync_parameters', [])) args.append(self.source_dir + "/") args.append(self.dest_dir + "/") self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), ) ) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring failed"), rc, ) ) return rc self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring completed successfully"), ) ) return 0
def post_run(self): self._output.output("[%s|%s] %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("executing post_run"), ) ) # run post iso script exec_script = self.metadata.get('post_iso_script') if exec_script: env = os.environ.copy() env['ISO_PATH'] = self.dest_iso env['ISO_CHECKSUM_PATH'] = self.dest_iso + IsoHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("post iso hook failed"), rc, ) ) return rc return 0
def kill(self, success=True): self._output.output("[%s|%s] %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("executing kill"), ) ) if not success: self._run_error_script(None, self.chroot_dir, self.dest_root) rc = 0 if self.squash_mounted: umounter = self.metadata.get('squash_umounter', self._squash_umounter) args = umounter + [self.tmp_squash_mount] rc = molecule.utils.exec_cmd(args) if rc == 0: try: os.rmdir(self.tmp_squash_mount) except OSError: self._output.output( "[%s|%s] %s: %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("unable to remove temp. dir"), self.tmp_squash_mount, ) ) rc = 0 if self.iso_mounted: umounter = self.metadata.get('iso_umounter', self._iso_umounter) args = umounter + [self.tmp_mount] rc = molecule.utils.exec_cmd(args) if rc == 0: try: os.rmdir(self.tmp_mount) except OSError: # if not empty, skip self._output.output( "[%s|%s] %s: %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("unable to remove temp. dir"), self.tmp_mount, ) ) if not success: tmp_dir = self.metadata['chroot_tmp_dir'] if tmp_dir is not None: try: shutil.rmtree(tmp_dir, True) except (shutil.Error, OSError,): self._output.output("[%s|%s] %s: %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("unable to remove temp. dir"), tmp_dir, ) ) return 0
def post_run(self): self._output.output("[%s|%s] %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("executing post_run"), )) # run post iso script exec_script = self.metadata.get('post_iso_script') if exec_script: env = os.environ.copy() env['ISO_PATH'] = self.dest_iso env['ISO_CHECKSUM_PATH'] = self.dest_iso + IsoHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), )) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("post iso hook failed"), rc, )) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) # run pre image script exec_script = self.metadata.get('pre_image_script') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env = env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("pre image hook failed"), rc, ) ) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) # run pre image script exec_script = self.metadata.get('pre_image_script') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output( "[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("pre image hook failed"), rc, ) ) return rc return 0
def ask_question(self, question, importance=0, responses=None): """ Questions asking function. It asks the user to answer the question given by choosing between a preset list of answers given by the "reposonses" argument. @param question: question text @type question: string @keyword importance: question importance (no default valid values) @type importance: int @keyword responses: list of valid answers which user has to choose from @type responses: tuple or list @return: None @rtype: None """ if responses is None: responses = ( _("Yes"), _("No"), ) colours = [ green, red, blue, darkgreen, darkred, darkblue, brown, purple ] colours_len = len(colours) try: sys.stdout.write(question + " ") except UnicodeEncodeError: sys.stdout.write(question.encode('utf-8') + " ") _flush_stdouterr() try: while True: xterm_title(_("Molecule got a question for you")) _flush_stdouterr() answer_items = [colours[x % colours_len](responses[x]) \ for x in range(len(responses))] response = _my_raw_input("[" + "/".join(answer_items) + "] ") _flush_stdouterr() for key in responses: if response.upper() == key[:len(response)].upper(): xterm_title_reset() return key _flush_stdouterr() except (EOFError, KeyboardInterrupt): msg = "%s.\n" % (_("Interrupted"), ) try: sys.stdout.write(msg) except UnicodeEncodeError: sys.stdout.write(msg.encode("utf-8")) xterm_title_reset() raise KeyboardInterrupt() xterm_title_reset() _flush_stdouterr()
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("executing pre_run"), )) # run pre image script exec_script = self.metadata.get('pre_image_script') if exec_script: env = os.environ.copy() env['TMP_IMAGE_PATH'] = self.tmp_loop_device_file env['LOOP_DEVICE'] = self.loop_device self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), )) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("pre image hook failed"), rc, )) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("TarHandler"), darkred(self.spec_name), _("executing pre_run"), )) # run pre tar script exec_script = self.metadata.get('pre_tar_script') if exec_script: env = os.environ.copy() env['CHROOT_DIR'] = self.chroot_path env['TAR_PATH'] = self.dest_path env['TAR_CHECKSUM_PATH'] = self.dest_path + \ TarHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), )) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("pre tar hook failed"), rc, )) return rc return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring running"), )) # running sync args = [self._mirror_syncer] args.extend(self._mirror_syncer_builtin_args) args.extend(self.metadata.get('extra_rsync_parameters', [])) args.append(self.source_dir + "/") args.append(self.dest_dir + "/") self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), )) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring failed"), rc, )) return rc self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("mirroring completed successfully"), )) return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("executing pre_run"), )) # run outer chroot script exec_script = self.metadata.get('outer_chroot_script') if exec_script: env = os.environ.copy() env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), )) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("outer chroot hook failed"), rc, )) return rc return 0
def _fill_image_file(self): """ Fill image file (using _tmp_loop_device_fd) with either zeroes or random data of image_mb size. @raises IOError: if space is not enough @raises OSError: well, sorry """ if self.randomize: self._output.output("[%s|%s] %s => %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("generating random base image file"), self.tmp_loop_device_file, )) else: self._output.output("[%s|%s] %s => %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("generating zeroed base image file"), self.tmp_loop_device_file, )) image_mb = self.image_mb loop_f = os.fdopen(self._tmp_loop_device_fd, "wb") self._tmp_loop_device_fd = None arr = None mb_bytes = ImageHandler.MB_IN_BYTES while image_mb > 0: image_mb -= 1 if self.randomize: arr = array.array('c', os.urandom(mb_bytes)) else: arr = array.array('c', chr(0) * mb_bytes) arr.tofile(loop_f) del arr gc.collect() # file self._tmp_loop_device_fd is closed here. # no more writes needed loop_f.flush() loop_f.close() # last but not least, tell the loop device that the file size changed args = [ImageHandler.LOSETUP_EXEC, "-c", self.loop_device] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), )) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image file resize failed"), rc, )) return rc
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("executing pre_run"), )) exec_script = self.metadata.get('inner_source_chroot_script') if exec_script: if os.path.isfile(exec_script[0]) and \ os.access(exec_script[0], os.R_OK): rc = self._exec_inner_script(exec_script, self.source_dir) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("inner_source_chroot_script failed"), rc, )) return rc self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("pre_run completed successfully"), )) return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) # run pre image script exec_script = self.metadata.get('pre_image_script') if exec_script: env = os.environ.copy() env['TMP_IMAGE_PATH'] = self.tmp_loop_device_file env['LOOP_DEVICE'] = self.loop_device self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("pre image hook failed"), rc, ) ) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) exec_script = self.metadata.get('inner_source_chroot_script') if exec_script: if os.path.isfile(exec_script[0]) and \ os.access(exec_script[0], os.R_OK): rc = self._exec_inner_script(exec_script, self.source_dir) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("inner_source_chroot_script failed"), rc, ) ) return rc self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("pre_run completed successfully"), ) ) return 0
def post_run(self): # run post tar script exec_script = self.metadata.get('post_image_script') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.image_name # self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = self.dest_path # self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir env['IMAGE_PATH'] = self.dest_path env['IMAGE_CHECKSUM_PATH'] = self.dest_path + \ FinalImageHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("post image hook failed"), rc, ) ) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("TarHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) # run pre tar script exec_script = self.metadata.get('pre_tar_script') if exec_script: env = os.environ.copy() env['CHROOT_DIR'] = self.chroot_path env['TAR_PATH'] = self.dest_path env['TAR_CHECKSUM_PATH'] = self.dest_path + \ TarHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output( "[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("pre tar hook failed"), rc, ) ) return rc return 0
def post_run(self): # run post tar script exec_script = self.metadata.get('post_image_script') if exec_script: env = os.environ.copy() env['IMAGE_NAME'] = self.image_name # self.metadata['image_name'] env['DESTINATION_IMAGE_DIR'] = self.dest_path # self.metadata['destination_image_directory'] env['CHROOT_DIR'] = self.source_dir env['IMAGE_PATH'] = self.dest_path env['IMAGE_CHECKSUM_PATH'] = self.dest_path + \ FinalImageHandler.MD5_EXT self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env = env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("post image hook failed"), rc, ) ) return rc return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) # run outer chroot script exec_script = self.metadata.get('outer_chroot_script') if exec_script: env = os.environ.copy() env['CHROOT_DIR'] = self.source_dir self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("outer chroot hook failed"), rc, ) ) return rc return 0
def _fill_image_file(self): """ Fill image file (using _tmp_loop_device_fd) with either zeroes or random data of image_mb size. @raises IOError: if space is not enough @raises OSError: well, sorry """ if self.randomize: self._output.output("[%s|%s] %s => %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("generating random base image file"), self.tmp_loop_device_file, ) ) else: self._output.output("[%s|%s] %s => %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("generating zeroed base image file"), self.tmp_loop_device_file, ) ) image_mb = self.image_mb loop_f = os.fdopen(self._tmp_loop_device_fd, "wb") self._tmp_loop_device_fd = None arr = None mb_bytes = ImageHandler.MB_IN_BYTES while image_mb > 0: image_mb -= 1 if self.randomize: arr = array.array('c', os.urandom(mb_bytes)) else: arr = array.array('c', chr(0)*mb_bytes) arr.tofile(loop_f) del arr gc.collect() # file self._tmp_loop_device_fd is closed here. # no more writes needed loop_f.flush() loop_f.close() # last but not least, tell the loop device that the file size changed args = [ImageHandler.LOSETUP_EXEC, "-c", self.loop_device] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), ) ) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image file resize failed"), rc, ) ) return rc
def _kill_loop_device(self, preserve_loop_device_file=False): kill_rc = 0 if self.image_mounted: umounter = self.metadata.get( 'image_umounter', ImageHandler.DEFAULT_IMAGE_UMOUNTER ) args = umounter + [self.tmp_image_mount] rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output( "[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to umount loop device"), self.loop_device, ) ) kill_rc = rc else: self.image_mounted = False if self.tmp_image_mount is not None: try: os.rmdir(self.tmp_image_mount) except OSError: pass # kill loop device if self.loop_device is not None: rc = molecule.utils.exec_cmd([ImageHandler.LOSETUP_EXEC, "-d", self.loop_device]) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to kill loop device"), self.loop_device, ) ) kill_rc = rc else: self.loop_device = None if (self.tmp_loop_device_file is not None) and \ (not preserve_loop_device_file): try: os.remove(self.tmp_loop_device_file) self.tmp_loop_device_file = None except OSError as err: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to remove temp. loop device file"), err, ) ) kill_rc = 1 return kill_rc
def ask_question(self, question, importance = 0, responses = None): """ Questions asking function. It asks the user to answer the question given by choosing between a preset list of answers given by the "reposonses" argument. @param question: question text @type question: string @keyword importance: question importance (no default valid values) @type importance: int @keyword responses: list of valid answers which user has to choose from @type responses: tuple or list @return: None @rtype: None """ if responses is None: responses = (_("Yes"), _("No"),) colours = [green, red, blue, darkgreen, darkred, darkblue, brown, purple] colours_len = len(colours) try: sys.stdout.write(question + " ") except UnicodeEncodeError: sys.stdout.write(question.encode('utf-8') + " ") _flush_stdouterr() try: while True: xterm_title(_("Molecule got a question for you")) _flush_stdouterr() answer_items = [colours[x % colours_len](responses[x]) \ for x in range(len(responses))] response = _my_raw_input("["+"/".join(answer_items)+"] ") _flush_stdouterr() for key in responses: if response.upper() == key[:len(response)].upper(): xterm_title_reset() return key _flush_stdouterr() except (EOFError, KeyboardInterrupt): msg = "%s.\n" % (_("Interrupted"),) try: sys.stdout.write(msg) except UnicodeEncodeError: sys.stdout.write(msg.encode("utf-8")) xterm_title_reset() raise KeyboardInterrupt() xterm_title_reset() _flush_stdouterr()
def _kill_loop_device(self, preserve_loop_device_file=False): kill_rc = 0 if self.image_mounted: umounter = self.metadata.get('image_umounter', ImageHandler.DEFAULT_IMAGE_UMOUNTER) args = umounter + [self.tmp_image_mount] rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to umount loop device"), self.loop_device, )) kill_rc = rc else: self.image_mounted = False if self.tmp_image_mount is not None: try: os.rmdir(self.tmp_image_mount) except OSError: pass # kill loop device if self.loop_device is not None: rc = molecule.utils.exec_cmd( [ImageHandler.LOSETUP_EXEC, "-d", self.loop_device]) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to kill loop device"), self.loop_device, )) kill_rc = rc else: self.loop_device = None if (self.tmp_loop_device_file is not None) and \ (not preserve_loop_device_file): try: os.remove(self.tmp_loop_device_file) self.tmp_loop_device_file = None except OSError as err: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("unable to remove temp. loop device file"), err, )) kill_rc = 1 return kill_rc
def run(self): count = 0 maxcount = len(self.execution_order) self._output.output( "[%s|%s] %s" % ( darkgreen("Runner"), brown(self.spec_name), _("preparing execution"),), count = (count, maxcount,) ) for myclass in self.execution_order: count += 1 self._output.output( "[%s|%s] %s %s" % ( darkgreen("Runner"), brown(self.spec_name), _("executing"), str(myclass),), count = (count, maxcount,) ) my = myclass(self.spec_path, self.metadata) rc = 0 while True: try: # setup hook rc = my.setup() if rc: break # pre-run rc = my.pre_run() if rc: break # run rc = my.run() if rc: break # post-run rc = my.post_run() if rc: break break except: my.kill(success = False) raise my.kill(success = rc == 0) if rc: return rc self._output.output( "[%s|%s] %s" % ( darkgreen("Runner"), brown(self.spec_name), _("All done"), ) ) return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("building ISO image"), )) args = [self._iso_builder] args.extend(self._iso_builder_builtin_args) args.extend(self.metadata.get('extra_mkisofs_parameters', [])) if self.iso_title.strip(): args.extend(["-V", self.iso_title[:32]]) args.extend(['-o', self.dest_iso, self.source_path]) self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), )) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("ISO image build failed"), rc, )) return rc self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("built ISO image"), self.dest_iso, )) if os.path.isfile(self.dest_iso) and os.access(self.dest_iso, os.R_OK): self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_iso, )) digest = molecule.utils.md5sum(self.dest_iso) md5file = self.dest_iso + IsoHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % ( digest, os.path.basename(self.dest_iso), )) f.flush() return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("executing pre_run"), )) self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("preparing environment"), )) return 0
def pre_run(self): self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("executing pre_run"), ) ) self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("preparing environment"), ) ) return 0
def post_run(self): self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("executing post_run"), ) ) return 0
def option_chooser(option_data): mydict = {} counter = 1 option_text, option_list = option_data self.output(option_text) for item in option_list: mydict[counter] = item txt = "[%s] %s" % ( darkgreen(str(counter)), blue(item), ) self.output(txt) counter += 1 while True: try: myresult = readtext( "%s: " % (_('Selected number'), )).decode('utf-8') except UnicodeDecodeError: continue try: myresult = int(myresult) except ValueError: continue selected = mydict.get(myresult) if selected != None: return myresult, selected
def run(self): self._output.output("[%s|%s] %s: %s => %s" % ( blue("ImageIsoUnpackHandler"), darkred(self.spec_name), _("iso unpacker running"), self.tmp_squash_mount, self.metadata['chroot_unpack_path'], ) ) def dorm(): if self.metadata['chroot_tmp_dir'] is not None: shutil.rmtree(self.metadata['chroot_tmp_dir'], True) # copy data into chroot, in our case, destination dir already # exists, so copy_dir() is a bit tricky try: rc = molecule.utils.copy_dir_existing_dest( self.tmp_squash_mount, self.metadata['chroot_unpack_path'] ) except Exception: dorm() raise if rc != 0: dorm() return rc
def readtext(request, password=False): """ Read text from stdin and return it (will be moved from here). @param request: textual request to print @type request: string @keyword password: if you are requesting a password, set this to True @type password: bool @return: text read back from stdin @rtype: string """ xterm_title(_("Molecule needs your attention")) if password: from getpass import getpass try: text = getpass(request + " ") except UnicodeEncodeError: text = getpass(request.encode('utf-8') + " ") else: try: sys.stdout.write(request) except UnicodeEncodeError: sys.stdout.write(request.encode('utf-8')) _flush_stdouterr() text = _my_raw_input() return text
def run(self): self._output.output("[%s|%s] %s: %s => %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("iso unpacker running"), self.tmp_squash_mount, self.metadata['chroot_unpack_path'], ) ) def dorm(): if self.metadata['chroot_tmp_dir'] is not None: shutil.rmtree(self.metadata['chroot_tmp_dir'], True) # create chroot path try: rc = molecule.utils.copy_dir(self.tmp_squash_mount, self.metadata['chroot_unpack_path']) except: dorm() raise if rc != 0: dorm() return rc
def post_run(self): self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("executing post_run"), )) return 0
def run(self): self._output.output("[%s|%s] %s: %s => %s" % ( blue("IsoUnpackHandler"), darkred(self.spec_name), _("iso unpacker running"), self.tmp_squash_mount, self.metadata['chroot_unpack_path'], ) ) def dorm(): if self.metadata['chroot_tmp_dir'] is not None: shutil.rmtree(self.metadata['chroot_tmp_dir'], True) # create chroot path try: rc = molecule.utils.copy_dir(self.tmp_squash_mount, self.metadata['chroot_unpack_path']) except Exception: dorm() raise if rc != 0: dorm() return rc
def _run_error_script(self, source_chroot_dir, chroot_dir, cdroot_dir, env=None): error_script = self.metadata.get('error_script') if error_script: if env is None: env = os.environ.copy() else: env = env.copy() if source_chroot_dir: env['SOURCE_CHROOT_DIR'] = source_chroot_dir if chroot_dir: env['CHROOT_DIR'] = chroot_dir if cdroot_dir: env['CDROOT_DIR'] = cdroot_dir self._output.output("[%s|%s] %s: %s" % ( blue("BuiltinHandler"), darkred(self.spec_name), _("spawning"), " ".join(error_script), )) molecule.utils.exec_cmd(error_script, env=env)
def run(self): self._output.output("[%s|%s] %s: %s => %s" % ( blue("ImageIsoUnpackHandler"), darkred(self.spec_name), _("iso unpacker running"), self.tmp_squash_mount, self.metadata['chroot_unpack_path'], )) def dorm(): if self.metadata['chroot_tmp_dir'] is not None: shutil.rmtree(self.metadata['chroot_tmp_dir'], True) # copy data into chroot, in our case, destination dir already # exists, so copy_dir() is a bit tricky try: rc = molecule.utils.copy_dir_existing_dest( self.tmp_squash_mount, self.metadata['chroot_unpack_path']) except Exception: dorm() raise if rc != 0: dorm() return rc
def readtext(request, password = False): """ Read text from stdin and return it (will be moved from here). @param request: textual request to print @type request: string @keyword password: if you are requesting a password, set this to True @type password: bool @return: text read back from stdin @rtype: string """ xterm_title(_("Molecule needs your attention")) if password: from getpass import getpass try: text = getpass(request+" ") except UnicodeEncodeError: text = getpass(request.encode('utf-8')+" ") else: try: sys.stdout.write(request) except UnicodeEncodeError: sys.stdout.write(request.encode('utf-8')) _flush_stdouterr() text = _my_raw_input() return text
def run(self): self._output.output("[%s|%s] %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("building ISO image"), ) ) args = [self._iso_builder] args.extend(self._iso_builder_builtin_args) args.extend(self.metadata.get('extra_mkisofs_parameters', [])) if self.iso_title.strip(): args.extend(["-V", self.iso_title[:32]]) args.extend(['-o', self.dest_iso, self.source_path]) self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("spawning"), " ".join(args), ) ) rc = molecule.utils.exec_cmd(args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("ISO image build failed"), rc, ) ) return rc self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("built ISO image"), self.dest_iso, ) ) if os.path.isfile(self.dest_iso) and os.access(self.dest_iso, os.R_OK): self._output.output("[%s|%s] %s: %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_iso, ) ) digest = molecule.utils.md5sum(self.dest_iso) md5file = self.dest_iso + IsoHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % (digest, os.path.basename(self.dest_iso),)) f.flush() return 0
def check_super_user(el_data): # check is super user is required su_required = el_data['__plugin__'].require_super_user() if su_required and (not super_user): molecule.output.print_error("%s: %s" % (el, _("required super user access"),)) return False return True
def run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("run hook called"), ) ) # run pre image script oexec_script = self.metadata.get('image_generator_script') if oexec_script: exec_script = oexec_script + [ self._tmp_image_file, str(self.metadata['image_mb']), self.metadata['source_boot_directory'], self.metadata['source_chroot']] env = os.environ.copy() env['PATHS_TO_REMOVE'] = ";".join( self.metadata.get('paths_to_remove', [])) env['PATHS_TO_EMPTY'] = ";".join( self.metadata.get('paths_to_empty', [])) env['RELEASE_STRING'] = self.metadata['release_string'] env['RELEASE_VERSION'] = self.metadata['release_version'] env['RELEASE_DESC'] = self.metadata['release_desc'] env['RELEASE_FILE'] = self.metadata['release_file'] env['IMAGE_NAME'] = self.metadata['image_name'] env['PACKAGES_TO_ADD'] = " ".join(self.metadata.get('packages_to_add', [])) env['PACKAGES_TO_REMOVE'] = " ".join(self.metadata.get('packages_to_remove', [])) env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env = env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image hook failed"), rc, ) ) return rc return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("run hook called"), ) ) # run pre image script oexec_script = self.metadata.get('image_generator_script') if oexec_script: exec_script = oexec_script + [ self._tmp_image_file, str(self.metadata['image_mb']), self.metadata['source_boot_directory'], self.metadata['source_chroot']] env = os.environ.copy() env['PATHS_TO_REMOVE'] = ";".join( self.metadata.get('paths_to_remove', [])) env['PATHS_TO_EMPTY'] = ";".join( self.metadata.get('paths_to_empty', [])) env['RELEASE_STRING'] = self.metadata['release_string'] env['RELEASE_VERSION'] = self.metadata['release_version'] env['RELEASE_DESC'] = self.metadata['release_desc'] env['RELEASE_FILE'] = self.metadata['release_file'] env['IMAGE_NAME'] = self.metadata['image_name'] env['PACKAGES_TO_ADD'] = " ".join(self.metadata.get('packages_to_add', [])) env['PACKAGES_TO_REMOVE'] = " ".join(self.metadata.get('packages_to_remove', [])) env['DESTINATION_IMAGE_DIR'] = \ self.metadata['destination_image_directory'] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(exec_script), ) ) rc = molecule.utils.exec_cmd(exec_script, env=env) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image hook failed"), rc, ) ) return rc return 0
def kill(self, success=True): if not success: self._run_error_script(self.source_dir, self.dest_dir, None) self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("executing kill"), ) ) return 0
def kill(self, success=True): if not success: self._run_error_script(self.source_dir, self.dest_dir, None) self._output.output("[%s|%s] %s" % ( blue("MirrorHandler"), darkred(self.spec_name), _("executing kill"), )) return 0
def kill(self, success=True): if not success: self._run_error_script(None, self.source_chroot, self.dest_root) self._output.output("[%s|%s] %s" % ( blue("CdrootHandler"), darkred(self.spec_name), _("executing kill"), )) return 0
def run(self): self._output.output("[%s|%s] %s => %s" % ( blue("TarHandler"), darkred(self.spec_name), _("compressing chroot"), self.chroot_path, )) dest_path_dir = os.path.dirname(self.dest_path) if not os.path.isdir(dest_path_dir) and not \ os.path.lexists(dest_path_dir): os.makedirs(dest_path_dir, 0o755) current_dir = os.getcwd() # change dir to chroot dir os.chdir(self.chroot_path) args = (TarHandler._TAR_EXEC, "cfp" + self._get_tar_comp_method(), self.dest_path, ".", "--atime-preserve", "--numeric-owner") rc = molecule.utils.exec_cmd(args) os.chdir(current_dir) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("chroot compression failed"), rc, )) return rc self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_path, )) digest = molecule.utils.md5sum(self.dest_path) md5file = self.dest_path + TarHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % ( digest, os.path.basename(self.dest_path), )) f.flush() return 0
def print_help(): config = Configuration() help_data = [ None, (0, " ~ Molecule %s ~ " % (config.get('version'), ), 1, 'Disc Image builder for Sabayon Linux - (C) %s' % (molecule.utils.get_year(), )), None, (0, _('Basic Options'), 0, None), None, (1, '--help', 2, _('this output')), (1, '--nocolor', 1, _('disable colorized output')), None, (0, _('Application Options'), 0, None), (1, '<spec file path 1> <spec file path 2> ...', 1, _('execute against specified specification files')), None, ] molecule.output.print_menu(help_data)
def check_super_user(el_data): # check is super user is required su_required = el_data['__plugin__'].require_super_user() if su_required and (not super_user): molecule.output.print_error("%s: %s" % ( el, _("required super user access"), )) return False return True
def print_help(): config = Configuration() help_data = [ None, (0, " ~ Molecule %s ~ " % (config.get('version'),), 1, 'Disc Image builder for Sabayon Linux - (C) %s' % ( molecule.utils.get_year(),) ), None, (0, _('Basic Options'), 0, None), None, (1, '--help', 2, _('this output')), (1, '--nocolor', 1, _('disable colorized output')), None, (0, _('Application Options'), 0, None), (1, '<spec file path 1> <spec file path 2> ...', 1, _('execute against specified specification files')), None, ] molecule.output.print_menu(help_data)
def kill(self, success=True): if not success: self._run_error_script(self.source_chroot, self.chroot_dir, self.source_path) self._output.output("[%s|%s] %s" % ( blue("IsoHandler"), darkred(self.spec_name), _("executing kill"), )) return 0
def run(self): self._output.output("[%s|%s] %s: %s %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("run hook called"), _("moving image file to destination"), self.dest_path, )) try: os.rename(self.tmp_loop_device_file, self.dest_path) except OSError as err: if err.errno != errno.EXDEV: raise # cannot move atomically, fallback to shutil.move try: shutil.move(self.tmp_loop_device_file, self.dest_path) except shutil.Error: raise self._loop_device_file_removed = True self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("built image"), self.dest_path, )) if os.path.isfile(self.dest_path) and os.access( self.dest_path, os.R_OK): self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_path, )) digest = molecule.utils.md5sum(self.dest_path) md5file = self.dest_path + FinalImageHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % ( digest, os.path.basename(self.dest_path), )) f.flush()
def setup(self): self.source_dir = self.metadata['source_chroot'] self.image_mb = self.metadata['image_mb'] try: self._tmp_image_file_fd, self._tmp_image_file = \ tempfile.mkstemp(prefix = "molecule", dir = self._config['tmp_dir'], suffix=".mmc_img") except (OSError, IOError,) as err: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("setup hook failed"), _("cannot create temporary file"), ) ) return 1 self.metadata['MmcImageHandler_image_file'] = self._tmp_image_file return 0
def setup(self): self.source_dir = self.metadata['source_chroot'] self.image_mb = self.metadata['image_mb'] try: self._tmp_image_file_fd, self._tmp_image_file = \ tempfile.mkstemp(prefix="molecule", dir=self._config['tmp_dir'], suffix=".mmc_img") except (OSError, IOError,): self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("setup hook failed"), _("cannot create temporary file"), ) ) return 1 self.metadata['MmcImageHandler_image_file'] = self._tmp_image_file return 0
def run(self): self._output.output("[%s|%s] %s => %s" % ( blue("TarHandler"), darkred(self.spec_name), _("compressing chroot"), self.chroot_path, ) ) dest_path_dir = os.path.dirname(self.dest_path) if not os.path.isdir(dest_path_dir) and not \ os.path.lexists(dest_path_dir): os.makedirs(dest_path_dir, 0o755) current_dir = os.getcwd() # change dir to chroot dir os.chdir(self.chroot_path) args = (TarHandler._TAR_EXEC, "cfp" + self._get_tar_comp_method(), self.dest_path, ".", "--atime-preserve", "--numeric-owner") rc = molecule.utils.exec_cmd(args) os.chdir(current_dir) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("chroot compression failed"), rc, ) ) return rc self._output.output("[%s|%s] %s: %s" % ( blue("TarHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_path, ) ) digest = molecule.utils.md5sum(self.dest_path) md5file = self.dest_path + TarHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % (digest, os.path.basename(self.dest_path),)) f.flush() return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("hooks running"), ) ) # run inner chroot script exec_script = self.metadata.get('inner_chroot_script') if exec_script: if os.path.isfile(exec_script[0]) and os.access(exec_script[0], os.R_OK): rc = self._exec_inner_script(exec_script, self.dest_dir) if rc != 0: return rc self._output.output("[%s|%s] %s" % ( blue("ChrootHandler"), darkred(self.spec_name), _("hooks completed succesfully"), ) ) return 0
def run(self): self._output.output("[%s|%s] %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("run hook called"), ) ) # fill image file try: rc = self._fill_image_file() if rc != 0: return rc except (IOError, OSError) as err: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image file generation failed"), err, ) ) return 1 # format image file image_formatter = self.metadata.get('image_formatter', ImageHandler.DEFAULT_IMAGE_FORMATTER) formatter_args = image_formatter + [self.loop_device] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(formatter_args), ) ) rc = molecule.utils.exec_cmd(formatter_args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image formatter hook failed"), rc, ) ) return rc # mount image file mounter = self.metadata.get('image_mounter', ImageHandler.DEFAULT_IMAGE_MOUNTER) mount_args = mounter + [self.loop_device, self.tmp_image_mount] self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("spawning"), " ".join(mount_args), ) ) rc = molecule.utils.exec_cmd(mount_args) if rc != 0: self._output.output("[%s|%s] %s: %s" % ( blue("ImageHandler"), darkred(self.spec_name), _("image mount failed"), rc, ) ) return rc self.image_mounted = True return 0
def selaction(): self.output('') self.output(darkred(_("Please select an option"))) if can_cancel: self.output(" ("+blue("-1")+") "+darkred(_("Discard all"))) self.output(" ("+blue("0")+") "+darkgreen(_("Confirm"))) self.output(" ("+blue("1")+") "+brown(_("Add item"))) self.output(" ("+blue("2")+") "+brown(_("Edit item"))) self.output(" ("+blue("3")+") "+darkblue(_("Remove item"))) self.output(" ("+blue("4")+") "+darkgreen(_("Show current list"))) # wait user interaction self.output('') try: action = readtext(darkgreen(_("Your choice (type a number and press enter):"))+" ") except UnicodeDecodeError: return '' return action
def run(self): self._output.output("[%s|%s] %s: %s %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("run hook called"), _("moving image file to destination"), self.dest_path, ) ) try: os.rename(self.tmp_loop_device_file, self.dest_path) except OSError as err: if err.errno != errno.EXDEV: raise # cannot move atomically, fallback to shutil.move try: shutil.move(self.tmp_loop_device_file, self.dest_path) except shutil.Error: raise self._loop_device_file_removed = True self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("built image"), self.dest_path, ) ) if os.path.isfile(self.dest_path) and os.access(self.dest_path, os.R_OK): self._output.output("[%s|%s] %s: %s" % ( blue("FinalImageHandler"), darkred(self.spec_name), _("generating md5 for"), self.dest_path, ) ) digest = molecule.utils.md5sum(self.dest_path) md5file = self.dest_path + FinalImageHandler.MD5_EXT with open(md5file, "w") as f: f.write("%s %s\n" % (digest, os.path.basename(self.dest_path),)) f.flush()