'''
Rackspace Cloud Module
======================

The Rackspace cloud module. This module uses the preferred means to set up a
libcloud based cloud module and should be used as the general template for
setting up additional libcloud based modules.

The rackspace cloud module interfaces with the Rackspace public cloud service
and requires that two configuration paramaters be set for use:

.. code-block:: yaml

    # The Rackspace login user
    RACKSPACE.user: fred
    # The Rackspace user's apikey
    RACKSPACE.apikey: 901d3f579h23c8v73q9

'''

# The import section is mostly libcloud boilerplate

# Import python libs
import os
import types

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

# Import generic libcloud functions
from saltcloud.libcloudfuncs import *

# Some of the libcloud functions need to be in the same namespace as the
# functions defined in the module, so we create new function objects inside
# this module namespace
avail_images = types.FunctionType(avail_images.__code__, globals())
avail_sizes = types.FunctionType(avail_sizes.__code__, globals())
script = types.FunctionType(script.__code__, globals())
destroy = types.FunctionType(destroy.__code__, globals())
list_nodes = types.FunctionType(list_nodes.__code__, globals())


# Only load in this module is the RACKSPACE configurations are in place
def __virtual__():
    '''
    Set up the libcloud funcstions and check for RACKSPACE configs
    '''
    if 'RACKSPACE.user' in __opts__ and 'RACKSPACE.apikey' in __opts__:
        return 'rackspace'
    return False


def get_conn():
    '''
    Return a conn object for the passed vm data
    '''
    driver = get_driver(Provider.RACKSPACE)
    return driver(
            __opts__['RACKSPACE.user'],
            __opts__['RACKSPACE.apikey'],
            )


def create(vm_):
    '''
    Create a single vm from a data dict
    '''
    print('Creating Cloud VM {0}'.format(vm_['name']))
    conn = get_conn()
    kwargs = {}
    kwargs['name'] = vm_['name']
    kwargs['deploy'] = script(vm_)
    kwargs['image'] = get_image(conn, vm_)
    kwargs['size'] = get_size(conn, vm_)
    try:
        data = conn.deploy_node(**kwargs)
    except DeploymentError as exc:
        err = ('Error creating {0} on RACKSPACE\n\n'
               'The following exception was thrown by libcloud when trying to '
               'run the initial deployment: \n{1}').format(
                       vm_['name'], exc
                       )
        sys.stderr.write(err)
        return False
    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))

@@ -641,16 +641,32 @@
 rt types
+%0Aimport paramiko
 %0A%0A# Impo
@@ -2114,16 +2114,48 @@
 _conn()%0A
+    deploy_script = script(vm_)%0A
     kwar
@@ -2199,43 +2199,8 @@
 e'%5D%0A
-    kwargs%5B'deploy'%5D = script(vm_)%0A
     
@@ -2308,22 +2308,22 @@
  = conn.
-deploy
+create
 _node(**
@@ -2674,16 +2674,420 @@
 n False%0A
+    if saltcloud.utils.wait_for_ssh(data.public_ips%5B0%5D):%0A        ssh = paramiko.SSHClient()%0A        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())%0A        ssh.connect(data.public_ips%5B0%5D, username='root', password=data.extra%5B'password'%5D)%0A        stdin, stdout, stderr = ssh.exec_command(deploy_script.script)%0A    else:%0A        print('Failed to start Salt on Cloud VM %7B0%7D'.format(vm_%5B'name'%5D))%0A%0A
     prin

Switch rackspace from using libcloud deploy to use salt-cloud wait_for_ssh