Introduce class Lorax and class InstRoot.

The lorax driver program will instantiate the Lorax class, which
drives the creation of the install images.  The InstRoot class is
the main object that represents the contents of the instroot
image (the tree that boot and stage2 images are made from).
This commit is contained in:
David Cantrell 2008-10-09 17:04:13 -10:00
parent 9546387afd
commit 81e7702393
3 changed files with 441 additions and 474 deletions

View File

@ -102,49 +102,25 @@ if __name__ == "__main__":
sys.stderr.write("ERROR: Missing repo to use for image generation.\n") sys.stderr.write("ERROR: Missing repo to use for image generation.\n")
sys.exit(1) sys.exit(1)
print("\n+=======================================================+") lorax = pylorax.Lorax(repos=args, output=output, mirrorlist=mirrorlist)
print("| Setting up work directories and configuration data... |") lorax.run()
print("+=======================================================+\n")
# collect all repos specified on the command line. the first repo is
# designated the main repo (I guess)
repo, extrarepos = pylorax.collectRepos(args)
# create directories that we will be using for image generation
buildinstdir, treedir, cachedir = pylorax.initializeDirs(output)
# write out yum.conf used for image generation
yumconf = pylorax.writeYumConf(cachedir=cachedir, repo=repo, extrarepos=extrarepos, mirrorlist=mirrorlist)
if yumconf is None:
sys.stderr.write("ERROR: Could not generate temporary yum.conf file.\n")
sys.exit(1)
buildarch = pylorax.getBuildArch()
print("\n+================================================+")
print("| Creating instroot tree to build images from... |")
print("+================================================+\n")
# upd-instroot
if not pylorax.instroot.createInstRoot(yumconf=yumconf, arch=buildarch, treedir=treedir, updates=updates):
sys.stderr.write("ERROR: Could not create inst root.\n")
sys.exit(1)
print("\n+=======================================+")
print("| Writing meta data to instroot tree... |")
print("+=======================================+\n")
# write .treeinfo file
if not pylorax.treeinfo.write(family=product, variant=variant, version=version, arch=buildarch, outdir=output):
sys.stderr.write("Error writing %s/.treeinfo file.\n" % (output,))
# mk-images
# XXX
# write .discinfo file
if not pylorax.discinfo.write(release=release, arch=buildarch, outdir=output):
sys.stderr.write("Error writing %s/.discinfo file.\n" % (output,))
# clean up
if cleanup: if cleanup:
pylorax.cleanup() lorax.cleanup()
# print("\n+=======================================+")
# print("| Writing meta data to instroot tree... |")
# print("+=======================================+\n")
#
# # write .treeinfo file
# if not pylorax.treeinfo.write(family=product, variant=variant, version=version, arch=buildarch, outdir=output):
# sys.stderr.write("Error writing %s/.treeinfo file.\n" % (output,))
#
# # mk-images
# # XXX
#
# # write .discinfo file
# if not pylorax.discinfo.write(release=release, arch=buildarch, outdir=output):
# sys.stderr.write("Error writing %s/.discinfo file.\n" % (output,))

View File

