From fcad4aac731a600dfdb846ea38560107917c9669 Mon Sep 17 00:00:00 2001 From: Ross Oliver Date: Fri, 2 Feb 2024 11:25:53 +0000 Subject: [PATCH] Add Make command to upload files from AKS pods We currently have a command that will download a file from a pod in a given AKS cluster, however we have no way of uploading a file to a pod. The current method of downloading a file will also choose a pod in the cluster at random; ideally we want to download a file from each pod in the cluster (in case we create the file in one pod and the command tries to download it from a different pod in the same cluster). Add make command to upload files and modify the command to download files to pull from each pod in the cluster (saving the files in a subdirectory of the pod name). --- Makefile | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index dc0848c593..f61685e878 100644 --- a/Makefile +++ b/Makefile @@ -159,6 +159,10 @@ install-konduit: ## Install the konduit script, for accessing backend services terraform-refresh: terraform-init terraform -chdir=terraform/application refresh -var-file config/$(CONFIG)/variables.tfvars.json +define SET_APP_ID_FROM_PULL_REQUEST_NUMBER + $(if $(PULL_REQUEST_NUMBER), $(eval export APP_ID=review-$(PULL_REQUEST_NUMBER)) , $(eval export APP_ID=$(CONFIG_LONG))) +endef + action-group-resources: set-azure-account # make env_aks action-group-resources ACTION_GROUP_EMAIL=notificationemail@domain.com . Must be run before setting enable_monitoring=true for each subscription $(if $(ACTION_GROUP_EMAIL), , $(error Please specify a notification email for the action group)) echo ${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-mn-rg @@ -166,23 +170,33 @@ action-group-resources: set-azure-account # make env_aks action-group-resources az monitor action-group create -n ${AZURE_RESOURCE_PREFIX}-cpd-ecf -g ${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-mn-rg --action email ${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-email ${ACTION_GROUP_EMAIL} aks-console: get-cluster-credentials - $(if $(PULL_REQUEST_NUMBER), $(eval export APP_ID=review-$(PULL_REQUEST_NUMBER)) , $(eval export APP_ID=$(CONFIG_LONG))) + $(SET_APP_ID_FROM_PULL_REQUEST_NUMBER) kubectl -n ${NAMESPACE} exec -ti --tty deployment/cpd-ecf-${APP_ID}-web -- /bin/sh -c "cd /app && bundle exec rails c" aks-ssh: get-cluster-credentials - $(if $(PULL_REQUEST_NUMBER), $(eval export APP_ID=review-$(PULL_REQUEST_NUMBER)) , $(eval export APP_ID=$(CONFIG_LONG))) + $(SET_APP_ID_FROM_PULL_REQUEST_NUMBER) kubectl -n ${NAMESPACE} exec -ti --tty deployment/cpd-ecf-${APP_ID}-web -- /bin/sh aks-worker-ssh: get-cluster-credentials - $(if $(PULL_REQUEST_NUMBER), $(eval export APP_ID=review-$(PULL_REQUEST_NUMBER))-worker , $(eval export APP_ID=$(CONFIG_LONG))) + $(SET_APP_ID_FROM_PULL_REQUEST_NUMBER) kubectl -n ${NAMESPACE} exec -ti --tty deployment/cpd-ecf-${APP_ID}-worker -- /bin/sh -## ie: FILENAME=restart.txt make staging aks-copy-tmp-file -## ie: FILENAME=restart.txt make ci production aks-copy-tmp-file -aks-copy-tmp-file: get-cluster-credentials - $(if $(PULL_REQUEST_NUMBER), $(eval export APP_ID=review-$(PULL_REQUEST_NUMBER)) , $(eval export APP_ID=$(CONFIG_LONG))) - $(if $(FILENAME), , $(error Usage: FILENAME=restart.txt make staging aks-copy-tmp-file)) - kubectl -n ${NAMESPACE} exec -ti --tty deployment/cpd-ecf-${APP_ID}-web -- cat /app/tmp/${FILENAME} > ${FILENAME} +# downloads the given file from the app/tmp directory of all +# pods in the cluster to the local computer (in a subdirectory matching the pod name). +## ie: FILENAME=restart.txt make staging aks-download-tmp-file +## ie: FILENAME=restart.txt make ci production aks-download-tmp-file +aks-download-tmp-file: get-cluster-credentials + $(SET_APP_ID_FROM_PULL_REQUEST_NUMBER) + $(if $(FILENAME), , $(error Usage: FILENAME=restart.txt make staging aks-download-tmp-file)) + kubectl get pods -n ${NAMESPACE} -l app=cpd-ecf-${APP_ID}-web -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -I {} sh -c 'mkdir -p {}/ && kubectl cp ${NAMESPACE}/{}:/app/tmp/${FILENAME} {}/${FILENAME}' + +# uploads the given file to the app/tmp directory of all +# pods in the cluster. +## ie: FILENAME=local_file.txt make staging aks-upload-tmp-file +aks-upload-tmp-file: get-cluster-credentials + $(SET_APP_ID_FROM_PULL_REQUEST_NUMBER) + $(if $(FILENAME), , $(error Usage: FILENAME=restart.txt make staging aks-upload-tmp-file)) + kubectl get pods -n ${NAMESPACE} -l app=cpd-ecf-${APP_ID}-web -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -I {} kubectl cp ${FILENAME} ${NAMESPACE}/{}:/app/tmp/${FILENAME} # Removes explicit postgres database URLs from database.yml konduit-cleanup: