<s> from django . core . management . base import BaseCommand , CommandError <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from expedient . clearinghouse . defaultsettings . site import SITE_DOMAIN as SITE_DOMAIN_DEFAULT <EOL> from expedient . clearinghouse . slice . models import Slice <EOL> from expedient . common . utils . mail import send_mail <EOL> import time <EOL> import signal <EOL> import getpass <EOL> try : <EOL> import cpickle as pickle <EOL> except : <EOL> import pickle <EOL> import os <EOL> import subprocess <EOL> path = "<STR_LIT>" <EOL> TIMEOUT = <NUM_LIT> <EOL> FLOWVISOR_SLEEP_TIME = <NUM_LIT:10> <EOL> class Command ( BaseCommand ) : <EOL> args = "<STR_LIT>" <EOL> help = "<STR_LIT>" <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if args [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> self . stdout . write ( "<STR_LIT>" % self . stop_slices ( ) ) <EOL> elif args [ <NUM_LIT:0> ] == "<STR_LIT:start>" : <EOL> self . stdout . write ( "<STR_LIT>" % self . start_slices ( ) ) <EOL> else : <EOL> raise Exception <EOL> except : <EOL> self . stdout . write ( "<STR_LIT>" % ( self . help , self . args ) ) <EOL> def stop_slices ( self ) : <EOL> """<STR_LIT>""" <EOL> ids = get_conflictive_slices_ids ( ) <EOL> errors = [ ] <EOL> slice_id = "<STR_LIT>" <EOL> slices = filter_own_slices ( ids ) <EOL> for slice in slices : <EOL> aggs = slice . aggregates . filter ( leaf_name = "<STR_LIT>" ) <EOL> for agg in aggs : <EOL> try : <EOL> slice_id = str ( SITE_DOMAIN_DEFAULT ) + "<STR_LIT:_>" + str ( slice . id ) <EOL> print "<STR_LIT>" , slice_id , "<STR_LIT>" , str ( agg ) <EOL> time . sleep ( FLOWVISOR_SLEEP_TIME ) <EOL> with Timeout ( TIMEOUT ) : <EOL> agg . as_leaf_class ( ) . client . proxy . delete_slice ( slice_id ) <EOL> slice . started = False <EOL> slice . save ( ) <EOL> except Exception as e : <EOL> message = """<STR_LIT>""" % ( str ( agg . as_leaf_class ( ) ) , slice . name , slice_id , e ) <EOL> send_mail ( "<STR_LIT>" , message , "<STR_LIT>" , [ settings . ROOT_EMAIL ] ) <EOL> errors . append ( message ) <EOL> if errors : <EOL> return "<STR_LIT>" % str ( errors ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def start_slices ( self ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> slice_ids = [ ] <EOL> try : <EOL> f = open ( "<STR_LIT>" % path , "<STR_LIT:r>" ) <EOL> ids = pickle . load ( f ) <EOL> f . close ( ) <EOL> os . remove ( "<STR_LIT>" % path ) <EOL> slices = filter_own_slices ( ids ) <EOL> for ( counter , slice ) in enumerate ( slices ) : <EOL> aggs = slice . aggregates . filter ( leaf_name = "<STR_LIT>" ) <EOL> slice_id = str ( settings . SITE_DOMAIN ) + "<STR_LIT:_>" + str ( slice . id ) <EOL> slice_ids . append ( { "<STR_LIT:id>" : slice_id , "<STR_LIT>" : ids [ counter ] [ '<STR_LIT>' ] } ) <EOL> for agg in aggs : <EOL> try : <EOL> print "<STR_LIT>" , slice_id , "<STR_LIT>" , str ( agg ) <EOL> time . sleep ( FLOWVISOR_SLEEP_TIME ) <EOL> with Timeout ( TIMEOUT ) : <EOL> agg . as_leaf_class ( ) . client . proxy . create_slice ( slice_id , slice . project . name , slice . project . description , slice . name , slice . description , slice . openflowsliceinfo . controller_url , slice . owner . email , slice . openflowsliceinfo . password , agg . as_leaf_class ( ) . _get_slivers ( slice ) ) <EOL> slice . started = True <EOL> slice . save ( ) <EOL> except Exception as e : <EOL> message = """<STR_LIT>""" % ( str ( agg . as_leaf_class ( ) ) , slice . name , slice_id , e ) <EOL> send_mail ( "<STR_LIT>" , message , "<STR_LIT>" , [ settings . ROOT_EMAIL ] ) <EOL> errors . append ( message ) <EOL> f = open ( "<STR_LIT>" % path , "<STR_LIT:w>" ) <EOL> pickle . dump ( slice_ids , f ) <EOL> f . close ( ) <EOL> if errors : <EOL> return "<STR_LIT>" % str ( errors ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> except Exception as e : <EOL> print e <EOL> return "<STR_LIT>" <EOL> def filter_own_slices ( slice_ids ) : <EOL> slices = [ ] <EOL> for slice_id in slice_ids : <EOL> try : <EOL> filtered_slice = Slice . objects . get ( id = slice_id [ '<STR_LIT:id>' ] ) <EOL> filter_ok = True <EOL> for keyword in slice_id [ '<STR_LIT>' ] : <EOL> if keyword not in filtered_slice . name : <EOL> filter_ok = False <EOL> break <EOL> if filter_ok : <EOL> slices . append ( filtered_slice ) <EOL> except : <EOL> pass <EOL> return slices <EOL> def get_conflictive_slices_ids ( ) : <EOL> def get_id ( slice_id ) : <EOL> i = len ( slice_id ) - <NUM_LIT:1> <EOL> slice_id <EOL> buffer = [ ] <EOL> while ( i ) > <NUM_LIT:0> : <EOL> try : <EOL> int ( slice_id [ i ] ) <EOL> buffer . insert ( <NUM_LIT:0> , str ( slice_id [ i ] ) ) <EOL> except : <EOL> break <EOL> i -= <NUM_LIT:1> <EOL> return "<STR_LIT>" . join ( buffer ) <EOL> def get_keywords ( slice_id ) : <EOL> return str ( slice_id ) . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . strip ( ) . split ( "<STR_LIT:_>" ) <EOL> def write_in_file ( ids ) : <EOL> f = open ( "<STR_LIT>" % path , "<STR_LIT:w>" ) <EOL> pickle . dump ( ids , f ) <EOL> f . close ( ) <EOL> def authenticate_fvctl ( ) : <EOL> print "<STR_LIT>" <EOL> try : <EOL> print "<STR_LIT>" <EOL> fvctl_pass = getpass . getpass ( ) <EOL> f = open ( "<STR_LIT>" % path , "<STR_LIT:w>" ) <EOL> f . write ( fvctl_pass ) <EOL> f . close ( ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' % path ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> p = subprocess . Popen ( cmd , shell = False , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> time . sleep ( FLOWVISOR_SLEEP_TIME ) <EOL> slices , err = p . communicate ( ) <EOL> if err : <EOL> raise Exception <EOL> except : <EOL> authenticate_fvctl ( ) <EOL> authenticate_fvctl ( ) <EOL> f = open ( "<STR_LIT>" % path , "<STR_LIT:r>" ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' % path ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> p = subprocess . Popen ( cmd , shell = False , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> time . sleep ( FLOWVISOR_SLEEP_TIME ) <EOL> slices , err = p . communicate ( ) <EOL> if not err : <EOL> slices = slices . split ( "<STR_LIT:\n>" ) <EOL> ids_keywords = list ( ) <EOL> current_site_domain = SITE_DOMAIN_DEFAULT <EOL> current_site_domain = current_site_domain . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) . replace ( "<STR_LIT::>" , "<STR_LIT:_>" ) . replace ( "<STR_LIT:=>" , "<STR_LIT:_>" ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) . replace ( "<STR_LIT:'>" , "<STR_LIT:_>" ) <EOL> if not SITE_DOMAIN_DEFAULT == settings . SITE_DOMAIN : <EOL> for slice in slices : <EOL> if str ( current_site_domain ) in str ( slice ) : <EOL> id = get_id ( slice ) <EOL> keywords = get_keywords ( slice ) <EOL> if id : <EOL> ids_keywords . append ( { "<STR_LIT:id>" : id , "<STR_LIT>" : keywords } ) <EOL> else : <EOL> print "<STR_LIT>" % str ( err ) <EOL> write_in_file ( ids_keywords ) <EOL> f . close ( ) <EOL> os . remove ( "<STR_LIT>" % path ) <EOL> return ids_keywords <EOL> class Timeout ( ) : <EOL> """<STR_LIT>""" <EOL> class Timeout ( Exception ) : <EOL> pass <EOL> def __init__ ( self , sec ) : <EOL> self . sec = sec <EOL> def __enter__ ( self ) : <EOL> signal . signal ( signal . SIGALRM , self . raise_timeout ) <EOL> signal . alarm ( self . sec ) <EOL> def __exit__ ( self , * args ) : <EOL> signal . alarm ( <NUM_LIT:0> ) <EOL> def raise_timeout ( self , * args ) : <EOL> raise Timeout . Timeout ( ) </s>
