def create(vm_):
    '''
    Create a single VM from a data dict.
    '''
    try:
        if vm_['profile'] and config.is_profile_configured(
            __opts__,
            __active_provider_name__ or 'azurearm',
            vm_['profile'],
            vm_=vm_
        ) is False:
            return False
    except AttributeError:
        pass

    if vm_.get('bootstrap_interface') is None:
        vm_['bootstrap_interface'] = 'public'

    __utils__['cloud.fire_event'](
        'event',
        'starting create',
        'salt/cloud/{0}/creating'.format(vm_['name']),
        args=__utils__['cloud.filter_event'](
            'creating', vm_, ['name', 'profile', 'provider', 'driver']
        ),
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )
    __utils__['cloud.cachedir_index_add'](
        vm_['name'], vm_['profile'], 'azurearm', vm_['driver']
    )
    if not vm_.get('location'):
        vm_['location'] = get_location(kwargs=vm_)

    log.info('Creating Cloud VM %s in %s', vm_['name'], vm_['location'])

    vm_request = request_instance(vm_=vm_)

    if not vm_request or 'error' in vm_request:
        err_message = 'Error creating VM {0}! ({1})'.format(vm_['name'], six.text_type(vm_request))
        log.error(err_message)
        raise SaltCloudSystemExit(err_message)

    def _query_node_data(name, bootstrap_interface):
        '''
        Query node data.
        '''
        data = show_instance(name, call='action')
        if not data:
            return False
        ip_address = None
        if bootstrap_interface == 'public':
            ip_address = data['public_ips'][0]
        if bootstrap_interface == 'private':
            ip_address = data['private_ips'][0]
        if ip_address is None:
            return False
        return ip_address

    try:
        data = salt.utils.cloud.wait_for_ip(
            _query_node_data,
            update_args=(vm_['name'], vm_['bootstrap_interface'],),
            timeout=config.get_cloud_config_value(
                'wait_for_ip_timeout', vm_, __opts__, default=10 * 60),
            interval=config.get_cloud_config_value(
                'wait_for_ip_interval', vm_, __opts__, default=10),
            interval_multiplier=config.get_cloud_config_value(
                'wait_for_ip_interval_multiplier', vm_, __opts__, default=1),
        )
    except (
        SaltCloudExecutionTimeout,
        SaltCloudExecutionFailure,
        SaltCloudSystemExit
    ) as exc:
        try:
            log.warning(exc)
        finally:
            raise SaltCloudSystemExit(six.text_type(exc))

    vm_['ssh_host'] = data
    if not vm_.get('ssh_username'):
        vm_['ssh_username'] = config.get_cloud_config_value(
            'ssh_username', vm_, __opts__
        )
    vm_['password'] = config.get_cloud_config_value(
        'ssh_password', vm_, __opts__
    )
    ret = __utils__['cloud.bootstrap'](vm_, __opts__)

    data = show_instance(vm_['name'], call='action')
    log.info('Created Cloud VM \'%s\'', vm_['name'])
    log.debug(
        '\'%s\' VM creation details:\n%s',
        vm_['name'],
        pprint.pformat(data)
    )

    ret.update(data)

    __utils__['cloud.fire_event'](
        'event',
        'created instance',
        'salt/cloud/{0}/created'.format(vm_['name']),
        args=__utils__['cloud.filter_event'](
            'created',
            vm_, ['name', 'profile', 'provider', 'driver']
        ),
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )

    return ret
def create ( vm_ ) : try : if vm_ [ 'profile' ] and config . is_profile_configured ( __opts__ , __active_provider_name__ or 'azurearm' , vm_ [ 'profile' ] , vm_ = vm_ ) is False : return False except AttributeError : pass if vm_ . get ( 'bootstrap_interface' ) is None : vm_ [ 'bootstrap_interface' ] = 'public' __utils__ [ 'cloud.fire_event' ] ( 'event' , 'starting create' , 'salt/cloud/{0}/creating' . format ( vm_ [ 'name' ] ) , args = __utils__ [ 'cloud.filter_event' ] ( 'creating' , vm_ , [ 'name' , 'profile' , 'provider' , 'driver' ] ) , sock_dir = __opts__ [ 'sock_dir' ] , transport = __opts__ [ 'transport' ] ) __utils__ [ 'cloud.cachedir_index_add' ] ( vm_ [ 'name' ] , vm_ [ 'profile' ] , 'azurearm' , vm_ [ 'driver' ] ) if not vm_ . get ( 'location' ) : vm_ [ 'location' ] = get_location ( kwargs = vm_ ) log . info ( 'Creating Cloud VM %s in %s' , vm_ [ 'name' ] , vm_ [ 'location' ] ) vm_request = request_instance ( vm_ = vm_ ) if not vm_request or 'error' in vm_request : err_message = 'Error creating VM {0}! ({1})' . format ( vm_ [ 'name' ] , six . text_type ( vm_request ) ) log . error ( err_message ) raise SaltCloudSystemExit ( err_message ) def _query_node_data ( name , bootstrap_interface ) : data = show_instance ( name , call = 'action' ) if not data : return False ip_address = None if bootstrap_interface == 'public' : ip_address = data [ 'public_ips' ] [ 0 ] if bootstrap_interface == 'private' : ip_address = data [ 'private_ips' ] [ 0 ] if ip_address is None : return False return ip_address try : data = salt . utils . cloud . wait_for_ip ( _query_node_data , update_args = ( vm_ [ 'name' ] , vm_ [ 'bootstrap_interface' ] , ) , timeout = config . get_cloud_config_value ( 'wait_for_ip_timeout' , vm_ , __opts__ , default = 10 * 60 ) , interval = config . get_cloud_config_value ( 'wait_for_ip_interval' , vm_ , __opts__ , default = 10 ) , interval_multiplier = config . get_cloud_config_value ( 'wait_for_ip_interval_multiplier' , vm_ , __opts__ , default = 1 ) , ) except ( SaltCloudExecutionTimeout , SaltCloudExecutionFailure , SaltCloudSystemExit ) as exc : try : log . warning ( exc ) finally : raise SaltCloudSystemExit ( six . text_type ( exc ) ) vm_ [ 'ssh_host' ] = data if not vm_ . get ( 'ssh_username' ) : vm_ [ 'ssh_username' ] = config . get_cloud_config_value ( 'ssh_username' , vm_ , __opts__ ) vm_ [ 'password' ] = config . get_cloud_config_value ( 'ssh_password' , vm_ , __opts__ ) ret = __utils__ [ 'cloud.bootstrap' ] ( vm_ , __opts__ ) data = show_instance ( vm_ [ 'name' ] , call = 'action' ) log . info ( 'Created Cloud VM \'%s\'' , vm_ [ 'name' ] ) log . debug ( '\'%s\' VM creation details:\n%s' , vm_ [ 'name' ] , pprint . pformat ( data ) ) ret . update ( data ) __utils__ [ 'cloud.fire_event' ] ( 'event' , 'created instance' , 'salt/cloud/{0}/created' . format ( vm_ [ 'name' ] ) , args = __utils__ [ 'cloud.filter_event' ] ( 'created' , vm_ , [ 'name' , 'profile' , 'provider' , 'driver' ] ) , sock_dir = __opts__ [ 'sock_dir' ] , transport = __opts__ [ 'transport' ] ) return ret
Create a single VM from a data dict.