Ejemplo n.º 1
0
def destroy(name):
    '''
    Wrap core libcloudfuncs destroy method, adding check for termination
    protection
    '''
    ret = {}

    newname = name
    if 'AWS.rename_on_destroy' in __opts__:
        if __opts__['AWS.rename_on_destroy'] is True:
            newname = '{0}-DEL{1}'.format(name, uuid.uuid4().hex)
            rename(name, kwargs={'newname': newname}, call='action')
            log.info('Machine will be identified as {0} until it has been '
                     'cleaned up by AWS.')

    from saltcloud.libcloudfuncs import destroy as libcloudfuncs_destroy
    location = get_location()
    conn = get_conn(location=location)
    libcloudfuncs_destroy = namespaced_function(
        libcloudfuncs_destroy, globals(), (conn,)
    )
    try:
        result = libcloudfuncs_destroy(newname, conn)
        ret[name] = result
    except Exception as e:
        if e.message.startswith('OperationNotPermitted'):
            log.info(
                'Failed: termination protection is enabled on {0}'.format(
                    name
                )
            )
        else:
            raise e

    return ret
Ejemplo n.º 2
0
def __virtual__():
    '''
    Set up the libcloud funcstions and check for AWS configs
    '''
    try:
        import botocore
        # Since we have botocore, we won't load the libcloud AWS module
        return False
    except ImportError:
        pass

    confs = [
        'AWS.id',
        'AWS.key',
        'AWS.keyname',
        'AWS.securitygroup',
        'AWS.private_key',
    ]
    for conf in confs:
        if conf not in __opts__:
            log.warning(
                '{0!r} not found in options. Not loading module.'.format(conf)
            )
            return False

    if not os.path.exists(__opts__['AWS.private_key']):
        raise SaltException(
            'The AWS key file {0} does not exist\n'.format(
                __opts__['AWS.private_key']
            )
        )
    keymode = str(
        oct(stat.S_IMODE(os.stat(__opts__['AWS.private_key']).st_mode))
    )
    if keymode not in ('0400', '0600'):
        raise SaltException(
            'The AWS key file {0} needs to be set to mode 0400 or '
            '0600\n'.format(
                __opts__['AWS.private_key']
            )
        )

    global avail_images, avail_sizes, script, destroy, list_nodes
    global list_nodes_full, list_nodes_select

    # open a connection in a specific region
    conn = get_conn(**{'location': get_location()})

    # Init the libcloud functions
    avail_images = namespaced_function(avail_images, globals(), (conn,))
    avail_sizes = namespaced_function(avail_sizes, globals(), (conn,))
    script = namespaced_function(script, globals(), (conn,))
    list_nodes = namespaced_function(list_nodes, globals(), (conn,))
    list_nodes_full = namespaced_function(list_nodes_full, globals(), (conn,))
    list_nodes_select = namespaced_function(list_nodes_select, globals(), (conn,))

    log.debug('Loading Libcloud AWS cloud module')
    return 'aws'
Ejemplo n.º 3
0
import sys
import logging

# Import generic libcloud functions
from saltcloud.libcloudfuncs import *

# Import salt cloud utils
from saltcloud.utils import namespaced_function

# Get logging started
log = logging.getLogger(__name__)

# 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 = namespaced_function(avail_images, globals())
avail_sizes = namespaced_function(avail_sizes, globals())
script = namespaced_function(script, globals())
destroy = namespaced_function(destroy, globals())
list_nodes = namespaced_function(list_nodes, globals())
list_nodes_full = namespaced_function(list_nodes_full, globals())
list_nodes_select = namespaced_function(list_nodes_select, globals())