@ -40,10 +40,42 @@ conf['confdir'] = '/etc/lorax'
conf['tmpdir'] = tempfile.gettempdir() conf['tmpdir'] = tempfile.gettempdir()
conf['datadir'] = '/usr/share/lorax' conf['datadir'] = '/usr/share/lorax'
def show_version(prog): class Lorax:
"""show_version(prog) def __init__(self, repos=[], output=None, mirrorlist=[], updates=None):
print("\n+=======================================================+")
print("| Setting up work directories and configuration data... |")
print("+=======================================================+\n")
Display program name (prog) and version number. If prog is an empty if repos != []:
self.repo, self.extrarepos = self._collectRepos(repos)
else:
self.repo = None
self.extrarepos = []
self.output = output
self.mirrorlist = mirrorlist
self.updates = updates
self.buildinstdir, self.treedir, self.cachedir = self._initializeDirs()
self.yumconf = self._writeYumConf()
self.buildarch = self.getBuildArch()
def run(self):
"""run()
Generate install images.
"""
print("\n+================================================+")
print("| Creating instroot tree to build images from... |")
print("+================================================+\n")
self.instroot = pylorax.instroot.InstRoot(yumconf=self.yumconf, arch=self.buildarch, treedir=self.treedir, updates=self.updates)
def showVersion(self, driver=None):
"""showVersion(driver)
Display program name (driver) and version number. If prog is an empty
string or None, use the value 'pylorax'. string or None, use the value 'pylorax'.
""" """
@ -53,132 +85,38 @@ def show_version(prog):
print "%s version %d.%d" % (prog, version[0], version[1],) print "%s version %d.%d" % (prog, version[0], version[1],)
def collectRepos(args): def cleanup(self, trash=[]):
"""collectRepos(args) """cleanup(trash)
Get the main repo (the first one) and then build a list of all remaining Given a list of things to remove, cleanup() will remove them if it can.
repos in the list. Sanitize each repo URL for proper yum syntax. Never fails, just tries to remove things and returns regardless of
failures removing things.
""" """
if args is None or args == []: if trash != []:
return '', [] for item in trash:
if os.path.isdir(item):
shutil.rmtree(item, ignore_errors=True)
else:
os.unlink(item)
repolist = [] if os.path.isdir(conf['tmpdir']):
for repospec in args: shutil.rmtree(conf['tmpdir'], ignore_errors=True)
if repospec.startswith('/'):
repo = "file://%s" % (repospec,)
print("Adding local repo:\n %s" % (repo,))
repolist.append(repo)
elif repospec.startswith('http://') or repospec.startswith('ftp://'):
print("Adding remote repo:\n %s" % (repospec,))
repolist.append(repospec)
repo = repolist[0] def getBuildArch(self):
extrarepos = [] """getBuildArch()
if len(repolist) > 1:
for extra in repolist[1:]:
print("Adding extra repo:\n %s" % (extra,))
extrarepos.append(extra)
return repo, extrarepos
def initializeDirs(output):
"""initializeDirs(output)
Create directories used for image generation. The only required
parameter is the main output directory specified by the user.
"""
if not os.path.isdir(output):
os.makedirs(output, mode=0755)
conf['tmpdir'] = tempfile.mkdtemp('XXXXXX', 'lorax.tmp.', conf['tmpdir'])
buildinstdir = tempfile.mkdtemp('XXXXXX', 'buildinstall.tree.', conf['tmpdir'])
treedir = tempfile.mkdtemp('XXXXXX', 'treedir.', conf['tmpdir'])
cachedir = tempfile.mkdtemp('XXXXXX', 'yumcache.', conf['tmpdir'])
print("Working directories:")
print(" tmpdir = %s" % (conf['tmpdir'],))
print(" buildinstdir = %s" % (buildinstdir,))
print(" treedir = %s" % (treedir,))
print(" cachedir = %s" % (cachedir,))
return buildinstdir, treedir, cachedir
def writeYumConf(cachedir=None, repo=None, extrarepos=[], mirrorlist=[]):
"""writeYumConf(cachedir=None, repo=None, [extrarepos=[], mirrorlist=[]])
Generate a temporary yum.conf file for image generation. The required
parameters are the cachedir that yum should use and the main repo to use.
Optional parameters are a list of extra repositories to add to the
yum.conf file. The mirrorlist parameter is a list of yum mirrorlists
that should be added to the yum.conf file.
Returns the path to the temporary yum.conf file on success, None of failure.
"""
if cachedir is None or repo is None:
return None
tmpdir = conf['tmpdir']
(fd, yumconf) = tempfile.mkstemp(prefix='yum.conf', dir=tmpdir)
f = os.fdopen(fd, 'w')
f.write("[main]\n")
f.write("cachedir=%s\n" % (cachedir,))
f.write("keepcache=0\n")
f.write("gpgcheck=0\n")
f.write("plugins=0\n")
f.write("reposdir=\n")
f.write("tsflags=nodocs\n\n")
f.write("[loraxrepo]\n")
f.write("name=lorax repo\n")
f.write("baseurl=%s\n" % (repo,))
f.write("enabled=1\n\n")
if extrarepos != []:
n = 1
for extra in extrarepos:
f.write("[lorax-extrarepo-%d]\n" % (n,))
f.write("name=lorax extra repo %d\n" % (n,))
f.write("baseurl=%s\n" % (extra,))
f.write("enabled=1\n")
n += 1
if mirrorlist != []:
n = 1
for mirror in mirrorlist:
f.write("[lorax-mirrorlistrepo-%d]\n" % (n,))
f.write("name=lorax mirrorlist repo %d\n" % (n,))
f.write("mirrorlist=%s\n" % (extra,))
f.write("enabled=1\n")
n += 1
f.close()
print("Wrote lorax yum configuration to %s" % (yumconf,))
return yumconf
def getBuildArch(yumconf=None):
"""getBuildArch(yumconf=None)
Query the configured yum repositories to determine our build architecture, Query the configured yum repositories to determine our build architecture,
which is the architecture of the anaconda package in the repositories. which is the architecture of the anaconda package in the repositories.
The required argument is yumconf, which is the path to the yum configuration
file to use.
This function is based on a subset of what repoquery(1) does. This function is based on a subset of what repoquery(1) does.
""" """
uname_arch = os.uname()[4] uname_arch = os.uname()[4]
if yumconf == '' or yumconf is None or not os.path.isfile(yumconf): if self.yumconf == '' or self.yumconf is None or not os.path.isfile(self.yumconf):
return uname_arch return uname_arch
repoq = yum.YumBase() repoq = yum.YumBase()
@ -207,23 +145,102 @@ def getBuildArch(yumconf=None):
return ret_arch return ret_arch
def cleanup(trash=[]): def _collectRepos(self, repos):
"""cleanup(trash) """_collectRepos(repos)
Given a list of things to remove, cleanup() will remove them if it can. Get the main repo (the first one) and then build a list of all remaining
Never fails, just tries to remove things and returns regardless of repos in the list. Sanitize each repo URL for proper yum syntax.
failures removing things.
""" """
if trash != []: if repos is None or repos == []:
for item in trash: return '', []
if os.path.isdir(item):
shutil.rmtree(item, ignore_errors=True)
else:
os.unlink(item)
if os.path.isdir(conf['tmpdir']): repolist = []
shutil.rmtree(conf['tmpdir'], ignore_errors=True) for repospec in repos:
if repospec.startswith('/'):
repo = "file://%s" % (repospec,)
print("Adding local repo:\n %s" % (repo,))
repolist.append(repo)
elif repospec.startswith('http://') or repospec.startswith('ftp://'):
print("Adding remote repo:\n %s" % (repospec,))
repolist.append(repospec)
return repo = repolist[0]
extrarepos = []
if len(repolist) > 1:
for extra in repolist[1:]:
print("Adding extra repo:\n %s" % (extra,))
extrarepos.append(extra)
return repo, extrarepos
def initializeDirs(self):
"""_initializeDirs()
Create directories used for image generation.
"""
if not os.path.isdir(self.output):
os.makedirs(self.output, mode=0755)
conf['tmpdir'] = tempfile.mkdtemp('XXXXXX', 'lorax.tmp.', conf['tmpdir'])
buildinstdir = tempfile.mkdtemp('XXXXXX', 'buildinstall.tree.', conf['tmpdir'])
treedir = tempfile.mkdtemp('XXXXXX', 'treedir.', conf['tmpdir'])
cachedir = tempfile.mkdtemp('XXXXXX', 'yumcache.', conf['tmpdir'])
print("Working directories:")
print(" tmpdir = %s" % (conf['tmpdir'],))
print(" buildinstdir = %s" % (buildinstdir,))
print(" treedir = %s" % (treedir,))
print(" cachedir = %s" % (cachedir,))
return buildinstdir, treedir, cachedir
def _writeYumConf():
"""_writeYumConf()
Generate a temporary yum.conf file for image generation. Returns the path
to the temporary yum.conf file on success, None of failure.
"""
tmpdir = conf['tmpdir']
(fd, yumconf) = tempfile.mkstemp(prefix='yum.conf', dir=tmpdir)
f = os.fdopen(fd, 'w')
f.write("[main]\n")
f.write("cachedir=%s\n" % (self.cachedir,))
f.write("keepcache=0\n")
f.write("gpgcheck=0\n")
f.write("plugins=0\n")
f.write("reposdir=\n")
f.write("tsflags=nodocs\n\n")
f.write("[loraxrepo]\n")
f.write("name=lorax repo\n")
f.write("baseurl=%s\n" % (self.repo,))
f.write("enabled=1\n\n")
if self.extrarepos != []:
n = 1
for extra in self.extrarepos:
f.write("[lorax-extrarepo-%d]\n" % (n,))
f.write("name=lorax extra repo %d\n" % (n,))
f.write("baseurl=%s\n" % (extra,))
f.write("enabled=1\n")
n += 1
if self.mirrorlist != []:
n = 1
for mirror in self.mirrorlist:
f.write("[lorax-mirrorlistrepo-%d]\n" % (n,))
f.write("name=lorax mirrorlist repo %d\n" % (n,))
f.write("mirrorlist=%s\n" % (mirror,))
f.write("enabled=1\n")
n += 1
f.close()
print("Wrote lorax yum configuration to %s" % (yumconf,))
return yumconf

