From 55c2e7f1b57128971222d0cac3b8c9107fe72e86 Mon Sep 17 00:00:00 2001 From: Avritt Rohwer Date: Wed, 15 Mar 2023 11:26:00 -0700 Subject: [PATCH] Add support for installing cloud-deploy-infra python dependencies --- bin/lib/checkout.sh | 158 ++++++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 63 deletions(-) diff --git a/bin/lib/checkout.sh b/bin/lib/checkout.sh index 2c7c000..e590fd4 100755 --- a/bin/lib/checkout.sh +++ b/bin/lib/checkout.sh @@ -8,7 +8,7 @@ # tag the user specifies in the command. ####################################### -# Delegates a command from a specified path to the main CiviForm repo at a +# Delegates a command from a specified path to the main CiviForm repo at a # specific git revision. # Expects arguments to include "--tag=" for resolving which revsion # to check out for the command. Then passes all arguments to the delegated @@ -19,42 +19,43 @@ # CMD_NAME: the name of the command to run ####################################### function checkout::exec_delegated_command_at_path() { - if [[ -z "${CMD_NAME_PATH}" ]]; then - out::error "CMD_NAME must be set for delegated command." - exit 1 - fi - - checkout::initialize - if [[ -z "${CIVIFORM_CLOUD_DEPLOYMENT_VERSION}" ]]; then - out::error "CIVIFORM_CLOUD_DEPLOYMENT_VERSION needs to be set to 'latest' or commit sha from https://github.com/civiform/cloud-deploy-infra." - exit 1 - else - checkout::at_sha "${CIVIFORM_CLOUD_DEPLOYMENT_VERSION}" - fi - - if [[ -z "${CIVIFORM_VERSION}" ]]; then - out::error "CIVIFORM_VERSION needs to be either 'latest' or a version from https://github.com/civiform/civiform/releases" - exit 1 - fi - if [[ "${CIVIFORM_VERSION}" == 'latest' && "${CIVIFORM_MODE}" == 'prod' ]]; then - out::error "For production deployments, CIVIFORM_VERSION needs to be a version from https://github.com/civiform/civiform/releases" - exit 1 - fi - - ( - cd checkout - CONFIG_FILE_ABSOLUTE_PATH="${CONFIG}" - if [[ "${CONFIG:0:1}" != "/" ]]; then - CONFIG_FILE_ABSOLUTE_PATH="../${CONFIG}" + if [[ -z ${CMD_NAME_PATH} ]]; then + out::error "CMD_NAME must be set for delegated command." + exit 1 fi - args=("--command=${CMD_NAME}" "--tag=${CIVIFORM_VERSION}" "--config=${CONFIG_FILE_ABSOLUTE_PATH}") - echo "Running ${CMD_NAME_PATH} ${args[@]}" - exec "${CMD_NAME_PATH}" "${args[@]}" - ) + + if [[ -z ${CIVIFORM_VERSION} ]]; then + out::error "CIVIFORM_VERSION needs to be either 'latest' or a version from https://github.com/civiform/civiform/releases" + exit 1 + fi + if [[ ${CIVIFORM_VERSION} == 'latest' && ${CIVIFORM_MODE} == 'prod' ]]; then + out::error "For production deployments, CIVIFORM_VERSION needs to be a version from https://github.com/civiform/civiform/releases" + exit 1 + fi + + checkout::initialize + if [[ -z ${CIVIFORM_CLOUD_DEPLOYMENT_VERSION} ]]; then + out::error "CIVIFORM_CLOUD_DEPLOYMENT_VERSION needs to be set to 'latest' or commit sha from https://github.com/civiform/cloud-deploy-infra." + exit 1 + else + checkout::at_sha "${CIVIFORM_CLOUD_DEPLOYMENT_VERSION}" + fi + + checkout::initialize_python_env + ( + cd checkout + CONFIG_FILE_ABSOLUTE_PATH="${CONFIG}" + if [[ ${CONFIG:0:1} != "/" ]]; then + CONFIG_FILE_ABSOLUTE_PATH="../${CONFIG}" + fi + args=("--command=${CMD_NAME}" "--tag=${CIVIFORM_VERSION}" "--config=${CONFIG_FILE_ABSOLUTE_PATH}") + echo "Running ${CMD_NAME_PATH} ${args[@]}" + exec "${CMD_NAME_PATH}" "${args[@]}" + ) } ####################################### -# Delegates a command from the shared bin to the main CiviForm repo at a +# Delegates a command from the shared bin to the main CiviForm repo at a # specific git revision. # Expects arguments to include "--tag=" for resolving which revsion # to check out for the command. Then passes all arguments to the delegated @@ -65,8 +66,8 @@ function checkout::exec_delegated_command_at_path() { # CMD_NAME: the name of the command to run ####################################### function checkout::exec_delegated_command() { - CMD_NAME_PATH="cloud/shared/bin/run.py" \ - checkout::exec_delegated_command_at_path "$@" + CMD_NAME_PATH="cloud/shared/bin/run.py" \ + checkout::exec_delegated_command_at_path "$@" } ####################################### @@ -75,24 +76,24 @@ function checkout::exec_delegated_command() { # that only contains the cloud/ directory. ####################################### function checkout::initialize() { - printf "Initializing checkout... " + printf "Initializing checkout... " - rm -rf checkout - mkdir checkout + rm -rf checkout + mkdir checkout - pushd checkout > /dev/null + pushd checkout > /dev/null - git init --initial-branch=main - git remote add origin http://github.com/civiform/cloud-deploy-infra - git config core.sparseCheckout true + git init --initial-branch=main + git remote add origin http://github.com/civiform/cloud-deploy-infra + git config core.sparseCheckout true - echo "/cloud" >> .git/info/sparse-checkout - echo "/bin" >> .git/info/sparse-checkout + echo "/cloud" >> .git/info/sparse-checkout + echo "/bin" >> .git/info/sparse-checkout - git pull --quiet origin main + git pull --quiet origin main - popd > /dev/null - echo "done" + popd > /dev/null + echo "done" } ####################################### @@ -102,34 +103,65 @@ function checkout::initialize() { # 1: The commit SHA to sync to. ####################################### function checkout::at_sha() { - local commit_sha="${1}" - if [[ ${commit_sha} == 'latest' ]]; then - checkout::at_main - else - printf "Setting checkout to ${commit_sha}... " + local commit_sha="${1}" + if [[ ${commit_sha} == 'latest' ]]; then + checkout::at_main + else + printf "Setting checkout to ${commit_sha}... " + + pushd checkout > /dev/null + + git pull --quiet origin main + git checkout --quiet "${commit_sha}" + + popd > /dev/null + echo "done" + fi +} + +####################################### +# Pulls the latest git revisions for the checkout directory from the mainline +# CiviForm repo. +####################################### +function checkout::at_main() { + printf "Syncing checkout directory to latest commit... " pushd checkout > /dev/null + git checkout --quiet main git pull --quiet origin main - git checkout --quiet "${commit_sha}" popd > /dev/null echo "done" - fi } ####################################### -# Pulls the latest git revisions for the checkout directory from the mainline -# CiviForm repo. +# Initializes a python venv and installs required dependencies to run +# cloud-deploy-infra python files. The cloud-deploy-infra repository must be +# already cloned into checkout directory before calling this function. +# +# Creates the venv outside of the checkout directory because the checkout +# directory is deleted on every run (see checkout::initialize). ####################################### -function checkout::at_main() { - printf "Syncing checkout directory to latest commit... " +function checkout::initialize_python_env() { + if [[ ! -f checkout/cloud/requirements.txt ]]; then + echo "cloud-deploy-infra version checked out does not have a requirements.txt file, no python setup is needed" + return + fi - pushd checkout > /dev/null + if [[ -d .venv ]]; then + source .venv/bin/activate + if [[ ! $(pip freeze | diff checkout/cloud/requirements.txt -) ]]; then + echo ".venv directory found with necessary dependencies installed" + return + fi - git checkout --quiet main - git pull --quiet origin main + echo ".venv directory found but necessary dependencies are not installed, installing..." + pip install -r checkout/cloud/requirements.txt + return + fi - popd > /dev/null - echo "done" + echo ".venv directory not found, creating and installing dependencies..." + python3 -m venv .venv + pip install -r checkout/cloud/requirements.txt }