#!/bin/sh

# This is a HTCondor SCHEDD only configuration
#
# It assumes this is a single node HTCondor installation without any local compute resources
# All jobs will flock to a Remote Cluster Connect (RCC) Factory Server on a specified port, the RCC Factory.
# Jobs are routed to this RCC Factory with a Requirements in the submitters HTCondor command file
#
# Example:
#
#       Requirements = ( UidDomain == "uct2-bosco.uchicago.edu:11015?sock=collector" )
#
# The RCC Factory will run the job on the remote Tier2 with a tier3 user account such as "tier3.uiuc"
#
#


# In general only two lines need to be changed in this script


# RCC
#
#    The Port used to designate a specific RCC Factory on the RCC Factory Server.
#    In general, this port is assigned by the RCC Factory Server Administrator.
#    If the local site has firewall restrictions in place, a mutually agreed upon port number can be used.

RCC_Factory_Port=


# RCC_Shared_Port
#
#    The Shared Port used by the local HTCondor SHARED_PORT daemon.
#    This port must be open in any local firewalls to the node specified by ${RCC_Factory_Server}
#    This port number might need to assigned by the local network administrator
#    If there are no firewalls between this node and the RCC_Factory Server, the default value will work

RCC_Shared_Port=${RCC_Factory_Port}


###################

# You should not need to change these values

# The FQDN of the RCC Factory Server

RCC_Factory_Server="uct2-bosco.uchicago.edu"


# The DN of the RCC Factory Server for GSI security

RCC_Factory_DN="/DC=com/DC=DigiCert-Grid/O=Open Science Grid/OU=Services/CN=uct2-bosco.uchicago.edu"


###################


# Sanity check - did you fill in blank
if [[ -z $RCC_Factory_Port ]]; then
  echo "You must assign a value to RCC_Factory_Port"
  exit 1
fi



# Get the Hardware platform type
myArch=$(uname -i)


# Which EL are we using (default to EL5)
myEL=

if [[ -f /etc/redhat-release ]]; then

  /bin/grep -q ' 6\.' /etc/redhat-release
  [[ $? -eq 0 ]] && myEL=el6

  /bin/grep -q ' 5\.' /etc/redhat-release
  [[ $? -eq 0 ]] && myEL=el5

fi

if [[ -z ${myEL} ]]; then
  echo
  echo "Unknown EL release level: ${myEL} - terminating"
  echo

  exit 1
fi



# Stop any running condor
/etc/init.d/condor stop


# Remove it completely
yum -y remove condor

rm -rf /var/lib/condor
rm -rf /var/log/condor
rm -rf /etc/condor
rm -rf /etc/sysconfig/condor
rm -rf /etc/yum.repos.d/htcondor-stable-rh${myEL}.repo
rm -rf /etc/yum.repos.d/htcondor-development-rh${myEL}.repo


# Fetch the latest repository from HTCondor
(cd /etc/yum.repos.d; wget http://research.cs.wisc.edu/htcondor/yum/repo.d/htcondor-stable-rh${myEL}.repo)

# Enable this line if you would like to use the development release of HTCondor
#(cd /etc/yum.repos.d; wget http://research.cs.wisc.edu/htcondor/yum/repo.d/htcondor-development-rh${myEL}.repo)


# Reset the yum repo cache
yum clean all

# Install the new condor
yum -y install condor.${myArch}


# Condor enables these but we want them off
[[ -f /etc/init.d/libvirtd       ]] && chkconfig libvirtd off
[[ -f /etc/init.d/libvirt-guests ]] && chkconfig libvirt-guests off



# Create our own simple HTCondor configuration file

rm -rf /etc/condor/condor_config.local
cat <<EOF>>/etc/condor/condor_config.local
# Condor configuration to allow a node to particpate as a SCHEDD for a Remote Cluster Connect


# This node is the central manager
CONDOR_HOST                              = \$(FULL_HOSTNAME)


# Pool's short description
COLLECTOR_NAME                           = RCC SCHEDD


# Turn off the collector reporting to Wisc
CONDOR_DEVELOPERS                        = NONE
CONDOR_DEVELOPERS_COLLECTOR              = NONE


# Only run the services we need to FLOCK jobs to a RCC Factory
DAEMON_LIST                              = MASTER, COLLECTOR, NEGOTIATOR, SCHEDD, SHARED_PORT


# Enabled Shared Port
USE_SHARED_PORT                          = TRUE
SHARED_PORT_ARGS                         = -p ${RCC_Shared_Port}


# Make the collector use the shared port
COLLECTOR_HOST                           = \$(CONDOR_HOST):${RCC_Shared_Port}?sock=collector


# Setup the FLOCK_TO the RCC Factory
FLOCK_TO                                 = \$(FLOCK_TO), ${RCC_Factory_Server}:${RCC_Factory_Port}?sock=collector


# Allow the RCC Factory server access to our SCHEDD
ALLOW_NEGOTIATOR_SCHEDD                  = \$(CONDOR_HOST), ${RCC_Factory_Server}


# Who do you trust?
GSI_DAEMON_NAME                          = \$(GSI_DAEMON_NAME), ${RCC_Factory_DN}
GSI_DAEMON_CERT                          = /etc/grid-security/hostcert.pem
GSI_DAEMON_KEY                           = /etc/grid-security/hostkey.pem
GSI_DAEMON_TRUSTED_CA_DIR                = /etc/grid-security/certificates

# Enable authentication from tne Negotiator (This is required to run on glidein jobs)
SEC_ENABLE_MATCH_PASSWORD_AUTHENTICATION = TRUE




# We can use 3 or more FD per job so give us more than the default
MAX_FILE_DESCRIPTORS                     = 20000


# We will never run any local jobs
START                                    = FALSE


# Never kill, suspend or preempt these jobs
KILL                                     = FALSE
PREEMPT                                  = FALSE
PREEMPTION_REQUIREMENTS                  = FALSE
SUSPEND                                  = FALSE
WANT_SUSPEND                             = FALSE
CONTINUE                                 = TRUE
PERIODIC_CHECKPOINT                      = FALSE
PREEMPTION_REQUIREMENTS                  = FALSE
PREEMPTION_RANK                          = 0
NEGOTIATOR_PRE_JOB_RANK                  = 0
NEGOTIATOR_POST_JOB_RANK                 = 0
MaxJobRetirementTime                     = 0
CLAIM_WORKLIFE                           = 0


EOF




# Increase the number of file descriptors we can use, etc

rm -rf /etc/security/limits.d/vt3.conf
cat <<EOF>>/etc/security/limits.d/vt3.conf

# Remove all the limits so we avoid trouble

* - nofile  1000000
* - nproc   unlimited
* - memlock unlimited
* - locks   unlimited
* - core    unlimited

EOF


# Lets start Condor 
#/etc/init.d/condor start
