# The template for the spot controller

name: {{dag_name}}

file_mounts:
  {{remote_user_yaml_prefix}}/{{dag_name}}-{{uuid}}.yaml: {{user_yaml_path}}
{% if user_config_path is not none %}
  {{remote_user_yaml_prefix}}/{{dag_name}}-{{uuid}}.config_yaml: {{user_config_path}}
{% endif %}

setup: |
  # Install cli dependencies
  # Not using SkyPilot wheels because the wheel can be cleaned up by another process.
  # TODO(zhwu): Keep the dependencies align with the ones in setup.py
  (pip list | grep boto3 > /dev/null 2>&1 && \
   pip list | grep google-api-python-client > /dev/null 2>&1 ) || \
   pip install boto3 awscli pycryptodome==3.12.0 google-api-python-client google-cloud-storage 2>&1 > /dev/null

  # We do not install azure dependencies for now since our subscription does not support spot instances.
  # pip list | grep azure-cli > /dev/null 2>&1 || \
  #  pip3 install azure-cli==2.31.0 azure-core
  {{google_sdk_installation_commands}}

  pip list | grep oci  > /dev/null 2>&1 || pip install oci 2>&1 > /dev/null

  # Internal: disable logging for manually logging into the spot controller for debugging.
  {% if is_dev %}
  echo 'export SKYPILOT_DEV=1' >> ~/.bashrc
  {% endif %}

  # Dashboard.
  pip list | grep flask  > /dev/null 2>&1 || pip install flask 2>&1 > /dev/null
  ((ps aux | grep -v nohup | grep -v grep | grep -q -- "python3 -m sky.spot.dashboard.dashboard") || (nohup python3 -m sky.spot.dashboard.dashboard >> ~/.sky/spot-dashboard.log 2>&1 &));

run: |
  python -u -m sky.spot.controller \
    {{remote_user_yaml_prefix}}/{{dag_name}}-{{uuid}}.yaml \
    --job-id $SKYPILOT_INTERNAL_JOB_ID {% if retry_until_up %}--retry-until-up{% endif %}

envs:
  SKYPILOT_USER_ID: {{logging_user_hash}}
  # skip cloud identity check for spot controller to avoid the overhead.
  SKYPILOT_SKIP_CLOUD_IDENTITY_CHECK: 1
  SKYPILOT_USER: {{user}}
{% if user_config_path is not none %}
  {{env_var_skypilot_config}}: {{remote_user_yaml_prefix}}/{{dag_name}}-{{uuid}}.config_yaml
{% endif %}
{% if is_dev %}
  SKYPILOT_DEV: 1
{% endif %}
{% if is_debug %}
  SKYPILOT_DEBUG: 1
{% endif %}
{% if disable_logging %}
  SKYPILOT_DISABLE_USAGE_COLLECTION: 1
{% endif %}
