#!/usr/bin/python
# -*- coding: utf-8 -*-

# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Eucalyptus Systems, Inc.
# All rights reserved.
#
# Redistribution and use of this software in source and binary forms, with or
# without modification, are permitted provided that the following conditions
# are met:
#
#   Redistributions of source code must retain the above
#   copyright notice, this list of conditions and the
#   following disclaimer.
#
#   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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER 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.
#
# Author: Neil Soman neil@eucalyptus.com

import getopt
import sys
import os
from euca2ools import Euca2ool, FileValidationError, \
    DirValidationError, CopyError, MetadataReadError, Util, \
    NotFoundError, CommandFailed, UnsupportedException
from subprocess import *
import platform

usage_string = \
    """
Bundle the local filesystem of a running instance as a bundled image.

euca-bundle-vol -u, --user user -s, --size size_in_MB
[-c, --cert cert_path] [-k, --privatekey private_key_path] 
[-a, --all] [-e, --exclude dir1, dir2,...dirN] [-p, --prefix prefix] [--[no-]inherit] [-v, --volume volume_path] [--fstab fstab_path] [--generate-fstab] [--kernel kernel_id] [--ramdisk ramdisk_id] [-B, --block-device-mapping mapping] 
[-d, --destination destination_path] [--ec2cert ec2cert_path] [-r, --arch target_architecture] [--batch] [--version]

REQUIRED PARAMETERS

-u, --user			User ID (12-digit) of the user who is bundling the image.

-s, --size			Size for the image in MB (default: 10GB or 10240MB).

OPTIONAL PARAMETERS

-c, --cert			Path to the user's PEM encoded certificate.

-k, --privatekey		Path to the user's PEM encoded private key.

-a, --all			Bundle all directories (including mounted filesystems).

-p, --prefix			The prefix for the bundle image files. (default: image name).

--[no-]inherit			Add (or do not add) instance metadata to the bundled image. Inherit is set by default.

-e, --exclude			comma-separated list of directories to exclude.

--kernel			The kernel to be associated with the bundled image.

--ramdisk			The ramdisk to be associated with the bundled image.

-B, --block-device-mapping	Default block device mapping for the image (comma-separated list of key=value pairs).

-d, --destination		Directory to store the bundled image in (default: "/tmp"). Recommended. 

--ec2cert			The path to the Cloud's X509 public key certificate.

-r, --arch			Target architecture for the image ('x86_64' or 'i386' default: 'x86_64').

-v, --volume			Path to mounted volume to create the bundle from (default: "/").

--fstab 			Path to the fstab to be bundled into the image.
	
--generate-fstab 		Generate fstab to bundle into the image.

--batch                         Run in batch mode (compatibility only. has no effect).
"""

MAX_IMAGE_SIZE = 1024 * 10


def usage(status=1):
    print usage_string
    sys.exit(status)


def version():
    print Util().version()
    sys.exit()


def check_root():
    if os.geteuid() == 0:
        return
    else:
        print 'Must be superuser to execute this command.'
        sys.exit()


def check_image_size(size):
    if size > MAX_IMAGE_SIZE:
        print 'Image Size is too large (Max = %d MB)' % MAX_IMAGE_SIZE
        sys.exit()


def parse_excludes(excludes_string):
    excludes = []
    if excludes_string:
        excludes_string = excludes_string.replace(' ', '')
        excludes = excludes_string.split(',')
    return excludes


