#!/bin/bash

# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the
# License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

set -e

usage() {
    cat <<EOF
Generate certificate suitable for use with an sidecar-injector webhook service.

This script uses k8s' CertificateSigningRequest API to a generate a
certificate signed by k8s CA suitable for use with sidecar-injector webhook
services. This requires permissions to create and approve CSR. See
https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster for
detailed explantion and additional instructions.

The server key/cert k8s CA cert are stored in a k8s secret.

usage: ${0} [OPTIONS]

The following flags are required.

       --service          Service name of webhook.
       --namespace        Namespace where webhook service and secret reside.
       --secret           Secret name for CA certificate and server certificate/key pair.
EOF
    exit 1
}

while [[ $# -gt 0 ]]; do
    case ${1} in
        --service)
            service="$2"
            shift
            ;;
        --secret)
            secret="$2"
            shift
            ;;
        --namespace)
            namespace="$2"
            shift
            ;;
        *)
            usage
            ;;
    esac
    shift
done

[ -z ${service} ] && service=vpc-admission-webhook-svc
[ -z ${secret} ] && secret=vpc-admission-webhook-certs
[ -z ${namespace} ] && namespace=default

if [ ! -x "$(command -v openssl)" ]; then
    echo "openssl not found"
    exit 1
fi

csrName=${service}.${namespace}
tmpdir=$(mktemp -d)
echo "creating certs in tmpdir ${tmpdir} "

cat <<EOF >> ${tmpdir}/csr.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${service}
DNS.2 = ${service}.${namespace}
DNS.3 = ${service}.${namespace}.svc
EOF

openssl genrsa -out ${tmpdir}/server-key.pem 2048
openssl req -new -key ${tmpdir}/server-key.pem -subj "/CN=${service}.${namespace}.svc" -out ${tmpdir}/server.csr -config ${tmpdir}/csr.conf

# clean-up any previously created CSR for our service. Ignore errors if not present.
kubectl delete csr ${csrName} 2>/dev/null || true

# create  server cert/key CSR and  send to k8s API
cat <<EOF | kubectl create -f -
apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest
metadata:
  name: ${csrName}
spec:
  groups:
  - system:authenticated
  request: $(cat ${tmpdir}/server.csr | base64 | tr -d '\n')
  usages:
  - digital signature
  - key encipherment
  - server auth
EOF

# verify CSR has been created
while true; do
    kubectl get csr ${csrName}
    if [ "$?" -eq 0 ]; then
        break
    fi
done

# approve and fetch the signed certificate
kubectl certificate approve ${csrName}
# verify certificate has been signed
for x in $(seq 10); do
    serverCert=$(kubectl get csr ${csrName} -o jsonpath='{.status.certificate}')
    if [[ ${serverCert} != '' ]]; then
        break
    fi
    sleep 1
done
if [[ ${serverCert} == '' ]]; then
    echo "ERROR: After approving csr ${csrName}, the signed certificate did not appear on the resource. Giving up after 10 attempts." >&2
    exit 1
fi
echo ${serverCert} | openssl base64 -d -A -out ${tmpdir}/server-cert.pem

# create the secret with CA cert and server cert/key
kubectl create secret generic ${secret} \
        --from-file=key.pem=${tmpdir}/server-key.pem \
        --from-file=cert.pem=${tmpdir}/server-cert.pem \
        --dry-run -o yaml |
    kubectl -n ${namespace} apply -f -


#!/bin/bash

# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the
# License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

ROOT=$(cd $(dirname $0)/../../; pwd)

set -o errexit
set -o nounset
set -o pipefail

export CA_BUNDLE=$(kubectl config view --raw -o json --minify | jq -r '.clusters[0].cluster."certificate-authority-data"' | tr -d '"')

if command -v envsubst >/dev/null 2>&1; then
    envsubst
else
    sed -e "s|\${CA_BUNDLE}|${CA_BUNDLE}|g"
fi


#!/bin/sh
set -e
# Start dockerd
/usr/local/bin/dockerd \
	--host=unix:///var/run/docker.sock \
	--host=tcp://127.0.0.1:2375 \
	--storage-driver=overlay2 &>/var/log/docker.log &

# Wait until dockerd is spun up
tries=0
d_timeout=60
until docker info >/dev/null 2>&1
do
	if [ "$tries" -gt "$d_timeout" ]; then
                cat /var/log/docker.log
		echo 'Timed out trying to connect to internal docker host.' >&2
		exit 1
	fi
        tries=$(( $tries + 1 ))
	sleep 1
done

echo $DOCKERHUB_TOKEN | docker login --password-stdin -u $DOCKERHUB_USERNAME

#Run all the e2e tests
cd /github.com/aws/copilot-cli/e2e/$TEST_SUITE && /go/bin/ginkgo -v -r --timeout=1h30m --fail-fast


#!/bin/sh
set -e
echo "e2e success: job running"
curl http://front-end.$COPILOT_SERVICE_DISCOVERY_ENDPOINT/job-setter/


#!/usr/bin/env sh
set -eu

envsubst '${NGINX_PORT}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf

exec "$@"


#! /bin/sh

[ -z "$STATUS" ] && STATUS="NOT OVERRIDDEN"

sleep 20

for i in 1 2 3 4 5 6 7 8; do
    echo "e2e environment variables: $STATUS"
    sleep 3
done


#!/bin/sh
set -e
echo "e2e success: job running"
curl http://front-end.$COPILOT_SERVICE_DISCOVERY_ENDPOINT/oraoraora-setter/


#! /usr/bin/env python3
import subprocess
import sys

cmd = subprocess.run(['aws', 'configure', 'list-profiles'], stdout=subprocess.PIPE)
profiles = cmd.stdout.decode('utf-8').split('\n')

if len(sys.argv) < 2:
  sys.exit('no arg passed to script, must be one of: "e2e" or "regression"')

if sys.argv[1] == "e2e":
  requiredProfiles = ['e2etestenv', 'e2eprodenv']
elif sys.argv[1] == "regression":
  requiredProfiles = ['regression']
else:
  sys.exit('invalid arg passed to script, must be one of: "e2e" or "regression"')

for profile in requiredProfiles:
  if profile in profiles:
    continue
  print(f'Profile [{profile}] is required but does not exist. Copying your [default] access key id and secret key...')
  cmd = subprocess.run(['aws', 'configure', 'get', 'default.region'], stdout=subprocess.PIPE)
  region = cmd.stdout.decode('utf-8').strip()
  cmd = subprocess.run(['aws', 'configure', 'get', 'default.aws_access_key_id'], stdout=subprocess.PIPE)
  access_key = cmd.stdout.decode('utf-8').strip()
  cmd = subprocess.run(['aws', 'configure', 'get', 'default.aws_secret_access_key'], stdout=subprocess.PIPE)
  secret = cmd.stdout.decode('utf-8').strip()
  subprocess.run(['aws', 'configure', 'set', 'region', region, '--profile', profile])
  subprocess.run(['aws', 'configure', 'set', 'aws_access_key_id', access_key, '--profile', profile])
  subprocess.run(['aws', 'configure', 'set', 'aws_secret_access_key', secret, '--profile', profile])


#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#
# license.sh checks that all Go files in the given correct-looking license header.

check_header() {
    got=$1
    want=$(cat <<EOF
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
EOF
    )
    if [[ "$got" = *"$want"* ]]; then
        return 0
    fi
    return 1
}

if [ $# != 1 ]; then
    echo "Usage: $0 rootdir" >&2
    exit 1
fi

fail=0
for file in $(find $1 -regextype 'posix-extended' -regex '.*\.(go|js)'); do
    case $file in
        $1/*/mocks/*)
            # Skip mocks packages.
        ;;
        $1/*/mock_*.go)
            # Skip mock files.
        ;;
        $1/*/node_modules/*)
            # Skip node modules for js files.
        ;;
        $1/site/*)
            # Skip website content
        ;;
        *)
            header="$(head -10 $file)"
            if ! check_header "$header"; then
                fail=1
                echo "${file#$1/} doesn't have the right copyright header:"
                echo "$header" | sed -e 's/^/    /g'
            fi
            ;;
    esac
done

if [ $fail -ne 0 ]; then
    exit 1
fi


#!/bin/bash

for((i=1; i <= 3 ; i++))
do
     service_account_name="WebApp${i}"
     domain_name="contoso"

     add_krb_msg="#######  Adding kerberos lease #######"
     echo ${add_krb_msg}

     add_kerberos_lease=$(grpc_cli call unix:/var/credentials-fetcher/socket/credentials_fetcher.sock AddKerberosLease "credspec_contents: '{\"CmsPlugins\":[\"ActiveDirectory\"],\"DomainJoinConfig\":{\"Sid\":\"S-1-5-21-4217655605-3681839426-3493040985\",\"MachineAccountName\":\"${service_account_name}\",\"Guid\":\"af602f85-d754-4eea-9fa8-fd76810485f1\",\"DnsTreeName\":\"${domain_name}.com\",\"DnsName\":\"${domain_name}.com\",\"NetBiosName\":\"${domain_name}\"},\"ActiveDirectoryConfig\":{\"GroupManagedServiceAccounts\":[{\"Name\":\"${service_account_name}\",\"Scope\":\"${domain_name}.com\"},{\"Name\":\"${service_account_name}\",\"Scope\":\"${domain_name}\"}]}}'")

   echo "Add kerberos lease response: ${add_kerberos_lease}"
   add_kerberos_lease_id=$(echo "${add_kerberos_lease}" | grep  'lease_id' | cut -f2 -d: | tr -d ' ' | tr -d '\n')

   echo "Added kerberos ticket for lease_id: ${add_kerberos_lease_id}"

   delete_krb_msg="#######  Deleting kerberos lease #######"
   echo ${delete_krb_msg}
   delete_kerberos_lease=$(grpc_cli call unix:/var/credentials-fetcher/socket/credentials_fetcher.sock DeleteKerberosLease "lease_id: '${add_kerberos_lease_id}'")

   echo "Delete kerberos tickets corresponding to lease: ${delete_kerberos_lease}"

done


#!/bin/sh

# Thanks to https://github.com/dotnet/sdk/issues/8742#issuecomment-890559867

DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_CLI_TELEMETRY_OPTOUT

sdkver=$(LC_ALL=C dotnet --version)
fwkver=$(LC_ALL=C dotnet --list-runtimes | \
    LC_ALL=C sed --posix -n '/^Microsoft.NETCore.App \([^ ]*\) .*$/{s//\1/p;q;}')

# dotnet-sdk-5.0 installed via .deb package
dotnethome=/usr/lib64/dotnet
dotnetlib=$dotnethome/shared/Microsoft.NETCore.App/$fwkver
dotnet_cscdll=$dotnethome/sdk/$sdkver/Roslyn/bincore/csc.dll
dotnet_csclib='-r:netstandard.dll -r:Microsoft.CSharp.dll -r:System.dll'
for x in "$dotnetlib"/System.*.dll; do
	dotnet_csclib="$dotnet_csclib -r:${x##*/}"
done
# add if needed
#dotnet_csclib="$dotnet_csclib -r:Microsoft.Win32.Primitives.dll"

exec dotnet "$dotnet_cscdll" "-lib:$dotnetlib" $dotnet_csclib "$@"

#!/bin/sh

DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_CLI_TELEMETRY_OPTOUT

sdkver=$(LC_ALL=C dotnet --version)
fwkver=$(LC_ALL=C dotnet --list-runtimes | \
    LC_ALL=C sed --posix -n '/^Microsoft.NETCore.App \([^ ]*\) .*$/{s//\1/p;q;}')

exename=$1
case $exename in
(*.exe|*.EXE) ;;
(*)
	echo >&2 "E: $exename is not a .exe file"
	exit 1
	;;
esac

jsonname=${exename%.*}.runtimeconfig.json
printf '%s"%s"%s\n' \
    '{"runtimeOptions":{"framework":{"name":"Microsoft.NETCore.App","version":' \
    "$fwkver" '}}}' >"$jsonname"


