Example #1
0
def destroy(name, call=None):
    """
    This function irreversibly destroys a virtual machine on the cloud provider.
    Before doing so, it should fire an event on the Salt event bus.

    The tag for this event is `salt/cloud/<vm name>/destroying`.
    Once the virtual machine has been destroyed, another event is fired.
    The tag for that event is `salt/cloud/<vm name>/destroyed`.

    Dependencies:
        list_nodes

    @param name:
    @type name: str
    @param call:
    @type call:
    @return: True if all went well, otherwise an error message
    @rtype: bool|str
    """
    log.info("Attempting to delete instance %s", name)
    if not vb_machine_exists(name):
        return "{0} doesn't exist and can't be deleted".format(name)

    cloud.fire_event('event',
                     'destroying instance',
                     'salt/cloud/{0}/destroying'.format(name), {'name': name},
                     transport=__opts__['transport'])

    vb_destroy_machine(name)

    cloud.fire_event('event',
                     'destroyed instance',
                     'salt/cloud/{0}/destroyed'.format(name), {'name': name},
                     transport=__opts__['transport'])
Example #2
0
def destroy(name, call=None):
    """
    This function irreversibly destroys a virtual machine on the cloud provider.
    Before doing so, it should fire an event on the Salt event bus.

    The tag for this event is `salt/cloud/<vm name>/destroying`.
    Once the virtual machine has been destroyed, another event is fired.
    The tag for that event is `salt/cloud/<vm name>/destroyed`.

    Dependencies:
        list_nodes

    @param name:
    @type name: str
    @param call:
    @type call:
    @return: True if all went well, otherwise an error message
    @rtype: bool|str
    """
    log.info("Attempting to delete instance %s", name)
    if not vb_machine_exists(name):
        return "{0} doesn't exist and can't be deleted".format(name)

    cloud.fire_event(
        'event',
        'destroying instance',
        'salt/cloud/{0}/destroying'.format(name),
        args={'name': name},
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )

    vb_destroy_machine(name)

    cloud.fire_event(
        'event',
        'destroyed instance',
        'salt/cloud/{0}/destroyed'.format(name),
        args={'name': name},
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )
Example #3
0
def create(container):
    config = sac.cloud_config('/etc/salt/cloud')
    master = sac.master_config('/etc/salt/master')
    api_key = config['providers']['my-nova']['nova']['api_key']
    ident = config['providers']['my-nova']['nova']['identity_url']
    tenant = config['providers']['my-nova']['nova']['tenant']
    user = config['providers']['my-nova']['nova']['user']
    region = config['providers']['my-nova']['nova']['compute_region']
    headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
    payload={
      "auth": {
        "RAX-KSKEY:apiKeyCredentials": {
          "username": user,
          "apiKey": api_key
        }
      }
    }
    identity = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json().get('access', {})
    headers['X-Auth-Token'] = identity['token']['id']
    catalog = identity['serviceCatalog']

    endpoint = _get_endpoint('object-store', region, catalog)

    containers = requests.get(endpoint, headers=headers).json()
    for c in containers:
        if c['name'] == container:
            log.debug('Container already exists!: {1}'.format(region, container))
            fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container))
            return None

    ret = requests.put('{0}/{1}'.format(endpoint, container), headers=headers)
    if ret.status_code == 201:
        fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container))
        return True
    else:
        log.debug('Failed to create container: {0}'.format(container))
        return False
