<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import logging <EOL> import socket <EOL> import pprint <EOL> import yaml <EOL> import salt . ext . six as six <EOL> import salt . utils <EOL> import salt . client <EOL> from salt . utils . openstack import nova <EOL> try : <EOL> import novaclient . exceptions <EOL> except ImportError as exc : <EOL> pass <EOL> from salt . cloud . libcloudfuncs import * <EOL> import salt . utils . cloud <EOL> import salt . utils . pycrypto as sup <EOL> import salt . config as config <EOL> from salt . utils import namespaced_function <EOL> from salt . exceptions import ( <EOL> SaltCloudConfigError , <EOL> SaltCloudNotFound , <EOL> SaltCloudSystemExit , <EOL> SaltCloudExecutionFailure , <EOL> SaltCloudExecutionTimeout <EOL> ) <EOL> try : <EOL> from netaddr import all_matching_cidrs <EOL> HAS_NETADDR = True <EOL> except ImportError : <EOL> HAS_NETADDR = False <EOL> log = logging . getLogger ( __name__ ) <EOL> request_log = logging . getLogger ( '<STR_LIT>' ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> script = namespaced_function ( script , globals ( ) ) <EOL> reboot = namespaced_function ( reboot , globals ( ) ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> request_log . setLevel ( getattr ( logging , __opts__ . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) ) ) <EOL> if get_configured_provider ( ) is False : <EOL> return False <EOL> if get_dependencies ( ) is False : <EOL> return False <EOL> return __virtualname__ <EOL> def get_configured_provider ( ) : <EOL> '''<STR_LIT>''' <EOL> return config . is_provider_configured ( <EOL> __opts__ , <EOL> __active_provider_name__ or __virtualname__ , <EOL> ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> ) <EOL> def get_dependencies ( ) : <EOL> '''<STR_LIT>''' <EOL> deps = { <EOL> '<STR_LIT>' : HAS_NETADDR , <EOL> '<STR_LIT>' : nova . check_nova ( ) , <EOL> } <EOL> return config . check_driver_dependencies ( <EOL> __virtualname__ , <EOL> deps <EOL> ) <EOL> def get_conn ( ) : <EOL> '''<STR_LIT>''' <EOL> vm_ = get_configured_provider ( ) <EOL> kwargs = vm_ . copy ( ) <EOL> kwargs [ '<STR_LIT:username>' ] = vm_ [ '<STR_LIT:user>' ] <EOL> kwargs [ '<STR_LIT>' ] = vm_ [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = vm_ [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = vm_ [ '<STR_LIT>' ] <EOL> if '<STR_LIT:password>' in vm_ : <EOL> kwargs [ '<STR_LIT:password>' ] = vm_ [ '<STR_LIT:password>' ] <EOL> conn = nova . SaltNova ( ** kwargs ) <EOL> return conn <EOL> def avail_locations ( conn = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if conn is None : <EOL> conn = get_conn ( ) <EOL> endpoints = nova . get_entry ( conn . get_catalog ( ) , '<STR_LIT:type>' , '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> ret = { } <EOL> for endpoint in endpoints : <EOL> ret [ endpoint [ '<STR_LIT>' ] ] = endpoint <EOL> return ret <EOL> def get_image ( conn , vm_ ) : <EOL> '''<STR_LIT>''' <EOL> vm_image = config . get_cloud_config_value ( '<STR_LIT:image>' , vm_ , __opts__ , default = '<STR_LIT>' ) . encode ( <EOL> '<STR_LIT:ascii>' , '<STR_LIT>' <EOL> ) <EOL> if not vm_image : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return None <EOL> image_list = conn . image_list ( ) <EOL> for img in image_list : <EOL> if vm_image in ( image_list [ img ] [ '<STR_LIT:id>' ] , img ) : <EOL> return image_list [ img ] [ '<STR_LIT:id>' ] <EOL> try : <EOL> image = conn . image_show ( vm_image ) <EOL> return image [ '<STR_LIT:id>' ] <EOL> except novaclient . exceptions . NotFound as exc : <EOL> raise SaltCloudNotFound ( <EOL> '<STR_LIT>' . format ( <EOL> vm_image , <EOL> str ( exc ) <EOL> ) <EOL> ) <EOL> def get_block_mapping_opts ( vm_ ) : <EOL> ret = { } <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = { } ) <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = [ ] ) <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = [ ] ) <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = None ) <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = None ) <EOL> ret [ '<STR_LIT>' ] = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ , default = None ) <EOL> return ret <EOL> def show_instance ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> conn = get_conn ( ) <EOL> node = conn . show_instance ( name ) . __dict__ <EOL> salt . utils . cloud . cache_node ( node , __active_provider_name__ , __opts__ ) <EOL> return node <EOL> def get_size ( conn , vm_ ) : <EOL> '''<STR_LIT>''' <EOL> sizes = conn . list_sizes ( ) <EOL> vm_size = config . get_cloud_config_value ( '<STR_LIT:size>' , vm_ , __opts__ ) <EOL> if not vm_size : <EOL> return sizes [ <NUM_LIT:0> ] <EOL> for size in sizes : <EOL> if vm_size and str ( vm_size ) in ( str ( sizes [ size ] [ '<STR_LIT:id>' ] ) , str ( size ) ) : <EOL> return sizes [ size ] [ '<STR_LIT:id>' ] <EOL> raise SaltCloudNotFound ( <EOL> '<STR_LIT>' . format ( vm_size ) <EOL> ) <EOL> def preferred_ip ( vm_ , ips ) : <EOL> '''<STR_LIT>''' <EOL> proto = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = '<STR_LIT>' , search_global = False <EOL> ) <EOL> family = socket . AF_INET <EOL> if proto == '<STR_LIT>' : <EOL> family = socket . AF_INET6 <EOL> for ip in ips : <EOL> try : <EOL> socket . inet_pton ( family , ip ) <EOL> return ip <EOL> except Exception : <EOL> continue <EOL> return False <EOL> def ignore_cidr ( vm_ , ip ) : <EOL> '''<STR_LIT>''' <EOL> if HAS_NETADDR is False : <EOL> log . error ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> cidr = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = '<STR_LIT>' , search_global = False <EOL> ) <EOL> if cidr != '<STR_LIT>' and all_matching_cidrs ( ip , [ cidr ] ) : <EOL> log . warning ( <EOL> '<STR_LIT>' . format ( ip , cidr ) <EOL> ) <EOL> return True <EOL> return False <EOL> def ssh_interface ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> return config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = '<STR_LIT>' , <EOL> search_global = False <EOL> ) <EOL> def rackconnect ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> return config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = False , <EOL> search_global = False <EOL> ) <EOL> def rackconnectv3 ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> return config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = False , <EOL> search_global = False <EOL> ) <EOL> def cloudnetwork ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> return config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = False , <EOL> search_global = False <EOL> ) <EOL> def managedcloud ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> return config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = False , <EOL> search_global = False <EOL> ) <EOL> def destroy ( name , conn = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( name ) , <EOL> { '<STR_LIT:name>' : name } , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> if not conn : <EOL> conn = get_conn ( ) <EOL> node = conn . server_by_name ( name ) <EOL> profiles = get_configured_provider ( ) [ '<STR_LIT>' ] <EOL> if node is None : <EOL> log . error ( '<STR_LIT>' . format ( name ) ) <EOL> profile = None <EOL> if '<STR_LIT>' in node . extra and '<STR_LIT>' in node . extra [ '<STR_LIT>' ] : <EOL> profile = node . extra [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> flush_mine_on_destroy = False <EOL> if profile and profile in profiles and '<STR_LIT>' in profiles [ profile ] : <EOL> flush_mine_on_destroy = profiles [ profile ] [ '<STR_LIT>' ] <EOL> if flush_mine_on_destroy : <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> salt_client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> minions = salt_client . cmd ( name , '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' . format ( <EOL> name , <EOL> flush_mine_on_destroy <EOL> ) ) <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> ret = conn . delete ( node . id ) <EOL> if ret : <EOL> log . info ( '<STR_LIT>' . format ( name ) ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( name ) , <EOL> { '<STR_LIT:name>' : name } , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> if __opts__ . get ( '<STR_LIT>' , False ) is True : <EOL> salt . utils . cloud . remove_sshkey ( getattr ( node , __opts__ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) [ <NUM_LIT:0> ] ) <EOL> if __opts__ . get ( '<STR_LIT>' , False ) is True : <EOL> salt . utils . cloud . delete_minion_cachedir ( name , __active_provider_name__ . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] , __opts__ ) <EOL> salt . utils . cloud . cachedir_index_del ( name ) <EOL> return True <EOL> log . error ( '<STR_LIT>' . format ( name ) ) <EOL> return False <EOL> def request_instance ( vm_ = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> log . info ( '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) ) <EOL> salt . utils . cloud . check_name ( vm_ [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> conn = get_conn ( ) <EOL> kwargs = vm_ . copy ( ) <EOL> try : <EOL> kwargs [ '<STR_LIT>' ] = get_image ( conn , vm_ ) <EOL> except Exception as exc : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] , vm_ [ '<STR_LIT:image>' ] , exc <EOL> ) <EOL> ) <EOL> try : <EOL> kwargs [ '<STR_LIT>' ] = get_size ( conn , vm_ ) <EOL> except Exception as exc : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] , vm_ [ '<STR_LIT:size>' ] , exc <EOL> ) <EOL> ) <EOL> kwargs [ '<STR_LIT>' ] = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> security_groups = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> if security_groups is not None : <EOL> vm_groups = security_groups . split ( '<STR_LIT:U+002C>' ) <EOL> avail_groups = conn . secgroup_list ( ) <EOL> group_list = [ ] <EOL> for vmg in vm_groups : <EOL> if vmg in [ name for name , details in six . iteritems ( avail_groups ) ] : <EOL> group_list . append ( vmg ) <EOL> else : <EOL> raise SaltCloudNotFound ( <EOL> '<STR_LIT>' . format ( vmg ) <EOL> ) <EOL> kwargs [ '<STR_LIT>' ] = group_list <EOL> avz = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = None , search_global = False <EOL> ) <EOL> if avz is not None : <EOL> kwargs [ '<STR_LIT>' ] = avz <EOL> kwargs [ '<STR_LIT>' ] = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False , default = None <EOL> ) <EOL> files = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> if files : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> for src_path in files : <EOL> if os . path . exists ( files [ src_path ] ) : <EOL> with salt . utils . fopen ( files [ src_path ] , '<STR_LIT:r>' ) as fp_ : <EOL> kwargs [ '<STR_LIT>' ] [ src_path ] = fp_ . read ( ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] [ src_path ] = files [ src_path ] <EOL> userdata_file = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> if userdata_file is not None : <EOL> with salt . utils . fopen ( userdata_file , '<STR_LIT:r>' ) as fp : <EOL> kwargs [ '<STR_LIT>' ] = fp . read ( ) <EOL> kwargs [ '<STR_LIT>' ] = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> kwargs . update ( get_block_mapping_opts ( vm_ ) ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) , <EOL> { '<STR_LIT>' : { '<STR_LIT:name>' : kwargs [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:image>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:size>' : kwargs [ '<STR_LIT>' ] } } , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> try : <EOL> data = conn . boot ( ** kwargs ) <EOL> except Exception as exc : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] , exc <EOL> ) <EOL> ) <EOL> if data . extra . get ( '<STR_LIT:password>' , None ) is None and vm_ . get ( '<STR_LIT>' , None ) is None : <EOL> raise SaltCloudSystemExit ( '<STR_LIT>' ) <EOL> floating_ip_conf = config . get_cloud_config_value ( '<STR_LIT>' , <EOL> vm_ , <EOL> __opts__ , <EOL> search_global = False , <EOL> default = { } ) <EOL> if floating_ip_conf . get ( '<STR_LIT>' , False ) : <EOL> pool = floating_ip_conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> floating_ip = None <EOL> for fl_ip , opts in conn . floating_ip_list ( ) . iteritems ( ) : <EOL> if opts [ '<STR_LIT>' ] is None and opts [ '<STR_LIT>' ] == pool : <EOL> floating_ip = fl_ip <EOL> break <EOL> if floating_ip is None : <EOL> floating_ip = conn . floating_ip_create ( pool ) [ '<STR_LIT>' ] <EOL> try : <EOL> conn . floating_ip_associate ( kwargs [ '<STR_LIT:name>' ] , floating_ip ) <EOL> vm_ [ '<STR_LIT>' ] = floating_ip <EOL> except Exception as exc : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] , exc <EOL> ) <EOL> ) <EOL> vm_ [ '<STR_LIT:password>' ] = data . extra . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> return data , vm_ <EOL> def create ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if vm_ [ '<STR_LIT>' ] and config . is_profile_configured ( __opts__ , <EOL> __active_provider_name__ or '<STR_LIT>' , <EOL> vm_ [ '<STR_LIT>' ] , <EOL> vm_ = vm_ ) is False : <EOL> return False <EOL> except AttributeError : <EOL> pass <EOL> deploy = config . get_cloud_config_value ( '<STR_LIT>' , vm_ , __opts__ ) <EOL> key_filename = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False , default = None <EOL> ) <EOL> if key_filename is not None and not os . path . isfile ( key_filename ) : <EOL> raise SaltCloudConfigError ( <EOL> '<STR_LIT>' . format ( <EOL> key_filename <EOL> ) <EOL> ) <EOL> vm_ [ '<STR_LIT>' ] = key_filename <EOL> if '<STR_LIT>' in vm_ : <EOL> vm_ [ '<STR_LIT>' ] = vm_ . pop ( '<STR_LIT>' ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) , <EOL> { <EOL> '<STR_LIT:name>' : vm_ [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] , <EOL> } , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> conn = get_conn ( ) <EOL> if '<STR_LIT>' in vm_ : <EOL> if '<STR_LIT>' not in vm_ and '<STR_LIT>' not in vm_ : <EOL> log . debug ( '<STR_LIT>' . format ( vm_ ) ) <EOL> vm_ [ '<STR_LIT>' ] , vm_ [ '<STR_LIT>' ] = salt . utils . cloud . gen_keys ( <EOL> salt . config . get_cloud_config_value ( <EOL> '<STR_LIT>' , <EOL> vm_ , <EOL> __opts__ <EOL> ) <EOL> ) <EOL> data = conn . server_show_libcloud ( vm_ [ '<STR_LIT>' ] ) <EOL> if vm_ [ '<STR_LIT>' ] is None and '<STR_LIT>' in __opts__ and __opts__ [ '<STR_LIT>' ] is True : <EOL> vm_ [ '<STR_LIT:password>' ] = sup . secure_password ( ) <EOL> conn . root_password ( vm_ [ '<STR_LIT>' ] , vm_ [ '<STR_LIT:password>' ] ) <EOL> else : <EOL> data , vm_ = request_instance ( vm_ ) <EOL> vm_ [ '<STR_LIT>' ] = data . id <EOL> def __query_node_data ( vm_ , data ) : <EOL> try : <EOL> node = show_instance ( vm_ [ '<STR_LIT:name>' ] , '<STR_LIT:action>' ) <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] , <EOL> pprint . pformat ( node ) <EOL> ) <EOL> ) <EOL> except Exception as err : <EOL> log . error ( <EOL> '<STR_LIT>' . format ( <EOL> err <EOL> ) , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> return False <EOL> running = node [ '<STR_LIT:state>' ] == '<STR_LIT>' <EOL> if not running : <EOL> return <EOL> if rackconnect ( vm_ ) is True : <EOL> extra = node . get ( '<STR_LIT>' , { } ) <EOL> rc_status = extra . get ( '<STR_LIT>' , { } ) . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> if rc_status != '<STR_LIT>' : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return <EOL> if managedcloud ( vm_ ) is True : <EOL> extra = conn . server_show_libcloud ( <EOL> node [ '<STR_LIT:id>' ] <EOL> ) . extra <EOL> mc_status = extra . get ( '<STR_LIT>' , { } ) . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> if mc_status != '<STR_LIT>' : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return <EOL> access_ip = node . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> rcv3 = rackconnectv3 ( vm_ ) in node [ '<STR_LIT>' ] <EOL> sshif = ssh_interface ( vm_ ) in node [ '<STR_LIT>' ] <EOL> if any ( ( rcv3 , sshif ) ) : <EOL> networkname = rackconnectv3 ( vm_ ) if rcv3 else ssh_interface ( vm_ ) <EOL> for network in node [ '<STR_LIT>' ] . get ( networkname , [ ] ) : <EOL> if network [ '<STR_LIT:version>' ] is <NUM_LIT:4> : <EOL> access_ip = network [ '<STR_LIT>' ] <EOL> break <EOL> vm_ [ '<STR_LIT>' ] = True <EOL> if any ( ( cloudnetwork ( vm_ ) , rackconnect ( vm_ ) ) ) and ( ssh_interface ( vm_ ) != '<STR_LIT>' or rcv3 ) : <EOL> data . public_ips = [ access_ip , ] <EOL> return data <EOL> result = [ ] <EOL> if '<STR_LIT>' not in node and '<STR_LIT>' not in node and '<STR_LIT>' not in node and '<STR_LIT>' not in node and '<STR_LIT>' in node . get ( '<STR_LIT>' , { } ) : <EOL> result = [ node [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> if not result : <EOL> temp_dd = node . get ( '<STR_LIT>' , { } ) <EOL> for k , addr_ll in temp_dd . iteritems ( ) : <EOL> for addr_dd in addr_ll : <EOL> addr = addr_dd . get ( '<STR_LIT>' , None ) <EOL> if addr is not None : <EOL> result . append ( addr . strip ( ) ) <EOL> private = node . get ( '<STR_LIT>' , [ ] ) <EOL> public = node . get ( '<STR_LIT>' , [ ] ) <EOL> fixed = node . get ( '<STR_LIT>' , [ ] ) <EOL> floating = node . get ( '<STR_LIT>' , [ ] ) <EOL> if private and not public : <EOL> log . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> for private_ip in private : <EOL> private_ip = preferred_ip ( vm_ , [ private_ip ] ) <EOL> if salt . utils . cloud . is_public_ip ( private_ip ) : <EOL> log . warning ( '<STR_LIT>' . format ( private_ip ) ) <EOL> data . public_ips . append ( private_ip ) <EOL> log . warning ( <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> ) <EOL> public = data . public_ips <EOL> else : <EOL> log . warning ( '<STR_LIT>' . format ( private_ip ) ) <EOL> ignore_ip = ignore_cidr ( vm_ , private_ip ) <EOL> if private_ip not in data . private_ips and not ignore_ip : <EOL> result . append ( private_ip ) <EOL> if not result and ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> for private_ip in private : <EOL> ignore_ip = ignore_cidr ( vm_ , private_ip ) <EOL> if private_ip not in data . private_ips and not ignore_ip : <EOL> result . append ( private_ip ) <EOL> non_private_ips = [ ] <EOL> if public : <EOL> data . public_ips = public <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> non_private_ips . append ( public ) <EOL> if floating : <EOL> data . floating_ips = floating <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> non_private_ips . append ( floating ) <EOL> if fixed : <EOL> data . fixed_ips = fixed <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> non_private_ips . append ( fixed ) <EOL> if non_private_ips : <EOL> log . debug ( '<STR_LIT>' . format ( non_private_ips ) ) <EOL> data . private_ips = result <EOL> if ssh_interface ( vm_ ) != '<STR_LIT>' : <EOL> return data <EOL> if result : <EOL> log . debug ( '<STR_LIT>' . format ( result ) ) <EOL> data . private_ips = result <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> return data <EOL> try : <EOL> data = salt . utils . cloud . wait_for_ip ( <EOL> __query_node_data , <EOL> update_args = ( vm_ , data ) , <EOL> timeout = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = <NUM_LIT:10> * <NUM_LIT> ) , <EOL> interval = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = <NUM_LIT:10> ) , <EOL> ) <EOL> except ( SaltCloudExecutionTimeout , SaltCloudExecutionFailure ) as exc : <EOL> try : <EOL> destroy ( vm_ [ '<STR_LIT:name>' ] ) <EOL> except SaltCloudSystemExit : <EOL> pass <EOL> finally : <EOL> raise SaltCloudSystemExit ( str ( exc ) ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> ip_address = preferred_ip ( vm_ , data . private_ips ) <EOL> elif ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> ip_address = preferred_ip ( vm_ , data . fixed_ips ) <EOL> elif ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> ip_address = preferred_ip ( vm_ , data . floating_ips ) <EOL> else : <EOL> ip_address = preferred_ip ( vm_ , data . public_ips ) <EOL> log . debug ( '<STR_LIT>' . format ( ip_address ) ) <EOL> if salt . utils . cloud . get_salt_interface ( vm_ , __opts__ ) == '<STR_LIT>' : <EOL> salt_ip_address = preferred_ip ( vm_ , data . private_ips ) <EOL> log . info ( '<STR_LIT>' . format ( salt_ip_address ) ) <EOL> elif salt . utils . cloud . get_salt_interface ( vm_ , __opts__ ) == '<STR_LIT>' : <EOL> salt_ip_address = preferred_ip ( vm_ , data . fixed_ips ) <EOL> log . info ( '<STR_LIT>' . format ( salt_ip_address ) ) <EOL> elif salt . utils . cloud . get_salt_interface ( vm_ , __opts__ ) == '<STR_LIT>' : <EOL> salt_ip_address = preferred_ip ( vm_ , data . floating_ips ) <EOL> log . info ( '<STR_LIT>' . format ( salt_ip_address ) ) <EOL> else : <EOL> salt_ip_address = preferred_ip ( vm_ , data . public_ips ) <EOL> log . debug ( '<STR_LIT>' . format ( salt_ip_address ) ) <EOL> if not ip_address : <EOL> raise SaltCloudSystemExit ( '<STR_LIT>' ) <EOL> vm_ [ '<STR_LIT>' ] = ip_address <EOL> vm_ [ '<STR_LIT>' ] = salt_ip_address <EOL> ret = salt . utils . cloud . bootstrap ( vm_ , __opts__ ) <EOL> ret . update ( data . __dict__ ) <EOL> if '<STR_LIT:password>' in ret [ '<STR_LIT>' ] : <EOL> del ret [ '<STR_LIT>' ] [ '<STR_LIT:password>' ] <EOL> log . info ( '<STR_LIT>' . format ( vm_ ) ) <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> vm_ , pprint . pformat ( data . __dict__ ) <EOL> ) <EOL> ) <EOL> event_data = { <EOL> '<STR_LIT:name>' : vm_ [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data . floating_ips , <EOL> '<STR_LIT>' : data . fixed_ips , <EOL> '<STR_LIT>' : data . private_ips , <EOL> '<STR_LIT>' : data . public_ips <EOL> } <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) , <EOL> event_data , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> salt . utils . cloud . cachedir_index_add ( <EOL> vm_ [ '<STR_LIT:name>' ] , vm_ [ '<STR_LIT>' ] , '<STR_LIT>' , vm_ [ '<STR_LIT>' ] <EOL> ) <EOL> return ret <EOL> def avail_images ( ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . image_list ( ) <EOL> def avail_sizes ( ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . flavor_list ( ) <EOL> def list_nodes ( call = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> ret = { } <EOL> conn = get_conn ( ) <EOL> server_list = conn . server_list ( ) <EOL> if not server_list : <EOL> return { } <EOL> for server in server_list : <EOL> server_tmp = conn . server_show ( server_list [ server ] [ '<STR_LIT:id>' ] ) . get ( server ) <EOL> if server_tmp is None : <EOL> continue <EOL> private = [ ] <EOL> public = [ ] <EOL> if '<STR_LIT>' not in server_tmp : <EOL> server_tmp [ '<STR_LIT>' ] = { } <EOL> for network in server_tmp [ '<STR_LIT>' ] . keys ( ) : <EOL> for address in server_tmp [ '<STR_LIT>' ] [ network ] : <EOL> if salt . utils . cloud . is_public_ip ( address . get ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> public . append ( address [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT::>' in address [ '<STR_LIT>' ] : <EOL> public . append ( address [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT:.>' in address [ '<STR_LIT>' ] : <EOL> private . append ( address [ '<STR_LIT>' ] ) <EOL> if server_tmp [ '<STR_LIT>' ] : <EOL> if salt . utils . cloud . is_public_ip ( server_tmp [ '<STR_LIT>' ] ) : <EOL> public . append ( server_tmp [ '<STR_LIT>' ] ) <EOL> else : <EOL> private . append ( server_tmp [ '<STR_LIT>' ] ) <EOL> if server_tmp [ '<STR_LIT>' ] : <EOL> public . append ( server_tmp [ '<STR_LIT>' ] ) <EOL> ret [ server ] = { <EOL> '<STR_LIT:id>' : server_tmp [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:image>' : server_tmp [ '<STR_LIT:image>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:size>' : server_tmp [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:state>' : server_tmp [ '<STR_LIT:state>' ] , <EOL> '<STR_LIT>' : private , <EOL> '<STR_LIT>' : public , <EOL> } <EOL> return ret <EOL> def list_nodes_full ( call = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> ) <EOL> ret = { } <EOL> conn = get_conn ( ) <EOL> server_list = conn . server_list ( ) <EOL> if not server_list : <EOL> return { } <EOL> for server in server_list : <EOL> try : <EOL> ret [ server ] = conn . server_show_libcloud ( <EOL> server_list [ server ] [ '<STR_LIT:id>' ] <EOL> ) . __dict__ <EOL> except IndexError as exc : <EOL> ret = { } <EOL> salt . utils . cloud . cache_node_list ( ret , __active_provider_name__ . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] , __opts__ ) <EOL> return ret <EOL> def list_nodes_select ( call = None ) : <EOL> '''<STR_LIT>''' <EOL> return salt . utils . cloud . list_nodes_select ( <EOL> list_nodes_full ( ) , __opts__ [ '<STR_LIT>' ] , call , <EOL> ) <EOL> def volume_create ( name , size = <NUM_LIT:100> , snapshot = None , voltype = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> create_kwargs = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:size>' : size , <EOL> '<STR_LIT>' : snapshot , <EOL> '<STR_LIT>' : voltype } <EOL> create_kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , None ) <EOL> return conn . volume_create ( ** create_kwargs ) <EOL> create_volume = volume_create <EOL> def volume_delete ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . volume_delete ( name ) <EOL> def volume_detach ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . volume_detach ( <EOL> name , <EOL> timeout = <NUM_LIT> <EOL> ) <EOL> def volume_attach ( name , server_name , device = '<STR_LIT>' , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . volume_attach ( <EOL> name , <EOL> server_name , <EOL> device , <EOL> timeout = <NUM_LIT> <EOL> ) <EOL> attach_volume = volume_attach <EOL> def volume_create_attach ( name , call = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if type ( kwargs [ '<STR_LIT>' ] ) is str : <EOL> volumes = yaml . safe_load ( kwargs [ '<STR_LIT>' ] ) <EOL> else : <EOL> volumes = kwargs [ '<STR_LIT>' ] <EOL> ret = [ ] <EOL> for volume in volumes : <EOL> created = False <EOL> volume_dict = { <EOL> '<STR_LIT:name>' : volume [ '<STR_LIT:name>' ] , <EOL> } <EOL> if '<STR_LIT>' in volume : <EOL> volume_dict [ '<STR_LIT>' ] = volume [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in volume : <EOL> volume_dict [ '<STR_LIT>' ] = volume [ '<STR_LIT>' ] <EOL> else : <EOL> volume_dict [ '<STR_LIT:size>' ] = volume [ '<STR_LIT:size>' ] <EOL> if '<STR_LIT:type>' in volume : <EOL> volume_dict [ '<STR_LIT:type>' ] = volume [ '<STR_LIT:type>' ] <EOL> if '<STR_LIT>' in volume : <EOL> volume_dict [ '<STR_LIT>' ] = volume [ '<STR_LIT>' ] <EOL> if '<STR_LIT:id>' not in volume_dict : <EOL> created_volume = create_volume ( ** volume_dict ) <EOL> created = True <EOL> volume_dict . update ( created_volume ) <EOL> attach = attach_volume ( <EOL> name = volume [ '<STR_LIT:name>' ] , <EOL> server_name = name , <EOL> device = volume . get ( '<STR_LIT>' , None ) , <EOL> call = '<STR_LIT:action>' <EOL> ) <EOL> if attach : <EOL> msg = ( <EOL> '<STR_LIT>' . format ( <EOL> volume_dict [ '<STR_LIT:id>' ] , <EOL> name , <EOL> volume_dict [ '<STR_LIT:name>' ] , <EOL> ) <EOL> ) <EOL> log . info ( msg ) <EOL> ret . append ( msg ) <EOL> return ret <EOL> create_attach_volumes = volume_create_attach <EOL> def volume_list ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . volume_list ( ) <EOL> def network_list ( call = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . network_list ( ) <EOL> def network_create ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . network_create ( name , ** kwargs ) <EOL> def virtual_interface_list ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . virtual_interface_list ( name ) <EOL> def virtual_interface_create ( name , net_name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> return conn . virtual_interface_create ( name , net_name ) <EOL> def floating_ip_pool_list ( call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> conn = get_conn ( ) <EOL> return conn . floating_ip_pool_list ( ) <EOL> def floating_ip_list ( call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> conn = get_conn ( ) <EOL> return conn . floating_ip_list ( ) <EOL> def floating_ip_create ( kwargs , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> conn = get_conn ( ) <EOL> return conn . floating_ip_create ( kwargs [ '<STR_LIT>' ] ) <EOL> def floating_ip_delete ( kwargs , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> conn = get_conn ( ) <EOL> return conn . floating_ip_delete ( kwargs [ '<STR_LIT>' ] ) <EOL> def floating_ip_associate ( name , kwargs , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> conn = get_conn ( ) <EOL> conn . floating_ip_associate ( name , kwargs [ '<STR_LIT>' ] ) <EOL> return list_nodes ( ) [ name ] <EOL> def floating_ip_disassociate ( name , kwargs , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call != '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> conn = get_conn ( ) <EOL> conn . floating_ip_disassociate ( name , kwargs [ '<STR_LIT>' ] ) <EOL> return list_nodes ( ) [ name ] </s>