def get_instance_metadata(
    euca,
    ramdisk,
    kernel,
    mapping,
    ):
    product_codes = None
    ramdisk_id = ramdisk
    kernel_id = kernel
    block_dev_mapping = mapping
    ancestor_ami_ids = None
    try:
        euca.can_read_instance_metadata()
        if not ramdisk_id:
            try:
                ramdisk_id = euca.get_instance_ramdisk()
            except MetadataReadError:
                print 'Unable to read ramdisk id'

        if not kernel_id:
            try:
                kernel_id = euca.get_instance_kernel()
            except MetadataReadError:
                print 'Unable to read kernel id'

        if not block_dev_mapping:
            try:
                block_dev_mapping = \
                    euca.get_instance_block_device_mappings()
            except MetadataReadError:
                print 'Unable to read block device mapping'

        try:
            product_codes = euca.get_instance_product_codes().split('\n'
                    )
        except MetadataReadError:
            print 'Unable to read product codes'

        try:
            ancestor_ami_ids = euca.get_ancestor_ami_ids().split('\n')
        except MetadataReadError:
            print 'Unable to read product codes'
    except IOError:

        print 'Unable to read instance metadata. Pass the --no-inherit option if you wish to exclude instance metadata.'
        sys.exit()

    return (ramdisk_id, kernel_id, block_dev_mapping, product_codes,
            ancestor_ami_ids)


def add_product_codes(product_code_string, product_codes):
    if not product_codes:
        product_codes = []
    product_code_values = product_code_string.split(',')

    for p in product_code_values:
        product_codes.append(p)

    return product_codes


def cleanup(path):
    if os.path.exists(path):
        os.remove(path)


