<s> """<STR_LIT>""" <EOL> import httplib , urllib <EOL> import time <EOL> import hashlib <EOL> import StringIO <EOL> import ssl <EOL> import os <EOL> import socket <EOL> import struct <EOL> from libcloud . common . base import ConnectionKey , ConnectionUserAndKey <EOL> from libcloud . compute . types import NodeState , DeploymentError <EOL> from libcloud . compute . ssh import SSHClient <EOL> from libcloud . httplib_ssl import LibcloudHTTPSConnection <EOL> from httplib import HTTPConnection as LibcloudHTTPConnection <EOL> class Node ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , name , state , public_ip , private_ip , <EOL> driver , extra = None ) : <EOL> self . id = str ( id ) if id else None <EOL> self . name = name <EOL> self . state = state <EOL> self . public_ip = public_ip <EOL> self . private_ip = private_ip <EOL> self . driver = driver <EOL> self . uuid = self . get_uuid ( ) <EOL> if not extra : <EOL> self . extra = { } <EOL> else : <EOL> self . extra = extra <EOL> def get_uuid ( self ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . sha1 ( "<STR_LIT>" % ( self . id , self . driver . type ) ) . hexdigest ( ) <EOL> def reboot ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . reboot_node ( self ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . destroy_node ( self ) <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % ( self . uuid , self . name , self . state , self . public_ip , <EOL> self . driver . name ) ) <EOL> class NodeSize ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , name , ram , disk , bandwidth , price , driver ) : <EOL> self . id = str ( id ) <EOL> self . name = name <EOL> self . ram = ram <EOL> self . disk = disk <EOL> self . bandwidth = bandwidth <EOL> self . price = price <EOL> self . driver = driver <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % ( self . id , self . name , self . ram , self . disk , self . bandwidth , <EOL> self . price , self . driver . name ) ) <EOL> class NodeImage ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , name , driver , extra = None ) : <EOL> self . id = str ( id ) <EOL> self . name = name <EOL> self . driver = driver <EOL> if not extra : <EOL> self . extra = { } <EOL> else : <EOL> self . extra = extra <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) <EOL> % ( self . id , self . name , self . driver . name ) ) <EOL> class NodeLocation ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , name , country , driver ) : <EOL> self . id = str ( id ) <EOL> self . name = name <EOL> self . country = country <EOL> self . driver = driver <EOL> def __repr__ ( self ) : <EOL> return ( ( '<STR_LIT>' ) <EOL> % ( self . id , self . name , self . country , self . driver . name ) ) <EOL> class NodeAuthSSHKey ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pubkey ) : <EOL> self . pubkey = pubkey <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class NodeAuthPassword ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , password ) : <EOL> self . password = password <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class NodeDriver ( object ) : <EOL> """<STR_LIT>""" <EOL> connectionCls = ConnectionKey <EOL> name = None <EOL> type = None <EOL> port = None <EOL> features = { "<STR_LIT>" : [ ] } <EOL> """<STR_LIT>""" <EOL> NODE_STATE_MAP = { } <EOL> def __init__ ( self , key , secret = None , secure = True , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> self . key = key <EOL> self . secret = secret <EOL> self . secure = secure <EOL> args = [ self . key ] <EOL> if self . secret != None : <EOL> args . append ( self . secret ) <EOL> args . append ( secure ) <EOL> if host != None : <EOL> args . append ( host ) <EOL> if port != None : <EOL> args . append ( port ) <EOL> self . connection = self . connectionCls ( * args ) <EOL> self . connection . driver = self <EOL> self . connection . connect ( ) <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def destroy_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def reboot_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_images ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_sizes ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def list_locations ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> def deploy_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> WAIT_PERIOD = <NUM_LIT:3> <EOL> password = None <EOL> if '<STR_LIT>' not in self . features [ "<STR_LIT>" ] : <EOL> if '<STR_LIT:password>' not in self . features [ "<STR_LIT>" ] : <EOL> raise NotImplementedError , '<STR_LIT>' <EOL> if not kwargs . has_key ( '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = NodeAuthPassword ( os . urandom ( <NUM_LIT:16> ) . encode ( '<STR_LIT>' ) ) <EOL> password = kwargs [ '<STR_LIT>' ] . password <EOL> node = self . create_node ( ** kwargs ) <EOL> try : <EOL> if '<STR_LIT>' in self . features [ "<STR_LIT>" ] : <EOL> password = node . extra . get ( '<STR_LIT:password>' ) <EOL> start = time . time ( ) <EOL> end = start + ( <NUM_LIT> * <NUM_LIT:15> ) <EOL> while time . time ( ) < end : <EOL> time . sleep ( WAIT_PERIOD ) <EOL> nodes = self . list_nodes ( ) <EOL> nodes = filter ( lambda n : n . uuid == node . uuid , nodes ) <EOL> if len ( nodes ) == <NUM_LIT:0> : <EOL> raise DeploymentError ( node , "<STR_LIT>" % node ) <EOL> if len ( nodes ) > <NUM_LIT:1> : <EOL> raise DeploymentError ( node , "<STR_LIT>" % node ) <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> if node . public_ip is not None and node . public_ip != "<STR_LIT>" and node . state == NodeState . RUNNING : <EOL> break <EOL> ssh_username = kwargs . get ( '<STR_LIT>' , '<STR_LIT:root>' ) <EOL> ssh_port = kwargs . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> client = SSHClient ( hostname = node . public_ip [ <NUM_LIT:0> ] , <EOL> port = ssh_port , username = ssh_username , <EOL> password = password ) <EOL> laste = None <EOL> while time . time ( ) < end : <EOL> laste = None <EOL> try : <EOL> client . connect ( ) <EOL> break <EOL> except ( IOError , socket . gaierror , socket . error ) , e : <EOL> laste = e <EOL> time . sleep ( WAIT_PERIOD ) <EOL> if laste is not None : <EOL> raise e <EOL> tries = <NUM_LIT:3> <EOL> while tries >= <NUM_LIT:0> : <EOL> try : <EOL> n = kwargs [ "<STR_LIT>" ] . run ( node , client ) <EOL> client . close ( ) <EOL> break <EOL> except Exception , e : <EOL> tries -= <NUM_LIT:1> <EOL> if tries == <NUM_LIT:0> : <EOL> raise <EOL> client . connect ( ) <EOL> except DeploymentError , e : <EOL> raise <EOL> except Exception , e : <EOL> raise DeploymentError ( node , e ) <EOL> return n <EOL> def is_private_subnet ( ip ) : <EOL> """<STR_LIT>""" <EOL> priv_subnets = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> ip = struct . unpack ( '<STR_LIT:I>' , socket . inet_aton ( ip ) ) [ <NUM_LIT:0> ] <EOL> for network in priv_subnets : <EOL> subnet = struct . unpack ( '<STR_LIT:I>' , socket . inet_aton ( network [ '<STR_LIT>' ] ) ) [ <NUM_LIT:0> ] <EOL> mask = struct . unpack ( '<STR_LIT:I>' , socket . inet_aton ( network [ '<STR_LIT>' ] ) ) [ <NUM_LIT:0> ] <EOL> if ( ip & mask ) == ( subnet & mask ) : <EOL> return True <EOL> return False <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
