Skip to content

Commit

Permalink
Use TF_WORKSPACE var to set workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
vulkoingim authored and roboquat committed Nov 25, 2022
1 parent 04fb36c commit 263699c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 79 deletions.
9 changes: 0 additions & 9 deletions .werft/platform-delete-preview-environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ pod:
readOnly: true
- name: harvester-kubeconfig
mountPath: /mnt/secrets/harvester-kubeconfig
- name: harvester-vm-ssh-keys
mountPath: /mnt/secrets/harvester-vm-ssh-keys
- name: harvester-k3s-dockerhub-pull-account
mountPath: /mnt/secrets/harvester-k3s-dockerhub-pull-account
env:
- name: LEEWAY_WORKSPACE_ROOT
value: /workspace
Expand All @@ -56,11 +52,6 @@ pod:
set -Eeuo pipefail
sudo chown -R gitpod:gitpod /workspace
mkdir /workspace/.ssh
cp /mnt/secrets/harvester-vm-ssh-keys/id_rsa /workspace/.ssh/id_rsa_harvester_vm
cp /mnt/secrets/harvester-vm-ssh-keys/id_rsa.pub /workspace/.ssh/id_rsa_harvester_vm.pub
sudo chmod 600 /workspace/.ssh/id_rsa_harvester_vm
sudo chmod 644 /workspace/.ssh/id_rsa_harvester_vm.pub
(cd .werft && yarn install && mv node_modules ..) | werft log slice prep
printf '{{ toJson . }}' > context.json
Expand Down
2 changes: 1 addition & 1 deletion dev/preview/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ scripts:
description: Delete an existing preview environment
script: |
export DESTROY=true
export TF_VAR_kubeconfig_path="${TF_VAR_kubeconfig_path:-/home/gitpod/.kube/config}"
export TF_VAR_kubeconfig_path="${TF_VAR_kubeconfig_path:-$HOME/.kube/config}"
export TF_VAR_preview_name="${TF_VAR_preview_name:-$(previewctl get-name)}"
./workflow/preview/deploy-harvester.sh
Expand Down
46 changes: 11 additions & 35 deletions dev/preview/workflow/lib/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,24 @@ if [ -n "${DESTROY-}" ]; then
export TF_CLI_ARGS_plan="${TF_CLI_ARGS_plan} -destroy"
fi