# Only load in this module is the GOGRID configurations are in place
def __virtual__():
    '''
    Set up the libcloud functions and check for GOGRID configs
    '''
    confs = [
            'apikey',
Ejemplo n.º 4
0
    log.info('Created Cloud VM {0[name]!r}'.format(vm_))
    log.debug('{0[name]!r} VM creation details:\n{1}'.format(
        vm_, pprint.pformat(node.__dict__)))

    ret.update(node.__dict__)

    saltcloud.utils.fire_event(
        'event',
        'created instance',
        'salt/cloud/{0}/created'.format(vm_['name']),
        {
            'name': vm_['name'],
            'profile': vm_['profile'],
            'provider': vm_['provider'],
        },
    )

    return ret


# TODO we shold be able to do something like this:
# script = namespaced_function(script, globals())
# however the globals don't seem to work.
def list_nodes(conn=None, call=None):
    if not conn:
        conn = get_conn()
    return saltcloud.libcloudfuncs.list_nodes(conn)


script = namespaced_function(script, globals())
Ejemplo n.º 5
0
import pprint
import logging

# Import libcloud
from libcloud.compute.base import NodeAuthPassword, NodeAuthSSHKey

# Import salt cloud libs
import saltcloud.config as config
from saltcloud.libcloudfuncs import *  # pylint: disable-msg=W0614,W0401
from saltcloud.utils import namespaced_function

# Get logging started
log = logging.getLogger(__name__)

# Redirect hostvirtual functions to this module namespace
get_size = namespaced_function(get_size, globals())
get_image = namespaced_function(get_image, globals())
avail_locations = namespaced_function(avail_locations, globals())
avail_images = namespaced_function(avail_images, globals())
avail_sizes = namespaced_function(avail_sizes, globals())
script = namespaced_function(script, globals())
destroy = namespaced_function(destroy, globals())
list_nodes = namespaced_function(list_nodes, globals())
list_nodes_full = namespaced_function(list_nodes_full, globals())
list_nodes_select = namespaced_function(list_nodes_select, globals())
show_instance = namespaced_function(show_instance, globals())


# Only load in this module if the HOSTVIRTUAL configurations are in place
def __virtual__():
    '''
Ejemplo n.º 6
0
            vm_, pprint.pformat(node.__dict__)
        )
    )

    ret.update(node.__dict__)

    saltcloud.utils.fire_event(
        'event',
        'created instance',
        'salt/cloud/{0}/created'.format(vm_['name']),
        {
            'name': vm_['name'],
            'profile': vm_['profile'],
            'provider': vm_['provider'],
        },
    )

    return ret


# TODO we shold be able to do something like this:
# script = namespaced_function(script, globals())
# however the globals don't seem to work.
def list_nodes(conn=None, call=None):
    if not conn:
        conn = get_conn()
    return saltcloud.libcloudfuncs.list_nodes(conn)

script = namespaced_function(script, globals())

Ejemplo n.º 7
0
def __virtual__():
    '''
    Set up the libcloud funcstions and check for AWS configs
    '''
    try:
        # Import botocore
        import botocore.session
    except ImportError:
        # Botocore is not available, the Libcloud AWS module will be loaded
        # instead.
        return False

    # "Patch" the imported libcloud_aws to have the current __opts__
    libcloud_aws.__opts__ = __opts__

    confs = [
        'AWS.id',
        'AWS.key',
        'AWS.keyname',
        'AWS.securitygroup',
        'AWS.private_key',
    ]
    for conf in confs:
        if conf not in __opts__:
            log.debug(
                '{0!r} not found in options. Not loading module.'.format(conf)
            )
            return False

    if not os.path.exists(__opts__['AWS.private_key']):
        raise SaltException(
            'The AWS key file {0} does not exist\n'.format(
                __opts__['AWS.private_key']
            )
        )
    keymode = str(
        oct(stat.S_IMODE(os.stat(__opts__['AWS.private_key']).st_mode))
    )
    if keymode not in ('0400', '0600'):
        raise SaltException(
            'The AWS key file {0} needs to be set to mode 0400 or '
            '0600\n'.format(
                __opts__['AWS.private_key']
            )
        )

    # Let's bring the functions imported from libcloud_aws to the current
    # namespace.
    keysdiff = set(POST_IMPORT_LOCALS_KEYS.keys()).difference(
        PRE_IMPORT_LOCALS_KEYS
    )
    for key in keysdiff:
        if not callable(POST_IMPORT_LOCALS_KEYS[key]):
            continue
        # skip callables that might be exceptions
        if any(['Error' in POST_IMPORT_LOCALS_KEYS[key].__name__,
                'Exception' in POST_IMPORT_LOCALS_KEYS[key].__name__]):
            continue
        globals().update(
            {
                key: namespaced_function(
                    POST_IMPORT_LOCALS_KEYS[key], globals(), ()
                )
            }
        )

    global avail_images, avail_sizes, script, destroy, list_nodes
    global list_nodes_full, list_nodes_select

    # open a connection in a specific region
    conn = get_conn(**{'location': get_location()})

    # Init the libcloud functions
    avail_images = namespaced_function(avail_images, globals(), (conn,))
    avail_sizes = namespaced_function(avail_sizes, globals(), (conn,))
    script = namespaced_function(script, globals(), (conn,))
    list_nodes = namespaced_function(list_nodes, globals(), (conn,))
    list_nodes_full = namespaced_function(list_nodes_full, globals(), (conn,))
    list_nodes_select = namespaced_function(list_nodes_select, globals(), (conn,))

    log.debug('Loading AWS botocore cloud module')
    return 'aws'
Ejemplo n.º 8
0
def __virtual__():
    '''
    Set up the libcloud funcstions and check for AWS configs
    '''
    try:
        # Import botocore
        import botocore.session
    except ImportError:
        # Botocore is not available, the Libcloud AWS module will be loaded
        # instead.
        log.debug(
            'The \'botocore\' library is not installed. The libcloud AWS '
            'support will be loaded instead.'
        )
        return False

    # "Patch" the imported libcloud_aws to have the current __opts__
    libcloud_aws.__opts__ = __opts__

    if get_configured_provider() is False:
        log.info(
            'There is no AWS cloud provider configuration available. Not '
            'loading module'
        )
        return False

    for provider, details in __opts__['providers'].iteritems():
        if 'provider' not in details or details['provider'] != 'aws':
            continue

        if not os.path.exists(details['private_key']):
            raise SaltCloudException(
                'The AWS key file {0!r} used in the {1!r} provider '
                'configuration does not exist\n'.format(
                    details['private_key'],
                    provider
                )
            )

        keymode = str(
            oct(stat.S_IMODE(os.stat(details['private_key']).st_mode))
        )
        if keymode not in ('0400', '0600'):
            raise SaltCloudException(
                'The AWS key file {0!r} used in the {1!r} provider '
                'configuration needs to be set to mode 0400 or 0600\n'.format(
                    details['private_key'],
                    provider
                )
            )

    # Let's bring the functions imported from libcloud_aws to the current
    # namespace.
    keysdiff = set(POST_IMPORT_LOCALS_KEYS.keys()).difference(
        PRE_IMPORT_LOCALS_KEYS
    )
    for key in keysdiff:
        if not callable(POST_IMPORT_LOCALS_KEYS[key]):
            continue
        # skip callables that might be exceptions
        if any(['Error' in POST_IMPORT_LOCALS_KEYS[key].__name__,
                'Exception' in POST_IMPORT_LOCALS_KEYS[key].__name__]):
            continue
        globals().update(
            {
                key: namespaced_function(
                    POST_IMPORT_LOCALS_KEYS[key], globals(), ()
                )
            }
        )

    global avail_images, avail_sizes, script, destroy, list_nodes
    global list_nodes_full, list_nodes_select

    # open a connection in a specific region
    conn = get_conn(**{'location': get_location()})

    # Init the libcloud functions
    avail_images = namespaced_function(avail_images, globals(), (conn,))
    avail_sizes = namespaced_function(avail_sizes, globals(), (conn,))
    script = namespaced_function(script, globals(), (conn,))
    list_nodes = namespaced_function(list_nodes, globals(), (conn,))
    list_nodes_full = namespaced_function(list_nodes_full, globals(), (conn,))
    list_nodes_select = namespaced_function(
        list_nodes_select, globals(), (conn,)
    )

    log.debug('Loading AWS botocore cloud module')
    return 'aws'