<s> '''<STR_LIT>''' <EOL> __author__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import os , sys , subprocess <EOL> import plistlib <EOL> import argparse <EOL> import time <EOL> import shutil <EOL> import syslog <EOL> from random import randint <EOL> class VMXMonitor ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , <EOL> plist = os . path . expanduser ( '<STR_LIT>' ) , <EOL> vmrun = '<STR_LIT>' ) : <EOL> self . plist = plist <EOL> if not os . path . exists ( self . plist ) : <EOL> plistlib . writePlist ( { } , plist ) <EOL> self . d = plistlib . readPlist ( plist ) <EOL> self . vmrun = vmrun <EOL> def set_start ( self , vmx , job_id = None ) : <EOL> if not self . d or not vmx in self . d . values ( ) [ <NUM_LIT:0> ] : <EOL> if not job_id : <EOL> job_id = randint ( <NUM_LIT:1000> , <NUM_LIT> ) <EOL> self . d [ str ( job_id ) ] = [ str ( vmx ) , '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> plistlib . writePlist ( self . d , self . plist ) <EOL> def set_stop ( self , vmx , job_id = None ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> self . d [ k ] [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> plistlib . writePlist ( self . d , self . plist ) <EOL> def set_reset ( self , vmx , job_id = None ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> self . d [ k ] [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> plistlib . writePlist ( self . d , self . plist ) <EOL> def remove ( self , vmx ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> del self . d [ k ] <EOL> plistlib . writePlist ( self . d , self . plist ) <EOL> def set_running ( self , vmx ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> self . d [ k ] [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> self . d [ k ] [ <NUM_LIT:2> ] = self . get_ip ( vmx ) <EOL> self . d [ k ] [ <NUM_LIT:3> ] = str ( self . get_pid ( vmx ) ) <EOL> plistlib . writePlist ( self . d , self . plist ) <EOL> def get_pid ( self , vmx ) : <EOL> for root , dirs , files in os . walk ( '<STR_LIT>' ) : <EOL> for i in files : <EOL> full_path = root + '<STR_LIT:/>' + i <EOL> if os . path . realpath ( full_path ) in vmx : <EOL> return os . path . dirname ( full_path ) . split ( '<STR_LIT:_>' ) [ - <NUM_LIT:1> ] <EOL> def get_monitored ( self ) : <EOL> l = [ ] <EOL> for i in self . d . values ( ) : <EOL> l . append ( i [ <NUM_LIT:0> ] ) <EOL> return l <EOL> def is_running ( self , vmx ) : <EOL> for root , dirs , files in os . walk ( '<STR_LIT>' ) : <EOL> for i in files : <EOL> full_path = root + '<STR_LIT:/>' + i <EOL> if os . path . realpath ( full_path ) in vmx : <EOL> return True <EOL> def is_scheduled ( self , vmx ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> if v [ <NUM_LIT:1> ] == '<STR_LIT:start>' or v [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return True <EOL> def to_be_removed ( self , vmx ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> if v [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return True <EOL> def to_be_reset ( self , vmx ) : <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:0> ] == vmx : <EOL> if v [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return True <EOL> def get_scheduled ( self ) : <EOL> d = { } <EOL> for k , v in self . d . items ( ) : <EOL> if v [ <NUM_LIT:1> ] == '<STR_LIT:start>' or v [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> d [ k ] = v <EOL> return d <EOL> def get_running ( self ) : <EOL> d = { } <EOL> cmd = [ self . vmrun , '<STR_LIT:list>' ] <EOL> output = subprocess . check_output ( cmd ) <EOL> for line in output . splitlines ( ) : <EOL> if '<STR_LIT>' in line : <EOL> if int ( line . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] ) == <NUM_LIT:0> : <EOL> return d <EOL> else : <EOL> d [ line . strip ( ) ] = self . get_pid ( line . strip ( ) ) <EOL> return d <EOL> def start_vmx ( self , vmx ) : <EOL> cmd = [ self . vmrun , '<STR_LIT:start>' , vmx ] <EOL> task = subprocess . Popen ( cmd , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> out , err = task . communicate ( ) <EOL> if err : <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> def stop_vmx ( self , vmx ) : <EOL> cmd = [ self . vmrun , '<STR_LIT>' , vmx ] <EOL> task = subprocess . Popen ( cmd , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> out , err = task . communicate ( ) <EOL> if err : <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> def reset_vmx ( self , vmx ) : <EOL> cmd = [ self . vmrun , '<STR_LIT>' , vmx ] <EOL> task = subprocess . Popen ( cmd , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> out , err = task . communicate ( ) <EOL> if err : <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> def delete_vm ( self , vmx ) : <EOL> if os . path . exists ( os . path . dirname ( vmx ) ) : <EOL> shutil . rmtree ( os . path . dirname ( vmx ) ) <EOL> def job_to_vmx ( self , job_id ) : <EOL> for k , v in self . d . items ( ) : <EOL> if job_id == k : <EOL> return v [ <NUM_LIT:0> ] <EOL> def get_ip ( self , vmx ) : <EOL> count = <NUM_LIT:1> <EOL> while count <= <NUM_LIT> : <EOL> cmd = [ self . vmrun , '<STR_LIT>' , vmx ] <EOL> task = subprocess . Popen ( cmd , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> out , err = task . communicate ( ) <EOL> if not task . returncode == <NUM_LIT:255> : <EOL> break <EOL> count += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> if '<STR_LIT>' in out : <EOL> return '<STR_LIT>' <EOL> return out . strip ( ) <EOL> def vmx_is_valid ( self , vmx ) : <EOL> if os . path . splitext ( vmx ) [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> if os . path . exists ( vmx ) : <EOL> return True <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> syslog . openlog ( '<STR_LIT>' ) <EOL> monitor = VMXMonitor ( ) <EOL> if args . list : <EOL> if not monitor . d . keys ( ) : <EOL> print '<STR_LIT>' <EOL> for k , v in monitor . get_scheduled ( ) . items ( ) : <EOL> print '<STR_LIT>' % ( v [ <NUM_LIT:0> ] , v [ <NUM_LIT:2> ] , v [ <NUM_LIT:3> ] , k ) <EOL> for k , v in monitor . get_running ( ) . items ( ) : <EOL> print '<STR_LIT>' % ( k , v ) <EOL> elif args . add : <EOL> if not monitor . vmx_is_valid ( args . add ) : <EOL> print '<STR_LIT>' % args . add <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not monitor . is_scheduled ( args . add ) : <EOL> monitor . set_start ( args . add ) <EOL> elif args . remove : <EOL> if not monitor . vmx_is_valid ( args . remove ) : <EOL> print '<STR_LIT>' % args . remove <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if monitor . is_scheduled ( args . remove ) : <EOL> monitor . set_stop ( args . remove ) <EOL> elif args . reset : <EOL> if not monitor . vmx_is_valid ( args . reset ) : <EOL> print '<STR_LIT>' % args . reset <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if monitor . is_scheduled : <EOL> monitor . set_reset ( args . reset ) <EOL> elif args . get_ip : <EOL> print monitor . get_ip ( args . get_ip ) <EOL> elif args . monitor : <EOL> while True : <EOL> try : <EOL> monitor = VMXMonitor ( ) <EOL> for vmx in monitor . get_monitored ( ) : <EOL> if monitor . is_scheduled ( vmx ) : <EOL> if not monitor . is_running ( vmx ) : <EOL> print '<STR_LIT>' % vmx <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> monitor . start_vmx ( vmx ) <EOL> monitor . set_running ( vmx ) <EOL> elif monitor . to_be_removed ( vmx ) : <EOL> if monitor . is_running ( vmx ) : <EOL> print '<STR_LIT>' % vmx <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> monitor . stop_vmx ( vmx ) <EOL> monitor . remove ( vmx ) <EOL> elif monitor . to_be_reset ( vmx ) : <EOL> if monitor . is_running ( vmx ) : <EOL> print '<STR_LIT>' % vmx <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> monitor . reset_vmx ( vmx ) <EOL> else : <EOL> print '<STR_LIT>' % vmx <EOL> syslog . syslog ( syslog . LOG_ALERT , '<STR_LIT>' % vmx ) <EOL> monitor . start_vmx ( vmx ) <EOL> monitor . set_running ( vmx ) <EOL> time . sleep ( <NUM_LIT:15> ) <EOL> except KeyboardInterrupt : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> parser . print_help ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