function check_workspace() {
function delete_workspace() {
local workspace=$1
if [[ $(terraform workspace show) != "${workspace}" ]]; then
log_error "Expected to be in [${workspace}]. We are in [$(terraform workspace show)]"
return "${ERROR_WRONG_WORKSPACE}"
local exists=0
terraform workspace list | grep -q "${workspace}" || exists=$?
if [[ "${exists}" == 0 ]]; then
TF_WORKSPACE="default" terraform workspace delete "${workspace}"
fi
}

function set_workspace() {
function create_workspace() {
local workspace=$1
if terraform workspace list | grep -q "${workspace}"; then
terraform workspace select "${workspace}"
return 0
else
terraform workspace new "${workspace}"
fi
}

function delete_workspace() {
local workspace=$1
if [[ $(terraform workspace show) == "${workspace}" ]]; then
terraform workspace select default
fi

exists=0
terraform workspace list | grep -q "${workspace}" || exists=$?
if [[ "${exists}" == 0 ]]; then
terraform workspace delete "${workspace}"
fi
}

function terraform_init() {
local target_dir=${1:-$TARGET_DIR}
if [ -z "${target_dir-}" ]; then
Expand All @@ -55,10 +43,10 @@ function terraform_init() {
fi
pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}"

terraform init
if [ -n "${WORKSPACE-}" ]; then
set_workspace "${WORKSPACE}"
check_workspace "${WORKSPACE}"
TF_WORKSPACE="default" terraform init

if [ -n "${TF_WORKSPACE}" ]; then
create_workspace "${TF_WORKSPACE}"
fi

popd || return "${ERROR_CHANGE_DIR}"
Expand All @@ -77,12 +65,6 @@ function terraform_plan() {

pushd "${target_dir}" || return "${ERROR_CHANGE_DIR}"

# check if we should be in a workspace, and bail otherwise
if [ -n "${WORKSPACE-}" ]; then
set_workspace "${WORKSPACE}"
check_workspace "${WORKSPACE}"
fi

# -detailed-exitcode will be 0=success no changes/1=failure/2=success changes
# therefore we capture the output so our function doesn't cause a script to terminate if the caller has `set -e`
EXIT_CODE=0
Expand Down Expand Up @@ -111,12 +93,6 @@ function terraform_apply() {
return "${ERROR_NO_PLAN}"
fi

# check if we should be in a workspace, and bail otherwise
if [ -n "${WORKSPACE-}" ]; then
set_workspace "${WORKSPACE}"
check_workspace "${WORKSPACE}"
fi

timeout --signal=INT --foreground 10m terraform apply "${plan_location}"

popd || return "${ERROR_CHANGE_DIR}"
Expand Down
17 changes: 14 additions & 3 deletions dev/preview/workflow/preview/deploy-gitpod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,20 @@ INSTALLER_RENDER_PATH="k8s.yaml" # k8s.yaml is hardcoded in post-prcess.sh - we

# 1. Read versions from the file system. We rely on `leeway dev/preview:deploy-dependencies` to create this file for us
# Or from the docker file if it doesn't exist
# shellcheck disable=SC2050,SC2057
if [ ! test -f "/tmp/versions.yaml" ]; then
docker run --rm "eu.gcr.io/gitpod-core-dev/build/versions:$VERSION" cat /versions.yaml > /tmp/versions.yaml
# Or just build it and get it from there
if ! test -f "/tmp/versions.yaml"; then
ec=0
docker run --rm "eu.gcr.io/gitpod-core-dev/build/versions:$VERSION" cat /versions.yaml > /tmp/versions.yaml || ec=$?
if [[ ec -ne 0 ]];then
VERSIONS_TMP_ZIP=$(mktemp "/tmp/XXXXXX.installer.tar.gz")
leeway build components:all-docker \
--dont-test \
-Dversion="${VERSION}" \
-DSEGMENT_IO_TOKEN="$(kubectl --context=dev -n werft get secret self-hosted -o jsonpath='{.data.segmentIOToken}' | base64 -d)" \
--save "${VERSIONS_TMP_ZIP}"
tar -xzvf "${VERSIONS_TMP_ZIP}" ./versions.yaml && sudo mv ./versions.yaml /tmp/versions.yaml
rm "${VERSIONS_TMP_ZIP}"
fi
fi

if ! command -v installer;then
Expand Down
6 changes: 3 additions & 3 deletions dev/preview/workflow/preview/deploy-harvester.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if [[ -n ${WERFT_SERVICE_HOST+x} ]]; then
export TF_IN_AUTOMATION=true
fi

WORKSPACE="${TF_VAR_preview_name:-$WORKSPACE}"
export TF_WORKSPACE="${TF_VAR_preview_name:-TF_WORKSPACE}"
TARGET_DIR="${PROJECT_ROOT}/dev/preview/infrastructure/harvester"
# Setting the TF_DATA_DIR is advisable if we set the PLAN_LOCATION in a different place than the dir with the tf
TF_DATA_DIR="${TARGET_DIR}"
Expand Down Expand Up @@ -54,8 +54,8 @@ case ${PLAN_EXIT_CODE} in
;;
esac

if [ -n "${DESTROY-}" ] && [ -n "${WORKSPACE}" ]; then
if [ -n "${DESTROY-}" ] && [ -n "${TF_WORKSPACE}" ]; then
pushd "${TARGET_DIR}"
delete_workspace "${WORKSPACE}"
delete_workspace "${TF_WORKSPACE}"
popd
fi
28 changes: 0 additions & 28 deletions dev/preview/workflow/terraform/terraform-workspace.sh

This file was deleted.

0 comments on commit 263699c

Please sign in to comment.