Skip to content
Snippets Groups Projects
main.py 5.92 KiB
Newer Older
#!/usr/bin/env python

Nikos Skalkotos's avatar
Nikos Skalkotos committed
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

Nikos Skalkotos's avatar
Nikos Skalkotos committed
from image_creator import get_os_class
from image_creator import __version__ as version
from image_creator import FatalError
Nikos Skalkotos's avatar
Nikos Skalkotos committed
from image_creator.disk import Disk
from image_creator.util import get_command

Nikos Skalkotos's avatar
Nikos Skalkotos committed
import sys
import os

dd = get_command('dd')
def check_writable_dir(option, opt_str, value, parser):
    dirname = os.path.dirname(value)
    name = os.path.basename(value)
    if dirname and not os.path.isdir(dirname):
        parser.error("`%s' is not an existing directory" % dirname)
    if not name:
        parser.error("`%s' is not a valid file name" % dirname)

    setattr(parser.values, option.dest, value)


def parse_options(input_args):
    usage = "Usage: %prog [options] <input_media>"
    parser = optparse.OptionParser(version=version, usage=usage)

    parser.add_option("-f", "--force", dest="force", default=False,
        action="store_true", help="Overwrite output files if they exist")

    parser.add_option("--no-cleanup", dest="cleanup", default=True,
        help="Don't cleanup sensitive data before extracting the image",
        action="store_false")

    parser.add_option("--no-sysprep", dest="sysprep", default=True,
        help="Don't perform system preperation before extracting the image",
        action="store_false")

    parser.add_option("--no-shrink", dest="shrink", default=True,
        help="Don't shrink any partition before extracting the image",
        action="store_false")

    parser.add_option("-o", "--outfile", type="string", dest="outfile",
        default=None, action="callback", callback=check_writable_dir,
        help="Output image file",
        metavar="FILE")

    parser.add_option("-u", "--upload", dest="upload", default=False,
        help="Upload image to a pithos repository using kamaki",
        action="store_true")

    parser.add_option("-r", "--register", dest="register", default=False,
        help="Register image to okeanos using kamaki", action="store_true")

    options, args = parser.parse_args(input_args)

    if len(args) != 1:
        parser.error('Wrong number of arguments')
    options.source = args[0]
    if not os.path.exists(options.source):
        parser.error('input media is not accessible')
    if options.register:
        options.upload = True

    if options.outfile is None and not options.upload:
        parser.error('either outfile (-o) or upload (-u) must be set.')

Nikos Skalkotos's avatar
Nikos Skalkotos committed

def image_creator():
Nikos Skalkotos's avatar
Nikos Skalkotos committed

    options = parse_options(sys.argv[1:])

    if os.geteuid() != 0:
        raise FatalError("You must run %s as root" \
                        % os.path.basename(sys.argv[0]))

    if not options.force:
        for extension in ('', '.meta'):
            filename = "%s%s" % (options.outfile, extension)
            if os.path.exists(filename):
                raise FatalError("Output file %s exists "
                    "(use --force to overwrite it)." % filename)

    disk = Disk(options.source)
Nikos Skalkotos's avatar
Nikos Skalkotos committed
    try:
        dev = disk.get_device()
        dev.mount()
Nikos Skalkotos's avatar
Nikos Skalkotos committed
        osclass = get_os_class(dev.distro, dev.ostype)
        image_os = osclass(dev.root, dev.g)
        metadata = image_os.get_metadata()

        if options.sysprep:
            image_os.sysprep()
        if options.cleanup:
            image_os.data_cleanup()

Nikos Skalkotos's avatar
Nikos Skalkotos committed
        dev.umount()
        size = options.shrink and dev.shrink() or dev.size()
        metadata['size'] = str(size // 2 ** 20)
        outfile = ""
        if options.outfile is not None:
            outfile = options.outfile
            f = open('%s.%s' % (options.outfile, 'meta'), 'w')
            try:
                for key in metadata.keys():
                    f.write("%s=%s\n" % (key, metadata[key]))
            finally:
                f.close()
        else:
            outfd, outfile = tmpfile.mkstemp()
            os.close(outfd)

        dd('if=%s' % dev.device,
            'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22))

Nikos Skalkotos's avatar
Nikos Skalkotos committed
    finally:
        disk.cleanup()

    #The image is ready, lets call kamaki if necessary
    if options.upload:

    if options.outfile is None:
        os.unlink(outfile)
    return 0

COLOR_BLACK = "\033[00m"
COLOR_RED = "\033[1;31m"

        ret = image_creator()
        sys.exit(ret)
    except FatalError as e:
        print >> sys.stderr, "\n%sError: %s%s\n" % (COLOR_RED, e, COLOR_BLACK)
        sys.exit(1)
Nikos Skalkotos's avatar
Nikos Skalkotos committed


if __name__ == '__main__':
    main()
Nikos Skalkotos's avatar
Nikos Skalkotos committed
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :