<s> """<STR_LIT>""" <EOL> from libcloud . interface import INodeDriver <EOL> from libcloud . base import NodeDriver , NodeSize , NodeLocation <EOL> from libcloud . base import NodeImage , Node <EOL> from libcloud . base import Response , ConnectionUserAndKey <EOL> from libcloud . types import Provider , NodeState , InvalidCredsException <EOL> from libcloud . base import is_private_subnet <EOL> from zope . interface import implements <EOL> import time <EOL> import base64 <EOL> import httplib <EOL> import socket <EOL> import os <EOL> try : import json <EOL> except : import simplejson as json <EOL> API_HOST = '<STR_LIT>' <EOL> API_PORT = ( <NUM_LIT> , <NUM_LIT> ) <EOL> class ECPResponse ( Response ) : <EOL> def success ( self ) : <EOL> if self . status == httplib . OK or self . status == httplib . CREATED : <EOL> try : <EOL> j_body = json . loads ( self . body ) <EOL> except ValueError : <EOL> self . error = "<STR_LIT>" <EOL> return False <EOL> if j_body [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> self . error = "<STR_LIT>" % j_body [ '<STR_LIT:message>' ] <EOL> return False <EOL> elif self . status == httplib . UNAUTHORIZED : <EOL> raise InvalidCredsException ( ) <EOL> else : <EOL> self . error = "<STR_LIT>" % self . status <EOL> return False <EOL> def parse_error ( self ) : <EOL> return self . error <EOL> def parse_body ( self ) : <EOL> return json . loads ( self . body ) <EOL> def getheaders ( self ) : <EOL> return self . headers <EOL> class ECPConnection ( ConnectionUserAndKey ) : <EOL> """<STR_LIT>""" <EOL> responseCls = ECPResponse <EOL> host = API_HOST <EOL> port = API_PORT <EOL> def add_default_headers ( self , headers ) : <EOL> username = self . user_id <EOL> password = self . key <EOL> base64string = base64 . encodestring ( <EOL> '<STR_LIT>' % ( username , password ) ) [ : - <NUM_LIT:1> ] <EOL> authheader = "<STR_LIT>" % base64string <EOL> headers [ '<STR_LIT>' ] = authheader <EOL> return headers <EOL> def _encode_multipart_formdata ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> boundary = '<STR_LIT>' <EOL> while boundary in '<STR_LIT>' . join ( fields ) : <EOL> boundary = os . urandom ( <NUM_LIT:16> ) . encode ( '<STR_LIT>' ) <EOL> L = [ ] <EOL> for i in fields : <EOL> L . append ( '<STR_LIT>' + boundary ) <EOL> L . append ( '<STR_LIT>' % i ) <EOL> L . append ( '<STR_LIT>' ) <EOL> L . append ( fields [ i ] ) <EOL> L . append ( '<STR_LIT>' + boundary + '<STR_LIT>' ) <EOL> L . append ( '<STR_LIT>' ) <EOL> body = '<STR_LIT:\r\n>' . join ( L ) <EOL> content_type = '<STR_LIT>' % boundary <EOL> header = { '<STR_LIT:Content-Type>' : content_type } <EOL> return header , body <EOL> class ECPNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> type = Provider . ECP <EOL> connectionCls = ECPConnection <EOL> implements ( INodeDriver ) <EOL> def list_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . connection . request ( '<STR_LIT>' ) . parse_body ( ) <EOL> nodes = [ ] <EOL> for vm in res [ '<STR_LIT>' ] : <EOL> node = self . _to_node ( vm ) <EOL> if not node == None : <EOL> nodes . append ( node ) <EOL> return nodes <EOL> def _to_node ( self , vm ) : <EOL> """<STR_LIT>""" <EOL> if not vm [ '<STR_LIT:state>' ] == "<STR_LIT>" : <EOL> return None <EOL> iplist = [ interface [ '<STR_LIT>' ] for interface in vm [ '<STR_LIT>' ] if interface [ '<STR_LIT>' ] != '<STR_LIT:127.0.0.1>' ] <EOL> public_ips = [ ] <EOL> private_ips = [ ] <EOL> for ip in iplist : <EOL> try : <EOL> socket . inet_aton ( ip ) <EOL> except socket . error : <EOL> continue <EOL> if is_private_subnet ( ip ) : <EOL> private_ips . append ( ip ) <EOL> else : <EOL> public_ips . append ( ip ) <EOL> n = Node ( <EOL> id = vm [ '<STR_LIT>' ] , <EOL> name = vm [ '<STR_LIT:name>' ] , <EOL> state = NodeState . RUNNING , <EOL> public_ip = public_ips , <EOL> private_ip = private_ips , <EOL> driver = self , <EOL> ) <EOL> return n <EOL> def reboot_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> d = self . connection . _encode_multipart_formdata ( { '<STR_LIT:action>' : '<STR_LIT>' } ) <EOL> self . connection . request ( <EOL> '<STR_LIT>' % node . id , <EOL> method = '<STR_LIT:POST>' , <EOL> headers = d [ <NUM_LIT:0> ] , <EOL> data = d [ <NUM_LIT:1> ] <EOL> ) . parse_body ( ) <EOL> node . state = NodeState . REBOOTING <EOL> while node . state == NodeState . REBOOTING : <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' % node . id <EOL> ) . parse_body ( ) <EOL> if response [ '<STR_LIT>' ] [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> node . state = NodeState . TERMINATED <EOL> else : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> d = self . connection . _encode_multipart_formdata ( { '<STR_LIT:action>' : '<STR_LIT:start>' } ) <EOL> self . connection . request ( <EOL> '<STR_LIT>' % node . id , <EOL> method = '<STR_LIT:POST>' , <EOL> headers = d [ <NUM_LIT:0> ] , <EOL> data = d [ <NUM_LIT:1> ] <EOL> ) . parse_body ( ) <EOL> node . state = NodeState . RUNNING <EOL> return True <EOL> def destroy_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> d = self . connection . _encode_multipart_formdata ( { '<STR_LIT:action>' : '<STR_LIT>' } ) <EOL> self . connection . request ( <EOL> '<STR_LIT>' % node . id , <EOL> method = '<STR_LIT:POST>' , <EOL> headers = d [ <NUM_LIT:0> ] , <EOL> data = d [ <NUM_LIT:1> ] <EOL> ) . parse_body ( ) <EOL> node . state = NodeState . PENDING <EOL> while node . state == NodeState . PENDING : <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' % node . id <EOL> ) . parse_body ( ) <EOL> if response [ '<STR_LIT>' ] [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> node . state = NodeState . TERMINATED <EOL> else : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> d = self . connection . _encode_multipart_formdata ( { '<STR_LIT:action>' : '<STR_LIT>' } ) <EOL> self . connection . request ( <EOL> '<STR_LIT>' % ( node . id ) , <EOL> method = '<STR_LIT:POST>' , <EOL> headers = d [ <NUM_LIT:0> ] , <EOL> data = d [ <NUM_LIT:1> ] <EOL> ) . parse_body ( ) <EOL> return True <EOL> def list_images ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' ) . parse_body ( ) <EOL> images = [ ] <EOL> for ptemplate in response [ '<STR_LIT>' ] : <EOL> images . append ( NodeImage ( <EOL> id = ptemplate [ '<STR_LIT>' ] , <EOL> name = '<STR_LIT>' % ( ptemplate [ '<STR_LIT:name>' ] , ptemplate [ '<STR_LIT:description>' ] ) , <EOL> driver = self , <EOL> ) ) <EOL> return images <EOL> def list_sizes ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' ) . parse_body ( ) <EOL> sizes = [ ] <EOL> for htemplate in response [ '<STR_LIT>' ] : <EOL> sizes . append ( NodeSize ( <EOL> id = htemplate [ '<STR_LIT>' ] , <EOL> name = htemplate [ '<STR_LIT:name>' ] , <EOL> ram = htemplate [ '<STR_LIT>' ] , <EOL> disk = <NUM_LIT:0> , <EOL> bandwidth = <NUM_LIT:0> , <EOL> price = <NUM_LIT:0> , <EOL> driver = self , <EOL> ) ) <EOL> return sizes <EOL> def list_locations ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> NodeLocation ( id = <NUM_LIT:1> , <EOL> name = "<STR_LIT>" , <EOL> country = '<STR_LIT>' , <EOL> driver = self ) , <EOL> ] <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> res = self . connection . request ( '<STR_LIT>' ) . parse_body ( ) <EOL> network = res [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> data = { <EOL> '<STR_LIT:name>' : str ( kwargs [ '<STR_LIT:name>' ] ) , <EOL> '<STR_LIT>' : str ( kwargs [ '<STR_LIT:image>' ] . id ) , <EOL> '<STR_LIT>' : str ( kwargs [ '<STR_LIT:size>' ] . id ) , <EOL> '<STR_LIT>' : str ( network ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> d = self . connection . _encode_multipart_formdata ( data ) <EOL> response = self . connection . request ( <EOL> '<STR_LIT>' , <EOL> method = '<STR_LIT>' , <EOL> headers = d [ <NUM_LIT:0> ] , <EOL> data = d [ <NUM_LIT:1> ] <EOL> ) . parse_body ( ) <EOL> n = Node ( <EOL> id = response [ '<STR_LIT>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> state = NodeState . PENDING , <EOL> public_ip = [ ] , <EOL> private_ip = [ ] , <EOL> driver = self , <EOL> ) <EOL> return n </s>
