<s> import subprocess , ConfigParser , sys , os , datetime <EOL> from galah . base . magic import memoize <EOL> from galah . base . config import load_config <EOL> config = load_config ( "<STR_LIT>" ) <EOL> vzctlPath = "<STR_LIT>" <EOL> vzlistPath = "<STR_LIT>" <EOL> containerDirectory = None <EOL> nullFile = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> def check_call ( * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> returnValue = subprocess . call ( * args , ** kwargs ) <EOL> if returnValue != <NUM_LIT:0> : <EOL> raise SystemError ( ( return_value , str ( zparams [ <NUM_LIT:0> ] ) ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def run_vzctl ( zparams , timeout = config [ "<STR_LIT>" ] ) : <EOL> cmd = [ vzctlPath ] + zparams <EOL> deadline = datetime . datetime . today ( ) + timeout <EOL> while True : <EOL> return_value = subprocess . call ( <EOL> cmd , stdout = nullFile , stderr = nullFile <EOL> ) <EOL> if return_value == <NUM_LIT:9> and datetime . datetime . today ( ) < deadline : <EOL> continue <EOL> elif return_value != <NUM_LIT:0> : <EOL> raise SystemError ( ( return_value , str ( zparams [ <NUM_LIT:0> ] ) ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> @ memoize <EOL> def find_container_directory ( config_path = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> vzconfig = ConfigParser . SafeConfigParser ( ) <EOL> try : <EOL> vzconfig . readfp ( FakeHeaderWrapper ( open ( config_path ) ) ) <EOL> except ( ConfigParser . NoSectionError , ConfigParser . NoOptionError ) : <EOL> raise IOError ( "<STR_LIT>" <EOL> % ( config_path ) ) <EOL> except : <EOL> raise IOError ( "<STR_LIT>" <EOL> % ( config_path ) ) <EOL> try : <EOL> containerDirectory = vzconfig . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> dirpath = containerDirectory . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> except ( ConfigParser . NoSectionError , ConfigParser . NoOptionError ) : <EOL> raise IOError ( "<STR_LIT>" <EOL> % ( config_path ) ) <EOL> return dirpath <EOL> class FakeHeaderWrapper ( object ) : <EOL> def __init__ ( self , file , default_section = "<STR_LIT>" ) : <EOL> self . configFile = file <EOL> self . sectionHeader = "<STR_LIT>" % default_section <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> if self . sectionHeader != None : <EOL> try : <EOL> return self . sectionHeader <EOL> finally : <EOL> self . sectionHeader = None <EOL> else : <EOL> temp = self . configFile . readline ( ) <EOL> if temp == "<STR_LIT>" : <EOL> raise StopIteration <EOL> return temp <EOL> def readline ( self ) : <EOL> if self . sectionHeader != None : <EOL> try : <EOL> return self . sectionHeader <EOL> finally : <EOL> self . sectionHeader = None <EOL> else : <EOL> return self . configFile . readline ( ) <EOL> def create_container ( id_range = range ( <NUM_LIT:1> , <NUM_LIT:255> ) , <EOL> subnet = "<STR_LIT>" , <EOL> os_template = None , <EOL> description = None ) : <EOL> """<STR_LIT>""" <EOL> containers = set ( get_containers ( ) ) <EOL> for i in id_range : <EOL> if i not in containers : <EOL> id = i <EOL> break <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( config . getint ( "<STR_LIT>" , "<STR_LIT>" ) ) ) <EOL> parameters = [ ] <EOL> if subnet != None : <EOL> parameters += [ "<STR_LIT>" , subnet + "<STR_LIT:.>" + str ( id ) ] <EOL> if os_template != None : <EOL> parameters += [ "<STR_LIT>" , os_template ] <EOL> if description != None : <EOL> parameters += [ "<STR_LIT>" , description ] <EOL> run_vzctl ( [ "<STR_LIT>" , str ( id ) ] + parameters ) <EOL> return id <EOL> def start_container ( id ) : <EOL> "<STR_LIT>" <EOL> run_vzctl ( [ "<STR_LIT:start>" , str ( id ) ] ) <EOL> def stop_container ( id ) : <EOL> "<STR_LIT>" <EOL> run_vzctl ( [ "<STR_LIT>" , str ( id ) ] ) <EOL> def destroy_container ( id ) : <EOL> "<STR_LIT>" <EOL> run_vzctl ( [ "<STR_LIT>" , str ( id ) ] ) <EOL> def extirpate_container ( id ) : <EOL> "<STR_LIT>" <EOL> stop_container ( id ) <EOL> destroy_container ( id ) <EOL> def inject_file ( id , source , to , move = False , permissions = "<STR_LIT>" , <EOL> unpack = False ) : <EOL> """<STR_LIT>""" <EOL> ztoReal = container_to_host_path ( id , to ) <EOL> if unpack and source . endswith ( ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> check_call ( [ "<STR_LIT>" , "<STR_LIT>" , source , "<STR_LIT>" , ztoReal ] , <EOL> stdout = nullFile , stderr = nullFile ) <EOL> if move : <EOL> check_call ( [ "<STR_LIT>" , source ] , stdout = nullFile , stderr = nullFile ) <EOL> else : <EOL> if os . path . isdir ( source ) : <EOL> files = [ os . path . join ( source , i ) for i in os . listdir ( source ) ] <EOL> else : <EOL> files = [ source ] <EOL> if move : <EOL> check_call ( [ "<STR_LIT>" , "<STR_LIT>" ] + files + [ ztoReal ] , <EOL> stdout = nullFile , stderr = nullFile ) <EOL> else : <EOL> check_call ( [ "<STR_LIT>" , "<STR_LIT>" ] + files + [ ztoReal ] , <EOL> stdout = nullFile , stderr = nullFile ) <EOL> check_call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ztoReal ] , stdout = nullFile , stderr = nullFile ) <EOL> check_call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" % ( permissions ) , ztoReal ] , <EOL> stdout = nullFile , stderr = nullFile ) <EOL> def run_shell_script_from_host ( id , script ) : <EOL> """<STR_LIT>""" <EOL> run_vzctl ( [ "<STR_LIT>" , str ( id ) , script ] ) <EOL> def container_to_host_path ( id , path ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( find_container_directory ( ) , str ( id ) , path [ <NUM_LIT:1> : ] ) <EOL> def host_to_container_path ( id , path ) : <EOL> """<STR_LIT>""" <EOL> prefix = os . path . join ( find_container_directory ( ) , str ( id ) ) <EOL> if not path . startswith ( prefix ) : <EOL> raise ValueError ( "<STR_LIT>" % path ) <EOL> return path [ len ( prefix ) : ] <EOL> def run_script ( id , script , interpreter = None ) : <EOL> """<STR_LIT>""" <EOL> command = ( "<STR_LIT>" if interpreter == None else interpreter + "<STR_LIT:U+0020>" ) <EOL> command += script <EOL> execute ( id , command , False ) <EOL> def execute ( id , code , block = True ) : <EOL> """<STR_LIT>""" <EOL> p = subprocess . Popen ( <EOL> [ vzctlPath , "<STR_LIT>" , str ( id ) , "<STR_LIT:->" ] , <EOL> stdin = subprocess . PIPE <EOL> ) <EOL> p . stdin . write ( code ) <EOL> p . stdin . close ( ) <EOL> if block : <EOL> p . wait ( ) <EOL> return p . returncode <EOL> else : <EOL> return <EOL> def set_attribute ( id , attribute , value , save = True ) : <EOL> """<STR_LIT>""" <EOL> run_vzctl ( [ "<STR_LIT>" , str ( id ) , "<STR_LIT>" + attribute , value ] + <EOL> ( [ "<STR_LIT>" ] if save else [ "<STR_LIT>" , "<STR_LIT:ignore>" ] ) ) <EOL> def get_attribute ( id , attribute ) : <EOL> p = subprocess . Popen ( [ vzlistPath , "<STR_LIT>" , attribute , str ( id ) ] , <EOL> stdout = subprocess . PIPE , <EOL> stderr = nullFile ) <EOL> output = p . communicate ( ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> raise SystemError ( ( p . returncode , <EOL> "<STR_LIT>" % <EOL> ( attribute , id ) ) ) <EOL> return output [ <NUM_LIT:0> ] . strip ( ) <EOL> def get_containers ( description_pattern = None ) : <EOL> "<STR_LIT>" <EOL> if description_pattern == None : <EOL> dirPath = find_container_directory ( ) <EOL> containers = [ ] <EOL> for i in os . listdir ( dirPath ) : <EOL> try : <EOL> containers . append ( int ( i ) ) <EOL> except ValueError : <EOL> pass <EOL> return containers <EOL> else : <EOL> flags = [ "<STR_LIT>" , description_pattern ] <EOL> p = subprocess . Popen ( [ vzlistPath ] + flags + [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> stdout = subprocess . PIPE , stderr = nullFile ) <EOL> output = p . communicate ( ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> raise SystemError ( ( p . returncode , "<STR_LIT>" ) ) <EOL> return [ int ( i . strip ( ) ) for i in output [ <NUM_LIT:0> ] . splitlines ( ) ] </s>
