-
Notifications
You must be signed in to change notification settings - Fork 103
/
kitchen.ec2.sh
executable file
·148 lines (126 loc) · 5.8 KB
/
kitchen.ec2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Variables you can set in your local .kitchen.env.sh (not to be pushed)
#
# KITCHEN_AWS_REGION: AWS region;
# falls back to AWS_DEFAULT_REGION, then to eu-west-1
#
# KITCHEN_ARCHITECTURE: [x86_64|arm64];
# default x86_64
#
# KITCHEN_INSTANCE_TYPE: instance type to use
# default t2.micro
#
# KITCHEN_KEY_NAME: KeyPair to use with EC2 instances
# default kitchen
#
# KITCHEN_SUBNET_ID: subnet to put EC2 instance into
# if not set will use Subnet tagged with Kitchen=true
#
# KITCHEN_SECURITY_GROUP_ID: security group to associate to the instance
# if not set will use SG tagged with Kitchen=true
#
# KITCHEN_IAM_PROFILE: IAM instance profile name
# if not set no profile will be attached
#
# KITCHEN_USER_DATA_SCRIPT: user-data script to launch on the instance
# if not set no user-data script will be launched
#
# KITCHEN_SSH_KEY_PATH: path to private key
# default ~/.ssh/${key-name}-${region}.pem
#
# KITCHEN_PCLUSTER_VERSION: ParallelCluster version to use
# defaults to the latest one
#
# KITCHEN_ALINUX2_AMI: specific AMI to use for alinux2
# if not specified, will look for the latest suitable ParallelCluster AMI
#
# KITCHEN_RHEL8_AMI: specific AMI to use for redhat8
# if not specified, will look for the latest suitable ParallelCluster AMI
#
# KITCHEN_CENTOS7_AMI: specific AMI to use for centos7
# if not specified, will look for the latest suitable ParallelCluster AMI
#
# KITCHEN_UBUNTU2004_AMI: specific AMI to use for ubuntu20.04
# if not specified, will look for the latest suitable ParallelCluster AMI
#
# KITCHEN_UBUNTU2204_AMI: specific AMI to use for ubuntu22.04
# if not specified, will look for the latest suitable ParallelCluster AMI
#
source kitchen/kitchen.local-yml.sh
THIS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
export KITCHEN_YAML="${THIS_DIR}/kitchen.ec2.yml"
export KITCHEN_GLOBAL_YAML="${THIS_DIR}/kitchen.global.yml"
export KITCHEN_DRIVER=ec2
if [ -e "${THIS_DIR}/.kitchen.env.sh" ]
then
echo "*** Apply local environment"
source "${THIS_DIR}/.kitchen.env.sh"
fi
: "${KITCHEN_AWS_REGION:=${AWS_DEFAULT_REGION:-eu-west-1}}"
: "${KITCHEN_KEY_NAME:=kitchen}"
: "${KITCHEN_SSH_KEY_PATH:="~/.ssh/${KITCHEN_KEY_NAME}-${KITCHEN_AWS_REGION}.pem"}"
: "${KITCHEN_AVAILABILITY_ZONE:=a}"
: "${KITCHEN_ARCHITECTURE:=x86_64}"
if [ "$1" == "create" ] || [ "$1" == "converge" ] || [ "$1" == "verify" ] || [ "$1" == "destroy" ] || [ "$1" == "test" ]; then
# Subnet
if [ -z "${KITCHEN_SUBNET_ID}" ]; then
echo "** KITCHEN_SUBNET_ID not explicitly set: looking for subnet tagged Kitchen=true"
KITCHEN_SUBNET_ID=$(aws ec2 describe-subnets --region "${KITCHEN_AWS_REGION}" \
--filters "Name=tag:Kitchen,Values=true" \
"Name=availability-zone,Values=${KITCHEN_AWS_REGION}${KITCHEN_AVAILABILITY_ZONE}" \
--query 'Subnets[0].SubnetId' --output text)
echo "** KITCHEN_SUBNET_ID: ${KITCHEN_SUBNET_ID}"
if [ "${KITCHEN_SUBNET_ID}" = "None" ]; then
echo "Subnet tagged Kitchen=true not found in AZ ${KITCHEN_AWS_REGION}${KITCHEN_AVAILABILITY_ZONE}"
exit 1
fi
fi
# VPC
if [ -z "${KITCHEN_VPC_ID}" ]; then
echo "** KITCHEN_VPC_ID not explicitly set: deriving from subnet"
KITCHEN_VPC_ID=$(aws ec2 describe-subnets --region "${KITCHEN_AWS_REGION}" \
--subnet-ids "${KITCHEN_SUBNET_ID}" \
--query 'Subnets[0].VpcId' --output text)
echo "** KITCHEN_VPC_ID: ${KITCHEN_VPC_ID}"
fi
# Security Group
if [ -z "${KITCHEN_SECURITY_GROUP_ID}" ]; then
echo "** KITCHEN_SECURITY_GROUP_ID not explicitly set"
if [ -z "${KITCHEN_SECURITY_GROUP_NAME}" ]; then
echo "** KITCHEN_SECURITY_GROUP_NAME not explicitly set, looking for tag Kitchen=true"
KITCHEN_SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --region "${KITCHEN_AWS_REGION}" \
--filters "Name=tag:Kitchen,Values=true" "Name=vpc-id,Values=${KITCHEN_VPC_ID}" \
--query 'SecurityGroups[0].GroupId' --output text)
else
echo "** Looking for SG named ${KITCHEN_SECURITY_GROUP_NAME}"
KITCHEN_SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --region "${KITCHEN_AWS_REGION}" \
--filters "Name=vpc-id,Values=${KITCHEN_VPC_ID}" "Name=group-name,Values=${KITCHEN_SECURITY_GROUP_NAME}" \
--query 'SecurityGroups[0].GroupId' --output text)
fi
echo "** KITCHEN_SECURITY_GROUP_ID: ${KITCHEN_SECURITY_GROUP_ID}"
fi
fi
export KITCHEN_AWS_REGION
export KITCHEN_KEY_NAME
export KITCHEN_SSH_KEY_PATH
export KITCHEN_AVAILABILITY_ZONE
export KITCHEN_ARCHITECTURE
export KITCHEN_INSTANCE_TYPE
export KITCHEN_SUBNET_ID
export KITCHEN_VPC_ID
export KITCHEN_SECURITY_GROUP_ID
echo "** KITCHEN_AWS_REGION: ${KITCHEN_AWS_REGION}"
echo "** KITCHEN_KEY_NAME: ${KITCHEN_KEY_NAME}"
echo "** KITCHEN_SSH_KEY_PATH: ${KITCHEN_SSH_KEY_PATH}"
echo "** KITCHEN_AVAILABILITY_ZONE: ${KITCHEN_AVAILABILITY_ZONE}"
echo "** KITCHEN_ARCHITECTURE: ${KITCHEN_ARCHITECTURE}"
echo "** KITCHEN_INSTANCE_TYPE: ${KITCHEN_INSTANCE_TYPE}"
echo "** KITCHEN_SUBNET_ID: ${KITCHEN_SUBNET_ID}"
echo "** KITCHEN_VPC_ID: ${KITCHEN_VPC_ID}"
echo "** KITCHEN_SECURITY_GROUP_ID: ${KITCHEN_SECURITY_GROUP_ID}"
echo "** KITCHEN_IAM_PROFILE: ${KITCHEN_IAM_PROFILE}"
echo "** KITCHEN_LOCAL_YAML: ${KITCHEN_LOCAL_YAML}"
echo "** KITCHEN_YAML: $KITCHEN_YAML"
echo "** KITCHEN_GLOBAL_YAML: $KITCHEN_GLOBAL_YAML"
echo "kitchen $*"
kitchen "$@"