<s> """<STR_LIT>""" <EOL> import os <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> import itertools <EOL> import binascii <EOL> from copy import copy <EOL> from libcloud . utils . py3 import PY3 <EOL> from libcloud . common . linode import ( API_ROOT , LinodeException , <EOL> LinodeConnection , LINODE_PLAN_IDS , <EOL> LINODE_DISK_FILESYSTEMS ) <EOL> from libcloud . compute . types import Provider , NodeState <EOL> from libcloud . compute . base import NodeDriver , NodeSize , Node , NodeLocation <EOL> from libcloud . compute . base import NodeAuthPassword , NodeAuthSSHKey <EOL> from libcloud . compute . base import NodeImage , StorageVolume <EOL> class LinodeNodeDriver ( NodeDriver ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . LINODE <EOL> name = "<STR_LIT>" <EOL> website = '<STR_LIT>' <EOL> connectionCls = LinodeConnection <EOL> _linode_plan_ids = LINODE_PLAN_IDS <EOL> _linode_disk_filesystems = LINODE_DISK_FILESYSTEMS <EOL> features = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:password>' ] } <EOL> def __init__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . datacenter = None <EOL> NodeDriver . __init__ ( self , key ) <EOL> LINODE_STATES = { <EOL> ( - <NUM_LIT:2> ) : NodeState . UNKNOWN , <EOL> ( - <NUM_LIT:1> ) : NodeState . PENDING , <EOL> <NUM_LIT:0> : NodeState . PENDING , <EOL> <NUM_LIT:1> : NodeState . RUNNING , <EOL> <NUM_LIT:2> : NodeState . TERMINATED , <EOL> <NUM_LIT:3> : NodeState . REBOOTING , <EOL> <NUM_LIT:4> : NodeState . UNKNOWN <EOL> } <EOL> def list_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> return self . _to_nodes ( data ) <EOL> def reboot_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : node . id } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> return True <EOL> def destroy_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : node . id , <EOL> "<STR_LIT>" : True } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> return True <EOL> def create_node ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = kwargs [ "<STR_LIT:name>" ] <EOL> image = kwargs [ "<STR_LIT:image>" ] <EOL> size = kwargs [ "<STR_LIT:size>" ] <EOL> auth = self . _get_and_check_auth ( kwargs [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT:location>" in kwargs : <EOL> chosen = kwargs [ "<STR_LIT:location>" ] . id <EOL> elif self . datacenter : <EOL> chosen = self . datacenter <EOL> else : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> plans = self . list_sizes ( ) <EOL> if size . id not in [ p . id for p in plans ] : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> payment = "<STR_LIT:1>" if "<STR_LIT>" not in kwargs else str ( kwargs [ "<STR_LIT>" ] ) <EOL> if payment not in [ "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ssh = None <EOL> root = None <EOL> if isinstance ( auth , NodeAuthSSHKey ) : <EOL> ssh = auth . pubkey <EOL> elif isinstance ( auth , NodeAuthPassword ) : <EOL> root = auth . password <EOL> if not ssh and not root : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if root is not None and len ( root ) < <NUM_LIT:6> : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> swap = <NUM_LIT> if "<STR_LIT>" not in kwargs else int ( kwargs [ "<STR_LIT>" ] ) <EOL> except : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> imagesize = ( size . disk - swap ) if "<STR_LIT>" not in kwargs else int ( kwargs [ "<STR_LIT>" ] ) <EOL> if ( imagesize + swap ) > size . disk : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> distros = self . list_images ( ) <EOL> if image . id not in [ d . id for d in distros ] : <EOL> raise LinodeException ( <NUM_LIT> , <EOL> "<STR_LIT>" ) <EOL> if "<STR_LIT>" in kwargs : <EOL> kernel = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> if image . extra [ '<STR_LIT>' ] : <EOL> kernel = <NUM_LIT> <EOL> else : <EOL> kernel = <NUM_LIT> <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> kernels = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> if kernel not in [ z [ "<STR_LIT>" ] for z in kernels ] : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> comments = "<STR_LIT>" if "<STR_LIT>" not in kwargs else kwargs [ "<STR_LIT>" ] <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : chosen , <EOL> "<STR_LIT>" : size . id , <EOL> "<STR_LIT>" : payment <EOL> } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> linode = { "<STR_LIT:id>" : data [ "<STR_LIT>" ] } <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : name <EOL> } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] : <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] <EOL> } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> label = { <EOL> "<STR_LIT>" : "<STR_LIT>" % linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( linode [ "<STR_LIT:id>" ] , image . name ) , <EOL> "<STR_LIT>" : "<STR_LIT>" % linode [ "<STR_LIT:id>" ] <EOL> } <EOL> for what in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if what in kwargs : <EOL> label [ what ] = kwargs [ what ] <EOL> if not root : <EOL> root = binascii . b2a_base64 ( os . urandom ( <NUM_LIT:8> ) ) . decode ( '<STR_LIT:ascii>' ) . strip ( ) <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : image . id , <EOL> "<STR_LIT>" : label [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : imagesize , <EOL> "<STR_LIT>" : root , <EOL> } <EOL> if ssh : <EOL> params [ "<STR_LIT>" ] = ssh <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> linode [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : label [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : swap <EOL> } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> linode [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] <EOL> disks = "<STR_LIT>" % ( linode [ "<STR_LIT>" ] , linode [ "<STR_LIT>" ] ) <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : kernel , <EOL> "<STR_LIT>" : label [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : comments , <EOL> "<STR_LIT>" : disks <EOL> } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> linode [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : linode [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : linode [ "<STR_LIT>" ] <EOL> } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> params = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : linode [ "<STR_LIT:id>" ] } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> nodes = self . _to_nodes ( data ) <EOL> if len ( nodes ) == <NUM_LIT:1> : <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> if getattr ( auth , "<STR_LIT>" , False ) : <EOL> node . extra [ '<STR_LIT:password>' ] = auth . password <EOL> return node <EOL> return None <EOL> def list_sizes ( self , location = None ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> sizes = [ ] <EOL> for obj in data : <EOL> n = NodeSize ( id = obj [ "<STR_LIT>" ] , name = obj [ "<STR_LIT>" ] , ram = obj [ "<STR_LIT>" ] , <EOL> disk = ( obj [ "<STR_LIT>" ] * <NUM_LIT> ) , bandwidth = obj [ "<STR_LIT>" ] , <EOL> price = obj [ "<STR_LIT>" ] , driver = self . connection . driver ) <EOL> sizes . append ( n ) <EOL> return sizes <EOL> def list_images ( self ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> distros = [ ] <EOL> for obj in data : <EOL> i = NodeImage ( id = obj [ "<STR_LIT>" ] , <EOL> name = obj [ "<STR_LIT>" ] , <EOL> driver = self . connection . driver , <EOL> extra = { '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] } ) <EOL> distros . append ( i ) <EOL> return distros <EOL> def list_locations ( self ) : <EOL> """<STR_LIT>""" <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> nl = [ ] <EOL> for dc in data : <EOL> country = None <EOL> if "<STR_LIT>" in dc [ "<STR_LIT>" ] : <EOL> country = "<STR_LIT>" <EOL> elif "<STR_LIT>" in dc [ "<STR_LIT>" ] : <EOL> country = "<STR_LIT>" <EOL> elif "<STR_LIT>" in dc [ "<STR_LIT>" ] : <EOL> country = "<STR_LIT>" <EOL> else : <EOL> country = "<STR_LIT>" <EOL> nl . append ( NodeLocation ( dc [ "<STR_LIT>" ] , <EOL> dc [ "<STR_LIT>" ] , <EOL> country , <EOL> self ) ) <EOL> return nl <EOL> def linode_set_datacenter ( self , dc ) : <EOL> """<STR_LIT>""" <EOL> did = dc . id <EOL> params = { "<STR_LIT>" : "<STR_LIT>" } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> for datacenter in data : <EOL> if did == dc [ "<STR_LIT>" ] : <EOL> self . datacenter = did <EOL> return <EOL> dcs = "<STR_LIT:U+002CU+0020>" . join ( [ d [ "<STR_LIT>" ] for d in data ] ) <EOL> self . datacenter = None <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" % dcs ) <EOL> def destroy_volume ( self , volume ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( volume , StorageVolume ) : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if volume . extra [ "<STR_LIT>" ] is None : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : volume . extra [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : volume . id , <EOL> } <EOL> self . connection . request ( API_ROOT , params = params ) <EOL> return True <EOL> def ex_create_volume ( self , size , name , node , fs_type ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node , Node ) : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> total_space = node . extra [ '<STR_LIT>' ] <EOL> existing_volumes = self . ex_list_volumes ( node ) <EOL> used_space = <NUM_LIT:0> <EOL> for volume in existing_volumes : <EOL> used_space = used_space + volume . size <EOL> available_space = total_space - used_space <EOL> if available_space < size : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" % available_space ) <EOL> if fs_type not in self . _linode_disk_filesystems : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : node . id , <EOL> "<STR_LIT>" : name , <EOL> "<STR_LIT>" : fs_type , <EOL> "<STR_LIT>" : size <EOL> } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> volume = data [ "<STR_LIT>" ] <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : node . id , <EOL> "<STR_LIT>" : volume <EOL> } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> return self . _to_volumes ( data ) [ <NUM_LIT:0> ] <EOL> def ex_list_volumes ( self , node , disk_id = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node , Node ) : <EOL> raise LinodeException ( <NUM_LIT> , "<STR_LIT>" ) <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : node . id <EOL> } <EOL> if disk_id is not None : <EOL> params [ "<STR_LIT>" ] = disk_id <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> return self . _to_volumes ( data ) <EOL> def _to_volumes ( self , objs ) : <EOL> """<STR_LIT>""" <EOL> volumes = { } <EOL> for o in objs : <EOL> vid = o [ "<STR_LIT>" ] <EOL> volumes [ vid ] = vol = StorageVolume ( id = vid , name = o [ "<STR_LIT>" ] , <EOL> size = int ( o [ "<STR_LIT>" ] ) , <EOL> driver = self . connection . driver ) <EOL> vol . extra = copy ( o ) <EOL> return list ( volumes . values ( ) ) <EOL> def _to_nodes ( self , objs ) : <EOL> """<STR_LIT>""" <EOL> nodes = { } <EOL> batch = [ ] <EOL> for o in objs : <EOL> lid = o [ "<STR_LIT>" ] <EOL> nodes [ lid ] = n = Node ( id = lid , name = o [ "<STR_LIT>" ] , public_ips = [ ] , <EOL> private_ips = [ ] , <EOL> state = self . LINODE_STATES [ o [ "<STR_LIT>" ] ] , <EOL> driver = self . connection . driver ) <EOL> n . extra = copy ( o ) <EOL> n . extra [ "<STR_LIT>" ] = self . _linode_plan_ids . get ( o . get ( "<STR_LIT>" ) ) <EOL> batch . append ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : lid } ) <EOL> ip_answers = [ ] <EOL> args = [ iter ( batch ) ] * <NUM_LIT> <EOL> if PY3 : <EOL> izip_longest = itertools . zip_longest <EOL> else : <EOL> izip_longest = getattr ( itertools , '<STR_LIT>' , _izip_longest ) <EOL> for twenty_five in izip_longest ( * args ) : <EOL> twenty_five = [ q for q in twenty_five if q ] <EOL> params = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : json . dumps ( twenty_five ) } <EOL> req = self . connection . request ( API_ROOT , params = params ) <EOL> if not req . success ( ) or len ( req . objects ) == <NUM_LIT:0> : <EOL> return None <EOL> ip_answers . extend ( req . objects ) <EOL> for ip_list in ip_answers : <EOL> for ip in ip_list : <EOL> lid = ip [ "<STR_LIT>" ] <EOL> which = nodes [ lid ] . public_ips if ip [ "<STR_LIT>" ] == <NUM_LIT:1> else nodes [ lid ] . private_ips <EOL> which . append ( ip [ "<STR_LIT>" ] ) <EOL> return list ( nodes . values ( ) ) <EOL> def _izip_longest ( * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> fillvalue = kwds . get ( '<STR_LIT>' ) <EOL> def sentinel ( counter = ( [ fillvalue ] * ( len ( args ) - <NUM_LIT:1> ) ) . pop ) : <EOL> yield counter ( ) <EOL> fillers = itertools . repeat ( fillvalue ) <EOL> iters = [ itertools . chain ( it , sentinel ( ) , fillers ) for it in args ] <EOL> try : <EOL> for tup in itertools . izip ( * iters ) : <EOL> yield tup <EOL> except IndexError : <EOL> pass </s>
