#!/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 xml.dom import minidom
from euca2ools import Euca2ool, FileValidationError, Util, \
    ConnectionFailed
from boto.exception import S3ResponseError, S3CreateError
from boto.s3 import Connection
from boto.s3.key import Key

usage_string = \
    """
Upload a previously bundled image to the cloud.

euca-upload-bundle -b, --bucket bucket -m, --manifest manifest_path [-a, --access-key access_key_id] 
[-s, --secret-key secret_key] [--acl canned_acl] [--ec2cert ec2cert_path] [-d, --directory bundle_path]
[--part part] [--url url] [--skipmanifest] [--debug] [-h, --help] [--version]

REQUIRED PARAMETERS

-b, --bucket 			The name of the bucket to upload to. Bucket will be created if it does not exist.

-m, --manifest 			The path to the manifest file for the bundled image.

OPTIONAL PARAMETERS

--acl				Canned access control policy (defaults to "aws-exec-read").

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

-d, --directory			The directory containing the bundled image to upload (defaults to the manifest directory).

--part				Uploads specified part and all subsequent parts.

--skipmanifest			Do not upload the manifest file.

"""


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


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


def create_bucket(connection, bucket, canned_acl=None):
    print 'Creating bucket:', bucket
    return connection.create_bucket(bucket, policy=canned_acl)


def ensure_bucket(connection, bucket, canned_acl=None):
    bucket_instance = None
    try:
        print 'Checking bucket:', bucket
        bucket_instance = connection.get_bucket(bucket)
    except S3ResponseError, s3error:
        s3error_string = '%s' % s3error
        if s3error_string.find('404') >= 0:
            try:
                bucket_instance = create_bucket(connection, bucket,
                        canned_acl)
            except S3CreateError:
                print 'Unable to create bucket %s' % bucket
                sys.exit()
        elif s3error_string.find('403') >= 0:
            print 'You do not have permission to access bucket:', bucket
            sys.exit()
        else:
            print s3error_string
            sys.exit()
    return bucket_instance


def get_parts(manifest_filename):
    parts = []
    dom = minidom.parse(manifest_filename)
    manifest_elem = dom.getElementsByTagName('manifest')[0]
    parts_list = manifest_elem.getElementsByTagName('filename')
    for part_elem in parts_list:
        nodes = part_elem.childNodes
        for node in nodes:
            if node.nodeType == node.TEXT_NODE:
                parts.append(node.data)
    return parts


def get_relative_filename(filename):
    f_parts = filename.split('/')
    return f_parts[len(f_parts) - 1]


def upload_manifest(bucket_instance, manifest_filename,
                    canned_acl=None):
    print 'Uploading manifest file'
    k = Key(bucket_instance)
    k.key = get_relative_filename(manifest_filename)
    manifest_file = open(manifest_filename, 'rb')
    try:
        k.set_contents_from_file(manifest_file, policy=canned_acl)
    except S3ResponseError, s3error:
        s3error_string = '%s' % s3error
        if s3error_string.find('403') >= 0:
            print 'Permission denied while writing:', k.key
        else:
            print s3error_string
        sys.exit()


def upload_parts(
    bucket_instance,
    directory,
    parts,
    part_to_start_from,
    canned_acl=None,
    ):
    if part_to_start_from:
        okay_to_upload = False
    else:
        okay_to_upload = True

    for part in parts:
        if part == part_to_start_from:
            okay_to_upload = True
        if okay_to_upload:
            print 'Uploading part:', part
            k = Key(bucket_instance)
            k.key = part
            part_file = open(os.path.join(directory, part), 'rb')
            try:
                k.set_contents_from_file(part_file, policy=canned_acl)
            except S3ResponseError, s3error:
                s3error_string = '%s' % s3error
                if s3error_string.find('403') >= 0:
                    print 'Permission denied while writing:', k.key
                else:
                    print s3error_string
                sys.exit()


def main():
    euca = None
    try:
        euca = Euca2ool('b:m:a:s:d:', [
            'bucket=',
            'manifest=',
            'acl=',
            'ec2cert=',
            'directory=',
            'part=',
            'skipmanifest',
            ], is_s3=True)
    except Exception, e:
        print e
        usage()

    bucket = None
    manifest_path = None
    ec2cert_path = None
    directory = None
    part = None
    canned_acl = 'aws-exec-read'
    skipmanifest = False
    debug = False

    for (name, value) in euca.opts:
        if name in ('-h', '--help'):
            usage(0)
        elif name in ('-b', '--bucket'):
            bucket = value
        elif name in ('-m', '--manifest'):
            manifest_path = value
        elif name == '--ec2cert':
            ec2cert_path = value
        elif name == '--acl':
            canned_acl = value
        elif name in ('-d', '--directory'):
            directory = value
        elif name == '--part':
            part = value
        elif name == '--url':
            url = value
        elif name == '--skipmanifest':
            skipmanifest = True
        elif name == '--version':
            version()

    if bucket and manifest_path:
        try:
            euca.validate_file(manifest_path)
        except FileValidationError:
            print 'Invalid manifest'
            sys.exit(1)

        try:
            conn = euca.make_connection()
        except ConnectionFailed, e:
            print e.message
            sys.exit(1)

        bucket_instance = ensure_bucket(conn, bucket, canned_acl)
        parts = get_parts(manifest_path)
        if not directory:
            manifest_path_parts = manifest_path.split('/')
            directory = \
                manifest_path.replace(manifest_path_parts[len(manifest_path_parts)
                    - 1], '')
        if not skipmanifest and not part:
            upload_manifest(bucket_instance, manifest_path, canned_acl)
        upload_parts(bucket_instance, directory, parts, part,
                     canned_acl)
        print 'Uploaded image as %s/%s' % (bucket,
                get_relative_filename(manifest_path))
    else:
        if not bucket:
            print 'bucket must be specified.'
        if not manifest_path:
            print 'manifest must be specified.'
        usage()


if __name__ == '__main__':
    main()

