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