Merge branch '18289-only-pass-allow-other-when-running-docker'
[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   --gcp-project-id (default: false, required if building for GCP)
37       GCP project id
38   --gcp-account-file (default: false, required if building for GCP)
39       GCP account file
40   --gcp-zone (default: us-central1-f)
41       GCP zone
42   --azure-secrets-file (default: false, required if building for Azure)
43       Azure secrets file which will be sourced from this script
44   --azure-resource-group (default: false, required if building for Azure)
45       Azure resource group
46   --azure-location (default: false, required if building for Azure)
47       Azure location, e.g. centralus, eastus, westeurope
48   --azure-sku (default: unset, required if building for Azure, e.g. 16.04-LTS)
49       Azure SKU image to use
50   --ssh_user  (default: packer)
51       The user packer will use to log into the image
52   --resolver (default: host's network provided)
53       The dns resolver for the machine
54   --reposuffix (default: unset)
55       Set this to "-dev" to track the unstable/dev Arvados repositories
56   --public-key-file (required)
57       Path to the public key file that a-d-c will use to log into the compute node
58   --mksquashfs-mem (default: 512M)
59       Only relevant when using Singularity. This is the amount of memory mksquashfs is allowed to use.
60   --debug
61       Output debug information (default: false)
62
63 EOF
64
65 JSON_FILE=
66 ARVADOS_CLUSTER_ID=
67 AWS_PROFILE=
68 AWS_SECRETS_FILE=
69 AWS_SOURCE_AMI=
70 AWS_VPC_ID=
71 AWS_SUBNET_ID=
72 GCP_PROJECT_ID=
73 GCP_ACCOUNT_FILE=
74 GCP_ZONE=
75 AZURE_SECRETS_FILE=
76 AZURE_RESOURCE_GROUP=
77 AZURE_LOCATION=
78 AZURE_CLOUD_ENVIRONMENT=
79 DEBUG=
80 SSH_USER=
81 AWS_DEFAULT_REGION=us-east-1
82 PUBLIC_KEY_FILE=
83 MKSQUASHFS_MEM=512M
84
85 PARSEDOPTS=$(getopt --name "$0" --longoptions \
86     help,json-file:,arvados-cluster-id:,aws-source-ami:,aws-profile:,aws-secrets-file:,aws-region:,aws-vpc-id:,aws-subnet-id:,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 \
87     -- "" "$@")
88 if [ $? -ne 0 ]; then
89     exit 1
90 fi
91
92 eval set -- "$PARSEDOPTS"
93 while [ $# -gt 0 ]; do
94     case "$1" in
95         --help)
96             echo >&2 "$helpmessage"
97             echo >&2
98             exit 1
99             ;;
100         --json-file)
101             JSON_FILE="$2"; shift
102             ;;
103         --arvados-cluster-id)
104             ARVADOS_CLUSTER_ID="$2"; shift
105             ;;
106         --aws-source-ami)
107             AWS_SOURCE_AMI="$2"; shift
108             ;;
109         --aws-profile)
110             AWS_PROFILE="$2"; shift
111             ;;
112         --aws-secrets-file)
113             AWS_SECRETS_FILE="$2"; shift
114             ;;
115         --aws-region)
116             AWS_DEFAULT_REGION="$2"; shift
117             ;;
118         --aws-vpc-id)
119             AWS_VPC_ID="$2"; shift
120             ;;
121         --aws-subnet-id)
122             AWS_SUBNET_ID="$2"; shift
123             ;;
124         --gcp-project-id)
125             GCP_PROJECT_ID="$2"; shift
126             ;;
127         --gcp-account-file)
128             GCP_ACCOUNT_FILE="$2"; shift
129             ;;
130         --gcp-zone)
131             GCP_ZONE="$2"; shift
132             ;;
133         --azure-secrets-file)
134             AZURE_SECRETS_FILE="$2"; shift
135             ;;
136         --azure-resource-group)
137             AZURE_RESOURCE_GROUP="$2"; shift
138             ;;
139         --azure-location)
140             AZURE_LOCATION="$2"; shift
141             ;;
142         --azure-sku)
143             AZURE_SKU="$2"; shift
144             ;;
145         --azure-cloud-environment)
146             AZURE_CLOUD_ENVIRONMENT="$2"; shift
147             ;;
148         --ssh_user)
149             SSH_USER="$2"; shift
150             ;;
151         --resolver)
152             RESOLVER="$2"; shift
153             ;;
154         --reposuffix)
155             REPOSUFFIX="$2"; shift
156             ;;
157         --public-key-file)
158             PUBLIC_KEY_FILE="$2"; shift
159             ;;
160         --mksquashfs-mem)
161             MKSQUASHFS_MEM="$2"; shift
162             ;;
163         --debug)
164             # If you want to debug a build issue, add the -debug flag to the build
165             # command in question.
166             # This will allow you to ssh in, if you use the .pem file that packer
167             # generates in this directory as the ssh key. The base image uses the admin
168             # user and ssh port 22.
169             EXTRA=" -debug"
170             ;;
171         --)
172             if [ $# -gt 1 ]; then
173                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
174                 exit 1
175             fi
176             ;;
177     esac
178     shift
179 done
180
181
182 if [[ "$JSON_FILE" == "" ]] || [[ ! -f "$JSON_FILE" ]]; then
183   echo >&2 "$helpmessage"
184   echo >&2
185   echo >&2 "ERROR: packer json file not found"
186   echo >&2
187   exit 1
188 fi
189
190 if [[ -z "$ARVADOS_CLUSTER_ID" ]]; then
191   echo >&2 "$helpmessage"
192   echo >&2
193   echo >&2 "ERROR: arvados cluster id not specified"
194   echo >&2
195   exit 1
196 fi
197
198 if [[ "$PUBLIC_KEY_FILE" == "" ]] || [[ ! -f "$PUBLIC_KEY_FILE" ]]; then
199   echo >&2 "$helpmessage"
200   echo >&2
201   echo >&2 "ERROR: public key file file not found"
202   echo >&2
203   exit 1
204 fi
205
206 if [[ ! -z "$AWS_SECRETS_FILE" ]]; then
207   source $AWS_SECRETS_FILE
208 fi
209
210 if [[ ! -z "$AZURE_SECRETS_FILE" ]]; then
211   source $AZURE_SECRETS_FILE
212 fi
213
214
215 EXTRA2=""
216
217 if [[ "$AWS_SOURCE_AMI" != "" ]]; then
218   EXTRA2+=" -var aws_source_ami=$AWS_SOURCE_AMI"
219 fi
220 if [[ "$AWS_PROFILE" != "" ]]; then
221   EXTRA2+=" -var aws_profile=$AWS_PROFILE"
222 fi
223 if [[ "$AWS_VPC_ID" != "" ]]; then
224   EXTRA2+=" -var vpc_id=$AWS_VPC_ID -var associate_public_ip_address=true "
225 fi
226 if [[ "$AWS_SUBNET_ID" != "" ]]; then
227   EXTRA2+=" -var subnet_id=$AWS_SUBNET_ID -var associate_public_ip_address=true "
228 fi
229 if [[ "$AWS_DEFAULT_REGION" != "" ]]; then
230   EXTRA2+=" -var aws_default_region=$AWS_DEFAULT_REGION"
231 fi
232 if [[ "$GCP_PROJECT_ID" != "" ]]; then
233   EXTRA2+=" -var project_id=$GCP_PROJECT_ID"
234 fi
235 if [[ "$GCP_ACCOUNT_FILE" != "" ]]; then
236   EXTRA2+=" -var account_file=$GCP_ACCOUNT_FILE"
237 fi
238 if [[ "$GCP_ZONE" != "" ]]; then
239   EXTRA2+=" -var zone=$GCP_ZONE"
240 fi
241 if [[ "$AZURE_RESOURCE_GROUP" != "" ]]; then
242   EXTRA2+=" -var resource_group=$AZURE_RESOURCE_GROUP"
243 fi
244 if [[ "$AZURE_LOCATION" != "" ]]; then
245   EXTRA2+=" -var location=$AZURE_LOCATION"
246 fi
247 if [[ "$AZURE_SKU" != "" ]]; then
248   EXTRA2+=" -var image_sku=$AZURE_SKU"
249 fi
250 if [[ "$AZURE_CLOUD_ENVIRONMENT" != "" ]]; then
251   EXTRA2+=" -var cloud_environment_name=$AZURE_CLOUD_ENVIRONMENT"
252 fi
253 if [[ "$SSH_USER" != "" ]]; then
254   EXTRA2+=" -var ssh_user=$SSH_USER"
255 fi
256 if [[ "$RESOLVER" != "" ]]; then
257   EXTRA2+=" -var resolver=$RESOLVER"
258 fi
259 if [[ "$REPOSUFFIX" != "" ]]; then
260   EXTRA2+=" -var reposuffix=$REPOSUFFIX"
261 fi
262 if [[ "$PUBLIC_KEY_FILE" != "" ]]; then
263   EXTRA2+=" -var public_key_file=$PUBLIC_KEY_FILE"
264 fi
265 if [[ "$MKSQUASHFS_MEM" != "" ]]; then
266   EXTRA2+=" -var mksquashfs_mem=$MKSQUASHFS_MEM"
267 fi
268
269
270 echo
271 packer version
272 echo
273 echo packer build$EXTRA -var "arvados_cluster=$ARVADOS_CLUSTER_ID"$EXTRA2 $JSON_FILE
274 packer build$EXTRA -var "arvados_cluster=$ARVADOS_CLUSTER_ID"$EXTRA2 $JSON_FILE