<s> import os <EOL> import pdb <EOL> import sys <EOL> import tempfile <EOL> import traceback <EOL> sys . path . append ( "<STR_LIT>" ) <EOL> from translator . toscalib . tosca_template import ToscaTemplate <EOL> from core . models import Slice , Instance , User , Flavor , Node , Image <EOL> from nodeselect import XOSNodeSelector <EOL> from imageselect import XOSImageSelector <EOL> import resources <EOL> class XOSTosca ( object ) : <EOL> def __init__ ( self , tosca_yaml , parent_dir = None , log_to_console = False ) : <EOL> if not parent_dir : <EOL> parent_dir = os . getcwd ( ) <EOL> tmp_pathname = None <EOL> try : <EOL> ( tmp_handle , tmp_pathname ) = tempfile . mkstemp ( dir = parent_dir ) <EOL> os . write ( tmp_handle , tosca_yaml ) <EOL> os . close ( tmp_handle ) <EOL> self . template = ToscaTemplate ( tmp_pathname ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> raise <EOL> finally : <EOL> if tmp_pathname : <EOL> os . remove ( tmp_pathname ) <EOL> self . log_to_console = log_to_console <EOL> self . log_msgs = [ ] <EOL> self . compute_dependencies ( ) <EOL> self . deferred_sync = [ ] <EOL> self . ordered_nodetemplates = [ ] <EOL> self . ordered_names = self . topsort_dependencies ( ) <EOL> self . log ( "<STR_LIT>" % self . ordered_names ) <EOL> for name in self . ordered_names : <EOL> if name in self . nodetemplates_by_name : <EOL> self . ordered_nodetemplates . append ( self . nodetemplates_by_name [ name ] ) <EOL> def log ( self , msg ) : <EOL> if self . log_to_console : <EOL> print msg <EOL> self . log_msgs . append ( msg ) <EOL> def compute_dependencies ( self ) : <EOL> nodetemplates_by_name = { } <EOL> for nodetemplate in self . template . nodetemplates : <EOL> nodetemplates_by_name [ nodetemplate . name ] = nodetemplate <EOL> self . nodetemplates_by_name = nodetemplates_by_name <EOL> for nodetemplate in self . template . nodetemplates : <EOL> nodetemplate . dependencies = [ ] <EOL> nodetemplate . dependencies_names = [ ] <EOL> for reqs in nodetemplate . requirements : <EOL> for ( k , v ) in reqs . items ( ) : <EOL> name = v [ "<STR_LIT>" ] <EOL> if ( name in nodetemplates_by_name ) : <EOL> nodetemplate . dependencies . append ( nodetemplates_by_name [ name ] ) <EOL> nodetemplate . dependencies_names . append ( name ) <EOL> for sd_req in v . get ( "<STR_LIT>" , [ ] ) : <EOL> for ( sd_req_k , sd_req_v ) in sd_req . items ( ) : <EOL> name = sd_req_v [ "<STR_LIT>" ] <EOL> if ( name in nodetemplates_by_name ) : <EOL> nodetemplate . dependencies . append ( nodetemplates_by_name [ name ] ) <EOL> nodetemplate . dependencies_names . append ( name ) <EOL> def topsort_dependencies ( self ) : <EOL> g = self . nodetemplates_by_name <EOL> keys = set ( g . keys ( ) ) <EOL> values = set ( { } ) <EOL> for v in g . values ( ) : <EOL> values = values | set ( v . dependencies_names ) <EOL> all_nodes = list ( keys | values ) <EOL> steps = all_nodes <EOL> order = [ ] <EOL> stack = [ ] <EOL> unmarked = all_nodes <EOL> while unmarked : <EOL> stack . insert ( <NUM_LIT:0> , unmarked [ <NUM_LIT:0> ] ) <EOL> while ( stack ) : <EOL> n = stack [ <NUM_LIT:0> ] <EOL> add = True <EOL> try : <EOL> for m in g [ n ] . dependencies_names : <EOL> if ( m in unmarked ) : <EOL> add = False <EOL> stack . insert ( <NUM_LIT:0> , m ) <EOL> except KeyError : <EOL> pass <EOL> if ( add ) : <EOL> if ( n in steps and n not in order ) : <EOL> order . append ( n ) <EOL> item = stack . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> unmarked . remove ( item ) <EOL> except ValueError : <EOL> pass <EOL> noorder = list ( set ( steps ) - set ( order ) ) <EOL> return order + noorder <EOL> def execute ( self , user ) : <EOL> for nodetemplate in self . ordered_nodetemplates : <EOL> self . execute_nodetemplate ( user , nodetemplate ) <EOL> for obj in self . deferred_sync : <EOL> self . log ( "<STR_LIT>" % obj ) <EOL> obj . no_sync = False <EOL> obj . save ( ) <EOL> def execute_nodetemplate ( self , user , nodetemplate ) : <EOL> if nodetemplate . type in resources . resources : <EOL> cls = resources . resources [ nodetemplate . type ] <EOL> obj = cls ( user , nodetemplate , self ) <EOL> obj . create_or_update ( ) <EOL> self . deferred_sync = self . deferred_sync + obj . deferred_sync <EOL> def destroy ( self , user ) : <EOL> nodetemplates = self . ordered_nodetemplates <EOL> models = [ ] <EOL> for nodetemplate in nodetemplates : <EOL> if nodetemplate . type in resources . resources : <EOL> cls = resources . resources [ nodetemplate . type ] <EOL> obj = cls ( user , nodetemplate , self ) <EOL> for model in obj . get_existing_objs ( ) : <EOL> models . append ( ( obj , model ) ) <EOL> models . reverse ( ) <EOL> for ( resource , model ) in models : <EOL> resource . delete ( model ) <EOL> def name_to_xos_class ( self , user , name ) : <EOL> nt = self . nodetemplates_by_name . get ( name ) <EOL> if not nt : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> cls = resources . resources . get ( nt . type ) <EOL> if not cls : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> return ( nt , cls , cls . xos_model ) <EOL> def name_to_xos_model ( self , user , name ) : <EOL> ( nt , cls , model_class ) = self . name_to_xos_class ( user , name ) <EOL> obj = cls ( user , nt , self ) <EOL> existing_objs = obj . get_existing_objs ( ) <EOL> if not existing_objs : <EOL> raise Exception ( "<STR_LIT>" % ( cls . __name__ , name ) ) <EOL> return existing_objs [ <NUM_LIT:0> ] </s>