Example #4
0
def create(vm_info):
    """
    Creates a virtual machine from the given VM information.
    This is what is used to request a virtual machine to be created by the
    cloud provider, wait for it to become available,
    and then (optionally) log in and install Salt on it.

    Fires:
        "starting create" : This event is tagged salt/cloud/<vm name>/creating.
        The payload contains the names of the VM, profile and provider.

    @param vm_info
            {
                name: <str>
                profile: <dict>
                driver: <provider>:<profile>
                clonefrom: <vm_name>
            }
    @type vm_info dict
    @return dict of resulting vm. !!!Passwords can and should be included!!!
    """
    try:
        # Check for required profile parameters before sending any API calls.
        if vm_info['profile'] and config.is_profile_configured(
                __opts__, __active_provider_name__ or 'virtualbox',
                vm_info['profile']) is False:
            return False
    except AttributeError:
        pass

    vm_name = vm_info["name"]
    deploy = config.get_cloud_config_value('deploy',
                                           vm_info,
                                           __opts__,
                                           search_global=False,
                                           default=True)
    wait_for_ip_timeout = config.get_cloud_config_value('wait_for_ip_timeout',
                                                        vm_info,
                                                        __opts__,
                                                        default=60)
    boot_timeout = config.get_cloud_config_value('boot_timeout',
                                                 vm_info,
                                                 __opts__,
                                                 default=60 * 1000)
    power = config.get_cloud_config_value('power_on',
                                          vm_info,
                                          __opts__,
                                          default=False)
    key_filename = config.get_cloud_config_value('private_key',
                                                 vm_info,
                                                 __opts__,
                                                 search_global=False,
                                                 default=None)

    log.debug("Going to fire event: starting create")
    cloud.fire_event('event',
                     'starting create',
                     'salt/cloud/{0}/creating'.format(vm_info['name']), {
                         'name': vm_info['name'],
                         'profile': vm_info['profile'],
                         'driver': vm_info['driver'],
                     },
                     transport=__opts__['transport'])

    # to create the virtual machine.
    request_kwargs = {
        'name': vm_info['name'],
        'clone_from': vm_info['clonefrom']
    }

    cloud.fire_event('event',
                     'requesting instance',
                     'salt/cloud/{0}/requesting'.format(vm_info['name']),
                     request_kwargs,
                     transport=__opts__['transport'])
    vm_result = vb_clone_vm(**request_kwargs)

    # Booting and deploying if needed
    if power:
        vb_start_vm(vm_name, timeout=boot_timeout)
        ips = vb_wait_for_network_address(wait_for_ip_timeout,
                                          machine_name=vm_name)

        if len(ips):
            ip = ips[0]
            log.info("[ {0} ] IPv4 is: {1}".format(vm_name, ip))
            # ssh or smb using ip and install salt only if deploy is True
            if deploy:
                vm_info['key_filename'] = key_filename
                vm_info['ssh_host'] = ip

                res = cloud.bootstrap(vm_info, __opts__)
                vm_result.update(res)

    cloud.fire_event('event',
                     'created machine',
                     'salt/cloud/{0}/created'.format(vm_info['name']),
                     vm_result,
                     transport=__opts__['transport'])

    # Passwords should be included in this object!!
    return vm_result
Example #5
0
def create(vm_info):
    """
    Creates a virtual machine from the given VM information.
    This is what is used to request a virtual machine to be created by the
    cloud provider, wait for it to become available,
    and then (optionally) log in and install Salt on it.

    Fires:
        "starting create" : This event is tagged salt/cloud/<vm name>/creating.
        The payload contains the names of the VM, profile and provider.

    @param vm_info
            {
                name: <str>
                profile: <dict>
                driver: <provider>:<profile>
                clonefrom: <vm_name>
            }
    @type vm_info dict
    @return dict of resulting vm. !!!Passwords can and should be included!!!
    """
    try:
        # Check for required profile parameters before sending any API calls.
        if vm_info['profile'] and config.is_profile_configured(
            __opts__,
                __active_provider_name__ or 'virtualbox',
            vm_info['profile']
        ) is False:
            return False
    except AttributeError:
        pass

    vm_name = vm_info["name"]
    deploy = config.get_cloud_config_value(
        'deploy', vm_info, __opts__, search_global=False, default=True
    )
    wait_for_ip_timeout = config.get_cloud_config_value(
        'wait_for_ip_timeout', vm_info, __opts__, default=60
    )
    boot_timeout = config.get_cloud_config_value(
        'boot_timeout', vm_info, __opts__, default=60 * 1000
    )
    power = config.get_cloud_config_value(
        'power_on', vm_info, __opts__, default=False
    )
    key_filename = config.get_cloud_config_value(
        'private_key', vm_info, __opts__, search_global=False, default=None
    )

    log.debug("Going to fire event: starting create")
    cloud.fire_event(
        'event',
        'starting create',
        'salt/cloud/{0}/creating'.format(vm_info['name']),
        args={
            'name': vm_info['name'],
            'profile': vm_info['profile'],
            'driver': vm_info['driver'],
        },
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )

    # to create the virtual machine.
    request_kwargs = {
        'name': vm_info['name'],
        'clone_from': vm_info['clonefrom']
    }

    cloud.fire_event(
        'event',
        'requesting instance',
        'salt/cloud/{0}/requesting'.format(vm_info['name']),
        args=request_kwargs,
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )
    vm_result = vb_clone_vm(**request_kwargs)

    # Booting and deploying if needed
    if power:
        vb_start_vm(vm_name, timeout=boot_timeout)
        ips = vb_wait_for_network_address(wait_for_ip_timeout, machine_name=vm_name)

        if len(ips):
            ip = ips[0]
            log.info("[ {0} ] IPv4 is: {1}".format(vm_name, ip))
            # ssh or smb using ip and install salt only if deploy is True
            if deploy:
                vm_info['key_filename'] = key_filename
                vm_info['ssh_host'] = ip

                res = cloud.bootstrap(vm_info, __opts__)
                vm_result.update(res)

    cloud.fire_event(
        'event',
        'created machine',
        'salt/cloud/{0}/created'.format(vm_info['name']),
        args=vm_result,
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )

    # Passwords should be included in this object!!
    return vm_result