#!/bin/bash
# Verify that tests can be successfully run from the source build.
#
# NOTE: Humans should not run this file directly. If you want to run this check, run "tox -e sourcebuildcheck".

WORKINGDIR=${1}
DISTDIR=${2}

echo "Performing source build check"
echo "Using working directory ${WORKINGDIR}"
echo "Using dist directory ${DISTDIR}"

echo "Locating the source build and copying it into the working directory."
DISTFILE=`ls $DISTDIR/{{cookiecutter.pypi_name}}-*.tar.gz | tail -1`
echo "Found source build at ${DISTFILE}"
cp ${DISTFILE} ${WORKINGDIR}

echo "Extracting the source build."
cd ${WORKINGDIR}
NEWDISTFILE=$(ls {{cookiecutter.pypi_name}}-*.tar.gz | tail -1)
echo "Using distfile ${NEWDISTFILE}"
tar xzvf ${NEWDISTFILE}
rm ${NEWDISTFILE}
EXTRACTEDDIR=$(ls | tail -1)
cd ${EXTRACTEDDIR}

echo "Installing requirements from extracted source build."
pip install -r test/requirements.txt
pip install -e .

echo "Running tests from extracted source build."
pytest --cov {{cookiecutter.module_name}} -m local


#!/bin/bash

# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

while [[ "$#" -gt 0 ]]
do case $1 in
    -c|--use-cycles) use_cycles="$2"
    shift;;
    -f|--filter) filter="$2"
esac
shift
done

EXEC=`cargo bench --no-run 2>&1 | grep -oP benches-[0-9a-f]+`
DIR=`pwd`

# Drop file system cache
echo 3 | sudo tee /proc/sys/vm/drop_caches
sudo sync

# Disable address space randomization
echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

sudo rm -Rf ${DIR}/target/criterion

sudo DCP_USE_CYCLES=${use_cycles} nice -n -5 taskset -c 0 ${DIR}/target/release/deps/${EXEC} --bench -n ${filter} | grep -i "time:\|thrpt:"


#!/bin/bash
#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EKS_CLUSTER_MANAGER_ROLE=<ARN-of-IAM-role>
#/ export EKS_TEST_ROLE=<ARN-of-IAM-role>
#/ ./add_iam_identity.sh eks_cluster_name

set -ex

# Function to add cluster manager IAM role to EKS cluster
function add_cluster_manager_identity() {

  eksctl create iamidentitymapping \
    --cluster ${1} \
    --arn ${2} \
    --group system:masters \
    --username cluster_manager

  eksctl get iamidentitymapping \
    --cluster ${1}
}

# Function to add test IAM role to EKS cluster
function add_test_build_role() {

  eksctl create iamidentitymapping \
    --cluster ${1} \
    --arn ${2} \
    --username test-role

  eksctl get iamidentitymapping \
    --cluster ${1}
}

# Function to create rbac rules for test role in EKS cluster
function create_rbac_rules() {
  kubectl create -f rbac.yaml
}

# Check for input arguments
if [ $# -lt 1 ]; then
  echo "usage: ./${0} eks_cluster_name"
  exit 1
fi

CLUSTER=${1}

# Check for IAM role environment variables
if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ]; then
  add_cluster_manager_identity ${CLUSTER} ${EKS_CLUSTER_MANAGER_ROLE}
fi

# Check for IAM role environment variables
if [ -n "${EKS_TEST_ROLE}" ]; then
  add_test_build_role ${CLUSTER} ${EKS_TEST_ROLE}
fi

if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ] && [ -n "${EKS_TEST_ROLE}" ]; then
  create_rbac_rules
fi


#!/bin/bash
#/ Usage: ./build.sh

set -ex

# Create operation function
#
# Invokes create_cluster.sh script to create EKS cluster nodegroups and namespaces.
# Invokes add_iam_identity.sh script to create RBAC rules for test IAM role. Add cluster manager IAM role
# and test IAM role authentication to the cluster.
# Invokes install_cluster_components.sh script to install cluster autoscalar and kubeflow components in the cluster.
function create_cluster() {

  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}

      if ! check_cluster_status $CLUSTER_NAME; then
        ./create_cluster.sh $CLUSTER_NAME $EKS_VERSION
        ./add_iam_identity.sh $CLUSTER_NAME
        ./install_cluster_components.sh $CLUSTER_NAME $CLUSTER_AUTOSCALAR_IMAGE_VERSION
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: already exists. Skipping create operation."
      fi
    done
  done
}

# Upgrade cluster operation function
#
# Invokes upgrade_operation.sh script to upgrade the EKS cluster in the following order
# 1. Scale cluster autoscalar to 0 to prevent unwanted scaling
# 2. Upgrade EKS control plane
# 3. Upgrade EKS nodegroups:
#    i) Delete Exisiting nodegroups
#    ii) Create nodegroups with updated configuration
# 4. Upgrade core k8s components
# 5. Scale cluster autoscalar back to 1
function upgrade_cluster() {
  TARGET="CLUSTER"
  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}
      if check_cluster_status $CLUSTER_NAME; then
        ./upgrade_operation.sh $TARGET $CLUSTER_NAME $EKS_VERSION $CLUSTER_AUTOSCALAR_IMAGE_VERSION
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: does not exists. Skipping upgrade operation."
      fi
    done
  done

}

# Upgrade nodegroup operation function
#
# Invokes upgrade_operation.sh script to upgrade the EKS nodegroup for a cluster
function upgrade_nodegroup() {
  TARGET="NODEGROUP"
  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}
      if check_cluster_status $CLUSTER_NAME; then
        ./upgrade_operation.sh $TARGET $CLUSTER_NAME $EKS_VERSION
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: does not exists. Skipping upgrade operation."
      fi
    done
  done

}

# Upgrade kubeflow version
#
# Invokes upgrade_kubeflow script to upgrade kubeflow manifests on each EKS cluster
function upgrade_kubeflow(){
  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}
      if check_cluster_status $CLUSTER_NAME; then
        ./upgrade_kubeflow.sh $CLUSTER_NAME
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: does not exists. Skipping upgrade operation."
      fi
    done
  done
}

# Delete operation function
#
# Invokes delete_cluster.sh script to delete EKS cluster, nodegroups and other related components
function delete_cluster() {
  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}
      if check_cluster_status $CLUSTER_NAME; then
        ./delete_cluster.sh $CLUSTER_NAME
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: does not exists. Skipping delete operation."
      fi
    done
  done

}

# new operation function to perform one time operation on the cluster
# Once deployed, the contents of the new_operation.sh should be moved to a dedicated script
# to allow upcoming features to be configured in the new_operation.sh script
# Invokes new_operation.sh script to add graviton nodegroup on the EKS cluster
function new_operation() {

  for CONTEXT in "${CONTEXTS[@]}"; do
    for CLUSTER in "${EKS_CLUSTERS[@]}"; do
      CLUSTER_NAME=${CLUSTER}-${CONTEXT}
      if check_cluster_status $CLUSTER_NAME; then
        ./new_operation.sh $CLUSTER_NAME $EKS_VERSION
      else
        echo "EKS Cluster :: ${CLUSTER_NAME} :: does not exists. Skipping delete operation."
      fi
    done
  done

}

function check_cluster_status() {
  aws eks describe-cluster --name ${1} --region ${AWS_REGION} --query cluster.status --out text | grep -q ACTIVE
}

# Check for input arguments
if [ -z "$1" ]; then
  EKS_CLUSTERS=($(jq -r '.eks_clusters[]' build_param.json))
else
  EKS_CLUSTERS=${1}-`date +%s`
fi

if [ -z "$2" ]; then
  CONTEXTS=($(jq -r '.contexts[]' build_param.json))
else
  CONTEXTS=${2}
fi

# Parse parameters from build_param.json config file
OPERATION=$(jq -r '.operation' build_param.json)
EKS_VERSION=$(jq -r '.eks_version' build_param.json)
CLUSTER_AUTOSCALAR_IMAGE_VERSION=$(jq -r '.cluster_autoscalar_image_version' build_param.json)

# Create a cluster if the context is predeploy and operation is other than create
if [ "${CONTEXTS}" = "BETA" ] && [ "${OPERATION}" != "create" ]; then
  echo "Create a cluster inorder to perform operations other than creating cluster"
  create_cluster
fi

case ${OPERATION} in

create)
  create_cluster
  ;;

upgrade_cluster)
  upgrade_cluster
  ;;

upgrade_nodegroup)
  upgrade_nodegroup
  ;;

upgrade_kubeflow)
  upgrade_kubeflow
  ;;

delete)
  delete_cluster
  ;;

new_operation)
  new_operation
  ;;
*)
  echo "Specify valid operation"
  ;;
esac

# Cleanup the EKS cluster if the context is BETA
if [ "${CONTEXT}" = "BETA" ]; then
  echo "Delete the pre-deploy cluster"
  delete_cluster
fi


#!/bin/bash
#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EC2_KEY_PAIR_NAME=<EC2-Key-Pair-Name>
#/ ./create_cluster.sh eks_cluster_name eks_version

set -ex

# Function to create EC2 key pair
function create_ec2_key_pair() {
  aws ec2 create-key-pair \
    --key-name ${1} \
    --query 'KeyMaterial' \
    --output text >./${1}.pem
}

# Function to create EKS cluster using eksctl.
# The cluster name follows the dlc-{framework}-{build_context} convention
function create_eks_cluster() {

  if [ "${3}" = "us-east-1" ]; then
    ZONE_LIST=(a b d)
  else
    ZONE_LIST=(a b c)
  fi

  eksctl create cluster \
    --name ${1} \
    --version ${2} \
    --zones=${3}${ZONE_LIST[0]},${3}${ZONE_LIST[1]},${3}${ZONE_LIST[2]} \
    --without-nodegroup
}

# Function to create static and dynamic nodegroups in EKS cluster
function create_node_group() {

  STATIC_NODEGROUP_INSTANCE_TYPE="m5.large"
  GPU_NODEGROUP_INSTANCE_TYPE="p3.16xlarge"
  INF_NODEGROUP_INSTANCE_TYPE="inf1.xlarge"

  # static nodegroup
  eksctl create nodegroup \
    --name ${1}-static-nodegroup-${2/./-} \
    --cluster ${1} \
    --node-type ${STATIC_NODEGROUP_INSTANCE_TYPE} \
    --nodes 1 \
    --node-labels "static=true" \
    --tags "k8s.io/cluster-autoscaler/node-template/label/static=true" \
    --asg-access \
    --ssh-access \
    --ssh-public-key "${3}"

  # dynamic gpu nodegroup
  eksctl create nodegroup \
    --name ${1}-gpu-nodegroup-${2/./-} \
    --cluster ${1} \
    --node-type ${GPU_NODEGROUP_INSTANCE_TYPE} \
    --nodes-min 0 \
    --nodes-max 100 \
    --node-volume-size 80 \
    --node-labels "test_type=gpu" \
    --tags "k8s.io/cluster-autoscaler/node-template/label/test_type=gpu" \
    --asg-access \
    --ssh-access \
    --ssh-public-key "${3}"

  # dynamic inf nodegroup
  eksctl create nodegroup \
    --name ${1}-inf-nodegroup-${2/./-} \
    --cluster ${1} \
    --node-type ${INF_NODEGROUP_INSTANCE_TYPE} \
    --nodes-min 0 \
    --nodes-max 100 \
    --node-volume-size 500 \
    --node-labels "test_type=inf" \
    --tags "k8s.io/cluster-autoscaler/node-template/label/test_type=inf,k8s.io/cluster-autoscaler/node-template/resources/aws.amazon.com/neuron=1,k8s.io/cluster-autoscaler/node-template/resources/hugepages-2Mi=256Mi" \
    --asg-access \
    --ssh-access \
    --ssh-public-key "${3}"

}

