1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#!/usr/bin/python -tt
#
# Package and unpackage images for distribution
#
# Copyright 2007 Red Hat, Inc.
# David Lutterkort <dlutter@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
import os, sys, string
from optparse import OptionParser, OptionValueError
import subprocess
import logging
import libxml2
import urlgrabber.progress as progress
import virtinst
import virtinst.ImageParser
import virtinst.CapabilitiesParser
import virtinst.cli as cli
from virtinst.cli import fail
import virtinst.util as util
import virtinst.UnWare
import tempfile
import gettext
import locale
from virtinst import _virtinst as _
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain(virtinst.gettext_app, virtinst.gettext_dir)
gettext.install(virtinst.gettext_app, virtinst.gettext_dir, unicode=1)
class PackageException(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
class Package:
def __init__(self, image):
self.image = image
if image.name is None or image.version is None:
raise PackageException(
_("The image name and version must be present"))
self.vername = "%s-%s" % (image.name, image.version)
self.tmpdir = tempfile.mkdtemp(dir="/var/tmp", prefix="virt-pack")
self.stagedir = os.path.join(self.tmpdir, self.vername)
self.files = []
def add_image_files(self):
cwd = os.getcwd()
img = self.image
self.files.append(os.path.basename(img.filename))
os.chdir(img.base)
for d in img.storage.keys():
disk = img.storage[d]
if disk.use == disk.USE_SCRATCH:
if disk.size is None:
raise PackageException(_("Scratch disk %s does not have a size attribute") % disk.id)
else:
if not os.path.exists(disk.file):
raise PackageException(_("Disk file %s could not be found") % disk.id)
self.files.append(disk.file)
os.path.exists(os.path.join(img.base, disk.file))
def make_vmx_files(self):
img = virtinst.UnWare.Image(self.image)
files = img.make(self.image.base)
self.files.extend(files)
def pack(self, outdir):
outfile = os.path.join(outdir, self.vername + ".tgz")
for f in set(self.files):
dir = os.path.join(self.stagedir, os.path.dirname(f))
if not os.path.exists(dir):
os.makedirs(dir)
os.symlink(os.path.join(self.image.base, f),
os.path.join(self.stagedir, f))
print _("Writing %s") % outfile
cmd = "tar chzv -C %s -f %s %s" % (self.tmpdir,
outfile,
os.path.basename(self.vername))
util.system(cmd)
return outfile
### Option parsing
def parse_args():
parser = cli.VirtOptionParser()
parser.set_usage("%prog [options] image.xml")
parser.add_option("-o", "--output", type="string", dest="output",
action="callback", callback=cli.check_before_store,
help=_("Directory in which packaged file will be put"))
parser.add_option("-d", "--debug", action="store_true", dest="debug",
help=_("Print debugging information"))
(options,args) = parser.parse_args()
if len(args) < 1:
parser.error(_("You need to provide an image XML descriptor"))
options.image = args[0]
return options
def main():
options = parse_args()
cli.setupLogging("virt-pack", options.debug)
image = virtinst.ImageParser.parse_file(os.path.abspath(options.image))
if image.name is None or image.version is None:
fail(_("The image descriptor must contain name and version"))
if options.output is None:
options.output = os.path.join(image.base, "..")
pkg = Package(image)
try:
pkg.add_image_files()
except PackageException, e:
fail(_("Validation failed: %s"), e)
try:
pkg.make_vmx_files()
pkg.pack(options.output)
except PackageException, e:
fail(_("Packaging failed: %s") % e)
if __name__ == "__main__":
main()
|