<s> import json <EOL> import unicodedata <EOL> from cloudferry import data_storage <EOL> from cloudferry . lib . utils import log <EOL> LOG = log . getLogger ( __name__ ) <EOL> TRANSFER = "<STR_LIT>" <EOL> MIGRATE = "<STR_LIT>" <EOL> CONDENSE = "<STR_LIT>" <EOL> def normalize ( string ) : <EOL> """<STR_LIT>""" <EOL> return unicodedata . normalize ( '<STR_LIT>' , string ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> def get_key ( iteration , name ) : <EOL> return "<STR_LIT:_>" . join ( [ str ( iteration ) , name ] ) <EOL> class Actions ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . iteration = <NUM_LIT:0> <EOL> self . name = name <EOL> self . new_step ( ) <EOL> def new_step ( self ) : <EOL> """<STR_LIT>""" <EOL> self . data = { <EOL> TRANSFER : [ ] , <EOL> MIGRATE : [ ] , <EOL> CONDENSE : [ ] <EOL> } <EOL> @ property <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> return get_key ( self . iteration , self . name ) <EOL> def dump_actions ( self ) : <EOL> """<STR_LIT>""" <EOL> data_storage . put ( self . key , json . dumps ( self . data ) ) <EOL> self . new_step ( ) <EOL> self . iteration += <NUM_LIT:1> <EOL> def add_migration_action ( self , vm_obj , target_node ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , vm_obj . vm_id , target_node . name ) <EOL> payload = [ normalize ( vm_obj . vm_id ) , normalize ( target_node . name ) ] <EOL> self . data [ MIGRATE ] . append ( payload ) <EOL> def add_transfer_action ( self , node_name ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , node_name ) <EOL> payload = normalize ( node_name ) <EOL> self . data [ TRANSFER ] . append ( payload ) <EOL> def add_condensation_action ( self , vm_obj , source_node , target_node ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , vm_obj . vm_id , <EOL> source_node . name , target_node . name ) <EOL> payload = [ normalize ( vm_obj . vm_id ) , normalize ( target_node . name ) ] <EOL> self . data [ CONDENSE ] . append ( payload ) <EOL> def get_freed_nodes ( iteration ) : <EOL> try : <EOL> condense_data = json . loads ( <EOL> data_storage . get ( get_key ( iteration , '<STR_LIT:source>' ) ) ) <EOL> return condense_data . get ( TRANSFER , [ ] ) <EOL> except ( TypeError , ValueError ) : <EOL> LOG . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