# Attach IAM policy to nodegroup IAM role
function add_iam_policy() {
  NODE_GROUP_NAME=${1}
  CLUSTER_NAME=${2}
  REGION=${3}

  ROLE_ARN=$(aws eks describe-nodegroup --nodegroup-name ${NODE_GROUP_NAME} --cluster-name ${CLUSTER_NAME} --region ${REGION} | jq -r '.nodegroup.nodeRole')
  ROLE_NAME=$(echo ${ROLE_ARN} | grep -oP 'arn:aws:iam::\d+:role/\K\S+')

  declare -a POLICY_ARN=("arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess")

  for policy in ${POLICY_ARN[@]}; do
    aws iam attach-role-policy \
      --role-name $ROLE_NAME \
      --policy-arn $policy \
      --region ${REGION}
  done

}

# Function to create namespaces in EKS cluster
function create_namespaces() {
  kubectl create -f namespace.yaml
}

function add_iam_permissions_nodegroup() {
  CLUSTER_NAME=${1}
  REGION=${2}
  LIST_NODE_GROUPS=$(eksctl get nodegroup --cluster ${CLUSTER_NAME} --region ${REGION} -o json | jq -r '.[].Name')

  if [ -n "${LIST_NODE_GROUPS}" ]; then
    for NODEGROUP in ${LIST_NODE_GROUPS}; do
      add_iam_policy ${NODEGROUP} ${CLUSTER_NAME} ${REGION}
    done
  else
    echo "No Nodegroups present in the EKS cluster ${CLUSTER_NAME}"
  fi
}

#/ Tags added to the nodegroup do not propogate to the underlying Auto Scaling Group.
#/ Hence adding the tags explicitly as it is required for cluster autoscalar functionality
#/ See https://github.com/aws/containers-roadmap/issues/608
function add_tags_asg() {

  CLUSTER_NAME=${1}
  REGION=${2}

  for details in $(eksctl get nodegroup --cluster ${CLUSTER_NAME} --region ${REGION} -o json | jq -c '.[]'); do
    nodegroup_name=$(echo $details | jq -r '.Name')
    asg_name=$(echo $details | jq -r '.AutoScalingGroupName')

    if [[ ${nodegroup_name} == *"gpu"* ]]; then
      aws autoscaling create-or-update-tags \
        --tags ResourceId=${asg_name},ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/node-template/label/test_type,Value=gpu,PropagateAtLaunch=true
    fi

    if [[ ${nodegroup_name} == *"inf"* ]]; then
      aws autoscaling create-or-update-tags \
        --tags ResourceId=${asg_name},ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/node-template/label/test_type,Value=inf,PropagateAtLaunch=true \
        ResourceId=${asg_name},ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/node-template/resources/aws.amazon.com/neuron,Value=1,PropagateAtLaunch=true \
        ResourceId=${asg_name},ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/node-template/resources/hugepages-2Mi,Value=256Mi,PropagateAtLaunch=true
    fi

  done

}

