#! /usr/bin/python

##################################################################
# This is the script that runs from inside the virtual machine.
# this script is invoked by /etc/init.d/local << renameme
# The responsibilities of the init script are 
# - untar the condor distribution: for starters, we do it into
#   the "/" folder so that we get "/condor-<version>"
# - set up personal condor using condor_configure
# - fix HOSTALLOW_WRITE parameter in /etc/condor_config
# - source the condor.sh file
# - start the condor_master
# - submit the job using the submit description file above
# - in a loop, check on the output of condor_q until the job
#   is done ?? (Is there a better way?)
# - shutdown the virtual machine once the job is done.
#################################################################

# NOTE: This script will be executed as root !

import os
import commands
import re
import time

#################################################################
# The following line provides a marker for the translate hook.
# The translate hook replaces this marker with the following.
#    CONDOR_TARBALL    - This is a full pathname
#    CONDOR_TARBALL_VERSION
#    DATA_DISK_MOUNT_POINT
#    MACHINE_CLASS
#    MACHINE_SIGNATURE
#    JOB_SIGNATURE
#    CONDOR_HOST
#################################################################
__CONDOR_MARKER_FOR_TRANSLATE_HOOK__



##############################################################
def runcommand ( cmd ):
    e_status, e_output = commands.getstatusoutput(cmd)
    if e_status != 0:
        commands.getstatusoutput("shutdown -h +5")
        sys.exit(-1)
    else:
        pass
    return (e_status, e_output)




def main():
    os.chdir("/")
    tmp_cmd = "tar -xvzf %s" % CONDOR_TARBALL
    runcommand(tmp_cmd)
    os.chdir("/condor-%s" % CONDOR_TARBALL_VERSION)
    tmp_cmd = "./condor_configure --install --prefix=/condor --local-dir=/condor/local --type=execute --central-manager=%s" % CONDOR_HOST
    runcommand(tmp_cmd)
#    os.chdir(DATA_DISK_MOUNT_POINT)
    os.environ["CONDOR_CONFIG"] = "/condor/etc/condor_config"
    old_path = os.environ["PATH"]
    os.environ["PATH"] = "/condor/bin:/condor/sbin:%s" % old_path
    cfg_f = open(os.environ["CONDOR_CONFIG"]) # open for read
    # Open a new file to write the modified config file
    cfg_fnew = open("%s.new" % os.environ["CONDOR_CONFIG"], 'w')
    line = cfg_f.readline()
    while line != '':   # while not end of file
        new_line = re.sub("YOU_MUST_CHANGE_THIS_INVALID_CONDOR_CONFIGURATION_VALUE", "*", line)
        # Note: The matching of UWCS_START and STARTD_ATTRS are pretty finicky
        if re.match("UWCS_START", line):
            while line[-2] == '\\':
                line = cfg_f.readline()
            new_line = 'UWCS_START = (Target.JobSignature =?= "%s")\n' % JOB_SIGNATURE
        if re.match("STARTD_ATTRS", line):
            new_line = line[:-1] + ", MachineClass, MachineSignature\n"
            new_line += 'MachineClass = "%s"\n' % MACHINE_CLASS
            new_line += 'MachineSignature = "%s"\n' % MACHINE_SIGNATURE
        cfg_fnew.write(new_line)
        line = cfg_f.readline()
    cfg_fnew.write("CCB_ADDRESS = %s\n" % CONDOR_HOST)
    cfg_f.close()
    cfg_fnew.close()
    tmp_cmd = "mv %s %s.old" % (os.environ["CONDOR_CONFIG"], os.environ["CONDOR_CONFIG"])
    runcommand(tmp_cmd)
    tmp_cmd = "mv %s.new %s" % (os.environ["CONDOR_CONFIG"], os.environ["CONDOR_CONFIG"])
    runcommand(tmp_cmd)
    runcommand("condor_master")
    time.sleep(360000)  # For now, we assume the jobs will get run
                      # in a max of 100 hour. Just so I can examine.
    
    # At this point, the script assumes the job is done
    tmp_cmd = "shutdown -h +3"
    runcommand(tmp_cmd)


main()











