Bugfix: make the create-ebs-volume patch apply.
[arvados.git] / tools / compute-images / build.sh
1 #!/bin/bash
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 JSON_FILE=$1
8 ARVADOS_CLUSTER=$2
9 PROJECT_ID=$3
10 ACCOUNT_FILE=$4
11
12 read -rd "\000" helpmessage <<EOF
13 $(basename $0): Build cloud images for arvados-dispatch-cloud
14
15 Syntax:
16         $(basename $0) [options]
17
18 Options:
19
20   --json-file (required)
21       Path to the packer json file
22   --arvados-cluster-id (required)
23       The ID of the Arvados cluster, e.g. zzzzz
24   --aws-profile (default: false)
25       AWS profile to use (valid profile from ~/.aws/config
26   --aws-secrets-file (default: false, required if building for AWS)
27       AWS secrets file which will be sourced from this script
28   --aws-source-ami (default: false, required if building for AWS)
29       The AMI to use as base for building the images
30   --aws-region (default: us-east-1)
31       The AWS region to use for building the images
32   --aws-vpc-id (optional)
33       VPC id for AWS, otherwise packer will pick the default one
34   --aws-subnet-id
35       Subnet id for AWS otherwise packer will pick the default one for the VPC
36   --aws-ebs-autoscale (default: false)
37       Install the AWS EBS autoscaler daemon.
38   --gcp-project-id (default: false, required if building for GCP)
39       GCP project id
40   --gcp-account-file (default: false, required if building for GCP)
41       GCP account file
42   --gcp-zone (default: us-central1-f)
43       GCP zone
44   --azure-secrets-file (default: false, required if building for Azure)
45       Azure secrets file which will be sourced from this script
46   --azure-resource-group (default: false, required if building for Azure)
47       Azure resource group
48   --azure-location (default: false, required if building for Azure)
49       Azure location, e.g. centralus, eastus, westeurope
50   --azure-sku (default: unset, required if building for Azure, e.g. 16.04-LTS)
51       Azure SKU image to use
52   --ssh_user  (default: packer)
53       The user packer will use to log into the image
54   --resolver (default: host's network provided)
55       The dns resolver for the machine
56   --reposuffix (default: unset)
57       Set this to "-dev" to track the unstable/dev Arvados repositories
58   --public-key-file (required)
59       Path to the public key file that a-d-c will use to log into the compute node
60   --mksquashfs-mem (default: 256M)
61       Only relevant when using Singularity. This is the amount of memory mksquashfs is allowed to use.
62   --debug
63       Output debug information (default: false)
64
65 For more information, see the Arvados documentation at https://doc.arvados.org/install/crunch2-cloud/install-compute-node.html
66
67 EOF
68
69 JSON_FILE=
70 ARVADOS_CLUSTER_ID=
71 AWS_PROFILE=
72 AWS_SECRETS_FILE=
73 AWS_SOURCE_AMI=
74 AWS_VPC_ID=
75 AWS_SUBNET_ID=
76 AWS_EBS_AUTOSCALE=
77 GCP_PROJECT_ID=
78 GCP_ACCOUNT_FILE=
79 GCP_ZONE=
80 AZURE_SECRETS_FILE=
81 AZURE_RESOURCE_GROUP=
82 AZURE_LOCATION=
83 AZURE_CLOUD_ENVIRONMENT=
84 DEBUG=
85 SSH_USER=
86 AWS_DEFAULT_REGION=us-east-1
87 PUBLIC_KEY_FILE=
88 MKSQUASHFS_MEM=256M
89
90 PARSEDOPTS=$(getopt --name "$0" --longoptions \
91     help,json-file:,arvados-cluster-id:,aws-source-ami:,aws-profile:,aws-secrets-file:,aws-region:,aws-vpc-id:,aws-subnet-id:,aws-ebs-autoscale,gcp-project-id:,gcp-account-file:,gcp-zone:,azure-secrets-file:,azure-resource-group:,azure-location:,azure-sku:,azure-cloud-environment:,ssh_user:,resolver:,reposuffix:,public-key-file:,mksquashfs-mem:,debug \
92     -- "" "$@")
93 if [ $? -ne 0 ]; then
94     exit 1
95 fi
96
97 eval set -- "$PARSEDOPTS"
98 while [ $# -gt 0 ]; do
99     case "$1" in
100         --help)
101             echo >&2 "$helpmessage"
102             echo >&2
103             exit 1
104             ;;
105         --json-file)
106             JSON_FILE="$2"; shift
107             ;;
108         --arvados-cluster-id)
109             ARVADOS_CLUSTER_ID="$2"; shift
110             ;;
111         --aws-source-ami)
112             AWS_SOURCE_AMI="$2"; shift
113             ;;
114         --aws-profile)
115             AWS_PROFILE="$2"; shift
116             ;;
117         --aws-secrets-file)
118             AWS_SECRETS_FILE="$2"; shift
119             ;;
120         --aws-region)
121             AWS_DEFAULT_REGION="$2"; shift
122             ;;
123         --aws-vpc-id)
124             AWS_VPC_ID="$2"; shift
125             ;;
126         --aws-subnet-id)
127             AWS_SUBNET_ID="$2"; shift
128             ;;
129         --aws-ebs-autoscale)
130             AWS_EBS_AUTOSCALE=1
131             ;;
132         --gcp-project-id)
133             GCP_PROJECT_ID="$2"; shift
134             ;;
135         --gcp-account-file)
136             GCP_ACCOUNT_FILE="$2"; shift
137             ;;
138         --gcp-zone)
139             GCP_ZONE="$2"; shift
140             ;;
141         --azure-secrets-file)
142             AZURE_SECRETS_FILE="$2"; shift
143             ;;
144         --azure-resource-group)
145             AZURE_RESOURCE_GROUP="$2"; shift
146             ;;
147         --azure-location)
148             AZURE_LOCATION="$2"; shift
149             ;;
150         --azure-sku)
151             AZURE_SKU="$2"; shift
152             ;;
153         --azure-cloud-environment)
154             AZURE_CLOUD_ENVIRONMENT="$2"; shift
155             ;;
156         --ssh_user)
157             SSH_USER="$2"; shift
158             ;;
159         --resolver)
160             RESOLVER="$2"; shift
161             ;;
162         --reposuffix)
163             REPOSUFFIX="$2"; shift
164             ;;
165         --public-key-file)
166             PUBLIC_KEY_FILE="$2"; shift
167             ;;
168         --mksquashfs-mem)
169             MKSQUASHFS_MEM="$2"; shift
170             ;;
171         --debug)
172             # If you want to debug a build issue, add the -debug flag to the build
173             # command in question.
174             # This will allow you to ssh in, if you use the .pem file that packer
175             # generates in this directory as the ssh key. The base image uses the admin
176             # user and ssh port 22.
177             EXTRA=" -debug"
178             ;;
179         --)
180             if [ $# -gt 1 ]; then
181                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
182                 exit 1
183             fi
184             ;;
185     esac
186     shift
187 done
188
189
190 if [[ -z "$JSON_FILE" ]] || [[ ! -f "$JSON_FILE" ]]; then
191   echo >&2 "$helpmessage"
192   echo >&2
193   echo >&2 "ERROR: packer json file not found"
194   echo >&2
195   exit 1
196 fi
197
198 if [[ -z "$ARVADOS_CLUSTER_ID" ]]; then
199   echo >&2 "$helpmessage"
200   echo >&2
201   echo >&2 "ERROR: arvados cluster id not specified"
202   echo >&2
203   exit 1
204 fi
205
206 if [[ -z "$PUBLIC_KEY_FILE" ]] || [[ ! -f "$PUBLIC_KEY_FILE" ]]; then
207   echo >&2 "$helpmessage"
208   echo >&2
209   echo >&2 "ERROR: public key file file not found"
210   echo >&2
211   exit 1
212 fi
213
214 if [[ ! -z "$AWS_SECRETS_FILE" ]]; then
215   source $AWS_SECRETS_FILE
216 fi
217
218 if [[ ! -z "$AZURE_SECRETS_FILE" ]]; then
219   source $AZURE_SECRETS_FILE
220 fi
221
222
223 EXTRA2=""
224
225 if [[ -n "$AWS_SOURCE_AMI" ]]; then
226   EXTRA2+=" -var aws_source_ami=$AWS_SOURCE_AMI"
227 fi
228 if [[ -n "$AWS_PROFILE" ]]; then
229   EXTRA2+=" -var aws_profile=$AWS_PROFILE"
230 fi
231 if [[ -n "$AWS_VPC_ID" ]]; then
232   EXTRA2+=" -var vpc_id=$AWS_VPC_ID -var associate_public_ip_address=true "
233 fi
234 if [[ -n "$AWS_SUBNET_ID" ]]; then
235   EXTRA2+=" -var subnet_id=$AWS_SUBNET_ID -var associate_public_ip_address=true "
236 fi
237 if [[ -n "$AWS_DEFAULT_REGION" ]]; then
238   EXTRA2+=" -var aws_default_region=$AWS_DEFAULT_REGION"
239 fi
240 if [[ -n "$AWS_EBS_AUTOSCALE" ]]; then
241   EXTRA2+=" -var aws_ebs_autoscale=$AWS_EBS_AUTOSCALE"
242 fi
243 if [[ -n "$GCP_PROJECT_ID" ]]; then
244   EXTRA2+=" -var project_id=$GCP_PROJECT_ID"
245 fi
246 if [[ -n "$GCP_ACCOUNT_FILE" ]]; then
247   EXTRA2+=" -var account_file=$GCP_ACCOUNT_FILE"
248 fi
249 if [[ -n "$GCP_ZONE" ]]; then
250   EXTRA2+=" -var zone=$GCP_ZONE"
251 fi
252 if [[ -n "$AZURE_RESOURCE_GROUP" ]]; then
253   EXTRA2+=" -var resource_group=$AZURE_RESOURCE_GROUP"
254 fi
255 if [[ -n "$AZURE_LOCATION" ]]; then
256   EXTRA2+=" -var location=$AZURE_LOCATION"
257 fi
258 if [[ -n "$AZURE_SKU" ]]; then
259   EXTRA2+=" -var image_sku=$AZURE_SKU"
260 fi
261 if [[ -n "$AZURE_CLOUD_ENVIRONMENT" ]]; then
262   EXTRA2+=" -var cloud_environment_name=$AZURE_CLOUD_ENVIRONMENT"
263 fi
264 if [[ -n "$SSH_USER" ]]; then
265   EXTRA2+=" -var ssh_user=$SSH_USER"
266 fi
267 if [[ -n "$RESOLVER" ]]; then
268   EXTRA2+=" -var resolver=$RESOLVER"
269 fi
270 if [[ -n "$REPOSUFFIX" ]]; then
271   EXTRA2+=" -var reposuffix=$REPOSUFFIX"
272 fi
273 if [[ -n "$PUBLIC_KEY_FILE" ]]; then
274   EXTRA2+=" -var public_key_file=$PUBLIC_KEY_FILE"
275 fi
276 if [[ -n "$MKSQUASHFS_MEM" ]]; then
277   EXTRA2+=" -var mksquashfs_mem=$MKSQUASHFS_MEM"
278 fi
279
280 echo
281 packer version
282 echo
283 echo packer build$EXTRA -var "arvados_cluster=$ARVADOS_CLUSTER_ID"$EXTRA2 $JSON_FILE
284 packer build$EXTRA -var "arvados_cluster=$ARVADOS_CLUSTER_ID"$EXTRA2 $JSON_FILE