# Check for input arguments
if [ $# -ne 2 ]; then
  echo "usage: ./${0} eks_cluster_name eks_version"
  exit 1
fi

# Check for IAM role environment variables
if [ -z "${AWS_REGION}" ]; then
  echo "AWS region not configured"
  exit 1
fi

CLUSTER=${1}
EKS_VERSION=${2}

# Check for EC2 keypair environment variable. If empty, create a new key pair.
if [ -z "${EC2_KEY_PAIR_NAME}" ]; then
  KEY_NAME=${CLUSTER}-KeyPair
  echo "No EC2 key pair name configured. Creating keypair ${KEY_NAME}"
  create_ec2_key_pair ${KEY_NAME}
  EC2_KEY_PAIR_NAME=${KEY_NAME}
else
  exist=$(aws ec2 describe-key-pairs --key-name ${EC2_KEY_PAIR_NAME} --region ${AWS_REGION} | grep KeyName | wc -l)
  if [ ${exist} -eq 0 ]; then
    echo "EC2 key pair ${EC2_KEY_PAIR_NAME} does not exist in ${AWS_REGION} region"
    exit 1
  fi
fi

create_eks_cluster ${CLUSTER} ${EKS_VERSION} ${AWS_REGION}
create_node_group ${CLUSTER} ${EKS_VERSION} ${EC2_KEY_PAIR_NAME}
add_tags_asg ${CLUSTER} ${AWS_REGION}
add_iam_permissions_nodegroup ${CLUSTER} ${AWS_REGION}
create_namespaces


#!/bin/bash

#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EKS_CLUSTER_MANAGER_ROLE=<ARN-of-IAM-role>
#/ ./delete_cluster.sh eks_cluster_name

set -ex

# Function to delete EKS cluster
function delete_cluster() {
  eksctl delete cluster \
    --name ${1} \
    --region ${2}
}

# Function to update kubeconfig at ~/.kube/config
function update_kubeconfig() {

  eksctl utils write-kubeconfig \
    --cluster ${1} \
    --authenticator-role-arn ${2} \
    --region ${3}

  kubectl config get-contexts
}

# Detach IAM policy from nodegroup IAM role
function remove_iam_policy() {

  NODE_GROUP_NAME=${1}
  CLUSTER_NAME=${2}
  REGION=${3}

  ROLE_ARN=$(aws eks describe-nodegroup --nodegroup-name ${NODE_GROUP_NAME} --cluster-name ${CLUSTER_NAME} --region ${REGION} | jq -r '.nodegroup.nodeRole')
  ROLE_NAME=$(echo ${ROLE_ARN} | grep -oP 'arn:aws:iam::\d+:role/\K\S+')

  declare -a POLICY_ARN=("arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess")

  for policy in ${POLICY_ARN[@]}; do
    aws iam detach-role-policy \
      --role-name $ROLE_NAME \
      --policy-arn $policy \
      --region ${REGION}
  done

}

function remove_iam_permissions_nodegroup() {
  CLUSTER_NAME=${1}
  REGION=${2}
  LIST_NODE_GROUPS=$(eksctl get nodegroup --cluster ${CLUSTER_NAME} --region ${REGION} -o json | jq -r '.[].Name')

  if [ -n "${LIST_NODE_GROUPS}" ]; then
    for NODEGROUP in ${LIST_NODE_GROUPS}; do
      remove_iam_policy ${NODEGROUP} ${CLUSTER_NAME} ${REGION}
    done
  else
    echo "No Nodegroups present in the EKS cluster ${CLUSTER_NAME}"
  fi
}

# Function to delete OIDC provider
function delete_oidc_provider() {
  CLUSTER_NAME=${1}

  account_id=$(aws sts get-caller-identity | jq -r '.Account')
  oidc_issuer=$(aws eks describe-cluster --name ${CLUSTER_NAME} | jq -r '.cluster.identity.oidc.issuer')
  oidc_url=$(echo $oidc_issuer | grep -oP 'https://\K\S+')
  oidc_provider_arn="arn:aws:iam::${account_id}:oidc-provider/${oidc_url}"

  if [ -n "${oidc_provider_arn}" ]; then
    echo "Deleting OICD provider ${oidc_provider_arn} attached to EKS cluster ${CLUSTER_NAME}"
    aws iam delete-open-id-connect-provider --open-id-connect-provider-arn ${oidc_provider_arn}
  fi

}

# Function to delete IAM roles related to OIDC
function delete_oidc_iam_roles() {
  CLUSTER_NAME=${1}

  declare -a OIDC_ROLE_LIST=("kf-admin-${CLUSTER_NAME}"
    "kf-user-${CLUSTER_NAME}"
  )

  for role in ${OIDC_ROLE_LIST[@]}; do
    role_policies=$(aws iam list-role-policies --role-name ${role} | jq -r '.PolicyNames[]')
    for policy in ${role_policies[@]}; do
      aws iam delete-role-policy --role-name ${role} --policy-name ${policy}
    done
    aws iam delete-role --role-name ${role}
  done

}
# Check for input arguments
if [ $# -ne 1 ]; then
  echo "usage: ./${0} eks_cluster_name"
  exit 1
fi

# Check for environment variables
if [ -z "${AWS_REGION}" ]; then
  echo "AWS region not configured"
  exit 1
fi

CLUSTER=${1}

if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ]; then
  update_kubeconfig ${CLUSTER} ${EKS_CLUSTER_MANAGER_ROLE} ${AWS_REGION}
fi

remove_iam_permissions_nodegroup ${CLUSTER} ${AWS_REGION}
# delete_oidc_provider ${CLUSTER}
# delete_oidc_iam_roles ${CLUSTER}
delete_cluster ${CLUSTER} ${AWS_REGION}


#!/bin/bash
#/ Usage: ./env_setup.sh

set -ex

# The below url/version is based on EKS v1.25.6. The same needs to be updated for EKS version upgrade.
KUBECTL_CLIENT="https://s3.us-west-2.amazonaws.com/amazon-eks/1.25.6/2023-01-30/bin/linux/amd64/kubectl"
EKSCTL_CLIENT="https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz"
AWS_IAM_AUTHENTICATOR="https://github.com/kubernetes-sigs/aws-iam-authenticator/releases/download/v0.6.2/aws-iam-authenticator_0.6.2_linux_amd64"

LATEST_KUBECTL_CLIENT_VERSION=1.25

function install_kubectl_client() {
    curl --silent --location ${KUBECTL_CLIENT} -o /usr/local/bin/kubectl
    chmod +x /usr/local/bin/kubectl
}
# aws caller identity
aws sts get-caller-identity

# install aws-iam-authenticator
curl --silent --location ${AWS_IAM_AUTHENTICATOR} -o /usr/local/bin/aws-iam-authenticator
chmod +x /usr/local/bin/aws-iam-authenticator

#aws-iam-authenticator version
aws-iam-authenticator version

# install kubectl
if ! [ -x "$(command -v kubectl)" ]; then
    install_kubectl_client
else
    # check if the kubectl client version is less than 1.20
    KUBECTL_VERSION=$(kubectl version --client -o json)
    CURRENT_KUBECTL_CLIENT_VERSION=$(echo "$KUBECTL_VERSION" | jq -r '.clientVersion.major').$(echo "$KUBECTL_VERSION" | jq -r '.clientVersion.minor' | sed 's/+$//g')

    if (($(echo "$CURRENT_KUBECTL_CLIENT_VERSION < $LATEST_KUBECTL_CLIENT_VERSION" | bc -l))); then
        install_kubectl_client
    fi
fi

#kubectl version
kubectl version --short --client

# install eksctl
if ! [ -x "$(command -v eksctl)" ]; then
    curl --silent --location ${EKSCTL_CLIENT} | tar xz -C /tmp
    mv /tmp/eksctl /usr/local/bin
fi

# eksctl version
eksctl version


#!/bin/bash

#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ ./install_cluster_components.sh eks_cluster_name cluster_autoscalar_image_version

set -ex

# Function to install cluster autoscalar
function install_cluster_autoscalar() {
  kubectl create -f cluster-autoscalar-autodiscover.yaml
  kubectl -n kube-system annotate deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"
  sed -e 's/<CLUSTER_NAME>/'"${1}"'/g;s/<VERSION>/'"${2}"'/g' cluster-autoscalar-autodiscover.yaml >/tmp/cluster-autoscalar-autodiscover-${1}.yaml &&
    kubectl replace -f /tmp/cluster-autoscalar-autodiscover-${1}.yaml

}

# Check for input arguments
if [ $# -ne 2 ]; then
  echo "usage: ./${0} eks_cluster_name cluster_autoscalar_image_version"
  exit 1
fi

# Check for environment variables
if [ -z "$AWS_REGION" ]; then
  echo "AWS region not configured"
  exit 1
fi

CLUSTER_NAME=${1}
CLUSTER_AUTOSCALAR_IMAGE_VERSION=${2}

install_cluster_autoscalar ${CLUSTER_NAME} ${CLUSTER_AUTOSCALAR_IMAGE_VERSION}

# install kubeflow
../test/dlc_tests/eks/eks_manifest_templates/kubeflow/install_kubeflow.sh ${CLUSTER_NAME}


#!/bin/bash
#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EKS_CLUSTER_MANAGER_ROLE=<ARN-of-IAM-role>
#/ ./new_operation.sh eks_cluster_name eks_version
set -ex

# Function to create EC2 key pair
function create_ec2_key_pair() {
  aws ec2 create-key-pair \
    --key-name ${1} \
    --query 'KeyMaterial' \
    --output text >./${1}.pem
}

# Function to update kubeconfig at ~/.kube/config
function update_kubeconfig() {

  eksctl utils write-kubeconfig \
    --cluster ${1} \
    --authenticator-role-arn ${2} \
    --region ${3}

  kubectl config get-contexts
}

# Attach IAM policy to nodegroup IAM role
function add_iam_policy() {
  NODE_GROUP_NAME=${1}
  CLUSTER_NAME=${2}
  REGION=${3}

  ROLE_ARN=$(aws eks describe-nodegroup --nodegroup-name ${NODE_GROUP_NAME} --cluster-name ${CLUSTER_NAME} --region ${REGION} | jq -r '.nodegroup.nodeRole')
  ROLE_NAME=$(echo ${ROLE_ARN} | grep -oP 'arn:aws:iam::\d+:role/\K\S+')

  declare -a POLICY_ARN=("arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess")

  for policy in ${POLICY_ARN[@]}; do
    aws iam attach-role-policy \
      --role-name $ROLE_NAME \
      --policy-arn $policy \
      --region ${REGION}
  done

}

function add_iam_permissions_nodegroup() {
  CLUSTER_NAME=${1}
  REGION=${2}
  LIST_NODE_GROUPS=$(eksctl get nodegroup --cluster ${CLUSTER_NAME} --region ${REGION} -o json | jq -r '.[].Name')

  if [ -n "${LIST_NODE_GROUPS}" ]; then
    for NODEGROUP in ${LIST_NODE_GROUPS}; do
      if [[ ${NODEGROUP} == *"graviton"* ]]; then
        add_iam_policy ${NODEGROUP} ${CLUSTER_NAME} ${REGION}
      fi
    done
  else
    echo "No Nodegroups present in the EKS cluster ${CLUSTER_NAME}"
  fi
}

#/ Tags added to the nodegroup do not propogate to the underlying Auto Scaling Group.
#/ Hence adding the tags explicitly as it is required for cluster autoscalar functionality
#/ See https://github.com/aws/containers-roadmap/issues/608
function add_tags_asg() {

  CLUSTER_NAME=${1}
  REGION=${2}

  for details in $(eksctl get nodegroup --cluster ${CLUSTER_NAME} --region ${REGION} -o json | jq -c '.[]'); do
    nodegroup_name=$(echo $details | jq -r '.Name')
    asg_name=$(echo $details | jq -r '.AutoScalingGroupName')

    if [[ ${nodegroup_name} == *"graviton"* ]]; then
      aws autoscaling create-or-update-tags \
        --tags ResourceId=${asg_name},ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/node-template/label/test_type,Value=graviton,PropagateAtLaunch=true
    fi

  done

}

# Function to create graviton nodegroup in EKS cluster
function create_graviton_node_group() {

  GRAVITON_NODEGROUP_INSTANCE_TYPE="c6g.4xlarge"

  # dynamic graviton nodegroup
  eksctl create nodegroup \
    --name ${1}-graviton-nodegroup-${2/./-} \
    --cluster ${1} \
    --node-type ${GRAVITON_NODEGROUP_INSTANCE_TYPE} \
    --nodes-min 0 \
    --nodes-max 100 \
    --node-volume-size 80 \
    --node-labels "test_type=graviton" \
    --tags "k8s.io/cluster-autoscaler/node-template/label/test_type=graviton" \
    --asg-access \
    --managed=true \
    --ssh-access \
    --ssh-public-key "${3}"
}

#Function to upgrade core k8s components
function update_eksctl_utils() {
  eksctl utils update-kube-proxy \
    --cluster ${1} \
    --region ${2} \
    --approve

  eksctl utils update-aws-node \
    --cluster ${1} \
    --region ${2} \
    --approve

  eksctl utils update-coredns \
    --cluster ${1} \
    --region ${2} \
    --approve
}

if [ $# -ne 2 ]; then
  echo "usage: ./${0} eks_cluster_name eks_version"
  exit 1
fi

if [ -z "${AWS_REGION}" ]; then
  echo "AWS region not configured"
  exit 1
fi

# Check for EC2 keypair environment variable. If empty, create a new key pair.
if [ -z "${EC2_KEY_PAIR_NAME}" ]; then
  KEY_NAME=${CLUSTER}-KeyPair
  echo "No EC2 key pair name configured. Creating keypair ${KEY_NAME}"
  create_ec2_key_pair ${KEY_NAME}
  EC2_KEY_PAIR_NAME=${KEY_NAME}
else
  exist=$(aws ec2 describe-key-pairs --key-name ${EC2_KEY_PAIR_NAME} --region ${AWS_REGION} | grep KeyName | wc -l)
  if [ ${exist} -eq 0 ]; then
    echo "EC2 key pair ${EC2_KEY_PAIR_NAME} does not exist in ${AWS_REGION} region"
    exit 1
  fi
fi

CLUSTER=${1}
EKS_VERSION=${2}

if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ]; then
  update_kubeconfig ${CLUSTER} ${EKS_CLUSTER_MANAGER_ROLE} ${AWS_REGION}
fi

update_eksctl_utils ${CLUSTER} ${AWS_REGION}
create_graviton_node_group ${CLUSTER} ${EKS_VERSION} ${EC2_KEY_PAIR_NAME}
add_tags_asg ${CLUSTER} ${AWS_REGION}
add_iam_permissions_nodegroup ${CLUSTER} ${AWS_REGION}


#!/bin/bash

#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EKS_CLUSTER_MANAGER_ROLE=<ARN-of-IAM-role>
#/ ./upgrade_kubeflow.sh eks_cluster_name

set -ex

# Function to update kubeconfig at ~/.kube/config
function update_kubeconfig() {

  eksctl utils write-kubeconfig \
    --cluster ${1} \
    --authenticator-role-arn ${2} \
    --region ${3}

  kubectl config get-contexts
}

# Check for eks cluster name
if [ $# -ne 1 ]; then
  echo "usage: ./${0} eks_cluster_name"
  exit 1
fi

# Check for environment variables
if [ -z "$AWS_REGION" ]; then
  echo "AWS region not configured"
  exit 1
fi

CLUSTER_NAME=${1}

if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ]; then
  update_kubeconfig ${CLUSTER_NAME} ${EKS_CLUSTER_MANAGER_ROLE} ${AWS_REGION}
fi

# install kubeflow
OPERATION="UPGRADE"
../test/dlc_tests/eks/eks_manifest_templates/kubeflow/install_kubeflow.sh ${CLUSTER_NAME} ${OPERATION}


#!/bin/bash
#/ Usage:
#/ export AWS_REGION=<AWS-Region>
#/ export EKS_CLUSTER_MANAGER_ROLE=<ARN-of-IAM-role>
#/ target can be one of [ cluster | nodegroup ]
#/ cluster_autoscalar_image_version option is not required for [nodegroup] target
#/ ./upgrade_operation.sh <target> eks_cluster_name eks_version cluster_autoscalar_image_version
set -ex

# Function to update kubeconfig at ~/.kube/config
function update_kubeconfig() {

  eksctl utils write-kubeconfig \
    --cluster ${1} \
    --authenticator-role-arn ${2} \
    --region ${3}

  kubectl config get-contexts
}

# Function to upgrade eks control plane
function upgrade_eks_control_plane() {

  eksctl upgrade cluster \
    --name ${1} \
    --version ${2} \
    --timeout 180m \
    --approve
}

# Function to control scaling of cluster autoscalar
function scale_cluster_autoscalar() {
  kubectl scale deployments/cluster-autoscaler \
    --replicas=${1} \
    -n kube-system
}
# Function to upgrade autoscalar image
function upgrade_autoscalar_image() {
  kubectl -n kube-system \
    set image deployment.apps/cluster-autoscaler cluster-autoscaler=k8s.gcr.io/autoscaling/cluster-autoscaler:${1}
}

# Function to upgrade nodegroups
function upgrade_nodegroups() {
  CLUSTER=${1}
  EKS_VERSION=${2}
  REGION=${3}

  LIST_NODE_GROUPS=$(eksctl get nodegroup --cluster ${CLUSTER} -o json | jq -r '.[].Name')

  if [ -n "${LIST_NODE_GROUPS}" ]; then

    for NODEGROUP in ${LIST_NODE_GROUPS}; do
      eksctl upgrade nodegroup \
        --name ${NODEGROUP} \
        --cluster ${CLUSTER} \
        --kubernetes-version ${EKS_VERSION} \
        --timeout 90m \
        --region ${REGION}
    done
  else
    echo "No Nodegroups present in the EKS cluster ${1}"
  fi
}

#Function to upgrade core k8s components
function update_eksctl_utils() {
  eksctl utils update-kube-proxy \
    --cluster ${1} \
    --region ${2} \
    --approve

  eksctl utils update-aws-node \
    --cluster ${1} \
    --region ${2} \
    --approve

  eksctl utils update-coredns \
    --cluster ${1} \
    --region ${2} \
    --approve
}

if [ $# -lt 3 ]; then
  echo "usage: ${0} target eks_cluster_name eks_version cluster_autoscalar_image_version"
  exit 1
fi

if [ -z "${AWS_REGION}" ]; then
  echo "AWS region not configured"
  exit 1
fi

TARGET=${1}
CLUSTER=${2}
EKS_VERSION=${3}
CLUSTER_AUTOSCALAR_IMAGE_VERSION=${4}

if [ -n "${EKS_CLUSTER_MANAGER_ROLE}" ]; then
  update_kubeconfig ${CLUSTER} ${EKS_CLUSTER_MANAGER_ROLE} ${AWS_REGION}
fi

if [ "${TARGET}" = "CLUSTER" ]; then
  #scale to 0 to avoid unwanted scaling
  scale_cluster_autoscalar 0
  upgrade_autoscalar_image ${CLUSTER_AUTOSCALAR_IMAGE_VERSION}
  upgrade_eks_control_plane ${CLUSTER} ${EKS_VERSION}
  upgrade_nodegroups ${CLUSTER} ${EKS_VERSION} ${AWS_REGION}
  update_eksctl_utils ${CLUSTER} ${AWS_REGION}
  #scale back to 1
  scale_cluster_autoscalar 1
elif [ "${TARGET}" = "NODEGROUP" ]; then
  upgrade_nodegroups ${CLUSTER} ${EKS_VERSION} ${AWS_REGION}
fi


#!/usr/bin/env bash

if [[ "$1" = "train" ]]; then
     CURRENT_HOST=$(jq .current_host  /opt/ml/input/config/resourceconfig.json)
     sed -ie "s/PLACEHOLDER_HOSTNAME/$CURRENT_HOST/g" changehostname.c
     gcc -o changehostname.o -c -fPIC -Wall changehostname.c
     gcc -o libchangehostname.so -shared -export-dynamic changehostname.o -ldl
     LD_PRELOAD=/libchangehostname.so train
else
     eval "$@"
fi


#!/bin/bash

neuron_monitor_running=0
if [[ ! -z "${NEURON_MONITOR_CW_REGION}" ]]; then
  # Start neuron monitor. If namespace/region variable is set then use it.
  if [[ ! -z "${NEURON_MONITOR_CONFIG_FILE}" ]]; then
    config="--config-file ${NEURON_MONITOR_CONFIG_FILE:+ $NEURON_MONITOR_CONFIG_FILE}"
  fi
  if [[ ! -z "${NEURON_MONITOR_CW_NAMESPACE}" ]]; then
    mnnamespace="--namespace ${NEURON_MONITOR_CW_NAMESPACE:+ $NEURON_MONITOR_CW_NAMESPACE}"
  fi
  region="--region ${NEURON_MONITOR_CW_REGION:+ $NEURON_MONITOR_CW_REGION}"
  /opt/aws/neuron/bin/neuron-monitor ${config:+ $config} | /opt/aws/neuron/bin/neuron-monitor-cloudwatch.py ${mnnamespace:+ $mnnamespace} ${region:+$region} >> /tmp/nm.log 2>&1 &
  nm_pid=$!
  echo "Neuron Monitor Started"
  neuron_monitor_running=1
fi


#!/bin/bash

MODEL_STORE=/opt/ml/model
MMS_CONFIG=/home/model-server/config.properties
MODEL_PATH=""

while getopts ":m:t:" opt; do
  case $opt in
    m) MODEL_PATH="$OPTARG"
    ;;
    t) MMS_CONFIG="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done

printf "Model path: %s\n" "$MODEL_PATH"
printf "MMS_CONFIG: %s\n" "$TS_CONFIG"
# Start the Model Server
if [[ -z "$MODEL_PATH" ]]; then
  multi-model-server --start --mms-config /home/model-server/config.properties --model-store /opt/ml/model &
else
  multi-model-server --start --mms-config $MMS_CONFIG --models $MODEL_PATH &
fi
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start MMS Model Server: $status"
  exit $status
fi


#!/usr/bin/env bash

if [ $1 == 'train' ]
then
    CURRENT_HOST=$(jq .current_host  /opt/ml/input/config/resourceconfig.json)

    sed -ie "s/PLACEHOLDER_HOSTNAME/$CURRENT_HOST/g" changehostname.c

    gcc -o changehostname.o -c -fPIC -Wall changehostname.c
    gcc -o libchangehostname.so -shared -export-dynamic changehostname.o -ldl

    LD_PRELOAD=/libchangehostname.so train
else
    serve
fi


#!/bin/bash

MODEL_STORE=/opt/ml/model
TS_CONFIG=/home/model-server/config.properties
MODEL_PATH=""

while getopts ":m:t:" opt; do
  case $opt in
    m) MODEL_PATH="$OPTARG"
    ;;
    t) TS_CONFIG="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done

