This repository has been archived by the owner on Nov 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.sh
executable file
·48 lines (41 loc) · 2.93 KB
/
build.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
#!/bin/bash
function usage() {
echo -e "
Usage: $0 [ec2ami|qemu-kvm]
Env Vars:
BUILD_REF - Reference to use when tagging/naming build artifacts - Default: Current git tag/branch/commit
ec2ami:
AMI_REGIONS - Comma-separated list of AWS regions to which the output AMI will be copied. Example: 'us-west-1,us-east-2' - Default: none
AWS_ACCESS_KEY_ID - AWS access key - Required
AWS_REGION - AWS region - Required
AWS_SECRET_ACCESS_KEY - AWS secret key
AWS_SECURITY_GROUP_ID - VPC Security Group ID (must be in configured region and VPC)
AWS_SUBNET_ID - VPC Subnet ID (must be in configured region and VPC)
AWS_VPC_ID - VPC ID (must be in configured region)
SOURCE_AMI - An existing Debian 9 EC2 AMI (must be in configured region) - Default: auto-discover latest official Debian 9 AMI.
"
exit 1
}
which packer > /dev/null || { echo "ERROR: 'packer' executable not found. Download and install packer before continuing."; exit 1; }
[[ -z ${BUILD_REF+x} ]] || [[ ${BUILD_REF} == "" ]] && export BUILD_REF=$(git describe --tags)
if [[ $1 == "ec2ami" ]] ; then
[[ -z ${AWS_ACCESS_KEY_ID+x} ]] || [[ ${AWS_ACCESS_KEY_ID} == "" ]] && { echo "ERROR: 'AWS_ACCESS_KEY_ID' environment variable not defined"; usage; }
[[ -z ${AWS_REGION+x} ]] || [[ ${AWS_REGION} == "" ]] && { echo "ERROR: 'AWS_REGION' environment variable not defined"; usage; }
[[ -z ${AWS_SECRET_ACCESS_KEY+x} ]] || [[ ${AWS_SECRET_ACCESS_KEY} == "" ]] && { echo "ERROR: 'AWS_SECRET_ACCESS_KEY' environment variable not defined"; usage; }
[[ -z ${AWS_SECURITY_GROUP_ID+x} ]] || [[ ${AWS_SECURITY_GROUP_ID} == "" ]] && { echo "ERROR: 'AWS_SECURITY_GROUP_ID' environment variable not defined"; usage; }
[[ -z ${AWS_SUBNET_ID+x} ]] || [[ ${AWS_SUBNET_ID} == "" ]] && { echo "ERROR: 'AWS_SUBNET_ID' environment variable not defined"; usage; }
[[ -z ${AWS_VPC_ID+x} ]] || [[ ${AWS_VPC_ID} == "" ]] && { echo "ERROR: 'AWS_VPC_ID' environment variable not defined"; usage; }
if [[ -z ${SOURCE_AMI+x} ]] || [[ ${SOURCE_AMI} == "" ]] ; then
which jq > /dev/null || { echo "ERROR: 'jq' executable not found. Set SOURCE_AMI or install 'jq' to allow AMI auto-discovery"; exit 1; }
which aws > /dev/null || { echo "ERROR: 'aws' executable not found. Set SOURCE_AMI or install 'awscli' to AMI auto-discovery"; exit 1; }
export SOURCE_AMI=$(aws --region $AWS_REGION ec2 describe-images --filters "Name=owner-id,Values=379101102735" "Name=root-device-type,Values=ebs" "Name=architecture,Values=x86_64" "Name=state,Values=available" | jq --raw-output '.Images | sort_by(.Name) | .[] | select(.Name | test("^debian-stretch-hvm-x86_64-gp2.*")) | .ImageId' | tail -n 1)
fi
aws --region $AWS_REGION ec2 describe-images --image-ids $SOURCE_AMI > /dev/null || { echo "ERROR: SOURCE_AMI is not available"; exit 1; }
shift 1
packer build $@ ./packer-debian9-ec2ami.json
elif [[ $1 == "qemu-kvm" ]] ; then
shift 1
packer build $@ ./packer-debian9-qemu-kvm.json
else
usage
fi