#!/usr/bin/execlineb -P
# Messages of this script go to the logger as well.
fdmove -c 2 1
# artie builds its Kubernetes client once at process start (in-cluster
# config: KUBERNETES_SERVICE_HOST/PORT from /run/init_env, token under
# /run/secrets, linked by the operationagent's run script). Without them the
# scheduler would run for ever without a client. So wait for them here --
# with a pause, so that a system without a pod environment (image build,
# plain container) sees one attempt every 30s instead of a restart storm.
ifelse -n { if { test -r /run/init_env/KUBERNETES_SERVICE_HOST } test -r /run/secrets/kubernetes.io/serviceaccount/token }
{
  foreground { echo "artie-schedule: no pod environment yet (/run/init_env/KUBERNETES_SERVICE_HOST or /run/secrets/kubernetes.io/serviceaccount/token missing), next attempt in 30s" }
  foreground { sleep 30 }
  exit 1
}
# Pod environment like the operationagent (its s6 run script)
s6-envdir /run/init_env
# HOME/USER of operationagent: artie keeps locks and state in $HOME/.artie
# (runner.StateDir), shared with the operations the operationagent starts --
# a scheduled run and an Operation of the same job must see the same lock.
# s6-setuidgid in run-script-daemon does not set HOME.
define USER operationagent
export USER ${USER}
backtick -n HOME { homeof ${USER} }
importas -i HOME HOME
export HOME ${HOME}
run-script-daemon artie-schedule
