<s> """<STR_LIT>""" <EOL> import time <EOL> import hashlib <EOL> import copy <EOL> from libcloud . utils . py3 import b <EOL> from libcloud . common . types import InvalidCredsError , LibcloudError <EOL> from libcloud . common . gogrid import GoGridConnection , BaseGoGridDriver <EOL> from libcloud . compute . providers import Provider <EOL> from libcloud . compute . types import NodeState <EOL> from libcloud . compute . base import Node , NodeDriver <EOL> from libcloud . compute . base import NodeSize , NodeImage , NodeLocation <EOL> STATE = { <EOL> "<STR_LIT>" : NodeState . PENDING , <EOL> "<STR_LIT>" : NodeState . RUNNING , <EOL> "<STR_LIT>" : NodeState . RUNNING , <EOL> "<STR_LIT>" : NodeState . PENDING , <EOL> "<STR_LIT>" : NodeState . REBOOTING , <EOL> "<STR_LIT>" : NodeState . PENDING , <EOL> "<STR_LIT>" : NodeState . PENDING , <EOL> } <EOL> GOGRID_INSTANCE_TYPES = { <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : None } , <EOL> } <EOL> class GoGridNode ( Node ) : <EOL> def get_uuid ( self ) : <EOL> return hashlib . sha1 ( <EOL> b ( "<STR_LIT>" % ( self . public_ips , self . driver . type ) ) <EOL> ) . hexdigest ( ) <EOL> class GoGridNodeDriver ( BaseGoGridDriver , NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> connectionCls = GoGridConnection <EOL> type = Provider . GOGRID <EOL> api_name = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> features = { "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> _instance_types = GOGRID_INSTANCE_TYPES <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( GoGridNodeDriver , self ) . __init__ ( * args , ** kwargs ) <EOL> def _get_state ( self , element ) : <EOL> try : <EOL> return STATE [ element [ '<STR_LIT:state>' ] [ '<STR_LIT:name>' ] ] <EOL> except : <EOL> pass <EOL> return NodeState . UNKNOWN <EOL> def _get_ip ( self , element ) : <EOL> return element . get ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> def _get_id ( self , element ) : <EOL> return element . get ( '<STR_LIT:id>' ) <EOL> def _to_node ( self , element , password = None ) : <EOL> state = self . _get_state ( element ) <EOL> ip = self . _get_ip ( element ) <EOL> id = self . _get_id ( element ) <EOL> n = GoGridNode ( id = id , <EOL> name = element [ '<STR_LIT:name>' ] , <EOL> state = state , <EOL> public_ips = [ ip ] , <EOL> private_ips = [ ] , <EOL> extra = { '<STR_LIT>' : element . get ( '<STR_LIT>' ) . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT:description>' : element . get ( '<STR_LIT:description>' , '<STR_LIT>' ) } , <EOL> driver = self . connection . driver ) <EOL> if password : <EOL> n . extra [ '<STR_LIT:password>' ] = password <EOL> return n <EOL> def _to_image ( self , element ) : <EOL> n = NodeImage ( id = element [ '<STR_LIT:id>' ] , <EOL> name = element [ '<STR_LIT>' ] , <EOL> driver = self . connection . driver ) <EOL> return n <EOL> def _to_images ( self , object ) : <EOL> return [ self . _to_image ( el ) <EOL> for el in object [ '<STR_LIT:list>' ] ] <EOL> def _to_location ( self , element ) : <EOL> location = NodeLocation ( id = element [ '<STR_LIT:id>' ] , <EOL> name = element [ '<STR_LIT:name>' ] , <EOL> country = "<STR_LIT>" , <EOL> driver = self . connection . driver ) <EOL> return location <EOL> def _to_locations ( self , object ) : <EOL> return [ self . _to_location ( el ) <EOL> for el in object [ '<STR_LIT:list>' ] ] <EOL> def list_images ( self , location = None ) : <EOL> params = { } <EOL> if location is not None : <EOL> params [ "<STR_LIT>" ] = location . id <EOL> images = self . _to_images ( <EOL> self . connection . request ( '<STR_LIT>' , params ) . object ) <EOL> return images <EOL> def list_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> passwords_map = { } <EOL> res = self . _server_list ( ) <EOL> try : <EOL> for password in self . _password_list ( ) [ '<STR_LIT:list>' ] : <EOL> try : <EOL> passwords_map [ password [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ] = password [ '<STR_LIT:password>' ] <EOL> except KeyError : <EOL> pass <EOL> except InvalidCredsError : <EOL> pass <EOL> return [ self . _to_node ( el , passwords_map . get ( el . get ( '<STR_LIT:id>' ) ) ) <EOL> for el in res [ '<STR_LIT:list>' ] ] <EOL> def reboot_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> id = node . id <EOL> power = '<STR_LIT>' <EOL> res = self . _server_power ( id , power ) <EOL> if not res . success ( ) : <EOL> raise Exception ( res . parse_error ( ) ) <EOL> return True <EOL> def destroy_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> id = node . id <EOL> res = self . _server_delete ( id ) <EOL> if not res . success ( ) : <EOL> raise Exception ( res . parse_error ( ) ) <EOL> return True <EOL> def _server_list ( self ) : <EOL> return self . connection . request ( '<STR_LIT>' ) . object <EOL> def _password_list ( self ) : <EOL> return self . connection . request ( '<STR_LIT>' ) . object <EOL> def _server_power ( self , id , power ) : <EOL> params = { '<STR_LIT:id>' : id , '<STR_LIT>' : power } <EOL> return self . connection . request ( "<STR_LIT>" , params , <EOL> method = '<STR_LIT:POST>' ) <EOL> def _server_delete ( self , id ) : <EOL> params = { '<STR_LIT:id>' : id } <EOL> return self . connection . request ( "<STR_LIT>" , params , <EOL> method = '<STR_LIT:POST>' ) <EOL> def _get_first_ip ( self , location = None ) : <EOL> ips = self . ex_list_ips ( public = True , assigned = False , location = location ) <EOL> try : <EOL> return ips [ <NUM_LIT:0> ] . ip <EOL> except IndexError : <EOL> raise LibcloudError ( '<STR_LIT>' , <EOL> GoGridNodeDriver ) <EOL> def list_sizes ( self , location = None ) : <EOL> sizes = [ ] <EOL> for key , values in self . _instance_types . items ( ) : <EOL> attributes = copy . deepcopy ( values ) <EOL> attributes . update ( { '<STR_LIT>' : self . _get_size_price ( size_id = key ) } ) <EOL> sizes . append ( NodeSize ( driver = self . connection . driver , ** attributes ) ) <EOL> return sizes <EOL> def list_locations ( self ) : <EOL> locations = self . _to_locations ( <EOL> self . connection . request ( '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : '<STR_LIT>' } ) . object ) <EOL> return locations <EOL> def ex_create_node_nowait ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = kwargs [ '<STR_LIT:name>' ] <EOL> image = kwargs [ '<STR_LIT:image>' ] <EOL> size = kwargs [ '<STR_LIT:size>' ] <EOL> try : <EOL> ip = kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> ip = self . _get_first_ip ( kwargs . get ( '<STR_LIT:location>' ) ) <EOL> params = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:image>' : image . id , <EOL> '<STR_LIT:description>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : size . id , <EOL> '<STR_LIT>' : ip } <EOL> object = self . connection . request ( '<STR_LIT>' , <EOL> params = params , method = '<STR_LIT:POST>' ) . object <EOL> node = self . _to_node ( object [ '<STR_LIT:list>' ] [ <NUM_LIT:0> ] ) <EOL> return node <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> node = self . ex_create_node_nowait ( ** kwargs ) <EOL> timeout = <NUM_LIT> * <NUM_LIT:20> <EOL> waittime = <NUM_LIT:0> <EOL> interval = <NUM_LIT:2> * <NUM_LIT> <EOL> while node . id is None and waittime < timeout : <EOL> nodes = self . list_nodes ( ) <EOL> for i in nodes : <EOL> if i . public_ips [ <NUM_LIT:0> ] == node . public_ips [ <NUM_LIT:0> ] and i . id is not None : <EOL> return i <EOL> waittime += interval <EOL> time . sleep ( interval ) <EOL> if id is None : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> % str ( node ) ) <EOL> return node <EOL> def ex_save_image ( self , node , name ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : node . id , <EOL> '<STR_LIT>' : name } <EOL> object = self . connection . request ( '<STR_LIT>' , params = params , <EOL> method = '<STR_LIT:POST>' ) . object <EOL> return self . _to_images ( object ) [ <NUM_LIT:0> ] <EOL> def ex_edit_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> node = kwargs [ '<STR_LIT>' ] <EOL> size = kwargs [ '<STR_LIT:size>' ] <EOL> params = { '<STR_LIT:id>' : node . id , <EOL> '<STR_LIT>' : size . id } <EOL> if '<STR_LIT>' in kwargs : <EOL> params [ '<STR_LIT:description>' ] = kwargs [ '<STR_LIT>' ] <EOL> object = self . connection . request ( '<STR_LIT>' , <EOL> params = params ) . object <EOL> return self . _to_node ( object [ '<STR_LIT:list>' ] [ <NUM_LIT:0> ] ) <EOL> def ex_edit_image ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> image = kwargs [ '<STR_LIT:image>' ] <EOL> public = kwargs [ '<STR_LIT>' ] <EOL> params = { '<STR_LIT:id>' : image . id , <EOL> '<STR_LIT>' : str ( public ) . lower ( ) } <EOL> if '<STR_LIT>' in kwargs : <EOL> params [ '<STR_LIT:description>' ] = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT:name>' in kwargs : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT:name>' ] <EOL> object = self . connection . request ( '<STR_LIT>' , <EOL> params = params ) . object <EOL> return self . _to_image ( object [ '<STR_LIT:list>' ] [ <NUM_LIT:0> ] ) <EOL> def ex_list_ips ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] is not None : <EOL> params [ "<STR_LIT>" ] = { True : "<STR_LIT>" , <EOL> False : "<STR_LIT>" } [ kwargs [ "<STR_LIT>" ] ] <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] is not None : <EOL> params [ "<STR_LIT>" ] = { True : "<STR_LIT>" , <EOL> False : "<STR_LIT>" } [ kwargs [ "<STR_LIT>" ] ] <EOL> if "<STR_LIT:location>" in kwargs and kwargs [ '<STR_LIT:location>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT:location>' ] . id <EOL> ips = self . _to_ips ( <EOL> self . connection . request ( '<STR_LIT>' , <EOL> params = params ) . object ) <EOL> return ips </s>
