'''
The generic libcloud template used to create the connections and deploy the
cloud virtual machines
'''

# Import python libs
import os
import sys
import subprocess

# Import libcloud
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.compute.deployment import MultiStepDeployment, ScriptDeployment, SSHKeyDeployment

# Import salt libs
import saltcloud.utils
from saltcloud.libcloudfuncs import *

# Import paramiko
import paramiko

def get_conn():
    '''
    Return a conn object for the passed vm data
    '''
    prov = 'EC2'
    if not hasattr(Provider, prov):
        return None
    driver = get_driver(getattr(Provider, 'EC2'))
    return driver(
            __opts__['EC2.id'],
            __opts__['EC2.key'],
            )


def keyname(vm_):
    '''
    Return the keyname
    '''
    return vm_.get('EC2.keyname', __opts__.get('EC2.keyname', ''))


def securitygroup(vm_):
    '''
    Return the keyname
    '''
    return vm_.get(
            'EC2.securitygroup',
            __opts__.get('EC2.securitygroup', 'default')
            )


def create(vm_):
    '''
    Create a single vm from a data dict
    '''
    print('Creating Cloud VM {0}'.format(vm_['name']))
    conn = get_conn()
    kwargs = {'ssh_username': 'ec2-user',
              'ssh_key': __opts__['EC2.private_key']}
    kwargs['name'] = vm_['name']
    kwargs['deploy'] = script(vm_)
    kwargs['image'] = get_image(conn, vm_)
    kwargs['size'] = get_size(conn, vm_)
    ex_keyname = keyname(vm_)
    if ex_keyname:
        kwargs['ex_keyname'] = ex_keyname
    ex_securitygroup = securitygroup(vm_)
    if ex_securitygroup:
        kwargs['ex_securitygroup'] = ex_securitygroup
    try:
        data = conn.deploy_node(**kwargs)
    except Exception as exc:
        err = ('The following exception was thrown by libcloud when trying to '
               'run the initial deployment: \n{0}\n\nThe vm {1} has been '
               'created but Salt could not be intsalled. Please verify that '
               'your ssh keys are in order and that the security group is '
               'accepting inbound connections from port 22.\n').format(
                       exc, vm_['name']
                       )
        sys.stderr.write(err)
        return False
    cmd = ('ssh -oStrictHostKeyChecking=no -t -i {0} {1}@{2} "sudo '
           '/home/ec2-user/deployment.sh"').format(
                   __opts__['EC2.private_key'],
                   'ec2-user',
                   data.public_ips[0]
                   )
    subprocess.call(cmd, shell=True)
    print('Created Cloud VM {0} with the following values:'.format(
        vm_['name']
        ))
    for key, val in data.__dict__.items():
        print('  {0}: {1}'.format(key, val))

@@ -5,102 +5,648 @@
 The 
-generic libcloud template used to create the connections and deploy the%0Acloud virtual machines
+EC2 cloud module%0A====================%0A%0AThe EC2 cloud module is used to interact with the amazon Web Services system.%0A%0ATo use the EC2 cloud module the following configuration parameters need to be%0Aset in the main cloud config:%0A%0A.. code-block:: yaml%0A%0A    # The EC2 API authentication id%0A    EC2.id: GKTADJGHEIQSXMKKRBJ08H%0A    # The EC2 API authentication key%0A    EC2.key: askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs%0A    # The ssh keyname to use%0A    EC2.keyname: default%0A    # The amazon security group%0A    EC2.securitygroup: ssh_open%0A    # The location of the private key which corresponds to the keyname%0A    EC2.private_key: /root/default.pem%0A
 %0A'''
@@ -689,16 +689,29 @@
 ort sys%0A
+import types%0A
 import s
@@ -1029,16 +1029,16 @@
 aramiko%0A
-
 import p
@@ -1046,16 +1046,736 @@
 ramiko%0A%0A
+# Init the libcloud functions%0Aavail_images = types.FunctionType(avail_images.__code__, globals())%0Aavail_sizes = types.FunctionType(avail_sizes.__code__, globals())%0Adestroy = types.FunctionType(destroy.__code__, globals())%0Alist_nodes = types.FunctionType(list_nodes.__code__, globals())%0A%0A%0A# Only load in this module if the EC2 configurations are in place%0Adef __virtual__():%0A    '''%0A    Set up the libcloud funcstions and check for RACKSPACE configs%0A    '''%0A    confs = %5B%0A            'EC2.id',%0A            'EC2.key',%0A            'EC2.keyname',%0A            'EC2.securitygroup',%0A            'EC2.private_key',%0A            %5D%0A    for conf in confs:%0A        if conf not in __opts__:%0A            return False%0A    return 'EC2'%0A%0A%0A
 def get_

update EC2 module with libcloud changes