1#!/bin/bash 2# Copyright 2020 Google LLC 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################################################################################ 16 17# This script creates a Tinkey distribution and uploads it to Google Cloud 18# Storage. 19# Prerequisites: 20# - Google Cloud SDK (https://cloud.google.com/sdk/install) 21# - Write access to the "tinkey" GCS bucket. Ping tink-dev@. 22# - Bazelisk (https://github.com/bazelbuild/bazelisk) or Bazel 23# (https://bazel.build/) 24 25usage() { 26 echo "Usage: $0 [-dh] <version>" 27 echo " -d: Dry run. Only execute idempotent commands (default: FALSE)." 28 echo " -h: Help. Print this usage information." 29 exit 1 30} 31 32# Process flags. 33 34DRY_RUN="false" 35 36while getopts "dh" opt; do 37 case "${opt}" in 38 d) DRY_RUN="true" ;; 39 h) usage ;; 40 *) usage ;; 41 esac 42done 43shift $((OPTIND - 1)) 44 45readonly DRY_RUN 46 47# Process script arguments. 48 49VERSION="$1" 50shift 1 51 52if [ -z "${VERSION}" ]; then 53 VERSION="snapshot" 54fi 55 56if [[ "${VERSION}" =~ " " ]]; then 57 echo "Version name must not have any spaces" 58 exit 3 59fi 60 61# Set up parameters. 62 63readonly GCS_LOCATION="gs://tinkey/" 64readonly TMP_DIR="$(mktemp -dt tinkey.XXXXXX)" 65BAZEL_CMD="bazel" 66if command -v "bazelisk" &> /dev/null; then 67 BAZEL_CMD="bazelisk" 68fi 69readonly BAZEL_CMD 70 71do_command() { 72 if ! "$@"; then 73 echo "*** Failed executing command. ***" 74 echo "Failed command: $@" 75 exit 1 76 fi 77 return $? 78} 79 80print_command() { 81 printf '%q ' '+' "$@" 82 echo 83} 84 85print_and_do() { 86 print_command "$@" 87 do_command "$@" 88 return $? 89} 90 91do_if_not_dry_run() { 92 # $@ is an array containing a command to be executed and its arguments. 93 print_command "$@" 94 if [[ "${DRY_RUN}" == "true" ]]; then 95 echo " *** Dry run, command not executed. ***" 96 return 0 97 fi 98 do_command "$@" 99 return $? 100} 101 102build_tinkey() { 103 print_and_do cd tools 104 105 print_and_do "${BAZEL_CMD}" build tinkey:tinkey_deploy.jar 106 107 print_and_do cp bazel-bin/tinkey/tinkey_deploy.jar "${TMP_DIR}" 108 109 print_and_do cd "${TMP_DIR}" 110 111 cat <<EOF > tinkey 112#!/usr/bin/env sh 113 114java -jar "\$(dirname \$0)/tinkey_deploy.jar" "\$@" 115EOF 116 117 cat <<EOF > tinkey.bat 118java -jar "%~dp0\tinkey_deploy.jar" %* 119EOF 120 121 chmod 755 tinkey 122 123 print_and_do tar -czvpf "tinkey-${VERSION}.tar.gz" tinkey_deploy.jar tinkey tinkey.bat 124} 125 126upload_to_gcs() { 127 print_and_do cd "${TMP_DIR}" 128 129 shasum -a 256 "tinkey-${VERSION}.tar.gz" 130 131 do_if_not_dry_run gsutil cp "tinkey-${VERSION}.tar.gz" "${GCS_LOCATION}" 132} 133 134main() { 135 build_tinkey 136 upload_to_gcs 137} 138 139main "$@" 140