View File

@ -29,9 +29,8 @@ import pylorax
sys.path.insert(0, '/usr/share/yum-cli') sys.path.insert(0, '/usr/share/yum-cli')
import yummain import yummain
# Create inst root from which stage 1 and stage 2 images are built. class InstRoot:
def createInstRoot(yumconf=None, arch=None, treedir=None, updates=None): """InstRoot(yumconf=None, arch=None, treedir=None, [updates=None])
"""createInstRoot(yumconf=None, arch=None, treedir=None, [updates=None])
Create a instroot tree for the specified architecture. The list of Create a instroot tree for the specified architecture. The list of
packages to install are specified in the /etc/lorax/packages and packages to install are specified in the /etc/lorax/packages and
@ -50,25 +49,43 @@ def createInstRoot(yumconf=None, arch=None, treedir=None, updates=None):
""" """
# Create inst root from which stage 1 and stage 2 images are built.
def __init__(self, yumconf=None, arch=None, treedir=None, updates=None):
self.yumconf = yumconf
self.arch = arch
self.treedir = treedir
self.updates = updates
# XXX: these tests should raise exceptions
if yumconf is None or arch is None or treedir is None: if yumconf is None or arch is None or treedir is None:
return False return False
# on 64-bit systems, make sure we use lib64 as the lib directory # on 64-bit systems, make sure we use lib64 as the lib directory
if arch.endswith('64') or arch == 's390x': if self.arch.endswith('64') or self.arch == 's390x':
libdir = 'lib64' self.libdir = 'lib64'
else: else:
libdir = 'lib' self.libdir = 'lib'
# the directory where the instroot will be created # the directory where the instroot will be created
destdir = os.path.join(treedir, 'install') self.destdir = os.path.join(self.treedir, 'install')
os.makedirs(destdir) if not os.path.isdir(self.destdir):
os.makedirs(self.destdir)
# build a list of packages to install # build a list of packages to install
self.packages = self._getPackageList()
# install the packages to the instroot
self._installPackages()
# scrub instroot
self._scrubInstRoot()
def _getPackageList(self):
packages = set() packages = set()
packages_files = [] packages_files = []
packages_files.append(os.path.join(pylorax.conf['confdir'], 'packages')) packages_files.append(os.path.join(pylorax.conf['confdir'], 'packages'))
packages_files.append(os.path.join(pylorax.conf['confdir'], arch, 'packages')) packages_files.append(os.path.join(pylorax.conf['confdir'], self.arch, 'packages'))
for pfile in packages_files: for pfile in packages_files:
if os.path.isfile(pfile): if os.path.isfile(pfile):
@ -92,70 +109,29 @@ def createInstRoot(yumconf=None, arch=None, treedir=None, updates=None):
packages = list(packages) packages = list(packages)
packages.sort() packages.sort()
# install the packages to the instroot return packages
if not installPackages(yumconf=yumconf, destdir=destdir, packages=packages):
sys.stderr.write("ERROR: Could not install packages.\n")
sys.exit(1)
if not scrubInstRoot(destdir=destdir, libdir=libdir, arch=arch):
sys.stderr.write("ERROR: Could not scrub instroot.\n")
sys.exit(1)
return True
# Call yummain to install the list of packages to destdir # Call yummain to install the list of packages to destdir
def installPackages(yumconf=None, destdir=None, packages=None): def _installPackages(self):
"""installPackages(yumconf=yumconf, destdir=destdir, packages=packages)
Call yummain to install the list of packages. All parameters are
required. yumconf is the yum configuration file created for this
run of lorax. destdir is the installroot that yum should install to.
packages is a list of package names that yum should install.
yum will resolve dependencies, so it is not necessary to list every
package you want in the instroot.
This function returns True on success, False on failre.
"""
if yumconf is None or destdir is None or packages is None or packages == []:
return False
# build the list of arguments to pass to yum # build the list of arguments to pass to yum
arglist = ['-c', yumconf] arglist = ['-c', self.yumconf]
arglist.append("--installroot=%s" % (destdir,)) arglist.append("--installroot=%s" % (self.destdir,))
arglist += ['install', '-y'] + packages arglist += ['install', '-y'] + self.packages
# do some prep work on the destdir before calling yum # do some prep work on the destdir before calling yum
os.makedirs(os.path.join(destdir, 'usr', 'lib', 'debug')) os.makedirs(os.path.join(self.destdir, 'usr', 'lib', 'debug'))
os.makedirs(os.path.join(destdir, 'usr', 'src', 'debug')) os.makedirs(os.path.join(self.destdir, 'usr', 'src', 'debug'))
os.makedirs(os.path.join(destdir, 'tmp')) os.makedirs(os.path.join(self.destdir, 'tmp'))
os.makedirs(os.path.join(destdir, 'var', 'log')) os.makedirs(os.path.join(self.destdir, 'var', 'log'))
os.makedirs(os.path.join(destdir, 'var', 'lib')) os.makedirs(os.path.join(self.destdir, 'var', 'lib'))
os.symlink(os.path.join(os.path.sep, 'tmp'), os.path.join(destdir, 'var', 'lib', 'xkb')) os.symlink(os.path.join(os.path.sep, 'tmp'), os.path.join(self.destdir, 'var', 'lib', 'xkb'))
# XXX: sort through yum errcodes and return False for actual bad things # XXX: sort through yum errcodes and return False for actual bad things
# we care about # we care about
errcode = yummain.user_main(arglist, exit_code=False) errcode = yummain.user_main(arglist, exit_code=False)
return True
# Scrub the instroot tree (remove files we don't want, modify settings, etc) # Scrub the instroot tree (remove files we don't want, modify settings, etc)
def scrubInstRoot(destdir=None, libdir='lib', arch=None): def _scrubInstRoot(self):
"""scrubInstRoot(destdir=None, libdir='lib', arch=None)
Clean up the newly created instroot and make the tree more suitable to
run the installer.
destdir is the path to the instroot. libdir is the subdirectory in
/usr for libraries (either lib or lib64). arch is the architecture
the image is for (e.g., i386, x86_64, ppc, s390x, alpha, sparc).
"""
if destdir is None or not os.path.isdir(destdir) or arch is None:
return False
print print
# drop custom configuration files in to the instroot # drop custom configuration files in to the instroot
@ -380,5 +356,3 @@ def scrubInstRoot(destdir=None, libdir='lib', arch=None):
for libname in glob.glob(os.path.join(destdir, 'usr', libdir), 'libunicode-lite*'): for libname in glob.glob(os.path.join(destdir, 'usr', libdir), 'libunicode-lite*'):
shutil.rmtree(libname, ignore_errors=True) shutil.rmtree(libname, ignore_errors=True)
return True