1#!/bin/bash
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -e
17
18function echo_and_do {
19  echo $@
20  $@
21}
22
23PROJECT_ROOT="$(cd -P ${BASH_SOURCE[0]%/*}/..; pwd)"
24OUT_DIR="$PROJECT_ROOT/out/ui-deploy.tmp"
25UI_DIST_DIR="$OUT_DIR/ui-dist"
26
27CLEAN_OUT_DIR=true
28DEPLOY_PROD=false
29DEPLOY_STAGING=false
30DEBUG_BUILD=true
31
32while [[ $# -gt 0 ]]; do
33  key="$1"
34  case $key in
35      --no-clean)
36      CLEAN_OUT_DIR=false
37      shift
38      ;;
39      --prod)
40      DEPLOY_PROD=true
41      DEBUG_BUILD=false
42      shift
43      ;;
44      --staging)
45      DEPLOY_STAGING=true
46      shift
47      ;;
48      --release)
49      DEBUG_BUILD=false
50      shift
51      ;;
52      -h|--help)
53      echo "Usage: $0 [--no-clean] [--prod] [--staging]"
54      echo "    --no-clean  Don't remove $OUT_DIR"
55      echo "    --prod      Deploy prod version (implies --release)"
56      echo "    --staging   Deploy staging version"
57      echo "    --release   Do a release build"
58      echo "    -h|--help   Display this message"
59      exit 0
60      shift
61      ;;
62  esac
63done
64
65if [ "$CLEAN_OUT_DIR" = true ]; then
66  echo_and_do rm -rf "$OUT_DIR"
67fi
68echo_and_do mkdir -p "$UI_DIST_DIR"
69
70if [ "$DEBUG_BUILD" = true ]; then
71  echo_and_do "$PROJECT_ROOT/tools/gn" gen "$OUT_DIR" --args="is_debug=true"
72else
73  echo_and_do "$PROJECT_ROOT/tools/gn" gen "$OUT_DIR" --args="is_debug=false"
74fi
75
76echo_and_do "$PROJECT_ROOT/tools/ninja" -C "$OUT_DIR" ui
77
78echo "Writing $UI_DIST_DIR/app.yaml"
79cat<<EOF > "$UI_DIST_DIR/app.yaml"
80runtime: python27
81api_version: 1
82threadsafe: yes
83instance_class: B1
84manual_scaling:
85  instances: 1
86handlers:
87- url: /
88  static_files: static/index.html
89  upload: static/index.html
90  secure: always
91  redirect_http_response_code: 301
92- url: /(.*[.]wasm)
93  static_files: static/\1
94  upload: static/(.*[.]wasm)
95  mime_type: application/wasm
96- url: /assets/(.*)
97  static_files: static/assets/\1
98  upload: static/assets/(.*)
99  expiration: "1d"
100- url: /(.*)
101  static_files: static/\1
102  upload: static/(.*)
103EOF
104
105echo_and_do ln -fs ../ui $UI_DIST_DIR/static
106
107(
108  echo_and_do cd "$UI_DIST_DIR";
109  if [ "$DEPLOY_PROD" = true ]; then
110    echo_and_do gcloud app deploy app.yaml --project perfetto-ui -v prod
111  elif [ "$DEPLOY_STAGING" = true ]; then
112    echo_and_do gcloud app deploy app.yaml --project perfetto-ui \
113        -v staging --no-promote --stop-previous-version
114  else
115    echo_and_do gcloud app deploy app.yaml --project perfetto-ui \
116        -v $USER --no-promote --stop-previous-version
117  fi
118)
119
120if [ "$CLEAN_OUT_DIR" = true ]; then
121  echo_and_do rm -rf "$OUT_DIR"
122fi
123