printf "Model path: %s\n" "$MODEL_PATH"
printf "TS_CONFIG: %s\n" "$TS_CONFIG"
# Start the Model Server
if [[ -z "$MODEL_PATH" ]]; then
  torchserve --start --ts-config /home/model-server/config.properties --model-store /opt/ml/model &
else
  torchserve --start --ts-config $TS_CONFIG --models $MODEL_PATH &
fi
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start TF Model Server: $status"
  exit $status
fi


FRAMEWORK=$1

set -x

cd ${FRAMEWORK}
pwd

if [ "$FRAMEWORK" = "tensorflow"  ]; then
    cd ../inference
    chmod +x ./scripts/*.sh
    chmod +x ./docker/build_artifacts/sagemaker/serve
fi

cd ../../


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# PreRequisities
# Install mxnet >=1.3b and gluoncv on each machine
# Ensure you have Horovod, OpenMPI installed on each machine
# Specify hosts in the file `hosts`

set -ex

# Training
# num-epochs is configurable.
# Ideally user can specify num-epochs of their choice or rely on default value=90
# CodeBuild timeout is currently 90mins.
# When num-epochs = 40 and warmup-epochs = 10, training completes within 90min timeout
# Hence, for purpose of the PR num-epochs has been set to 40

python mxnet_imagenet_resnet50.py --num-epochs "40"


#!/bin/bash
aws configure set region "us-west-2"
aws cloudwatch put-metric-data --metric-name "test2" --namespace "PyTorch/SM/Benchmarks/TorchDynamo/Inductor" --value 5
PYTHON_VERSION=$(python -c 'import sys; print(sys.version_info[0])' | tr -d "'")
if [ "$PYTHON_VERSION" -eq 2 ]
then
  exit 0
fi
HOME_DIR=/opt/ml/output/data/test/benchmark
BIN_DIR=${HOME_DIR}/bin
LOG_DIR=${HOME_DIR}/logs

mkdir -p ${HOME_DIR}
mkdir -p ${BIN_DIR}
mkdir -p ${LOG_DIR}

python -c "import sys;
assert sys.version_info < (3, 7);"
PYTHON_VERSION_CHECK=`echo $?`

set -e
cd $BIN_DIR
# tmp fix for numpy deprecations, PR raised: https://github.com/HewlettPackard/dlcookbook-dlbs/pull/19
git clone --quiet --single-branch --depth 1 --branch v2.0.0 https://github.com/pytorch/pytorch.git pytorch

cd pytorch

pip install -U numpy
pip install deepspeed==0.8.2
pip install gitpython
pip install tabulate==0.9.0

TRAINING_LOG=${LOG_DIR}/pytorch_inductor_huggingface_benchmark.log

python benchmarks/dynamo/runner.py --suites=huggingface --training --dtypes=amp --compilers=inductor --output-dir=huggingface_logs --extra-args='--output-directory=./' > $TRAINING_LOG 2>&1

RETURN_VAL=`echo $?`
set -e

if [ ${RETURN_VAL} -eq 0 ]; then
    echo "Training Huggingface Benchmark Test Complete using PyTorch Inductor."
else
    echo "Training Huggingface Benchmark Test Failed using PyTorch Inductor."
    cat $TRAINING_LOG
    exit 1
fi

exit 0


#!/bin/bash

PYTHON_VERSION=$(python -c 'import sys; print(sys.version_info[0])' | tr -d "'")
if [ "$PYTHON_VERSION" -eq 2 ]
then
  exit 0
fi
HOME_DIR=/opt/ml/output/data/test/benchmark
BIN_DIR=${HOME_DIR}/bin
LOG_DIR=${HOME_DIR}/logs

mkdir -p ${HOME_DIR}
mkdir -p ${BIN_DIR}
mkdir -p ${LOG_DIR}

python -c "import sys;
assert sys.version_info < (3, 7);"
PYTHON_VERSION_CHECK=`echo $?`

set -e
cd $BIN_DIR
# tmp fix for numpy deprecations, PR raised: https://github.com/HewlettPackard/dlcookbook-dlbs/pull/19
git clone --quiet --single-branch --depth 1 --branch v2.0.0 https://github.com/pytorch/pytorch.git pytorch

cd pytorch

pip install -U numpy
pip install deepspeed==0.8.2
pip install gitpython
pip install tabulate==0.9.0

TRAINING_LOG=${LOG_DIR}/pytorch_inductor_timm_benchmark.log

python benchmarks/dynamo/runner.py --suites=timm_models --training --dtypes=amp --compilers=inductor --output-dir=timm_logs --extra-args='--output-directory=./' > $TRAINING_LOG 2>&1

RETURN_VAL=`echo $?`
set -e

if [ ${RETURN_VAL} -eq 0 ]; then
    echo "Training Timm Complete using PyTorch Inductor."
else
    echo "Training Timm Failed using PyTorch Inductor."
    cat $TRAINING_LOG
    exit 1
fi

exit 0


#!/bin/bash

PYTHON_VERSION=$(python -c 'import sys; print(sys.version_info[0])' | tr -d "'")
if [ "$PYTHON_VERSION" -eq 2 ]
then
  exit 0
fi
HOME_DIR=/opt/ml/output/data/test/benchmark
BIN_DIR=${HOME_DIR}/bin
LOG_DIR=${HOME_DIR}/logs

mkdir -p ${HOME_DIR}
mkdir -p ${BIN_DIR}
mkdir -p ${LOG_DIR}

python -c "import sys;
assert sys.version_info < (3, 7);"
PYTHON_VERSION_CHECK=`echo $?`

set -e
cd $BIN_DIR
# tmp fix for numpy deprecations, PR raised: https://github.com/HewlettPackard/dlcookbook-dlbs/pull/19
git clone --quiet --single-branch --depth 1 --branch v2.0.0 https://github.com/pytorch/pytorch.git pytorch

cd pytorch

pip install -U numpy
pip install deepspeed==0.8.2
pip install gitpython
pip install tabulate==0.9.0

# install torchdata and torchtext before installing torchbench
git clone --branch v0.6.0 https://github.com/pytorch/data.git
cd data
pip install .

cd ..
git clone --branch v0.15.1 https://github.com/pytorch/text torchtext
cd torchtext
git submodule update --init --recursive
FORCE_CUDA=1 python setup.py clean install

cd ../..
git clone --quiet --single-branch --depth 1 https://github.com/pytorch/benchmark.git
cd benchmark
python install.py

cd ../pytorch

TRAINING_LOG=${LOG_DIR}/pytorch_inductor_torchbench_benchmark.log
python benchmarks/dynamo/runner.py --suites=torchbench --training --dtypes=amp --compilers=inductor --output-dir=torchbench_logs --extra-args='--output-directory=./' > $TRAINING_LOG 2>&1

RETURN_VAL=`echo $?`
set -e

if [ ${RETURN_VAL} -eq 0 ]; then
    echo "Training Torchbench Complete using PyTorch Inductor."
else
    echo "Training Torchbench Failed using PyTorch Inductor."
    cat $TRAINING_LOG
    exit 1
fi

exit 0


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Ensure you have Horovod, OpenMPI, and Tensorflow installed on each machine
# Specify hosts in the file `hosts`

set -ex

# Training

export TENSORFLOW_INTER_OP_PARALLELISM=2
# For an EC2 c5.18xlarge instance, number of logical cores = 72
export TENSORFLOW_INTRA_OP_PARALLELISM=72
# For an EC2 c5.18xlarge instance, number of physical cores = 36
export OMP_NUM_THREADS=36
export KMP_AFFINITY='granularity=fine,verbose,compact,1,0'
# For an EC2 c5.18xlarge instance, number of physical cores / 4 = 36 /4 = 9
export TENSORFLOW_SESSION_PARALLELISM=9
export KMP_BLOCKTIME=1
export KMP_SETTINGS=0

python -W ignore horovod/examples/tensorflow_synthetic_benchmark.py --no-cuda --num-warmup-batche 10 --num-iters 10 \
--model ResNet50


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Ensure you have Horovod, OpenMPI, and Tensorflow installed on each machine
# Specify hosts in the file `hosts`

set -ex

# p3 instances have larger GPU memory, so a higher batch size can be used
GPU_MEM=`nvidia-smi --query-gpu=memory.total --format=csv,noheader -i 0 | awk '{print $1}'`
if [ $GPU_MEM -gt 15000 ] ; then BATCH_SIZE=256; else BATCH_SIZE=128; fi

# Training

python -W ignore deep-learning-models/models/resnet/tensorflow/train_imagenet_resnet_hvd.py \
--data_dir $SM_CHANNEL_TRAIN --num_epochs 90 -b $BATCH_SIZE \
--lr_decay_mode poly --warmup_epochs 10 --clear_log


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Ensure you have Horovod, OpenMPI, and Tensorflow installed on each machine
# Specify hosts in the file `hosts`

set -ex

# Training

export TENSORFLOW_INTER_OP_PARALLELISM=2
# For an EC2 c5.18xlarge instance, number of logical cores = 72
export TENSORFLOW_INTRA_OP_PARALLELISM=72
# For an EC2 c5.18xlarge instance, number of physical cores = 36
export OMP_NUM_THREADS=36
export KMP_AFFINITY='granularity=fine,verbose,compact,1,0'
# For an EC2 c5.18xlarge instance, number of physical cores / 4 = 36 /4 = 9
export TENSORFLOW_SESSION_PARALLELISM=9
export KMP_BLOCKTIME=1
export KMP_SETTINGS=0

python -W ignore horovod/examples/tensorflow2_synthetic_benchmark.py --no-cuda --num-warmup-batches 10 --num-iters 10 \
--model ResNet50


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Ensure you have Horovod, OpenMPI, and Tensorflow installed on each machine
# Specify hosts in the file `hosts`

set -ex

# p3 instances have larger GPU memory, so a higher batch size can be used
GPU_MEM=`nvidia-smi --query-gpu=memory.total --format=csv,noheader -i 0 | awk '{print $1}'`
if [ $GPU_MEM -gt 15000 ] ; then BATCH_SIZE=256; else BATCH_SIZE=128; fi

# Training

python -W ignore deep-learning-models/models/resnet/tensorflow2/train_tf2_resnet.py \
--data_dir $SM_CHANNEL_TRAIN --synthetic --batch_size 128 --num_batches 1000 --clear_log 2


#!/usr/bin/env bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Ensure you have Horovod, OpenMPI, and Tensorflow installed on each machine
# Specify hosts in the file `hosts`

set -ex

# p3 instances have larger GPU memory, so a higher batch size can be used
GPU_MEM=`nvidia-smi --query-gpu=memory.total --format=csv,noheader -i 0 | awk '{print $1}'`
if [ $GPU_MEM -gt 15000 ] ; then BATCH_SIZE=256; else BATCH_SIZE=128; fi

# Training

python -W ignore deep-learning-models/models/resnet/tensorflow2/train_tf2_resnet.py \
--data_dir $SM_CHANNEL_TRAIN --synthetic --batch_size 128 --num_batches 1000 --clear_log 2 --xla-off


#!/bin/bash

# For dockerfiles of PyTorch >= 2.0, CUDA_HOME is already set as an env, and is configured as /opt/conda
python -c "import torch; from packaging.version import Version; assert Version(torch.__version__) >= Version('2.0')"
TORCH_VERSION_2x=$?
if [ $TORCH_VERSION_2x -ne 0 ]; then
  CUDA_HOME=/usr/local/cuda
fi

set -e

echo "Building all_reduce_perf from nccl-tests"
cd /tmp/
rm -rf nccl-tests/
git clone https://github.com/NVIDIA/nccl-tests.git
cd nccl-tests/
make MPI=1 MPI_HOME=/opt/amazon/openmpi NCCL_HOME=/usr/local CUDA_HOME=${CUDA_HOME}
cp build/all_reduce_perf /all_reduce_perf
cd /tmp/
rm -rf nccl-tests/


#!/bin/bash
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.

set -ex

# Script to run fi_pingpong locally over EFA to test connectivity.

if ! command -v fi_info >/dev/null 2>&1 || ! command -v fi_pingpong >/dev/null 2>&1; then
        echo "Error: required libfabric binaries not found."
        exit 1
fi

if ! fi_info -p efa >/dev/null 2>&1; then
        echo "Error: EFA libfabric provider not detected." >&2
        exit 1
fi

echo "Starting server..."
FI_EFA_ENABLE_SHM_TRANSFER=0 fi_pingpong -e rdm -p efa >/dev/null 2>&1 &
sleep 0.5

echo "Starting client..."
FI_EFA_ENABLE_SHM_TRANSFER=0 timeout 8 fi_pingpong -e rdm -p efa localhost
ret=$?
if [ $ret -ne 0 ]; then
        if [ $ret -eq 124 ]; then
                echo "Error: fi_pingpong test timed out." >&2
        else
                echo "Error: fi_pingpong test returned $ret." >&2
        fi
fi
kill %1
exit $ret


# Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

#!/usr/bin/env bash
sanity | grep 'Failures: 0, Errors: 0' &> /dev/null
if [ $? != 0 ]; then
    echo "GDRCopy Sanity check failed!"
    exit 1
fi
echo "GDRCopy Sanity check succeed!"


#!/bin/bash
#/ Usage:
#/ cluster name is required. operation is optional
#/ ./install_kubeflow.sh eks_cluster_name [operation]

set -ex

# Function to install kustomize
install_kustomize(){
    KUSTOMIZE_VERSION="v4.5.7"
    KUSTOMIZE_URL="https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2F${KUSTOMIZE_VERSION}/kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz"

    if ! command -v kustomize &> /dev/null
    then
        wget -O /tmp/kustomize_${KUSTOMIZE_VERSION}_linux.tar.gz ${KUSTOMIZE_URL}
        tar -xvf /tmp/kustomize_${KUSTOMIZE_VERSION}_linux.tar.gz -C /tmp/
        if ! [ -x "$command -v sudo" ]; then
            mv /tmp/kustomize /usr/local/bin
        else
            sudo mv /tmp/kustomize /usr/local/bin
        fi
    fi
}

# Function to install kubeflow in EKS cluster using kustomize
install_kubeflow(){

    echo "> Installing kubeflow namespace"
    kustomize build manifests/common/kubeflow-namespace/base | kubectl apply -f -

    echo "> Installing training operators"
    kustomize build manifests/apps/training-operator/upstream/overlays/kubeflow | kubectl apply -f -
}

# Function to remove kubeflow training operators in EKS cluster using kustomize
uninstall_kubeflow(){

    echo "> Uninstalling training operators"
    while ! kustomize build manifests/apps/training-operator/upstream/overlays/kubeflow | kubectl delete -f -; do echo "Retrying to delete training operator resources"; sleep 10; done

}

# Function to create directory and download kubeflow components
setup_kubeflow(){
    KUBEFLOW_VERSION="v1.7.0"
    local EKS_CLUSTER_NAME=$1
    DIRECTORY="${HOME}/${EKS_CLUSTER_NAME}"

    if [ -d "${DIRECTORY}" ]; then
        rm -rf ${DIRECTORY};
    fi

    mkdir ${DIRECTORY}
    cd ${DIRECTORY}

    # clones manifests from kubeflow github into a folder named manifests
    git clone -b ${KUBEFLOW_VERSION} --single-branch https://github.com/kubeflow/manifests.git
}

# Check for input arguments
if [ $# ge 1 ]; then
    echo "usage: ./${0} eks_cluster_name [operation]"
    exit 1
fi

EKS_CLUSTER_NAME=${1}
OPERATION=${2}

echo "> Installing kustomize"
install_kustomize

echo "> Setup installation directory"
setup_kubeflow ${EKS_CLUSTER_NAME}

# uninstall the manifest if operation is to upgrade kubeflow
if [ "${OPERATION}" = "UPGRADE" ]; then
    echo "> Uninstalling kubeflow manifest"
    uninstall_kubeflow
fi

echo "> Setting up kubeflow"
install_kubeflow

echo "> Installation complete"


# Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

#!/usr/bin/env bash

/opt/amazon/efa/bin/fi_info -p efa | grep 'efa' &> /dev/null
if [ $? == 1 ]; then
    echo "EFA not picked on instance"
    exit 1
fi
echo "EFA picked on instance"


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

python smdataparallel_mnist.py


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

if [[ $(python -c "import torch; from packaging.version import Version; is_less_than_pt10 = Version(torch.__version__) < Version('1.10'); print(is_less_than_pt10)") == 'True' ]]
then
    smddpsinglenode python smdataparallel_mnist.py
else
    smddp_singlenode_dev python smdataparallel_mnist.py
fi

bash smmodelparallel_mnist_script_mode.sh


set -ex

export SM_HP_MP_PARAMETERS=\{\"ddp\":true,\"microbatches\":4,\"partitions\":2,\"pipeline\":\"interleaved\"\}
mpirun -mca btl_vader_single_copy_mechanism none --allow-run-as-root -np 8 python smmodelparallel_pt_mnist.py --assert-losses 1 --data-dir data/training


#!/bin/bash

ab -k -n 10000 -c 16 -p test/resources/inputs/test.json -T 'application/json' http://localhost:8080/tfs/v1/models/half_plus_three:predict
ab -k -n 10000 -c 16 -p test/resources/inputs/test.json -T 'application/json' http://localhost:8080/invocations
ab -k -n 10000 -c 16 -p test/resources/inputs/test.jsons -T 'application/json' http://localhost:8080/invocations
ab -k -n 10000 -c 16 -p test/resources/inputs/test.csv -T 'text/csv' http://localhost:8080/invocations
ab -k -n 10000 -c 16 -p test/resources/inputs/test-cifar.json -T 'application/json'     -H 'X-Amzn-SageMaker-Custom-Attributes: tfs-model-name=cifar' http://localhost:8080/invocations

# Larger payloads are generated and removed when this script exits.
TEMPFILE='/tmp/perftest_data'
trap 'rm -f $TEMPFILE' EXIT

echo "Generating data"
# Creates a 10MB file with 10000 columns per line.
python test/perf/data_generator.py -c 'text/csv' -s 10000 -p 10 -u MB > $TEMPFILE || exit $?
ab -k -n 10 -c 1 -p "$TEMPFILE" -T 'text/csv' http://localhost:8080/invocations

python test/perf/data_generator.py -c 'application/json' -s 10000 -p 10 -u MB > $TEMPFILE || exit $?
ab -k -n 10 -c 1 -p "$TEMPFILE" -T 'application/json' http://localhost:8080/invocations

python test/perf/data_generator.py -c 'application/jsonlines' -s 10000 -p 10 -u MB > $TEMPFILE || exit $?
ab -k -n 10 -c 1 -p "$TEMPFILE" -T 'application/jsonlines' http://localhost:8080/invocations


#!/bin/bash

instance_type="${1:-c5.xlarge}"
if [[ "$instance_type" == p* ]]; then
    arch='gpu'
else
    arch='cpu'
fi

endpoint_name=$(echo "sagemaker-tensorflow-serving-$instance_type" | tr . -)

aws sagemaker create-endpoint-config \
    --endpoint-config-name $endpoint_name \
    --production-variants '[{
        "VariantName": "variant-name-1",
        "ModelName": "sagemaker-tensorflow-serving-model-'$arch'",
        "InitialInstanceCount": 1,
        "InstanceType": "ml.'$instance_type'"
    }]'

aws sagemaker create-endpoint --endpoint-name $endpoint_name --endpoint-config-name $endpoint_name


#!/bin/bash

set -e

arch=${1:-'cpu'}
aws_region=$(aws configure get region)
aws_account=$(aws --region $aws_region sts --endpoint-url https://sts.$aws_region.amazonaws.com get-caller-identity --query 'Account' --output text)

# change this to match SageMaker execution role in your account
sagemaker_role="arn:aws:iam::$aws_account:role/service-role/AmazonSageMaker-ExecutionRole-20180510T114550"

tar -C test/resources/models -czf /tmp/sagemaker-tensorflow-serving-model.tar.gz .
aws s3 mb s3://sagemaker-$aws_region-$aws_account || true
aws s3 cp /tmp/sagemaker-tensorflow-serving-model.tar.gz s3://sagemaker-$aws_region-$aws_account/sagemaker-tensorflow-serving/test-models/sagemaker-tensorflow-serving-model.tar.gz
rm /tmp/sagemaker-tensorflow-serving-model.tar.gz

aws sagemaker create-model \
    --model-name sagemaker-tensorflow-serving-model-$arch \
    --primary-container '{
        "Image": "'$aws_account'.dkr.ecr.'$aws_region'.amazonaws.com/sagemaker-tensorflow-serving:1.11.1-'$arch'",
        "ModelDataUrl": "s3://sagemaker-'$aws_region'-'$aws_account'/sagemaker-tensorflow-serving/test-models/sagemaker-tensorflow-serving-model.tar.gz",
        "Environment": {
            "SAGEMAKER_TFS_DEFAULT_MODEL_NAME": "half_plus_three"
        }
    }' \
    --execution-role-arn "$sagemaker_role"


#!/bin/bash

endpoint=${1-'sagemaker-tensorflow-serving-cpu-c5-xlarge'}
aws sagemaker delete-endpoint --endpoint-name $endpoint
aws sagemaker delete-endpoint-config --endpoint-config-name $endpoint


#!/bin/bash

for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model sm-c5xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model sm-c5xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model sm-c5xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model sm-c5xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model sm-c518xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model sm-c518xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model sm-c518xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model sm-c518xl >> sm-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 64 --model sm-c518xl >> sm-perftest.log; done

for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model tfs-c5xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model tfs-c5xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model tfs-c5xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model tfs-c5xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model tfs-c518xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model tfs-c518xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model tfs-c518xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model tfs-c518xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 64 --model tfs-c518xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python perftest_endpoint.py --count 5000 --warmup 100 --workers 128 --model tfs-c518xl >> tfs-perftest.log; done

for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model tfs-p2xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model tfs-p2xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model tfs-p2xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model tfs-p2xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 64 --model tfs-p2xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 4 --model tfs-p316xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 8 --model tfs-p316xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 16 --model tfs-p316xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 32 --model tfs-p316xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 64 --model tfs-p316xl >> tfs-perftest.log; done
for i in $(seq 1 5); do python test/perf/perftest_endpoint.py --count 5000 --warmup 100 --workers 128 --model tfs-p316xl >> tfs-perftest.log; done


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

mpirun --allow-run-as-root -np 2 python send_receive_checkpoint.py


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

smddp_singlenode_dev python smdataparallel_mnist.py

mpirun --allow-run-as-root -np 2 python tf2_conv.py


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

mpirun --allow-run-as-root -np 4 python smmodelparallel_hvd2_conv.py


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

mpirun --allow-run-as-root -np 2 python tf2_conv.py
mpirun --allow-run-as-root -np 2 python tf2_conv.py xla


#!/usr/bin/env bash
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

set -ex

mpirun --allow-run-as-root -np 2 python tf2_checkpoint_test.py


#!/usr/bin/env bash
set -euo pipefail

log() { echo "> $*" >&2; }

if ! hash lipo 2>/dev/null; then
    log "Unable to build universal binary without 'lipo' (macOS only)."
    exit 1
fi

log "Building EC2 macOS Init..."

# Get commit date and version tag
COMMITDATE="$(git show -s --format=%ci HEAD)"
VERSION="$(git describe --always --tags)"

log "Commit date: ${COMMITDATE}"
log "Version: ${VERSION}"

for arch in amd64 arm64; do
    log "Building for darwin/$arch"
    GOOS=darwin GOARCH="$arch" CGO_ENABLED=0 \
        go build -trimpath \
        -ldflags="-s -w -X 'main.CommitDate=${COMMITDATE}' -X 'main.Version=${VERSION}'" \
        -o "ec2-macos-init_$arch"
done

lipo -create -output ec2-macos-init \
    ec2-macos-init_amd64 ec2-macos-init_arm64

log "Build complete"


#!/usr/bin/env sh
#
# Copyright 2017-2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
#

set -ex

BASE_DIR=$(pwd)
BUILD_ROOT=${BASE_DIR}/build/debbuild
VERSION=1.35.0
RELEASE=1
DEB_SYSTEM_RELEASE_PATH=/etc/os-release

echo 'Cleaning deb build workspace'
rm -rf ${BUILD_ROOT}
mkdir -p ${BUILD_ROOT}

echo 'Creating application directories'
mkdir -p ${BUILD_ROOT}/etc/amazon/efs
mkdir -p ${BUILD_ROOT}/etc/init/
mkdir -p ${BUILD_ROOT}/etc/systemd/system
mkdir -p ${BUILD_ROOT}/sbin
mkdir -p ${BUILD_ROOT}/usr/bin
mkdir -p ${BUILD_ROOT}/var/log/amazon/efs
mkdir -p ${BUILD_ROOT}/usr/share/man/man8

echo 'Copying application files'
install -p -m 644 dist/amazon-efs-mount-watchdog.conf ${BUILD_ROOT}/etc/init
install -p -m 644 dist/amazon-efs-mount-watchdog.service ${BUILD_ROOT}/etc/systemd/system
install -p -m 444 dist/efs-utils.crt ${BUILD_ROOT}/etc/amazon/efs
install -p -m 644 dist/efs-utils.conf ${BUILD_ROOT}/etc/amazon/efs
install -p -m 755 src/mount_efs/__init__.py ${BUILD_ROOT}/sbin/mount.efs
install -p -m 755 src/watchdog/__init__.py ${BUILD_ROOT}/usr/bin/amazon-efs-mount-watchdog

echo 'Copying install scripts'
install -p -m 755 dist/scriptlets/after-install-upgrade ${BUILD_ROOT}/postinst
install -p -m 755 dist/scriptlets/before-remove ${BUILD_ROOT}/prerm
install -p -m 755 dist/scriptlets/after-remove ${BUILD_ROOT}/postrm

echo 'Copying control file'
install -p -m 644 dist/amazon-efs-utils.control ${BUILD_ROOT}/control

echo 'Copying conffiles'
install -p -m 644 dist/amazon-efs-utils.conffiles ${BUILD_ROOT}/conffiles

echo 'Copying manpages'
install -p -m 644 man/mount.efs.8 ${BUILD_ROOT}/usr/share/man/man8/mount.efs.8

echo 'Creating deb binary file'
echo '2.0'> ${BUILD_ROOT}/debian-binary

echo 'Setting permissions'
find ${BUILD_ROOT} -type d | xargs chmod 755;

echo 'Creating tar'
cd ${BUILD_ROOT}
tar czf control.tar.gz control conffiles postinst prerm postrm --owner=0 --group=0
tar czf data.tar.gz etc sbin usr var --owner=0 --group=0
cd ${BASE_DIR}

echo 'Building deb'
DEB=${BUILD_ROOT}/amazon-efs-utils-${VERSION}-${RELEASE}_all.deb
ar r ${DEB} ${BUILD_ROOT}/debian-binary
ar r ${DEB} ${BUILD_ROOT}/control.tar.gz
ar r ${DEB} ${BUILD_ROOT}/data.tar.gz

echo 'Copying deb to output directory'
cp ${BUILD_ROOT}/amazon-efs-utils*deb build/


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BUILD_LIB_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/" && pwd -P)"

if [ -f "/buildkit.sh" ] && ! buildctl debug workers > /dev/null 2>&1; then
    # on the builder base this helper file exists to run buildkitd
    # in prow buildkitd is run as a separate container so it will be running already
    # in codebuild its run directly on the instance so we want to use this helper
    /buildkit.sh "$@"
else
    buildctl "$@"
fi


# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function buildkit_ready() {
  for i in {1..24}
  do
    if [ ! -S "/run/buildkit/buildkitd.sock" ]
    then
      echo "Buildkit daemon is not running. Retrying."
      sleep 5s
    else
      exit 0
    fi
  done
  echo "Buildkit daemon is not available"
  exit 1
}

buildkit_ready


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a diff impl than what it is eks-anywhere-build-tooling
# we can rely on the git_tag file existing in this repo
# instead we pull it from eks-anywhere-build-tooling git
function build::common::get_latest_eksa_asset_url() {
  local -r artifact_bucket=$1
  local -r project=$2
  local -r arch=${3-amd64}
  local -r latesttag=${4-latest}

  local branch=main
  if [[ "$latesttag" != "latest" ]]; then
    branch=$latesttag
  fi

  local -r git_tag=$(curl -sL https:/raw.githubusercontent.com/aws/eks-anywhere-build-tooling/$branch/projects/$project/GIT_TAG)
  local -r url="https://$(basename $artifact_bucket).s3-us-west-2.amazonaws.com/projects/$project/$latesttag/$(basename $project)-linux-$arch-${git_tag}.tar.gz"

  local -r http_code=$(curl -I -L -s -o /dev/null -w "%{http_code}" $url)
  if [[ "$http_code" == "200" ]]; then
    echo "$url"
  else
    echo "https://$(basename $artifact_bucket).s3-us-west-2.amazonaws.com/projects/$project/latest/$(basename $project)-linux-$arch-${git_tag}.tar.gz"
  fi
}


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -x
set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "${SCRIPT_ROOT}/common.sh"

BINARY_DEPS_DIR="$1"
DEP="$2"
ARTIFACTS_BUCKET="$3"
LATEST_TAG="$4"
#RELEASE_BRANCH="${5-$(build::eksd_releases::get_release_branch)}"

DEP=${DEP#"$BINARY_DEPS_DIR"}
OS_ARCH="$(cut -d '/' -f1 <<< ${DEP})"
PRODUCT=$(cut -d '/' -f2 <<< ${DEP})
REPO_OWNER=$(cut -d '/' -f3 <<< ${DEP})
REPO=$(cut -d '/' -f4 <<< ${DEP})

ARCH="$(cut -d '-' -f2 <<< ${OS_ARCH})"

OUTPUT_DIR_FILE=$BINARY_DEPS_DIR/linux-$ARCH/$PRODUCT/$REPO_OWNER/$REPO
if [[ $REPO == *.tar.gz ]]; then
    mkdir -p $(dirname $OUTPUT_DIR_FILE)
else
    mkdir -p $OUTPUT_DIR_FILE
fi

if [[ $PRODUCT = 'eksd' ]]; then
    if [[ $REPO_OWNER = 'kubernetes' ]]; then
        TARBALL="kubernetes-$REPO-linux-$ARCH.tar.gz"
        URL=$(build::eksd_releases::get_eksd_kubernetes_asset_url $TARBALL $RELEASE_BRANCH $ARCH)
        # these tarballs will extra with the kubernetes/{client,server} folders
        OUTPUT_DIR_FILE=$BINARY_DEPS_DIR/linux-$ARCH/$PRODUCT
    else
        URL=$(build::eksd_releases::get_eksd_component_url $REPO_OWNER $RELEASE_BRANCH $ARCH)
    fi
else
    TARBALL="$REPO-linux-$ARCH.tar.gz"
    URL=$(build::common::get_latest_eksa_asset_url $ARTIFACTS_BUCKET $REPO_OWNER/$REPO $ARCH $LATEST_TAG)
fi

if [[ $REPO == *.tar.gz ]]; then
    curl -sSL $URL -o $OUTPUT_DIR_FILE
else
    curl -sSL $URL | tar xz -C $OUTPUT_DIR_FILE
fi


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -x
set -o errexit
set -o nounset
set -o pipefail

BINARY_DEPS_DIR="$1"
ARCH="$2"

OUTPUT_DIR="${BINARY_DEPS_DIR}/linux-$ARCH"
EKS_A_TOOL_BINARY_DIR="${OUTPUT_DIR}/eks-a-tools/binary"
EKS_A_TOOL_LICENSE_DIR="${OUTPUT_DIR}/eks-a-tools/licenses"

mkdir -p $EKS_A_TOOL_BINARY_DIR
mkdir -p $EKS_A_TOOL_LICENSE_DIR

declare -A project_bin_licenses=(["eksa/vmware/govmomi"]="govc GOVMOMI"
                                 ["eksa/helm/helm"]="helm HELM"
                                 ["eksa/apache/cloudstack-cloudmonkey"]="cmk CMK")

for project in "${!project_bin_licenses[@]}"
do
  bin_license_map=(${project_bin_licenses[$project]})
  binary=${bin_license_map[0]}
  license_prefix=${bin_license_map[1]}
  cp $OUTPUT_DIR/$project/$binary $EKS_A_TOOL_BINARY_DIR/$(basename $binary)
  cp $OUTPUT_DIR/$project/ATTRIBUTION.txt $EKS_A_TOOL_LICENSE_DIR/${license_prefix}_ATTRIBUTION.txt
  cp -r $OUTPUT_DIR/$project/LICENSES $EKS_A_TOOL_LICENSE_DIR/${license_prefix}_LICENSES
done


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x
set -o pipefail

cat << EOF > ${INTEGRATION_TEST_INFRA_CONFIG}
---

ec2:
  amiId: ${INTEGRATION_TEST_AL2_AMI_ID}
  subnetId: ${INTEGRATION_TEST_SUBNET_ID}

vSphere:
  url: ${TEST_RUNNER_GOVC_URL}
  insecure: True
  library: ${TEST_RUNNER_GOVC_LIBRARY}
  template: ${TEST_RUNNER_GOVC_TEMPLATE}
  datacenter: ${TEST_RUNNER_GOVC_DATACENTER}
  datastore: ${TEST_RUNNER_GOVC_DATASTORE}
  resourcePool: ${TEST_RUNNER_GOVC_RESOURCE_POOL}
  network: ${TEST_RUNNER_GOVC_NETWORK}
  folder: ${TEST_RUNNER_GOVC_FOLDER}
EOF


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x
set -o pipefail

if [ "$TEST_ROLE_ARN" == "" ]; then
    echo "Empty TEST_ROLE_ARN, this script is used in CodeBuild to set profile to assume an IAM role"
    exit 1
fi

export AWS_SDK_LOAD_CONFIG=1
export AWS_CONFIG_FILE=$(pwd)/awscliconfig
export AWS_PROFILE=e2e-test-account

cat << EOF > ${AWS_CONFIG_FILE}
[profile ${AWS_PROFILE}]
role_arn=${TEST_ROLE_ARN}
region=us-west-2
credential_source=EcsContainer
EOF


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x
set -o pipefail

source /docker.sh
start::dockerd
wait::for::dockerd


#!/usr/bin/env bash

set -x
set -o errexit
set -o nounset
set -o pipefail

ARTIFACTS_BUCKET="${1?Specify first argument - artifacts bucket}"
REPO_ROOT="${2?Specify second argument - repo root directory}"
PROJECT_PATH="${3? Specify third argument - project path}"
BUILD_IDENTIFIER="${4? Specify fourth argument - build identifier}"
GIT_HASH="${5?Specify fifth argument - git hash of the tar builds}"
OS_LIST_CSV="${6?Specify sixth argument - comma-separated list of operating systems for CLI build}"
ARCH_LIST_CSV="${7?Specify seventh argument - comma-separated list of architectures for CLI build}"
BRANCH_NAME="${8?Specify eighth argument - branch name that CLI was built from}"
DRY_RUN="${9?Specify ninth argument - Dry run upload}"

REPO="eksctl-anywhere"
BINARY_PATH="bin"
TAR_PATH="${REPO_ROOT}/${PROJECT_PATH}/${BUILD_IDENTIFIER}-${GIT_HASH}/artifacts"
LATEST=latest
if [[ $BRANCH_NAME != "main" ]]; then
  LATEST=$BRANCH_NAME
fi
CODEBUILD_CI="${CODEBUILD_CI:-false}"

function build::cli::move_artifacts() {
  local -r os=$1
  local -r arch=$2
  local -r cli_artifacts_path=$3

  mv ${BINARY_PATH}/${os}/${arch}/eksctl-anywhere ${cli_artifacts_path}
  cp ATTRIBUTION.txt ${cli_artifacts_path}
}

function build::cli::create_tarball() {
  local -r os=$1
  local -r arch=$2
  local -r tar_file=$3
  local -r tar_path=$4
  local -r cli_artifacts_path=$5

  build::ensure_tar

  "${TAR}" czf "${tar_path}/${os}/${arch}/${tar_file}" -C ${cli_artifacts_path} . --owner=0 --group=0
}

function build::cli::generate_shasum() {
  local -r tar_path=$1
  local -r os=$2
  local -r arch=$3

  echo "Writing artifact hashes to shasum files..."

  if [ ! -d "$tar_path" ]; then
    echo "  Unable to find tar directory $tar_path"
    exit 1
  fi

  cd $tar_path/$os/$arch
  for file in $(find . -name '*.tar.gz'); do
    filepath=$(basename $file)
    sha256sum "$filepath" > "$file.sha256"
    sha512sum "$filepath" > "$file.sha512"
  done
  cd -
}

function build::ensure_tar() {
  if [[ -n "${TAR:-}" ]]; then
    return
  fi

  # Find gnu tar if it is available, bomb out if not.
  TAR=tar
  if which gtar &>/dev/null; then
      TAR=gtar
  elif which gnutar &>/dev/null; then
      TAR=gnutar
  fi
  if ! "${TAR}" --version | grep -q GNU; then
    echo "  !!! Cannot find GNU tar. Build on Linux or install GNU tar"
    echo "      on Mac OS X (brew install gnu-tar)."
    return 1
  fi
}

function build::cli::upload() {
  local -r artifactspath=$1
  local -r artifactsbucket=$2
  local -r projectpath=$3
  local -r buildidentifier=$4
  local -r githash=$5
  local -r latesttag=$6
  local -r dry_run=$7

  upload_path="$artifactsbucket"/"$projectpath"/"$buildidentifier"-"$githash"/artifacts
  latest_upload_path="$artifactsbucket"/"$projectpath"/"$latesttag"
  if [ "$CODEBUILD_CI" = "true" ]; then
    if [[ "$CODEBUILD_BUILD_ID" =~ "aws-staging-eks-a-build" ]]; then
      upload_path="$upload_path"/staging
      latest_upload_path="$artifactsbucket"/"$projectpath"/staging/"$latesttag"
    fi
  fi
  echo "$githash" >> "$artifactspath"/githash

  if [ "$dry_run" = "true" ]; then
    aws s3 cp "$artifactspath" "$upload_path" --recursive --dryrun
    aws s3 cp "$artifactspath" "$latest_upload_path" --dryrun
  else
    # Upload artifacts to s3
    # 1. To proper path on s3 with buildId-githash
    # 2. Latest path to indicate the latest build, with --delete option to delete stale files in the dest path
    aws s3 sync "$artifactspath" "$upload_path" --acl public-read
    aws s3 sync "$artifactspath" "$latest_upload_path" --delete --acl public-read
  fi
}

OS_LIST=($(echo $OS_LIST_CSV | tr "," "\n"))
ARCH_LIST=($(echo $ARCH_LIST_CSV | tr "," "\n"))

for OS in "${OS_LIST[@]}"; do
  for ARCH in "${ARCH_LIST[@]}"; do
    TAR_FILE="${REPO}-${OS}-${ARCH}.tar.gz"
    CLI_ARTIFACTS_PATH="cli-artifacts/${OS}/${ARCH}"
    mkdir -p $TAR_PATH/$OS/$ARCH
    mkdir -p $CLI_ARTIFACTS_PATH

    build::cli::move_artifacts $OS $ARCH $CLI_ARTIFACTS_PATH
    build::cli::create_tarball $OS $ARCH $TAR_FILE $TAR_PATH $CLI_ARTIFACTS_PATH
    build::cli::generate_shasum $TAR_PATH $OS $ARCH
  done
done
build::cli::upload ${TAR_PATH} ${ARTIFACTS_BUCKET} ${PROJECT_PATH} ${BUILD_IDENTIFIER} ${GIT_HASH} ${LATEST} ${DRY_RUN}


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

EKSA_K8S_VERSION="${1?First argument is EKS-A k8s version}"
CONFORMANCE_VERSION="${2?Second argument is conformance test version}"
TARGET_DIR="${3?Third argument is target directory}"

TARGET=${TARGET_DIR}/README.md

cat template/README.md.begin >${TARGET}
sed -e 's/^/   /' -e "s/kubernetesVersion: .*$/kubernetesVersion: \"${EKSA_K8S_VERSION}\"/" <template/example.yaml >>${TARGET}
cat template/README.md.end >>${TARGET}
sed -i "" -e "s/{{eksa_k8s_version}}/${EKSA_K8S_VERSION}/" -e "s/{{conformance_version}}/${CONFORMANCE_VERSION}/" ${TARGET_DIR}/README.md


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -x
set -o errexit
set -o nounset
set -o pipefail

TARGET_DIR="${1?First argument is target directory}"

RESULTS_DIR=$(mktemp -d results.XXXXXX)
RESULTS=$(./sonobuoy retrieve ${RESULTS_DIR})
tar xzf $RESULTS -C ${RESULTS_DIR}
mv ${RESULTS_DIR}/plugins/e2e/results/global/* ${TARGET_DIR}
./sonobuoy results ${RESULTS}
rm -rf ${RESULTS_DIR}


#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# To deploy to a development environment make sure you have
# ${AWS_PROFILE} set to your account
# ${AMPLIFY_APP_ID} (optional) set to your Amplify app or a stack named eks-anywhere-docs-devstack
# ${AMPLIFY_APP_BRANCH} (optional) set to Amplify branch you want to deploy to [default: main]

set -euo pipefail

# if $AWS_PROFILE is not set we should assume we're in a deployment pipeline
if [ -z "${AWS_PROFILE}" ]; then

	# set up AWS credentials file
	cat << EOF > config
[default]
output = json
region = us-west-2
role_arn=$AWS_ROLE_ARN
web_identity_token_file=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
[profile release-prod]
role_arn = arn:aws:iam::153288728732:role/DocsDeploymentRole
region = us-east-1
source_profile=default
EOF

	AWS_CONFIG_FILE=$(pwd)/config
	AWS_DEFAULT_PROFILE=release-prod
fi

# Use this function if the script is cancelled (ctrl+c) or times out
cancel_job() {
	aws amplify stop-job --app-id $APP --branch $BRANCH --job-id $JOB_ID
}

# vars for deployment
GIT_COMMIT=$(git rev-parse --short HEAD)
FILE=${1:-public.zip}
APP=${AMPLIFY_APP_ID:-$(aws cloudformation describe-stacks \
    --stack-name eks-anywhere-docs-devstack \
    --query 'Stacks[].Outputs[?OutputKey==`EksAnywhereDocsappId`].OutputValue' \
    --output text)}
BRANCH=${AMPLIFY_APP_BRANCH:-main}

# Check if we're on a tag commit
GIT_TAG=$(git tag --points-at ${GIT_COMMIT})

# If we're on a git tag we should create a branch so documentation versions work
# Branches are configured in config.toml
if [ -n "${GIT_TAG}" ]; then
	# replaces tag text like v0.2.0 with v0-2
	BRANCH="$(echo ${GIT_TAG%.*} | tr '.' '-')"
	# create an amplify app branch for the tag
	aws amplify create-branch --app-id $APP --branch-name $BRANCH --no-cli-pager
fi

# Create a deployment and save the job id and upload url
read -r JOB_ID URL < <(aws amplify create-deployment --app-id  $APP --branch $BRANCH --output text)

# cancel the amplify job if we exit or are inturrupted
trap cancel_job INT EXIT
# upload our zip
curl --silent --show-error --fail --upload-file $FILE "${URL}"

aws amplify start-deployment --app-id $APP --branch $BRANCH --job-id $JOB_ID

# get job status and wait for it to finish
STATUS=$(aws amplify get-job --app-id $APP --branch $BRANCH --job-id $JOB_ID --output text --query 'job.summary.status')
# Don't trap for exits after this point
trap - EXIT
# Total timeout will be 20+19+18...
TIMEOUT=20
while [ $TIMEOUT -gt 0 ]; do
if [ "${STATUS}" = 'SUCCEED' ]; then
	echo "Successfully deployed $BRANCH"
	exit 0
elif [ "${STATUS}" = 'FAILED' ]; then
	echo "Applify job $JOB_ID failed"
	aws amplify get-job --app-id $APP --branch $BRANCH --job-id $JOB_ID
	exit 1
else
	echo "Waiting for amplify job $JOB_ID on app $APP"
	STATUS=$(aws amplify get-job --app-id $APP --branch $BRANCH --job-id $JOB_ID --output text --query 'job.summary.status')
fi
sleep $(( TIMEOUT-- ))
done
# If we timeout waiting for the job this will run
cancel_job


* vCenter endpoint (must be accessible to EKS Anywhere clusters)
* public.ecr.aws
* anywhere-assets.eks.amazonaws.com (to download the EKS Anywhere binaries, manifests and OVAs)
* distro.eks.amazonaws.com (to download EKS Distro binaries and manifests)
* d2glxqk2uabbnd.cloudfront.net (for EKS Anywhere and EKS Distro ECR container images)
* api.ecr.us-west-2.amazonaws.com (for EKS Anywhere package authentication matching your region)
* d5l0dvt14r5h8.cloudfront.net (for EKS Anywhere package ECR container images)
* api.github.com (only if GitOps is enabled)