<s> '''<STR_LIT>''' <EOL> import os , threading , subprocess <EOL> from core . misc . ipaddr import * <EOL> from core . misc . utils import * <EOL> from core . constants import * <EOL> from core . api import coreapi <EOL> from core . coreobj import PyCoreNode , PyCoreNetIf <EOL> from core . emane . nodes import EmaneNode <EOL> if os . uname ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> from core . netns . vnet import LxBrNet <EOL> from core . netns . vif import GreTap <EOL> elif os . uname ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> from core . bsd . vnet import NetgraphNet <EOL> class PhysicalNode ( PyCoreNode ) : <EOL> def __init__ ( self , session , objid = None , name = None , <EOL> nodedir = None , verbose = False , start = True ) : <EOL> PyCoreNode . __init__ ( self , session , objid , name , verbose = verbose , <EOL> start = start ) <EOL> self . nodedir = nodedir <EOL> self . up = start <EOL> self . lock = threading . RLock ( ) <EOL> self . _mounts = [ ] <EOL> if start : <EOL> self . startup ( ) <EOL> def boot ( self ) : <EOL> self . session . services . bootnodeservices ( self ) <EOL> def validate ( self ) : <EOL> self . session . services . validatenodeservices ( self ) <EOL> def startup ( self ) : <EOL> self . lock . acquire ( ) <EOL> try : <EOL> self . makenodedir ( ) <EOL> except OSError , e : <EOL> self . exception ( coreapi . CORE_EXCP_LEVEL_ERROR , <EOL> "<STR_LIT>" , e ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> def shutdown ( self ) : <EOL> if not self . up : <EOL> return <EOL> self . lock . acquire ( ) <EOL> while self . _mounts : <EOL> source , target = self . _mounts . pop ( - <NUM_LIT:1> ) <EOL> self . umount ( target ) <EOL> for netif in self . netifs ( ) : <EOL> netif . shutdown ( ) <EOL> self . rmnodedir ( ) <EOL> self . lock . release ( ) <EOL> def termcmdstring ( self , sh = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> return sh <EOL> def cmd ( self , args , wait = True ) : <EOL> '''<STR_LIT>''' <EOL> os . chdir ( self . nodedir ) <EOL> try : <EOL> if wait : <EOL> subprocess . call ( args ) <EOL> else : <EOL> subprocess . Popen ( args ) <EOL> except CalledProcessError , e : <EOL> self . warn ( "<STR_LIT>" % ( e , str ( args ) ) ) <EOL> def cmdresult ( self , args ) : <EOL> '''<STR_LIT>''' <EOL> os . chdir ( self . nodedir ) <EOL> tmp = subprocess . Popen ( args , stdin = open ( os . devnull , '<STR_LIT:r>' ) , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) <EOL> result , err = tmp . communicate ( ) <EOL> status = tmp . wait ( ) <EOL> return ( status , result ) <EOL> def shcmd ( self , cmdstr , sh = "<STR_LIT>" ) : <EOL> return self . cmd ( [ sh , "<STR_LIT:-c>" , cmdstr ] ) <EOL> def sethwaddr ( self , ifindex , addr ) : <EOL> '''<STR_LIT>''' <EOL> self . _netif [ ifindex ] . sethwaddr ( addr ) <EOL> ifname = self . ifname ( ifindex ) <EOL> if self . up : <EOL> ( status , result ) = self . cmdresult ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ifname , "<STR_LIT:address>" , str ( addr ) ] ) <EOL> if status : <EOL> self . exception ( coreapi . CORE_EXCP_LEVEL_ERROR , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % str ( addr ) ) <EOL> def addaddr ( self , ifindex , addr ) : <EOL> '''<STR_LIT>''' <EOL> if self . up : <EOL> self . cmd ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , str ( addr ) , <EOL> "<STR_LIT>" , self . ifname ( ifindex ) ] ) <EOL> self . _netif [ ifindex ] . addaddr ( addr ) <EOL> def deladdr ( self , ifindex , addr ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> self . _netif [ ifindex ] . deladdr ( addr ) <EOL> except ValueError : <EOL> self . warn ( "<STR_LIT>" % addr ) <EOL> if self . up : <EOL> self . cmd ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , str ( addr ) , <EOL> "<STR_LIT>" , self . ifname ( ifindex ) ] ) <EOL> def adoptnetif ( self , netif , ifindex , hwaddr , addrlist ) : <EOL> '''<STR_LIT>''' <EOL> netif . name = "<STR_LIT>" % ifindex <EOL> netif . node = self <EOL> self . addnetif ( netif , ifindex ) <EOL> if self . up : <EOL> self . cmd ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , netif . localname , "<STR_LIT>" ] ) <EOL> self . cmd ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , netif . localname , "<STR_LIT:name>" , netif . name ] ) <EOL> netif . localname = netif . name <EOL> if hwaddr : <EOL> self . sethwaddr ( ifindex , hwaddr ) <EOL> for addr in maketuple ( addrlist ) : <EOL> self . addaddr ( ifindex , addr ) <EOL> if self . up : <EOL> self . cmd ( [ IP_BIN , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , netif . localname , "<STR_LIT>" ] ) <EOL> def linkconfig ( self , netif , bw = None , delay = None , <EOL> loss = None , duplicate = None , jitter = None , netif2 = None ) : <EOL> '''<STR_LIT>''' <EOL> if os . uname ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> netcls = LxBrNet <EOL> elif os . uname ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> netcls = NetgraphNet <EOL> else : <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> tmp = netcls ( session = self . session , start = False ) <EOL> tmp . up = True <EOL> tmp . linkconfig ( netif , bw = bw , delay = delay , loss = loss , <EOL> duplicate = duplicate , jitter = jitter , netif2 = netif2 ) <EOL> del tmp <EOL> def newifindex ( self ) : <EOL> self . lock . acquire ( ) <EOL> try : <EOL> while self . ifindex in self . _netif : <EOL> self . ifindex += <NUM_LIT:1> <EOL> ifindex = self . ifindex <EOL> self . ifindex += <NUM_LIT:1> <EOL> return ifindex <EOL> finally : <EOL> self . lock . release ( ) <EOL> def newnetif ( self , net = None , addrlist = [ ] , hwaddr = None , <EOL> ifindex = None , ifname = None ) : <EOL> if self . up and net is None : <EOL> raise NotImplementedError <EOL> if ifindex is None : <EOL> ifindex = self . newifindex ( ) <EOL> if self . up : <EOL> gt = self . session . broker . addnettunnel ( net . objid ) <EOL> if gt is None or len ( gt ) != <NUM_LIT:1> : <EOL> self . session . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> gt = gt [ <NUM_LIT:0> ] <EOL> net . detach ( gt ) <EOL> self . adoptnetif ( gt , ifindex , hwaddr , addrlist ) <EOL> return ifindex <EOL> if ifname is None : <EOL> ifname = "<STR_LIT>" % ifindex <EOL> netif = GreTap ( node = self , name = ifname , session = self . session , <EOL> start = False ) <EOL> self . adoptnetif ( netif , ifindex , hwaddr , addrlist ) <EOL> return ifindex <EOL> def privatedir ( self , path ) : <EOL> if path [ <NUM_LIT:0> ] != "<STR_LIT:/>" : <EOL> raise ValueError , "<STR_LIT>" + path <EOL> hostpath = os . path . join ( self . nodedir , <EOL> os . path . normpath ( path ) . strip ( '<STR_LIT:/>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) ) <EOL> try : <EOL> os . mkdir ( hostpath ) <EOL> except OSError : <EOL> pass <EOL> except Exception , e : <EOL> raise Exception , e <EOL> self . mount ( hostpath , path ) <EOL> def mount ( self , source , target ) : <EOL> source = os . path . abspath ( source ) <EOL> self . info ( "<STR_LIT>" % ( source , target ) ) <EOL> try : <EOL> os . makedirs ( target ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> self . cmd ( [ MOUNT_BIN , "<STR_LIT>" , source , target ] ) <EOL> self . _mounts . append ( ( source , target ) ) <EOL> except : <EOL> self . warn ( "<STR_LIT>" % ( source , target ) ) <EOL> def umount ( self , target ) : <EOL> self . info ( "<STR_LIT>" % target ) <EOL> try : <EOL> self . cmd ( [ UMOUNT_BIN , "<STR_LIT>" , target ] ) <EOL> except : <EOL> self . warn ( "<STR_LIT>" % target ) <EOL> def opennodefile ( self , filename , mode = "<STR_LIT:w>" ) : <EOL> dirname , basename = os . path . split ( filename ) <EOL> if not basename : <EOL> raise ValueError , "<STR_LIT>" + filename <EOL> if dirname and dirname [ <NUM_LIT:0> ] == "<STR_LIT:/>" : <EOL> dirname = dirname [ <NUM_LIT:1> : ] <EOL> dirname = dirname . replace ( "<STR_LIT:/>" , "<STR_LIT:.>" ) <EOL> dirname = os . path . join ( self . nodedir , dirname ) <EOL> if not os . path . isdir ( dirname ) : <EOL> os . makedirs ( dirname , mode = <NUM_LIT:0> <NUM_LIT> ) <EOL> hostfilename = os . path . join ( dirname , basename ) <EOL> return open ( hostfilename , mode ) <EOL> def nodefile ( self , filename , contents , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> f = self . opennodefile ( filename , "<STR_LIT:w>" ) <EOL> f . write ( contents ) <EOL> os . chmod ( f . name , mode ) <EOL> f . close ( ) <EOL> self . info ( "<STR_LIT>" % ( f . name , mode ) ) </s>
