diff --git a/build-util/README.md b/build-util/README.md index 91c11d6..dc39afd 100644 --- a/build-util/README.md +++ b/build-util/README.md @@ -5,7 +5,8 @@ This folder contains useful scripts and other files used in the development/buil | **File/Folder** | **Description** | | -- | -- | | `copy-to-co-dnr-gcp.bash` | Copy the TSTool installer to State of Colorado GCP server. The istaller is in the repository `/dist` folder and the TSTool version correspond to current code version. | -| `create-gcp-tstool-index.bash` | Create and upload an index of TSTool installers. | +| `create-gcp-tstool-index.bash` | Create and upload the index of TSTool installers. | +| `create-gcp-tstool-usage-index.bash` | Create and upload the TSTool usage index. | | `git-check-tstool.sh` | Determine status of TSTool repositories compared to remote repositories. | | `git-clone-all-tstool.sh | Clone all TSTool repositories, helpful when first setting up a development environment. | | `git-tag-all-tstool.sh` | Tag all TSTool repositories with the same tag, used to coordinate releases. | diff --git a/build-util/copy-to-co-dnr-gcp.bash b/build-util/copy-to-co-dnr-gcp.bash index 8d55c43..3ca9854 100644 --- a/build-util/copy-to-co-dnr-gcp.bash +++ b/build-util/copy-to-co-dnr-gcp.bash @@ -185,9 +185,10 @@ syncFiles() { updateIndex() { local answer echo "" - read -p "Do you want to update the GCP index file [Y/n]? " answer + read -p "Do you want to update the GCP index and usage index files [Y/n]? " answer if [ -z "${answer}" -o "${answer}" = "y" -o "${answer}" = "Y" ]; then ${scriptFolder}/create-gcp-tstool-index.bash + ${scriptFolder}/create-gcp-tstool-usage-index.bash fi } diff --git a/build-util/create-gcp-tstool-usage-index.bash b/build-util/create-gcp-tstool-usage-index.bash new file mode 100644 index 0000000..62bf4e5 --- /dev/null +++ b/build-util/create-gcp-tstool-usage-index.bash @@ -0,0 +1,299 @@ +#!/bin/bash +# +# Create the following files on GCP bucket, such as +# +# opencdss.state.co.us/tstool/14.8.0/usage/index.html +# +# These index.html file contain Google Analytics 4 tracking and are accessed by TSTool +# at startup to track usage by version. + +# Supporting functions, alphabetized. + +# Determine the operating system that is running the script: +# - sets the variable operatingSystem to cygwin, linux, or mingw (Git Bash) +checkOperatingSystem() { + if [ ! -z "${operatingSystem}" ]; then + # Have already checked operating system so return. + return + fi + operatingSystem="unknown" + os=`uname | tr [a-z] [A-Z]` + case "${os}" in + CYGWIN*) + operatingSystem="cygwin" + operatingSystemShort="cyg" + ;; + LINUX*) + operatingSystem="linux" + operatingSystemShort="lin" + ;; + MINGW*) + operatingSystem="mingw" + operatingSystemShort="min" + ;; + esac + echoStderr "" + echoStderr "Detected operatingSystem=${operatingSystem} operatingSystemShort=${operatingSystemShort}" + echoStderr "" +} + +# Echo a string to standard error (stderr). +# This is done so that output printed to stdout is not mixed with stderr. +echoStderr() { + echo "$@" >&2 +} + +# Check whether a file exists on GCP storage: +# - function argument should be Google storage URL gs:opencdss... etc. +gcpUtilFileExists() { + local fileToCheck + fileToCheck=$1 + # The following will return 0 if the file exists, 1 if not. + gsutil.cmd -q stat ${fileToCheck} + return $? +} + +# Get the user's login to local temporary files: +# - Git Bash apparently does not set ${USER} environment variable +# - Set USER as script variable only if environment variable is not already set +# - See: https://unix.stackexchange.com/questions/76354/who-sets-user-and-username-environment-variables +getUserLogin() { + if [ -z "${USER}" ]; then + if [ ! -z "${LOGNAME}" ]; then + USER=${LOGNAME} + fi + fi + if [ -z "${USER}" ]; then + USER=$(logname) + fi + # Else - not critical since used for temporary files. +} + +# Parse the command parameters: +# - use the getopt command line program so long options can be handled +parseCommandLine() { + local optstring optstringLong + local exitCode + local GETOPT_OUT + + # Single character options. + optstring="dhv" + # Long options. + optstringLong="debug,help,version" + # Parse the options using getopt command. + GETOPT_OUT=$(getopt --options ${optstring} --longoptions ${optstringLong} -- "$@") + exitCode=$? + if [ ${exitCode} -ne 0 ]; then + # Error parsing the parameters such as unrecognized parameter. + echoStderr "" + printUsage + exit 1 + fi + # The following constructs the command by concatenating arguments. + eval set -- "${GETOPT_OUT}" + # Loop over the options. + while true; do + #echo "Command line option is ${opt}" + case "${1}" in + --debug) # --debug Indicate to output debug messages. + echoStderr "--debug detected - will print debug messages." + debug="true" + shift 1 + ;; + -h|--help) # -h or --help Print the program usage. + printUsage + exit 0 + ;; + -v|--version) # -v or --version Print the program version. + printVersion + exit 0 + ;; + --) # No more arguments. + shift + break + ;; + *) # Unknown option. + echoStderr "" + echoStderr "Invalid option: ${1}" >&2 + printUsage + exit 1 + ;; + esac + done +} + +# Print the usage. +printUsage() { + echoStderr "" + echoStderr "Usage: ${scriptName}" + echoStderr "" + echoStderr "Create the product GCP usage index file: ${gcpIndexHtmlUrl}" + echoStderr "" + echoStderr "--debug Print debug messages, for troubleshooting." + echoStderr "-h, --help Print usage." + echoStderr "-v, --version Print script version." + echoStderr "" +} + +# Print the version. +printVersion() { + echoStderr "${version}" +} + +# Upload the index.html file for the static website download page +# - this is basic at the moment but can be improved in the future such as +# software.openwaterfoundation.org page, but for only one product, with list of variants and versions +uploadIndexHtmlFile() { + local indexHtmlTmpFile gcpIndexHtmlUrl + + # Create an index.html file for upload. + indexHtmlTmpFile="/tmp/${USER}-tstool-usage-index.html" + gcpIndexHtmlUrl="${gcpFolderUrl}/${tstoolVersion}/usage/index.html" + + echo ' +
+ + + + + +' > ${indexHtmlTmpFile} + +echo " + + + + +" >> ${indexHtmlTmpFile} + +echo ' ++See the OpenCDSS TSTool page, +which provides additional information about TSTool. +
++This page is accessed by TSTool at startup to track software usage. +The information helps software developers and support understand which TSTool versions are being used. +
' >> ${indexHtmlTmpFile} + + echo '' >> ${indexHtmlTmpFile} + echo '