def main():
    euca = None
    try:
        euca = Euca2ool('a:c:k:u:B:d:br:p:s:v:e:', [
            'cert=',
            'privatekey=',
            'user=',
            'prefix=',
            'volume=',
            'all',
            'kernel=',
            'ramdisk=',
            'block-device-mapping=',
            'destination=',
            'ec2cert=',
            'arch=',
            'size=',
            'exclude=',
            'inherit=',
            'no-inherit',
            'batch',
            'fstab=',
            'generate-fstab',
            'productcodes=',
            ])
    except Exception, e:
        print e
        usage()

    kernel = None
    user = None
    ramdisk = None
    try:
        cert_path = euca.get_environ('EC2_CERT')
        private_key_path = euca.get_environ('EC2_PRIVATE_KEY')
        ec2cert_path = euca.get_environ('EUCALYPTUS_CERT')
        user_string = euca.get_environ('EC2_USER_ID')
    except NotFoundError:
        sys.exit(1)

    prefix = 'image'
    size_in_MB = 10 * 1024
    destination_path = '/disk1'
    target_arch = 'x86_64'
    mapping = None
    excludes_string = None
    excludes = None
    all = False
    volume_path = '/'
    inherit = True
    product_codes = None
    ancestor_ami_ids = None
    fstab_path = None
    generate_fstab = False
    product_code_string = None
    if user_string:
        try:
            user = int(user_string)
        except ValueError:
            print 'Invalid user', user_string
            sys.exit()
    user = user_string

    for (name, value) in euca.opts:
        if name in ('-h', '--help'):
            usage(0)
        elif name in ('-c', '--cert'):
            cert_path = value
        elif name in ('-k', '--privatekey'):
            private_key_path = value
        elif name in ('-u', '--user'):
            try:
                value = value.replace('-', '')
                user = int(value)
            except ValueError:
                print 'Invalid user', value
                sys.exit()
            user = value
        elif name == '--kernel':
            kernel = value
        elif name == '--ramdisk':
            ramdisk = value
        elif name in ('-p', '--prefix'):
            prefix = value
        elif name in ('-s', '--size'):
            size_in_MB = int(value)
        elif name in ('-v', '--volume'):
            volume_path = value
        elif name in ('-e', '--exclude'):
            excludes_string = value
        elif name in ('-d', '--destination'):
            destination_path = value
        elif name == '--ec2cert':
            ec2cert_path = value
        elif name in ('-a', '--all'):
            all = True
        elif name in ('-r', '--arch'):
            target_arch = value
            if target_arch != 'i386' and target_arch != 'x86_64':
                print 'target architecture must be i386 or x86_64'
                usage()
        elif name in ('-B', '--block-device-mapping'):
            mapping = value
        elif name == '--no-inherit':
            inherit = False
        elif name == '--generate-fstab':
            generate_fstab = True
        elif name == '--fstab':
            fstab_path = value
        elif name == '--productcodes':
            product_code_string = value
        elif name == '--version':
            version()

    if size_in_MB and cert_path and private_key_path and user \
        and ec2cert_path:
        try:
            euca.validate_file(cert_path)
        except FileValidationError:
            print 'Invalid cert'
            sys.exit(1)
        try:
            euca.validate_file(private_key_path)
        except FileValidationError:
            print 'Invalid private key'
            sys.exit(1)
        try:
            euca.validate_file(ec2cert_path)
        except FileValidationError:
            print 'Invalid ec2cert'
            sys.exit(1)
        try:
            euca.validate_dir(volume_path)
        except DirValidationError:
            print 'Invalid directory', volume_path
            sys.exit(1)
            if generate_fstab and fstab_path:
                print '--generate-fstab and --fstab path cannot both be set.'
                sys.exit(1)
            if fstab_path:
                try:
                    euca.validate_file(fstab_path)
                except FileValidationError:
                    print 'Invalid fstab path'
                    sys.exit(1)
        if not fstab_path:
            if platform.machine() == 'i386':
                fstab_path = 'old'
            else:
                fstab_path = 'new'

        check_root()
        check_image_size(size_in_MB)
        volume_path = os.path.normpath(volume_path)
        if not all:
            excludes = parse_excludes(excludes_string)
            euca.add_excludes(volume_path, excludes)
        if inherit:
            (ramdisk, kernel, mapping, product_codes,
             ancestor_ami_ids) = get_instance_metadata(euca, ramdisk,
                    kernel, mapping)
        if product_code_string:
            product_codes = add_product_codes(product_code_string,
                    product_codes)
        try:
            image_path = euca.make_image(size_in_MB, excludes, prefix,
                    destination_path)
        except NotFoundError:
            sys.exit(1)
        except UnsupportedException:
            sys.exit(1)
        image_path = os.path.normpath(image_path)
        if image_path.find(volume_path) == 0:
            exclude_image = image_path.replace(volume_path, '', 1)
            image_path_parts = exclude_image.split('/')
            if len(image_path_parts) > 1:
                exclude_image = \
                    exclude_image.replace(image_path_parts[0] + '/', ''
                        , 1)
            excludes.append(exclude_image)
        try:
            euca.copy_volume(image_path, volume_path, excludes,
                             generate_fstab, fstab_path)
        except CopyError:
            print 'Unable to copy files'
            cleanup(image_path)
            sys.exit(1)
        except NotFoundError:
            cleanup(image_path)
            sys.exit(1)
        except CommandFailed:
            cleanup(image_path)
            sys.exit(1)
        except UnsupportedException:
            cleanup(image_path)
            sys.exit(1)

        (image_size, sha_image_digest) = euca.check_image(image_path,
                destination_path)
        if not prefix:
            prefix = euca.get_relative_filename(image_path)
        try:
            tgz_file = euca.tarzip_image(prefix, image_path,
                    destination_path)
        except NotFoundError:
            sys.exit(1)
        except CommandFailed:
            sys.exit(1)

        (encrypted_file, key, iv, bundled_size) = \
            euca.encrypt_image(tgz_file)
        os.remove(tgz_file)
        (parts, parts_digest) = euca.split_image(encrypted_file)
        euca.generate_manifest(
            destination_path,
            prefix,
            parts,
            parts_digest,
            image_path,
            key,
            iv,
            cert_path,
            ec2cert_path,
            private_key_path,
            target_arch,
            image_size,
            bundled_size,
            sha_image_digest,
            user,
            kernel,
            ramdisk,
            mapping,
            product_codes,
            ancestor_ami_ids,
            )
        os.remove(encrypted_file)
    else:

# ....cleanup(image_path)

        if not size_in_MB:
            print 'size must be specified.'
        if not cert_path:
            print 'cert must be specified.'
        if not private_key_path:
            print 'privatekey must be specified.'
        if not user:
            print 'user must be specified.'
        if not ec2cert_path:
            print 'ec2cert must be specified.'
        usage()


if __name__ == '__main__':
    main()

