<commit_before>#!/bin/bash

# Copyright 2022 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Creates instance templates for (cpu, gpu) x (presubmit, postsubmit) instances
# according to the current configuration.

set -euo pipefail

SCRIPT_DIR="$(dirname -- "$( readlink -f -- "$0"; )")";

TESTING="${TEMPLATE_TESTING:-0}"

TEMPLATE_BASE_NAME="${TEMPLATE_BASE_NAME:-github-runner}"
TEMPLATE_CONFIG_REPO="${TEMPLATE_CONFIG_REPO:-iree-org/iree}"
TEMPLATE_CONFIG_REF="${TEMPLATE_CONFIG_REF:-$(git rev-parse HEAD)}"
GPU_IMAGE="github-runner-gpu-2022-09-29-1664451806"
GPU_DISK_SIZE_GB=100
CPU_IMAGE="github-runner-cpu-2022-09-29-1664451255"
CPU_DISK_SIZE_GB=100

if (( TESTING==0 )); then
  if [[ "${TEMPLATE_CONFIG_REPO}" != iree-org/iree ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_CONFIG_REPO='${TEMPLATE_CONFIG_REPO}'. Aborting"
    exit 1
  fi
  if [[ "${TEMPLATE_BASE_NAME}" != github-runner ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_BASE_NAME='${TEMPLATE_BASE_NAME}'. Aborting"
    exit 1
  fi
  if [[ "${TEMPLATE_CONFIG_REF}" != "$(git rev-parse main)" ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_CONFIG_REF='${TEMPLATE_CONFIG_REF}'. Aborting"
    exit 1
  fi
fi

TIME_STRING="$(date +%Y-%m-%d-%s)"
SHORT_REF="${TEMPLATE_CONFIG_REF:0:10}"
VERSION="${SHORT_REF}-${TIME_STRING}"
STARTUP_SCRIPT_PATH="/tmp/startup_script.${SHORT_REF}.sh"
GITHUB_RUNNER_SCOPE=iree-org
GITHUB_RUNNER_VERSION="2.298.2"
GITHUB_RUNNER_ARCHIVE_DIGEST="0bfd792196ce0ec6f1c65d2a9ad00215b2926ef2c416b8d97615265194477117"
GITHUB_TOKEN_PROXY_URL="https://ght-proxy-zbhz5clunq-ue.a.run.app"

declare -a METADATA=(
  "github-runner-version=${GITHUB_RUNNER_VERSION}"
  "github-runner-archive-digest=${GITHUB_RUNNER_ARCHIVE_DIGEST}"
  "github-runner-config-ref=${TEMPLATE_CONFIG_REF}"
  "github-runner-config-repo=${TEMPLATE_CONFIG_REPO}"
  "github-runner-scope=${GITHUB_RUNNER_SCOPE}"
  "github-token-proxy-url=${GITHUB_TOKEN_PROXY_URL}"
)

if (( TESTING==1 )); then
  METADATA+=(github-runner-environment=testing)
else
  METADATA+=(github-runner-environment=prod)
fi

declare -a common_args=(
  --project=iree-oss
  # `address=''` indicates an ephemeral IP. This *shouldn't* be necessary here,
  # as the gcloud docs say that this is the default, but in fact if you leave it
  # off the VM gets no external IP and is impossible to SSH into. This knowledge
  # was hard won.
  --network-interface=network=default,address='',network-tier=PREMIUM
  --provisioning-model=STANDARD
  --scopes=https://www.googleapis.com/auth/cloud-platform
  --no-shielded-secure-boot
  --shielded-vtpm
  --shielded-integrity-monitoring
  --reservation-affinity=any
  --metadata-from-file=startup-script="${SCRIPT_DIR}/startup_script.sh"
)


function create_template() {
  local group="$1"
  local type="$2"
  if [[ "${group}" == presubmit ]]; then
    local trust=minimal
  elif [[ "${group}" == postsubmit ]]; then
    local trust=basic
  else
    echo "Got unrecognized group '${group}'" >2
    exit 1
  fi

  local -a metadata=(<commit_msg>Tag runners to allow health checks (#10975)<commit_after>#!/bin/bash

# Copyright 2022 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Creates instance templates for (cpu, gpu) x (presubmit, postsubmit) instances
# according to the current configuration.

set -euo pipefail

SCRIPT_DIR="$(dirname -- "$( readlink -f -- "$0"; )")";

TESTING="${TEMPLATE_TESTING:-0}"

TEMPLATE_BASE_NAME="${TEMPLATE_BASE_NAME:-github-runner}"
TEMPLATE_CONFIG_REPO="${TEMPLATE_CONFIG_REPO:-iree-org/iree}"
TEMPLATE_CONFIG_REF="${TEMPLATE_CONFIG_REF:-$(git rev-parse HEAD)}"
GPU_IMAGE="github-runner-gpu-2022-09-29-1664451806"
GPU_DISK_SIZE_GB=100
CPU_IMAGE="github-runner-cpu-2022-09-29-1664451255"
CPU_DISK_SIZE_GB=100

if (( TESTING==0 )); then
  if [[ "${TEMPLATE_CONFIG_REPO}" != iree-org/iree ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_CONFIG_REPO='${TEMPLATE_CONFIG_REPO}'. Aborting"
    exit 1
  fi
  if [[ "${TEMPLATE_BASE_NAME}" != github-runner ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_BASE_NAME='${TEMPLATE_BASE_NAME}'. Aborting"
    exit 1
  fi
  if [[ "${TEMPLATE_CONFIG_REF}" != "$(git rev-parse main)" ]]; then
    echo "Expected default settings for non-testing template, but TEMPLATE_CONFIG_REF='${TEMPLATE_CONFIG_REF}'. Aborting"
    exit 1
  fi
fi

TIME_STRING="$(date +%Y-%m-%d-%s)"
SHORT_REF="${TEMPLATE_CONFIG_REF:0:10}"
VERSION="${SHORT_REF}-${TIME_STRING}"
STARTUP_SCRIPT_PATH="/tmp/startup_script.${SHORT_REF}.sh"
GITHUB_RUNNER_SCOPE=iree-org
GITHUB_RUNNER_VERSION="2.298.2"
GITHUB_RUNNER_ARCHIVE_DIGEST="0bfd792196ce0ec6f1c65d2a9ad00215b2926ef2c416b8d97615265194477117"
GITHUB_TOKEN_PROXY_URL="https://ght-proxy-zbhz5clunq-ue.a.run.app"

declare -a METADATA=(
  "github-runner-version=${GITHUB_RUNNER_VERSION}"
  "github-runner-archive-digest=${GITHUB_RUNNER_ARCHIVE_DIGEST}"
  "github-runner-config-ref=${TEMPLATE_CONFIG_REF}"
  "github-runner-config-repo=${TEMPLATE_CONFIG_REPO}"
  "github-runner-scope=${GITHUB_RUNNER_SCOPE}"
  "github-token-proxy-url=${GITHUB_TOKEN_PROXY_URL}"
)

if (( TESTING==1 )); then
  METADATA+=(github-runner-environment=testing)
else
  METADATA+=(github-runner-environment=prod)
fi

declare -a common_args=(
  --project=iree-oss
  # `address=''` indicates an ephemeral IP. This *shouldn't* be necessary here,
  # as the gcloud docs say that this is the default, but in fact if you leave it
  # off the VM gets no external IP and is impossible to SSH into. This knowledge
  # was hard won.
  --network-interface=network=default,address='',network-tier=PREMIUM
  # Matches firewall rule for health check traffic
  --tags="allow-health-checks"
  --provisioning-model=STANDARD
  --scopes=https://www.googleapis.com/auth/cloud-platform
  --no-shielded-secure-boot
  --shielded-vtpm
  --shielded-integrity-monitoring
  --reservation-affinity=any
  --metadata-from-file=startup-script="${SCRIPT_DIR}/startup_script.sh"
)


function create_template() {
  local group="$1"
  local type="$2"
  if [[ "${group}" == presubmit ]]; then
    local trust=minimal
  elif [[ "${group}" == postsubmit ]]; then
    local trust=basic
  else
    echo "Got unrecognized group '${group}'" >2
    exit 1
  fi

  local -a metadata=(
{'fraction_non_alphanumeric': 0.14390977443609024, 'fraction_numerical': 0.035338345864661655, 'mean_word_length': 5.227528089887641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '933699', 'n_tokens_mistral': 2583, 'n_tokens_neox': 2384, 'n_words': 465}