def create(vm_):
    '''
    Create a single Packet VM.
    '''
    name = vm_['name']

    if not is_profile_configured(vm_):
        return False

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

    log.info('Creating Packet VM %s', name)

    manager = packet.Manager(auth_token=vm_['token'])

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

    device = manager.create_device(project_id=vm_['project_id'],
                                hostname=name,
                                plan=vm_['size'], facility=vm_['location'],
                                operating_system=vm_['image'])

    device = _wait_for_status('device', device.id, status="active")

    if device.state != "active":
        log.error(
            'Error creating %s on PACKET\n\n'
            'while waiting for initial ready status',
            name, exc_info_on_loglevel=logging.DEBUG
        )

    # Define which ssh_interface to use
    ssh_interface = _get_ssh_interface(vm_)

    # Pass the correct IP address to the bootstrap ssh_host key
    if ssh_interface == 'private_ips':
        for ip in device.ip_addresses:
            if ip['public'] is False:
                vm_['ssh_host'] = ip['address']
                break
    else:
        for ip in device.ip_addresses:
            if ip['public'] is True:
                vm_['ssh_host'] = ip['address']
                break

    key_filename = config.get_cloud_config_value(
        'private_key', vm_, __opts__, search_global=False, default=None
    )

    vm_['key_filename'] = key_filename

    vm_['private_key'] = key_filename

    # Bootstrap!
    ret = __utils__['cloud.bootstrap'](vm_, __opts__)

    ret.update({'device': device.__dict__})

    if vm_.get('storage_tier') and vm_.get('storage_size'):
        # create storage and attach it to device

        volume = manager.create_volume(
            vm_['project_id'], "{0}_storage".format(name), vm_.get('storage_tier'),
            vm_.get('storage_size'), vm_.get('location'), snapshot_count=vm_.get('storage_snapshot_count', 0),
            snapshot_frequency=vm_.get('storage_snapshot_frequency'))

        volume.attach(device.id)

        volume = _wait_for_status('volume', volume.id, status="active")

        if volume.state != "active":
            log.error(
                'Error creating %s on PACKET\n\n'
                'while waiting for initial ready status',
                name, exc_info_on_loglevel=logging.DEBUG
            )

        ret.update({'volume': volume.__dict__})

    log.info('Created Cloud VM \'%s\'', name)

    log.debug(
        '\'%s\' VM creation details:\n%s',
        name, pprint.pformat(device.__dict__)
    )

    __utils__['cloud.fire_event'](
        'event',
        'created instance',
        'salt/cloud/{0}/created'.format(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_ ) : name = vm_ [ 'name' ] if not is_profile_configured ( vm_ ) : return False __utils__ [ 'cloud.fire_event' ] ( 'event' , 'starting create' , 'salt/cloud/{0}/creating' . format ( name ) , args = __utils__ [ 'cloud.filter_event' ] ( 'creating' , vm_ , [ 'name' , 'profile' , 'provider' , 'driver' ] ) , sock_dir = __opts__ [ 'sock_dir' ] , transport = __opts__ [ 'transport' ] ) log . info ( 'Creating Packet VM %s' , name ) manager = packet . Manager ( auth_token = vm_ [ 'token' ] ) __utils__ [ 'cloud.fire_event' ] ( 'event' , 'requesting instance' , 'salt/cloud/{0}/requesting' . format ( vm_ [ 'name' ] ) , args = __utils__ [ 'cloud.filter_event' ] ( 'requesting' , vm_ , [ 'name' , 'profile' , 'provider' , 'driver' ] ) , sock_dir = __opts__ [ 'sock_dir' ] , transport = __opts__ [ 'transport' ] ) device = manager . create_device ( project_id = vm_ [ 'project_id' ] , hostname = name , plan = vm_ [ 'size' ] , facility = vm_ [ 'location' ] , operating_system = vm_ [ 'image' ] ) device = _wait_for_status ( 'device' , device . id , status = "active" ) if device . state != "active" : log . error ( 'Error creating %s on PACKET\n\n' 'while waiting for initial ready status' , name , exc_info_on_loglevel = logging . DEBUG ) ssh_interface = _get_ssh_interface ( vm_ ) if ssh_interface == 'private_ips' : for ip in device . ip_addresses : if ip [ 'public' ] is False : vm_ [ 'ssh_host' ] = ip [ 'address' ] break else : for ip in device . ip_addresses : if ip [ 'public' ] is True : vm_ [ 'ssh_host' ] = ip [ 'address' ] break key_filename = config . get_cloud_config_value ( 'private_key' , vm_ , __opts__ , search_global = False , default = None ) vm_ [ 'key_filename' ] = key_filename vm_ [ 'private_key' ] = key_filename ret = __utils__ [ 'cloud.bootstrap' ] ( vm_ , __opts__ ) ret . update ( { 'device' : device . __dict__ } ) if vm_ . get ( 'storage_tier' ) and vm_ . get ( 'storage_size' ) : volume = manager . create_volume ( vm_ [ 'project_id' ] , "{0}_storage" . format ( name ) , vm_ . get ( 'storage_tier' ) , vm_ . get ( 'storage_size' ) , vm_ . get ( 'location' ) , snapshot_count = vm_ . get ( 'storage_snapshot_count' , 0 ) , snapshot_frequency = vm_ . get ( 'storage_snapshot_frequency' ) ) volume . attach ( device . id ) volume = _wait_for_status ( 'volume' , volume . id , status = "active" ) if volume . state != "active" : log . error ( 'Error creating %s on PACKET\n\n' 'while waiting for initial ready status' , name , exc_info_on_loglevel = logging . DEBUG ) ret . update ( { 'volume' : volume . __dict__ } ) log . info ( 'Created Cloud VM \'%s\'' , name ) log . debug ( '\'%s\' VM creation details:\n%s' , name , pprint . pformat ( device . __dict__ ) ) __utils__ [ 'cloud.fire_event' ] ( 'event' , 'created instance' , 'salt/cloud/{0}/created' . format ( 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 Packet VM.