<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import socket <EOL> import pprint <EOL> try : <EOL> from libcloud . compute . base import NodeState <EOL> from libcloud . compute . base import NodeAuthPassword <EOL> from libcloud . compute . types import Provider <EOL> from libcloud . compute . providers import get_driver <EOL> from libcloud . loadbalancer . base import Member <EOL> from libcloud . loadbalancer . types import Provider as Provider_lb <EOL> from libcloud . loadbalancer . providers import get_driver as get_driver_lb <EOL> import libcloud . security <EOL> libcloud . security . CA_CERTS_PATH . append ( '<STR_LIT>' ) <EOL> HAS_LIBCLOUD = True <EOL> except ImportError : <EOL> HAS_LIBCLOUD = False <EOL> import salt . utils <EOL> from salt . cloud . libcloudfuncs import * <EOL> from salt . utils import namespaced_function <EOL> import salt . utils . cloud <EOL> import salt . config as config <EOL> from salt . exceptions import ( <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> get_size = namespaced_function ( get_size , globals ( ) ) <EOL> get_image = namespaced_function ( get_image , globals ( ) ) <EOL> avail_locations = namespaced_function ( avail_locations , globals ( ) ) <EOL> avail_images = namespaced_function ( avail_images , globals ( ) ) <EOL> avail_sizes = namespaced_function ( avail_sizes , globals ( ) ) <EOL> script = namespaced_function ( script , globals ( ) ) <EOL> destroy = namespaced_function ( destroy , globals ( ) ) <EOL> reboot = namespaced_function ( reboot , globals ( ) ) <EOL> list_nodes = namespaced_function ( list_nodes , globals ( ) ) <EOL> list_nodes_full = namespaced_function ( list_nodes_full , globals ( ) ) <EOL> list_nodes_select = namespaced_function ( list_nodes_select , globals ( ) ) <EOL> show_instance = namespaced_function ( show_instance , globals ( ) ) <EOL> get_node = namespaced_function ( get_node , globals ( ) ) <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if get_configured_provider ( ) is False : <EOL> return False <EOL> if get_dependencies ( ) is False : <EOL> return False <EOL> for provider , details in six . iteritems ( __opts__ [ '<STR_LIT>' ] ) : <EOL> if '<STR_LIT>' not in details : <EOL> continue <EOL> return __virtualname__ <EOL> def get_configured_provider ( ) : <EOL> '''<STR_LIT>''' <EOL> return config . is_provider_configured ( <EOL> __opts__ , <EOL> __active_provider_name__ or '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT:key>' , '<STR_LIT>' ) <EOL> ) <EOL> def get_dependencies ( ) : <EOL> '''<STR_LIT>''' <EOL> deps = { <EOL> '<STR_LIT>' : HAS_LIBCLOUD , <EOL> '<STR_LIT>' : HAS_NETADDR <EOL> } <EOL> return config . check_driver_dependencies ( <EOL> __virtualname__ , <EOL> deps <EOL> ) <EOL> def create ( vm_ ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if vm_ [ '<STR_LIT>' ] and config . is_profile_configured ( <EOL> __opts__ , <EOL> __active_provider_name__ or '<STR_LIT>' , <EOL> vm_ [ '<STR_LIT>' ] ) is False : <EOL> return False <EOL> except AttributeError : <EOL> pass <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> log . info ( '<STR_LIT>' , vm_ [ '<STR_LIT:name>' ] ) <EOL> conn = get_conn ( ) <EOL> rootPw = NodeAuthPassword ( vm_ [ '<STR_LIT>' ] ) <EOL> try : <EOL> location = conn . ex_get_location_by_id ( vm_ [ '<STR_LIT:location>' ] ) <EOL> images = conn . list_images ( location = location ) <EOL> image = [ x for x in images if x . id == vm_ [ '<STR_LIT:image>' ] ] [ <NUM_LIT:0> ] <EOL> networks = conn . ex_list_network_domains ( location = location ) <EOL> network_domain = [ y for y in networks if y . name == <EOL> vm_ [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> vlan = conn . ex_list_vlans ( location = location , <EOL> network_domain = network_domain ) [ <NUM_LIT:0> ] <EOL> kwargs = { <EOL> '<STR_LIT:name>' : vm_ [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:image>' : image , <EOL> '<STR_LIT>' : rootPw , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT:description>' ] , <EOL> '<STR_LIT>' : network_domain , <EOL> '<STR_LIT>' : vlan , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT>' ] <EOL> } <EOL> data = conn . create_node ( ** kwargs ) <EOL> except Exception as exc : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> vm_ [ '<STR_LIT:name>' ] , exc , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> return False <EOL> def __query_node_data ( vm_ , data ) : <EOL> running = False <EOL> try : <EOL> node = show_instance ( vm_ [ '<STR_LIT:name>' ] , '<STR_LIT:action>' ) <EOL> running = ( node [ '<STR_LIT:state>' ] == NodeState . RUNNING ) <EOL> log . debug ( <EOL> '<STR_LIT>' , <EOL> vm_ [ '<STR_LIT:name>' ] , <EOL> pprint . pformat ( node [ '<STR_LIT:name>' ] ) , <EOL> node [ '<STR_LIT:state>' ] <EOL> ) <EOL> except Exception as err : <EOL> log . error ( <EOL> '<STR_LIT>' , err , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> return False <EOL> if not running : <EOL> return <EOL> private = node [ '<STR_LIT>' ] <EOL> public = node [ '<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>' , private_ip ) <EOL> data . public_ips . append ( private_ip ) <EOL> else : <EOL> log . warning ( '<STR_LIT>' , private_ip ) <EOL> if private_ip not in data . private_ips : <EOL> data . private_ips . append ( private_ip ) <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' and data . private_ips : <EOL> return data <EOL> if private : <EOL> data . private_ips = private <EOL> if ssh_interface ( vm_ ) == '<STR_LIT>' : <EOL> return data <EOL> if public : <EOL> data . public_ips = public <EOL> if ssh_interface ( vm_ ) != '<STR_LIT>' : <EOL> return data <EOL> log . debug ( '<STR_LIT>' ) <EOL> log . debug ( 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> * <NUM_LIT> ) , <EOL> interval = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = <NUM_LIT:30> ) , <EOL> max_failures = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = <NUM_LIT> ) , <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> else : <EOL> ip_address = preferred_ip ( vm_ , data . public_ips ) <EOL> log . debug ( '<STR_LIT>' , 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>' , salt_ip_address ) <EOL> else : <EOL> salt_ip_address = preferred_ip ( vm_ , data . public_ips ) <EOL> log . debug ( '<STR_LIT>' , salt_ip_address ) <EOL> if not ip_address : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> vm_ [ '<STR_LIT>' ] = salt_ip_address <EOL> vm_ [ '<STR_LIT>' ] = ip_address <EOL> vm_ [ '<STR_LIT:password>' ] = vm_ [ '<STR_LIT>' ] <EOL> ret = salt . utils . cloud . bootstrap ( vm_ , __opts__ ) <EOL> ret . update ( data . __dict__ ) <EOL> if '<STR_LIT:password>' in data . extra : <EOL> del data . extra [ '<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> 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> return ret <EOL> def create_lb ( kwargs = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> if call != '<STR_LIT>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if not kwargs or '<STR_LIT:name>' not in kwargs : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return False <EOL> if '<STR_LIT:port>' not in kwargs : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return False <EOL> if '<STR_LIT>' not in kwargs : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return False <EOL> if '<STR_LIT>' in kwargs : <EOL> members = [ ] <EOL> ip = "<STR_LIT>" <EOL> membersList = kwargs . get ( '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> log . debug ( '<STR_LIT>' , membersList ) <EOL> for member in membersList : <EOL> try : <EOL> log . debug ( '<STR_LIT>' , member ) <EOL> node = get_node ( conn , member ) <EOL> log . debug ( '<STR_LIT>' , node ) <EOL> ip = node . private_ips [ <NUM_LIT:0> ] <EOL> except Exception as err : <EOL> log . error ( <EOL> '<STR_LIT>' , err , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> members . append ( Member ( ip , ip , kwargs [ '<STR_LIT:port>' ] ) ) <EOL> else : <EOL> members = None <EOL> log . debug ( '<STR_LIT>' , members ) <EOL> networkdomain = kwargs [ '<STR_LIT>' ] <EOL> name = kwargs [ '<STR_LIT:name>' ] <EOL> port = kwargs [ '<STR_LIT:port>' ] <EOL> protocol = kwargs . get ( '<STR_LIT>' , None ) <EOL> algorithm = kwargs . get ( '<STR_LIT>' , None ) <EOL> lb_conn = get_lb_conn ( conn ) <EOL> network_domains = conn . ex_list_network_domains ( ) <EOL> network_domain = [ y for y in network_domains if y . name == networkdomain ] [ <NUM_LIT:0> ] <EOL> log . debug ( '<STR_LIT>' , network_domain . id ) <EOL> lb_conn . ex_set_current_network_domain ( network_domain . id ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> kwargs , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> lb = lb_conn . create_balancer ( <EOL> name , port , protocol , algorithm , members <EOL> ) <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> kwargs , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> return _expand_balancer ( lb ) <EOL> def _expand_balancer ( lb ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> ret . update ( lb . __dict__ ) <EOL> return ret <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 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 stop ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> node = get_node ( conn , name ) <EOL> log . debug ( '<STR_LIT>' , node ) <EOL> status = conn . ex_shutdown_graceful ( node ) <EOL> log . debug ( '<STR_LIT>' , status ) <EOL> return status <EOL> def start ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> conn = get_conn ( ) <EOL> node = get_node ( conn , name ) <EOL> log . debug ( '<STR_LIT>' , node ) <EOL> status = conn . ex_start_node ( node ) <EOL> log . debug ( '<STR_LIT>' , status ) <EOL> return status <EOL> def get_conn ( ) : <EOL> '''<STR_LIT>''' <EOL> vm_ = get_configured_provider ( ) <EOL> driver = get_driver ( Provider . DIMENSIONDATA ) <EOL> region = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ <EOL> ) <EOL> user_id = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ <EOL> ) <EOL> key = config . get_cloud_config_value ( <EOL> '<STR_LIT:key>' , vm_ , __opts__ <EOL> ) <EOL> if key is not None : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return driver ( <EOL> user_id , <EOL> key , <EOL> region = region <EOL> ) <EOL> def get_lb_conn ( dd_driver = None ) : <EOL> '''<STR_LIT>''' <EOL> vm_ = get_configured_provider ( ) <EOL> region = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ <EOL> ) <EOL> user_id = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ <EOL> ) <EOL> key = config . get_cloud_config_value ( <EOL> '<STR_LIT:key>' , vm_ , __opts__ <EOL> ) <EOL> if not dd_driver : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return get_driver_lb ( Provider_lb . DIMENSIONDATA ) ( user_id , key , region = region ) </s>
