|
| 1 | +#!/bin/bash |
| 2 | +launcherstackname=${12} |
| 3 | +Region=$2 |
| 4 | +trap '/opt/aws/bin/cfn-signal --exit-code 1 --resource EC2Instance --region ${Region} --stack ${launcherstackname}' ERR |
| 5 | +function wait_stack_create() { |
| 6 | + CLUSTER_STACK_NAME=$1 |
| 7 | + LAUNCHER_STACK_NAME=$2 |
| 8 | + REGION=$3 |
| 9 | + echo "Waiting for [$CLUSTER_STACK_NAME] stack creation." |
| 10 | + aws cloudformation wait stack-create-complete --region ${REGION} --stack-name ${CLUSTER_STACK_NAME} |
| 11 | + status=$? |
| 12 | + if [[ ${status} -ne 0 ]] ; then |
| 13 | + # Waiter encountered a failure state. |
| 14 | + echo "Stack [${CLUSTER_STACK_NAME}] creation failed. AWS error code is ${status}." |
| 15 | + /opt/aws/bin/cfn-signal --exit-code 1 --resource EC2Instance --region ${REGION} --stack ${LAUNCHER_STACK_NAME} |
| 16 | + exit 1 |
| 17 | + fi |
| 18 | +} |
| 19 | +source ~/apc-ve/bin/activate |
| 20 | +echo "Activated Virtual Environment" |
| 21 | +echo "Retrieving Tags from Running Instance" |
| 22 | +INSTANCE_ID=`wget -qO- http://instance-data/latest/meta-data/instance-id` |
| 23 | +IFS='-' read -ra TRIMMED <<< "$INSTANCE_ID" |
| 24 | +CLUSTER_NAME=RG-Pcluster-${TRIMMED[1]} |
| 25 | +REGION=`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed 's/.$//'` |
| 26 | +aws ec2 describe-tags --region $REGION --filter "Name=resource-id,Values=$INSTANCE_ID" --query 'Tags[*].{Key:Key,Value:Value}' | jq -r '.[] | select( .Key as $a | ["cost_resource", "project_name","researcher_name"] | index($a) )' >> out.json |
| 27 | +jq -s '.' out.json >> valid.json |
| 28 | +echo "valid.json file is updated with Tags" |
| 29 | +yq eval -P valid.json > valid.yaml |
| 30 | +echo "Json file is converted to yaml" |
| 31 | +sed -i '1 i\Tags:' valid.yaml |
| 32 | +scheduler=$1 |
| 33 | +Region=$2 |
| 34 | +headnodeinstancetype=$3 |
| 35 | +headnodesubnetId=$4 |
| 36 | +keyname=$5 |
| 37 | +computenodeinstancetype=$6 |
| 38 | +minvpc=$7 |
| 39 | +maxvpc=$8 |
| 40 | +computenodesubnetId=$9 |
| 41 | +desiredvpc=${10} |
| 42 | +spotbid=${11} |
| 43 | +CustomAMI=${13} |
| 44 | +FileSystemId=${14} |
| 45 | +QueueCapacityType=${15} |
| 46 | +disableSimultaneousMultithreading=${16} |
| 47 | +efa=${17} |
| 48 | +placementGroup=${18} |
| 49 | + |
| 50 | +IFS='-' read -ra TRIMMED <<< "$CustomAMI" |
| 51 | +CustomAMIStartsWith=${TRIMMED[0]} |
| 52 | +if [ "$scheduler" == "slurm" ]; then |
| 53 | + echo "slurm.yaml exists" |
| 54 | + if [ "$CustomAMIStartsWith" == "ami" ]; then |
| 55 | + yq -i ".Image.CustomAmi=\"$CustomAMI\"" slurm.yaml |
| 56 | + fi |
| 57 | + if [ "$FileSystemId" != "default" ]; then |
| 58 | + yq -i '.SharedStorage=[{"MountDir": "/fsx", "Name":"RG_Filesysytem", "StorageType": "FsxLustre", "FsxLustreSettings":{"FileSystemId":"'$FileSystemId'"}}]' slurm.yaml |
| 59 | + fi |
| 60 | + yq -i ".Region=\"$Region\"" slurm.yaml |
| 61 | + yq -i ".HeadNode.InstanceType=\"$headnodeinstancetype\"" slurm.yaml |
| 62 | + yq -i ".HeadNode.Networking.SubnetId=\"$headnodesubnetId\"" slurm.yaml |
| 63 | + yq -i ".HeadNode.Ssh.KeyName=\"$keyname\"" slurm.yaml |
| 64 | + yq -i ".Scheduling.SlurmQueues[0].CapacityType=\"$QueueCapacityType\"" slurm.yaml |
| 65 | + yq -i ".Scheduling.SlurmQueues[0].ComputeResources[0].InstanceType=\"$computenodeinstancetype\"" slurm.yaml |
| 66 | + yq -i ".Scheduling.SlurmQueues[0].ComputeResources[0].MinCount=\"$minvpc\"" slurm.yaml |
| 67 | + yq -i ".Scheduling.SlurmQueues[0].ComputeResources[0].MaxCount=\"$maxvpc\"" slurm.yaml |
| 68 | + yq -i ".Scheduling.SlurmQueues[0].ComputeResources[0].DisableSimultaneousMultithreading=\"$disableSimultaneousMultithreading\"" slurm.yaml |
| 69 | + yq -i ".Scheduling.SlurmQueues[0].ComputeResources[0].Efa.Enabled=\"$efa\"" slurm.yaml |
| 70 | + yq -i ".Scheduling.SlurmQueues[0].Networking.PlacementGroup.Enabled=\"$placementGroup\"" slurm.yaml |
| 71 | + yq -i ".Scheduling.SlurmQueues[0].Networking.SubnetIds[0]=\"$computenodesubnetId\"" slurm.yaml |
| 72 | + sed -i 's/\"//g' slurm.yaml |
| 73 | + yq eval-all "select(fileIndex == 1) *+ select(fileIndex == 0)" valid.yaml slurm.yaml >> cluster-config-slurm.yaml |
| 74 | + echo "valid.yaml file and cluster-config.yaml file is merged into cluster-config-slurm.yaml" |
| 75 | + echo "Modified cluster-config-slurm.yaml with Tags" |
| 76 | + pcluster create-cluster --cluster-name $CLUSTER_NAME --cluster-configuration cluster-config-slurm.yaml |
| 77 | +else |
| 78 | + echo "batch.yaml exists" |
| 79 | + if [ "$CustomAMIStartsWith" == "ami" ]; then |
| 80 | + yq -i ".Image.CustomAmi=\"$CustomAMI\"" batch.yaml |
| 81 | + fi |
| 82 | + yq -i ".Region=\"$Region\"" batch.yaml |
| 83 | + yq -i ".HeadNode.InstanceType=\"$headnodeinstancetype\"" batch.yaml |
| 84 | + yq -i ".HeadNode.Networking.SubnetId=\"$headnodesubnetId\"" batch.yaml |
| 85 | + yq -i ".HeadNode.Ssh.KeyName=\"$keyname\"" batch.yaml |
| 86 | + yq -i ".Scheduling.AwsBatchQueues[0].CapacityType=\"$QueueCapacityType\"" batch.yaml |
| 87 | + yq -i ".Scheduling.AwsBatchQueues[0].ComputeResources[0].InstanceTypes[0]=\"$computenodeinstancetype\"" batch.yaml |
| 88 | + yq -i ".Scheduling.AwsBatchQueues[0].ComputeResources[0].MinvCpus=\"$minvpc\"" batch.yaml |
| 89 | + yq -i ".Scheduling.AwsBatchQueues[0].ComputeResources[0].MaxvCpus=\"$maxvpc\"" batch.yaml |
| 90 | + yq -i ".Scheduling.AwsBatchQueues[0].ComputeResources[0].DesiredvCpus=\"$desiredvpc\"" batch.yaml |
| 91 | + yq -i ".Scheduling.AwsBatchQueues[0].ComputeResources[0].SpotBidPercentage=\"$spotbid\"" batch.yaml |
| 92 | + yq -i ".Scheduling.AwsBatchQueues[0].Networking.SubnetIds[0]=\"$computenodesubnetId\"" batch.yaml |
| 93 | + sed -i 's/\"//g' batch.yaml |
| 94 | + yq eval-all "select(fileIndex == 1) *+ select(fileIndex == 0)" valid.yaml batch.yaml >> cluster-config-batch.yaml |
| 95 | + echo "valid.yaml file and cluster-config.yaml file is merged into cluster-config-batch.yaml" |
| 96 | + echo "Modified cluster-config-batch.yaml with Tags" |
| 97 | + pcluster create-cluster --cluster-name $CLUSTER_NAME --cluster-configuration cluster-config-batch.yaml |
| 98 | + |
| 99 | +fi |
| 100 | + |
| 101 | +wait_stack_create $CLUSTER_NAME $launcherstackname $REGION |
| 102 | +HEAD_INSTANCE_ID=`pcluster describe-cluster -n $CLUSTER_NAME -r $REGION --query headNode | jq -r '.instanceId'` |
| 103 | +# PRIVATE_IP_ADDRESS=`pcluster describe-cluster -n $1 --query headNode.privateIpAddress` |
| 104 | +PARAMETER_NAME="/rg/pcluster/headnode-instance-id/${CLUSTER_NAME}" |
| 105 | +aws ssm put-parameter --name "${PARAMETER_NAME}" --type "String" --value "${HEAD_INSTANCE_ID}" --region $REGION --overwrite |
| 106 | +echo "Instance id of the head node is stored on ${PARAMETER_NAME}" |
| 107 | +echo "Instance id is : ${HEAD_INSTANCE_ID}" |
0 commit comments