1#!/bin/bash
2#
3# Copyright 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17if [ "$#" -lt 1 ]; then
18  echo "usage: deploy-webapp.sh prod|test|public|local [deploy options]"
19  exit 1
20fi
21
22NPM_PATH=$(which npm)
23NG_PATH=$(which ng)
24if [ ! -f "${NPM_PATH}" ]; then
25  echo "Cannot find npm in your PATH."
26  echo "Please install node.js and npm to deploy frontend."
27  exit 0
28fi
29if [ ! -f "${NG_PATH}" ]; then
30  echo "Cannot find Angular CLI in your PATH."
31  echo "Please install Angular CLI to deploy frontend."
32  exit 0
33fi
34
35pushd frontend
36echo "Installing frontend dependencies..."
37npm install
38
39echo "Removing files in dist directory..."
40rm -r dist/*
41
42echo "Building frontend codes..."
43if [ $1 = "local" ]; then
44  ng build
45else
46  ng build --prod
47fi
48popd
49
50echo "Copying frontend files to webapp/static directory..."
51rm -rf webapp/static/
52mkdir webapp/static
53cp -r frontend/dist/* webapp/static/
54
55if [ $1 = "public" ]; then
56  SERVICE="vtslab-schedule"
57elif [ $1 = "local" ]; then
58  dev_appserver.py ./
59  exit 0
60else
61  SERVICE="vtslab-schedule-$1"
62fi
63
64echo "Fetching endpoints service version of $SERVICE ..."
65ENDPOINTS=$(gcloud endpoints configs list --service=$SERVICE.appspot.com)
66arr=($ENDPOINTS)
67
68if [ ${#arr[@]} -lt 4 ]; then
69  echo "You need to deploy endpoints first."
70  exit 0
71else
72  VERSION=${arr[2]}
73  NAME=${arr[3]}
74  echo "ENDPOINTS_SERVICE_NAME: $NAME"
75  echo "ENDPOINTS_SERVICE_VERSION: $VERSION"
76fi
77
78echo "Updating app.yaml ..."
79if [ "$(uname)" == "Darwin" ]; then
80  sed -i "" "s/ENDPOINTS_SERVICE_NAME:.*/ENDPOINTS_SERVICE_NAME: $NAME/g" app.yaml
81  sed -i "" "s/ENDPOINTS_SERVICE_VERSION:.*/ENDPOINTS_SERVICE_VERSION: $VERSION/g" app.yaml
82else
83  sed -i "s/ENDPOINTS_SERVICE_NAME:.*/ENDPOINTS_SERVICE_NAME: $NAME/g" app.yaml
84  sed -i "s/ENDPOINTS_SERVICE_VERSION:.*/ENDPOINTS_SERVICE_VERSION: $VERSION/g" app.yaml
85fi
86
87echo "Deploying the web app to $SERVICE ..."
88
89gcloud app deploy app.yaml cron.yaml index.yaml queue.yaml worker.yaml --project=$SERVICE ${@:2}
90
91echo "Deployment done!"
92