def _parse_kickstart(self, ksconf=None): if not ksconf: return ksconf = misc.normalize_ksfile(ksconf, self.create['release'], self.create['arch']) ks = kickstart.read_kickstart(ksconf) self.create['ks'] = ks self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] self.create['name'] = misc.build_name(ksconf, self.create['release'], self.create['name_prefix'], self.create['name_suffix']) msger.info("Retrieving repo metadata:") ksrepos = misc.get_repostrs_from_ks(ks) if not ksrepos: raise errors.KsError('no valid repos found in ks file') for repo in ksrepos: if 'baseurl' in repo and repo['baseurl'].startswith("file:"): repourl = repo['baseurl'].replace('file:', '') repourl = "/%s" % repourl.lstrip('/') self.create['localrepos'].append(repourl) self.create['repomd'] = misc.get_metadata_from_repos( ksrepos, self.create['cachedir']) msger.raw(" DONE") target_archlist, archlist = misc.get_arch(self.create['repomd']) if self.create['arch']: if self.create['arch'] not in archlist: raise errors.ConfigError("Invalid arch %s for repository. " "Valid arches: %s" \ % (self.create['arch'], ', '.join(archlist))) else: if len(target_archlist) == 1: self.create['arch'] = str(target_archlist[0]) msger.info("\nUse detected arch %s." % target_archlist[0]) else: raise errors.ConfigError("Please specify a valid arch, " "the choice can be: %s" \ % ', '.join(archlist)) kickstart.resolve_groups(self.create, self.create['repomd']) # check selinux, it will block arm and btrfs image creation misc.selinux_check(self.create['arch'], [p.fstype for p in ks.handler.partition.partitions])
def select_ks(ksfiles): msger.info("Available kickstart files:") i = 0 for ks in ksfiles: i += 1 msger.raw("\t%d. %s (%s)" % (i, ks["description"], os.path.basename(ks["filename"]))) while True: choice = raw_input("Please input your choice and press ENTER. [1..%d] ? " % i) if choice.lower() == "q": sys.exit(1) if choice.isdigit(): choice = int(choice) if choice >= 1 and choice <= i: break return ksfiles[choice-1]["filename"]
def select_ks(ksfiles): msger.info("Available kickstart files:") i = 0 for ks in ksfiles: i += 1 msger.raw("\t%d. %s (%s)" % (i, ks["description"], os.path.basename(ks["filename"]))) while True: choice = raw_input( "Please input your choice and press ENTER. [1..%d] ? " % i) if choice.lower() == "q": sys.exit(1) if choice.isdigit(): choice = int(choice) if choice >= 1 and choice <= i: break return ksfiles[choice - 1]["filename"]
def _pager_file(savepath): if os.path.splitext(savepath)[1].upper() in ('.HTM', '.HTML'): pagers = ('w3m', 'links', 'lynx', 'less', 'more') else: pagers = ('less', 'more') file_showed = False for pager in pagers: cmd = "%s %s" % (pager, savepath) try: os.system(cmd) except OSError: continue else: file_showed = True break if not file_showed: f = open(savepath) msger.raw(f.read()) f.close() msger.pause()
def _parse_kickstart(self, ksconf=None): if not ksconf: return ksconf = misc.normalize_ksfile(ksconf, self.create['release'], self.create['arch']) ks = kickstart.read_kickstart(ksconf) self.create['ks'] = ks self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] self.create['name'] = misc.build_name(ksconf, self.create['release'], self.create['name_prefix'], self.create['name_suffix']) self.create['destdir'] = self.create['outdir'] if self.create['release'] is not None: self.create['destdir'] = "%s/%s/images/%s/" % (self.create['outdir'], self.create['release'], self.create['name']) self.create['name'] = self.create['release'] + '_' + self.create['name'] if not self.create['logfile']: self.create['logfile'] = os.path.join(self.create['destdir'], self.create['name'] + ".log") self.create['releaselog'] = True self.set_logfile() msger.info("Retrieving repo metadata:") ksrepos = kickstart.get_repos(ks, self.create['extrarepos'], self.create['ignore_ksrepo']) if not ksrepos: raise errors.KsError('no valid repos found in ks file') for repo in ksrepos: if hasattr(repo, 'baseurl') and repo.baseurl.startswith("file:"): repourl = repo.baseurl.replace('file:', '') repourl = "/%s" % repourl.lstrip('/') self.create['localrepos'].append(repourl) self.create['repomd'] = misc.get_metadata_from_repos( ksrepos, self.create['cachedir']) msger.raw(" DONE") target_archlist, archlist = misc.get_arch(self.create['repomd']) if self.create['arch']: if self.create['arch'] not in archlist: raise errors.ConfigError("Invalid arch %s for repository. " "Valid arches: %s" \ % (self.create['arch'], ', '.join(archlist))) else: if len(target_archlist) == 1: self.create['arch'] = str(target_archlist[0]) msger.info("Use detected arch %s." % target_archlist[0]) else: raise errors.ConfigError("Please specify a valid arch, " "the choice can be: %s" \ % ', '.join(archlist)) kickstart.resolve_groups(self.create, self.create['repomd']) # check selinux, it will block arm and btrfs image creation misc.selinux_check(self.create['arch'], [p.fstype for p in ks.handler.partition.partitions])
def end(self, *args): if self.counter == self.total: msger.raw("\n") if self.total is not None: self.counter += 1
def callback(self, what, bytes, total, h, user): if what == rpm.RPMCALLBACK_TRANS_START: if bytes == 6: self.total_actions = total elif what == rpm.RPMCALLBACK_TRANS_PROGRESS: pass elif what == rpm.RPMCALLBACK_TRANS_STOP: pass elif what == rpm.RPMCALLBACK_INST_OPEN_FILE: self.lastmsg = None hdr = None if h is not None: try: hdr, rpmloc = h except: rpmloc = h hdr = readRpmHeader(self.ts, h) handle = self._makeHandle(hdr) fd = os.open(rpmloc, os.O_RDONLY) self.callbackfilehandles[handle]=fd self.total_installed += 1 self.installed_pkg_names.append(hdr['name']) return fd else: self._localprint("No header - huh?") elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE: hdr = None if h is not None: try: hdr, rpmloc = h except: rpmloc = h hdr = readRpmHeader(self.ts, h) handle = self._makeHandle(hdr) os.close(self.callbackfilehandles[handle]) fd = 0 # log stuff #pkgtup = self._dopkgtup(hdr) self.logString.append(self._logPkgString(hdr)) elif what == rpm.RPMCALLBACK_INST_PROGRESS: if h is not None: percent = (self.total_installed*100L)/self.total_actions if total > 0: try: hdr, rpmloc = h except: rpmloc = h m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) if m: pkgname = m.group(1) else: pkgname = os.path.basename(rpmloc) if self.output and (sys.stdout.isatty() or self.total_installed == self.total_actions): fmt = self._makefmt(percent) msg = fmt % (self.headmsg, pkgname) if msg != self.lastmsg: self.lastmsg = msg msger.info(msg) if self.total_installed == self.total_actions: msger.raw('') msger.verbose('\n'.join(self.logString)) elif what == rpm.RPMCALLBACK_UNINST_START: pass elif what == rpm.RPMCALLBACK_UNINST_PROGRESS: pass elif what == rpm.RPMCALLBACK_UNINST_STOP: self.total_removed += 1 elif what == rpm.RPMCALLBACK_REPACKAGE_START: pass elif what == rpm.RPMCALLBACK_REPACKAGE_STOP: pass elif what == rpm.RPMCALLBACK_REPACKAGE_PROGRESS: pass
def do_chroot(cls, target, cmd=[]): img = target imgsize = misc.get_file_size(img) * 1024L * 1024L partedcmd = fs_related.find_binary_path("parted") disk = fs_related.SparseLoopbackDisk(img, imgsize) imgmnt = misc.mkdtemp() imgloop = PartitionedMount(imgmnt, skipformat=True) imgloop.add_disk('/dev/sdb', disk) img_fstype = "ext3" msger.info("Partition Table:") partnum = [] for line in runner.outs([partedcmd, "-s", img, "print"]).splitlines(): # no use strip to keep line output here if "Number" in line: msger.raw(line) if line.strip() and line.strip()[0].isdigit(): partnum.append(line.strip()[0]) msger.raw(line) rootpart = None if len(partnum) > 1: rootpart = msger.choice("please choose root partition", partnum) # Check the partitions from raw disk. # if choose root part, the mark it as mounted if rootpart: root_mounted = True else: root_mounted = False partition_mounts = 0 for line in runner.outs([partedcmd, "-s", img, "unit", "B", "print"]).splitlines(): line = line.strip() # Lines that start with number are the partitions, # because parted can be translated we can't refer to any text lines. if not line or not line[0].isdigit(): continue # Some vars have extra , as list seperator. line = line.replace(",", "") # Example of parted output lines that are handled: # Number Start End Size Type File system Flags # 1 512B 3400000511B 3400000000B primary # 2 3400531968B 3656384511B 255852544B primary linux-swap(v1) # 3 3656384512B 3720347647B 63963136B primary fat16 boot, lba partition_info = re.split("\s+", line) size = partition_info[3].split("B")[0] if len(partition_info) < 6 or partition_info[5] in ["boot"]: # No filesystem can be found from partition line. Assuming # btrfs, because that is the only MeeGo fs that parted does # not recognize properly. # TODO: Can we make better assumption? fstype = "btrfs" elif partition_info[5] in ["ext2", "ext3", "ext4", "btrfs"]: fstype = partition_info[5] elif partition_info[5] in ["fat16", "fat32"]: fstype = "vfat" elif "swap" in partition_info[5]: fstype = "swap" else: raise errors.CreatorError( "Could not recognize partition fs type '%s'." % partition_info[5]) if rootpart and rootpart == line[0]: mountpoint = '/' elif not root_mounted and fstype in [ "ext2", "ext3", "ext4", "btrfs" ]: # TODO: Check that this is actually the valid root partition from /etc/fstab mountpoint = "/" root_mounted = True elif fstype == "swap": mountpoint = "swap" else: # TODO: Assing better mount points for the rest of the partitions. partition_mounts += 1 mountpoint = "/media/partition_%d" % partition_mounts if "boot" in partition_info: boot = True else: boot = False msger.verbose( "Size: %s Bytes, fstype: %s, mountpoint: %s, boot: %s" % (size, fstype, mountpoint, boot)) # TODO: add_partition should take bytes as size parameter. imgloop.add_partition((int)(size) / 1024 / 1024, "/dev/sdb", mountpoint, fstype=fstype, boot=boot) try: imgloop.mount() except errors.MountError: imgloop.cleanup() raise try: if len(cmd) != 0: cmdline = ' '.join(cmd) else: cmdline = "/bin/bash" envcmd = fs_related.find_binary_inchroot("env", imgmnt) if envcmd: cmdline = "%s HOME=/root %s" % (envcmd, cmdline) chroot.chroot(imgmnt, None, cmdline) except: raise errors.CreatorError("Failed to chroot to %s." % img) finally: chroot.cleanup_after_chroot("img", imgloop, None, imgmnt)
def do_chroot(cls, target, cmd=[]): img = target imgsize = misc.get_file_size(img) * 1024L * 1024L partedcmd = fs_related.find_binary_path("parted") disk = fs_related.SparseLoopbackDisk(img, imgsize) imgmnt = misc.mkdtemp() imgloop = PartitionedMount(imgmnt, skipformat = True) imgloop.add_disk('/dev/sdb', disk) img_fstype = "ext3" msger.info("Partition Table:") partnum = [] for line in runner.outs([partedcmd, "-s", img, "print"]).splitlines(): # no use strip to keep line output here if "Number" in line: msger.raw(line) if line.strip() and line.strip()[0].isdigit(): partnum.append(line.strip()[0]) msger.raw(line) rootpart = None if len(partnum) > 1: rootpart = msger.choice("please choose root partition", partnum) # Check the partitions from raw disk. # if choose root part, the mark it as mounted if rootpart: root_mounted = True else: root_mounted = False partition_mounts = 0 for line in runner.outs([partedcmd,"-s",img,"unit","B","print"]).splitlines(): line = line.strip() # Lines that start with number are the partitions, # because parted can be translated we can't refer to any text lines. if not line or not line[0].isdigit(): continue # Some vars have extra , as list seperator. line = line.replace(",","") # Example of parted output lines that are handled: # Number Start End Size Type File system Flags # 1 512B 3400000511B 3400000000B primary # 2 3400531968B 3656384511B 255852544B primary linux-swap(v1) # 3 3656384512B 3720347647B 63963136B primary fat16 boot, lba partition_info = re.split("\s+",line) size = partition_info[3].split("B")[0] if len(partition_info) < 6 or partition_info[5] in ["boot"]: # No filesystem can be found from partition line. Assuming # btrfs, because that is the only MeeGo fs that parted does # not recognize properly. # TODO: Can we make better assumption? fstype = "btrfs" elif partition_info[5] in ["ext2","ext3","ext4","btrfs"]: fstype = partition_info[5] elif partition_info[5] in ["fat16","fat32"]: fstype = "vfat" elif "swap" in partition_info[5]: fstype = "swap" else: raise errors.CreatorError("Could not recognize partition fs type '%s'." % partition_info[5]) if rootpart and rootpart == line[0]: mountpoint = '/' elif not root_mounted and fstype in ["ext2","ext3","ext4","btrfs"]: # TODO: Check that this is actually the valid root partition from /etc/fstab mountpoint = "/" root_mounted = True elif fstype == "swap": mountpoint = "swap" else: # TODO: Assing better mount points for the rest of the partitions. partition_mounts += 1 mountpoint = "/media/partition_%d" % partition_mounts if "boot" in partition_info: boot = True else: boot = False msger.verbose("Size: %s Bytes, fstype: %s, mountpoint: %s, boot: %s" % (size, fstype, mountpoint, boot)) # TODO: add_partition should take bytes as size parameter. imgloop.add_partition((int)(size)/1024/1024, "/dev/sdb", mountpoint, fstype = fstype, boot = boot) try: imgloop.mount() except errors.MountError: imgloop.cleanup() raise try: if len(cmd) != 0: cmdline = ' '.join(cmd) else: cmdline = "/bin/bash" envcmd = fs_related.find_binary_inchroot("env", imgmnt) if envcmd: cmdline = "%s HOME=/root %s" % (envcmd, cmdline) chroot.chroot(imgmnt, None, cmdline) except: raise errors.CreatorError("Failed to chroot to %s." %img) finally: chroot.cleanup_after_chroot("img", imgloop, None, imgmnt)
def callback(self, what, bytes, total, h, user): if what == rpm.RPMCALLBACK_TRANS_START: if bytes == 6: self.total_actions = total elif what == rpm.RPMCALLBACK_TRANS_PROGRESS: pass elif what == rpm.RPMCALLBACK_TRANS_STOP: pass elif what == rpm.RPMCALLBACK_INST_OPEN_FILE: self.lastmsg = None hdr = None if h is not None: try: hdr, rpmloc = h except: rpmloc = h hdr = readRpmHeader(self.ts, h) m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) if m: pkgname = m.group(1) else: pkgname = os.path.basename(rpmloc) msger.info("Next install: %s " % pkgname) handle = self._makeHandle(hdr) fd = os.open(rpmloc, os.O_RDONLY) self.callbackfilehandles[handle] = fd if hdr['name'] not in self.installed_pkg_names: self.installed_pkg_names.append(hdr['name']) self.total_installed += 1 return fd else: self._localprint("No header - huh?") elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE: hdr = None if h is not None: try: hdr, rpmloc = h except: rpmloc = h hdr = readRpmHeader(self.ts, h) handle = self._makeHandle(hdr) os.close(self.callbackfilehandles[handle]) fd = 0 # log stuff #pkgtup = self._dopkgtup(hdr) self.logString.append(self._logPkgString(hdr)) elif what == rpm.RPMCALLBACK_INST_START: self.total_installing += 1 elif what == rpm.RPMCALLBACK_UNINST_STOP: pass elif what == rpm.RPMCALLBACK_INST_PROGRESS: if h is not None: percent = (self.total_installed * 100L) / self.total_actions if total > 0: try: hdr, rpmloc = h except: rpmloc = h m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) if m: pkgname = m.group(1) else: pkgname = os.path.basename(rpmloc) if self.output: fmt = self._makefmt(percent) msg = fmt % (self.headmsg, pkgname) if msg != self.lastmsg: self.lastmsg = msg msger.info(msg) if self.total_installed == self.total_actions: msger.raw('') msger.verbose('\n'.join(self.logString)) elif what == rpm.RPMCALLBACK_UNINST_START: pass elif what == rpm.RPMCALLBACK_UNINST_PROGRESS: pass elif what == rpm.RPMCALLBACK_UNINST_STOP: self.total_removed += 1 elif what == rpm.RPMCALLBACK_REPACKAGE_START: pass elif what == rpm.RPMCALLBACK_REPACKAGE_STOP: pass elif what == rpm.RPMCALLBACK_REPACKAGE_PROGRESS: pass elif what == rpm.RPMCALLBACK_SCRIPT_ERROR: if h is not None: try: hdr, rpmloc = h except: rpmloc = h m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) if m: pkgname = m.group(1) else: pkgname = os.path.basename(rpmloc) msger.warning('(%s) Post script failed' % pkgname)
def testRaw(self): excepted = "hello\n" msger.raw("hello") self.assertEqual(excepted, sys.stdout.getvalue())
def _parse_kickstart(self, ksconf=None): if not ksconf: return ksconf = misc.normalize_ksfile(ksconf, self.create['release'], self.create['arch']) ks = kickstart.read_kickstart(ksconf) self.create['ks'] = ks self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] self.create['name'] = misc.build_name(ksconf, self.create['release'], self.create['name_prefix'], self.create['name_suffix']) self.create['destdir'] = self.create['outdir'] if self.create['release'] is not None: self.create['destdir'] = "%s/%s/images/%s/" % (self.create['outdir'], self.create['release'], self.create['name']) self.create['name'] = self.create['release'] + '_' + self.create['name'] if self.create['pack_to'] is not None: if '@NAME@' in self.create['pack_to']: self.create['pack_to'] = self.create['pack_to'].replace('@NAME@', self.create['name']) self.create['name'] = misc.strip_archive_suffix(self.create['pack_to']) if self.create['name'] is None: raise errors.CreatorError("Not supported archive file format: %s" % self.create['pack_to']) if not self.create['logfile']: self.create['logfile'] = os.path.join(self.create['destdir'], self.create['name'] + ".log") self.create['releaselog'] = True self.set_logfile() elif self.create['pack_to'] is not None: if '@NAME@' in self.create['pack_to']: self.create['pack_to'] = self.create['pack_to'].replace('@NAME@', self.create['name']) self.create['name'] = misc.strip_archive_suffix(self.create['pack_to']) if self.create['name'] is None: raise errors.CreatorError("Not supported archive file format: %s" % self.create['pack_to']) msger.info("Retrieving repo metadata:") ksrepos = kickstart.get_repos(ks, self.create['extrarepos'], self.create['ignore_ksrepo']) if not ksrepos: raise errors.KsError('no valid repos found in ks file') for repo in ksrepos: if hasattr(repo, 'baseurl') and repo.baseurl.startswith("file:"): repourl = repo.baseurl.replace('file:', '') repourl = "/%s" % repourl.lstrip('/') self.create['localrepos'].append(repourl) self.create['repomd'] = misc.get_metadata_from_repos( ksrepos, self.create['cachedir']) kstpkrepos = kickstart.get_tpkrepos(ks) if kstpkrepos: for tpk_repo in kstpkrepos: if hasattr(tpk_repo,'baseurl') and tpk_repo.baseurl.startswith("file:"): tpk_repourl = tpk_repo.baseurl.replace('file:','') tpk_repourl = "/%s" % tpk_repourl.lstrip('/') self.create['localtpkrepos'].append(tpk_repourl) msger.raw(" DONE") target_archlist, archlist = misc.get_arch(self.create['repomd']) if self.create['arch']: if self.create['arch'] not in archlist: raise errors.ConfigError("Invalid arch %s for repository. " "Valid arches: %s" \ % (self.create['arch'], ', '.join(archlist))) else: if len(target_archlist) == 1: self.create['arch'] = str(target_archlist[0]) msger.info("Use detected arch %s." % target_archlist[0]) else: raise errors.ConfigError("Please specify a valid arch, " "the choice can be: %s" \ % ', '.join(archlist)) kickstart.resolve_groups(self.create, self.create['repomd']) # check selinux, it will block arm and btrfs image creation misc.selinux_check(self.create['arch'], [p.fstype for p in ks.handler.partition.partitions])