<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import pprint <EOL> import logging <EOL> import time <EOL> import hashlib <EOL> import salt . config as config <EOL> import salt . utils . cloud <EOL> from salt . exceptions import SaltCloudSystemExit , SaltCloudException <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> 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>' , '<STR_LIT>' ) <EOL> ) <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> 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> if len ( vm_ [ '<STR_LIT:name>' ] ) > <NUM_LIT:20> : <EOL> raise SaltCloudException ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) ) <EOL> image_id = avail_images ( ) [ vm_ [ '<STR_LIT:image>' ] ] [ '<STR_LIT:id>' ] <EOL> if '<STR_LIT>' in vm_ : <EOL> host_ip = vm_ [ '<STR_LIT>' ] <EOL> else : <EOL> public_ips = list_public_ips ( ) <EOL> if len ( public_ips . keys ( ) ) < <NUM_LIT:1> : <EOL> raise SaltCloudException ( '<STR_LIT>' ) <EOL> host_ip = public_ips . keys ( ) [ <NUM_LIT:0> ] <EOL> create_kwargs = { <EOL> '<STR_LIT:name>' : vm_ [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:image>' : image_id , <EOL> '<STR_LIT>' : vm_ [ '<STR_LIT:size>' ] , <EOL> '<STR_LIT>' : host_ip , <EOL> } <EOL> salt . utils . cloud . fire_event ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( vm_ [ '<STR_LIT:name>' ] ) , <EOL> { '<STR_LIT>' : create_kwargs } , <EOL> transport = __opts__ [ '<STR_LIT>' ] <EOL> ) <EOL> try : <EOL> data = _query ( '<STR_LIT>' , '<STR_LIT>' , args = create_kwargs ) <EOL> except Exception : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> vm_ [ '<STR_LIT:name>' ] <EOL> ) , <EOL> exc_info_on_loglevel = logging . DEBUG <EOL> ) <EOL> return False <EOL> ssh_username = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = '<STR_LIT:root>' <EOL> ) <EOL> def wait_for_apipass ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> passwords = list_passwords ( ) <EOL> return passwords [ vm_ [ '<STR_LIT:name>' ] ] [ <NUM_LIT:0> ] [ '<STR_LIT:password>' ] <EOL> except KeyError : <EOL> pass <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> return False <EOL> vm_ [ '<STR_LIT:password>' ] = salt . utils . cloud . wait_for_fun ( <EOL> wait_for_apipass , <EOL> timeout = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , default = <NUM_LIT:15> * <NUM_LIT> ) , <EOL> ) <EOL> vm_ [ '<STR_LIT>' ] = host_ip <EOL> ret = salt . utils . cloud . bootstrap ( vm_ , __opts__ ) <EOL> ret . update ( data ) <EOL> log . info ( '<STR_LIT>' . format ( vm_ ) ) <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> vm_ , pprint . pformat ( data ) <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 list_nodes ( full = False , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if call == '<STR_LIT:action>' : <EOL> raise SaltCloudSystemExit ( <EOL> '<STR_LIT>' <EOL> ) <EOL> ret = { } <EOL> nodes = list_nodes_full ( '<STR_LIT>' ) <EOL> if full : <EOL> return nodes <EOL> for node in nodes : <EOL> ret [ node ] = { } <EOL> for item in ( '<STR_LIT:id>' , '<STR_LIT:image>' , '<STR_LIT:size>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' ) : <EOL> ret [ node ] [ item ] = nodes [ node ] [ item ] <EOL> return ret <EOL> def list_nodes_full ( call = None ) : <EOL> '''<STR_LIT>''' <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT:name>' ] <EOL> ret [ name ] = item <EOL> ret [ name ] [ '<STR_LIT>' ] = item [ '<STR_LIT:image>' ] <EOL> ret [ name ] [ '<STR_LIT:image>' ] = item [ '<STR_LIT:image>' ] [ '<STR_LIT>' ] <EOL> ret [ name ] [ '<STR_LIT:size>' ] = item [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] <EOL> ret [ name ] [ '<STR_LIT>' ] = [ item [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> ret [ name ] [ '<STR_LIT>' ] = [ ] <EOL> ret [ name ] [ '<STR_LIT>' ] = item [ '<STR_LIT:state>' ] <EOL> if '<STR_LIT>' in item [ '<STR_LIT:state>' ] [ '<STR_LIT:description>' ] : <EOL> ret [ name ] [ '<STR_LIT:state>' ] = '<STR_LIT>' <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 ( '<STR_LIT>' ) , __opts__ [ '<STR_LIT>' ] , call , <EOL> ) <EOL> def avail_locations ( ) : <EOL> '''<STR_LIT>''' <EOL> response = list_common_lookups ( kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT:name>' ] <EOL> ret [ name ] = item <EOL> return ret <EOL> def avail_sizes ( ) : <EOL> '''<STR_LIT>''' <EOL> response = list_common_lookups ( kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT:name>' ] <EOL> ret [ name ] = item <EOL> return ret <EOL> def avail_images ( ) : <EOL> '''<STR_LIT>''' <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT>' ] <EOL> ret [ name ] = item <EOL> return ret <EOL> def list_passwords ( kwargs = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> server = item [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] <EOL> if server not in ret : <EOL> ret [ server ] = [ ] <EOL> ret [ server ] . append ( item ) <EOL> return ret <EOL> def list_public_ips ( kwargs = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> args = { } <EOL> if '<STR_LIT:state>' in kwargs : <EOL> if kwargs [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' , args = args ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT>' ] <EOL> ret [ name ] = item <EOL> return ret <EOL> def list_common_lookups ( kwargs = None , call = None ) : <EOL> '''<STR_LIT>''' <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> args = { } <EOL> if '<STR_LIT>' in kwargs : <EOL> args [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' , args = args ) <EOL> return response <EOL> def destroy ( name , 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> response = _query ( '<STR_LIT>' , '<STR_LIT>' , args = { '<STR_LIT:name>' : 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 . delete_minion_cachedir ( name , __active_provider_name__ . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] , __opts__ ) <EOL> return response <EOL> def reboot ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> return _query ( '<STR_LIT>' , '<STR_LIT>' , args = { '<STR_LIT:name>' : name , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def stop ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> return _query ( '<STR_LIT>' , '<STR_LIT>' , args = { '<STR_LIT:name>' : name , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def start ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> return _query ( '<STR_LIT>' , '<STR_LIT>' , args = { '<STR_LIT:name>' : name , '<STR_LIT>' : '<STR_LIT:start>' } ) <EOL> def show_instance ( name , call = None ) : <EOL> '''<STR_LIT>''' <EOL> response = _query ( '<STR_LIT>' , '<STR_LIT>' , args = { '<STR_LIT:name>' : name } ) <EOL> ret = { } <EOL> for item in response [ '<STR_LIT:list>' ] : <EOL> name = item [ '<STR_LIT:name>' ] <EOL> ret [ name ] = item <EOL> ret [ name ] [ '<STR_LIT>' ] = item [ '<STR_LIT:image>' ] <EOL> ret [ name ] [ '<STR_LIT:image>' ] = item [ '<STR_LIT:image>' ] [ '<STR_LIT>' ] <EOL> ret [ name ] [ '<STR_LIT:size>' ] = item [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] <EOL> ret [ name ] [ '<STR_LIT>' ] = [ item [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> ret [ name ] [ '<STR_LIT>' ] = [ ] <EOL> ret [ name ] [ '<STR_LIT>' ] = item [ '<STR_LIT:state>' ] <EOL> if '<STR_LIT>' in item [ '<STR_LIT:state>' ] [ '<STR_LIT:description>' ] : <EOL> ret [ name ] [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> return ret <EOL> def _query ( action = None , <EOL> command = None , <EOL> args = None , <EOL> method = '<STR_LIT:GET>' , <EOL> header_dict = None , <EOL> data = None ) : <EOL> '''<STR_LIT>''' <EOL> vm_ = get_configured_provider ( ) <EOL> apikey = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> sharedsecret = config . get_cloud_config_value ( <EOL> '<STR_LIT>' , vm_ , __opts__ , search_global = False <EOL> ) <EOL> path = '<STR_LIT>' <EOL> if action : <EOL> path += action <EOL> if command : <EOL> path += '<STR_LIT>' . format ( command ) <EOL> log . debug ( '<STR_LIT>' . format ( path ) ) <EOL> if not isinstance ( args , dict ) : <EOL> args = { } <EOL> epoch = str ( int ( time . time ( ) ) ) <EOL> hashtext = '<STR_LIT>' . join ( ( apikey , sharedsecret , epoch ) ) <EOL> args [ '<STR_LIT>' ] = hashlib . md5 ( hashtext ) . hexdigest ( ) <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> args [ '<STR_LIT:v>' ] = '<STR_LIT:1.0>' <EOL> args [ '<STR_LIT>' ] = apikey <EOL> if header_dict is None : <EOL> header_dict = { } <EOL> if method != '<STR_LIT:POST>' : <EOL> header_dict [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> decode = True <EOL> if method == '<STR_LIT>' : <EOL> decode = False <EOL> return_content = None <EOL> result = salt . utils . http . query ( <EOL> path , <EOL> method , <EOL> params = args , <EOL> data = data , <EOL> header_dict = header_dict , <EOL> decode = decode , <EOL> decode_type = '<STR_LIT>' , <EOL> text = True , <EOL> status = True , <EOL> opts = __opts__ , <EOL> ) <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> result [ '<STR_LIT:status>' ] <EOL> ) <EOL> ) <EOL> return result [ '<STR_LIT>' ] </s>
