<s> """<STR_LIT>""" <EOL> from twisted . internet import reactor , defer <EOL> from twisted . web import server , resource <EOL> from twisted . python import log <EOL> import json <EOL> import pprint <EOL> import os <EOL> from twisted . web . client import Agent <EOL> from treq . client import HTTPClient <EOL> import treq <EOL> class AdapterResource ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> isLeaf = True <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . _agent = Agent ( reactor ) <EOL> self . client = HTTPClient ( self . _agent ) <EOL> return resource . Resource . __init__ ( self , * args , ** kw ) <EOL> def render_POST ( self , request ) : <EOL> """<STR_LIT>""" <EOL> requestJson = json . loads ( request . content . read ( ) ) <EOL> if requestJson [ "<STR_LIT>" ] != "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" % <EOL> ( requestJson [ "<STR_LIT>" ] , ) ) <EOL> pprint . pprint ( os . environ ) <EOL> json_payload = requestJson [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> json_parsed = json . loads ( json_payload ) <EOL> self . base_url = os . environ . get ( "<STR_LIT>" ) <EOL> self . ip = os . environ . get ( "<STR_LIT>" ) <EOL> self . host_uuid = os . environ . get ( "<STR_LIT>" ) <EOL> def wait_until_volume_in_place ( result , fs ) : <EOL> """<STR_LIT>""" <EOL> dataset_id = result [ "<STR_LIT>" ] <EOL> def dataset_exists ( ) : <EOL> d = self . client . get ( self . base_url + "<STR_LIT>" ) <EOL> d . addCallback ( treq . json_content ) <EOL> def check_dataset_exists ( datasets ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" , self . ip , "<STR_LIT>" , datasets <EOL> matching_datasets = [ ] <EOL> for dataset in datasets : <EOL> if dataset [ "<STR_LIT>" ] == dataset_id : <EOL> matching_datasets . append ( dataset ) <EOL> if len ( matching_datasets ) == <NUM_LIT:1> : <EOL> if matching_datasets [ <NUM_LIT:0> ] [ "<STR_LIT>" ] == self . ip : <EOL> return True <EOL> return False <EOL> d . addCallback ( check_dataset_exists ) <EOL> return d <EOL> d = loop_until ( dataset_exists ) <EOL> d . addCallback ( lambda ignored : ( fs , dataset_id ) ) <EOL> return d <EOL> d = self . client . get ( self . base_url + "<STR_LIT>" ) <EOL> d . addCallback ( treq . json_content ) <EOL> def got_dataset_configuration ( configured_datasets ) : <EOL> configured_dataset_mapping = { } <EOL> for dataset in configured_datasets : <EOL> if dataset [ "<STR_LIT>" ] . get ( "<STR_LIT:name>" ) : <EOL> configured_dataset_mapping [ dataset [ "<STR_LIT>" ] . get ( "<STR_LIT:name>" ) ] = dataset <EOL> fs_create_deferreds = [ ] <EOL> old_binds = [ ] <EOL> if json_parsed [ '<STR_LIT>' ] [ '<STR_LIT>' ] is not None : <EOL> for bind in json_parsed [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> host_path , remainder = bind . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> if host_path . startswith ( "<STR_LIT>" ) : <EOL> fs = host_path [ len ( "<STR_LIT>" ) : ] <EOL> old_binds . append ( ( fs , remainder ) ) <EOL> if fs in configured_dataset_mapping : <EOL> dataset = configured_dataset_mapping [ fs ] <EOL> if dataset [ "<STR_LIT>" ] == self . ip : <EOL> fs_create_deferreds . append ( defer . succeed ( ( fs , dataset [ "<STR_LIT>" ] ) ) ) <EOL> else : <EOL> d = self . client . post ( <EOL> self . base_url + "<STR_LIT>" % ( <EOL> dataset [ "<STR_LIT>" ] . encode ( '<STR_LIT:ascii>' ) , ) , <EOL> json . dumps ( { "<STR_LIT>" : self . ip } ) , <EOL> headers = { '<STR_LIT:Content-Type>' : [ '<STR_LIT:application/json>' ] } ) <EOL> d . addCallback ( treq . json_content ) <EOL> d . addCallback ( wait_until_volume_in_place , fs = fs ) <EOL> fs_create_deferreds . append ( d ) <EOL> else : <EOL> d = self . client . post ( self . base_url + "<STR_LIT>" , <EOL> json . dumps ( { "<STR_LIT>" : self . ip , "<STR_LIT>" : { "<STR_LIT:name>" : fs } } ) , <EOL> headers = { '<STR_LIT:Content-Type>' : [ '<STR_LIT:application/json>' ] } ) <EOL> d . addCallback ( treq . json_content ) <EOL> d . addCallback ( wait_until_volume_in_place , fs = fs ) <EOL> fs_create_deferreds . append ( d ) <EOL> d = defer . gatherResults ( fs_create_deferreds ) <EOL> def got_created_and_moved_datasets ( list_new_datasets ) : <EOL> dataset_mapping = dict ( list_new_datasets ) <EOL> new_binds = [ ] <EOL> for fs , reminder in old_binds : <EOL> new_binds . append ( "<STR_LIT>" % <EOL> ( self . host_uuid , dataset_mapping [ fs ] , remainder ) ) <EOL> new_json_parsed = json_parsed . copy ( ) <EOL> new_json_parsed [ '<STR_LIT>' ] [ '<STR_LIT>' ] = new_binds <EOL> request . write ( json . dumps ( { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:POST>" , <EOL> "<STR_LIT>" : request . uri , <EOL> "<STR_LIT>" : json . dumps ( new_json_parsed ) } } ) ) <EOL> request . finish ( ) <EOL> d . addCallback ( got_created_and_moved_datasets ) <EOL> return d <EOL> d . addCallback ( got_dataset_configuration ) <EOL> d . addErrback ( log . err , '<STR_LIT>' ) <EOL> return server . NOT_DONE_YET <EOL> from twisted . internet . defer import maybeDeferred <EOL> from twisted . internet . task import deferLater <EOL> def loop_until ( predicate ) : <EOL> """<STR_LIT>""" <EOL> d = maybeDeferred ( predicate ) <EOL> def loop ( result ) : <EOL> if not result : <EOL> d = deferLater ( reactor , <NUM_LIT:0.1> , predicate ) <EOL> d . addCallback ( loop ) <EOL> return d <EOL> return result <EOL> d . addCallback ( loop ) <EOL> return d </s>
