Skip to content

Commit

Permalink
#24 update makefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kahoona77 committed Jun 25, 2024
1 parent d789515 commit 5f56792
Show file tree
Hide file tree
Showing 28 changed files with 1,348 additions and 85 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
STAGE=development
LOG_LEVEL=debug
NAMESPACE=ecosystem
#RUNTIME_ENV=remote
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
*.iml
.env
27 changes: 14 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MAKEFILES_VERSION=5.0.0
MAKEFILES_VERSION=9.0.5

.DEFAULT_GOAL:=help

Expand Down Expand Up @@ -30,22 +30,23 @@ include build/make/variables.mk
# You may want to overwrite existing variables for target actions to fit into your project.

include build/make/self-update.mk
include build/make/dependencies-gomod.mk
include build/make/build.mk
include build/make/test-common.mk
include build/make/test-integration.mk
include build/make/test-unit.mk
include build/make/static-analysis.mk
#include build/make/dependencies-gomod.mk
#include build/make/build.mk
#include build/make/test-common.mk
#include build/make/test-integration.mk
#include build/make/test-unit.mk
#include build/make/static-analysis.mk
include build/make/clean.mk
# either package-tar.mk
include build/make/package-tar.mk
#include build/make/package-tar.mk
# or package-debian.mk
include build/make/package-debian.mk
#include build/make/package-debian.mk
# deploy-debian.mk depends on package-debian.mk
include build/make/deploy-debian.mk
include build/make/digital-signature.mk
include build/make/yarn.mk
include build/make/bower.mk
#include build/make/deploy-debian.mk
#include build/make/digital-signature.mk
#include build/make/yarn.mk
#include build/make/bower.mk
# only include this in dogu repositories
include build/make/release.mk
include build/make/k8s-dogu.mk

64 changes: 64 additions & 0 deletions build/make/bats.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
WORKSPACE=/workspace
BATS_LIBRARY_DIR=$(TARGET_DIR)/bats_libs
TESTS_DIR=$(WORKDIR)/batsTests
BASH_TEST_REPORT_DIR=$(TARGET_DIR)/shell_test_reports
BASH_TEST_REPORTS=$(BASH_TEST_REPORT_DIR)/TestReport-*.xml
BATS_ASSERT=$(BATS_LIBRARY_DIR)/bats-assert
BATS_MOCK=$(BATS_LIBRARY_DIR)/bats-mock
BATS_SUPPORT=$(BATS_LIBRARY_DIR)/bats-support
BATS_FILE=$(BATS_LIBRARY_DIR)/bats-file
BATS_BASE_IMAGE?=bats/bats
BATS_CUSTOM_IMAGE?=cloudogu/bats
BATS_TAG?=1.2.1
BATS_DIR=build/make/bats
BATS_WORKDIR="${WORKDIR}"/"${BATS_DIR}"

.PHONY unit-test-shell:
unit-test-shell: unit-test-shell-$(ENVIRONMENT)

$(BATS_ASSERT):
@git clone --depth 1 https://github.com/bats-core/bats-assert $@

$(BATS_MOCK):
@git clone --depth 1 https://github.com/grayhemp/bats-mock $@

$(BATS_SUPPORT):
@git clone --depth 1 https://github.com/bats-core/bats-support $@

$(BATS_FILE):
@git clone --depth 1 https://github.com/bats-core/bats-file $@

$(BASH_SRC):
BASH_SRC:=$(shell find "${WORKDIR}" -type f -name "*.sh")

${BASH_TEST_REPORT_DIR}: $(TARGET_DIR)
@mkdir -p $(BASH_TEST_REPORT_DIR)

unit-test-shell-ci: $(BASH_SRC) $(BASH_TEST_REPORT_DIR) $(BATS_ASSERT) $(BATS_MOCK) $(BATS_SUPPORT) $(BATS_FILE)
@echo "Test shell units on CI server"
@make unit-test-shell-generic

unit-test-shell-local: $(BASH_SRC) $(PASSWD) $(ETCGROUP) $(HOME_DIR) buildTestImage $(BASH_TEST_REPORT_DIR) $(BATS_ASSERT) $(BATS_MOCK) $(BATS_SUPPORT) $(BATS_FILE)
@echo "Test shell units locally (in Docker)"
@docker run --rm \
-v $(HOME_DIR):/home/$(USER) \
-v $(WORKDIR):$(WORKSPACE) \
-w $(WORKSPACE) \
--entrypoint="" \
$(BATS_CUSTOM_IMAGE):$(BATS_TAG) \
"${BATS_DIR}"/customBatsEntrypoint.sh make unit-test-shell-generic-no-junit

unit-test-shell-generic:
@bats --formatter junit --output ${BASH_TEST_REPORT_DIR} ${TESTS_DIR}

unit-test-shell-generic-no-junit:
@bats ${TESTS_DIR}

.PHONY buildTestImage:
buildTestImage:
@echo "Build shell test container"
@cd $(BATS_WORKDIR) && docker build \
--build-arg=BATS_BASE_IMAGE=${BATS_BASE_IMAGE} \
--build-arg=BATS_TAG=${BATS_TAG} \
-t ${BATS_CUSTOM_IMAGE}:${BATS_TAG} \
.
7 changes: 7 additions & 0 deletions build/make/bats/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG BATS_BASE_IMAGE
ARG BATS_TAG

FROM ${BATS_BASE_IMAGE}:${BATS_TAG}

# Make bash more findable by scripts and tests
RUN apk add make git bash
6 changes: 6 additions & 0 deletions build/make/bats/customBatsEntrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

"$@"
2 changes: 1 addition & 1 deletion build/make/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ADDITIONAL_LDFLAGS?=-extldflags -static
LDFLAGS?=-ldflags "$(ADDITIONAL_LDFLAGS) -X main.Version=$(VERSION) -X main.CommitID=$(COMMIT_ID)"
GOIMAGE?=golang
GOTAG?=1.14.13
GOTAG?=1.22
GOOS?=linux
GOARCH?=amd64
PRE_COMPILE?=
Expand Down
182 changes: 182 additions & 0 deletions build/make/coder-lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/bin/bash
# a collection of helpful functions to update coder workspaces for rapid development
set -e -u -x -o pipefail

function getContainerBin() {
if [ -x "$(command -v podman)" ]; then
echo "podman";
else
echo "docker";
fi
}

function getCoderUser() {
# check if coder is installed, so that there is no problem with build and release targets if this is called before
if [ -x "$(command -v coder)" ]; then
coder users show me -o json | jq -r '.username';
fi
}

function getAllWorkspaces() {
coder list -c workspace | tail -n+2
}

function doesWorkspaceExist() {
coderUser="$1"
workspaceName="$2"

workspace=$(coder list -a -o json | jq -r "select(.[].owner_name == \"${coderUser}\" and .[].name == \"${workspaceName}\") | .[0].name")
if [ -z "$workspace" ]; then
return 1 #workspace does not exist
else
return 0
fi
}

function generateUniqueWorkspaceName() {
local wantedWorkspacePrefix="$1"
# use time to make name unique
local time
time=$(date +'%H-%M-%S')
local lengthOfTime=${#time}
local delimiter='-'
local lengthOfDelimiter=${#delimiter}
# trim prefix, as workspace names are limited to 32 chars
local trimmedPrefix="${wantedWorkspacePrefix:0:$((32 - lengthOfDelimiter - lengthOfTime))}"
local uniqueName="${trimmedPrefix}${delimiter}${time}"
# '--' is forbidden in coder, replace multiple '-' with a single one.
echo "${uniqueName}" | awk '{gsub(/[-]+/,"-")}1'
# returns sth like 'myPrefix-12-45-23'
}

function buildImage() {
local tag="$1"
local containerBuildDir="${2:-./container}"
local secretDir="${3:-./secrets}"
local containerExec="${4:-podman}"

# include build-secrets if there are any
local secretArgs=()
if [ -d "$secretDir" ]; then
# shellcheck disable=SC2231
for secretPath in $secretDir/*; do
# do not match .sh scripts
[[ $secretPath == *.sh ]] && continue
local secretName
secretName=$(basename "$secretPath")
secretArgs+=("--secret=id=$secretName,src=$secretDir/$secretName")
done
fi

if [ "$containerExec" = "podman" ]; then
$containerExec build -t "$tag" --pull=newer "$containerBuildDir" "${secretArgs[@]}"
else
$containerExec build -t "$tag" --pull "$containerBuildDir" "${secretArgs[@]}"
fi
}

function doTrivyConvert() {
local trivyFlags=$1
local outputFile=$2
local containerExec=$3
local jsonScanToConvert=$4

local containerJsonScanFile="/tmp/scan.json"

# shellcheck disable=SC2086
# as globbing is what we want here
"$containerExec" run --rm --pull=always \
-v trivy-cache:/root/.cache \
-v "$jsonScanToConvert:$containerJsonScanFile" \
aquasec/trivy -q \
convert $trivyFlags "$containerJsonScanFile" > "$outputFile"
}

function uploadTemplate() {
local templateDir="${1:?"Error. you need to add the template directory as the first parameter"}"
local templateName="${2:?"Error. you need to add the template name as the second parameter"}"
# for terraform variables (not editable by workspace users)
local variablesFile="${templateDir}/variables.yaml"
if [ -f "$variablesFile" ]; then
local doesVariablesFileExist=1
fi
if ! coder template push -y -d "$templateDir" ${doesVariablesFileExist:+--variables-file "$variablesFile"} "$templateName"; then
# if template does not exist yet, create it in coder
coder template create -y -d "$templateDir" ${doesVariablesFileExist:+--variables-file "$variablesFile"} "$templateName"
fi
}

function createNewWorkspace() {
local templateName="$1"
local workspaceName="$2"
# 3. param is optional, set it to autofill prompts for coder params
local templateDir="${3-unset}"
local richParametersFile="${templateDir}/rich-parameters.yaml"
if [ -n "${templateDir+x}" ] && [ -f "$richParametersFile" ]; then
local doesRichParametersFileExist=1
fi
coder create -t "$templateName" -y "$workspaceName" ${doesRichParametersFileExist:+--rich-parameter-file "$richParametersFile"}
}

function removeAllOtherWorkspaces() {
local CODER_USER="$1"
local WORKSPACE_PREFIX="$2"
local IGNORED_WORKSPACE="$3"
WORKSPACES="$(getAllWorkspaces)"
for ws in $WORKSPACES; do
if [ "$ws" != "$CODER_USER/$IGNORED_WORKSPACE" ] && [[ "$ws" =~ ^"$CODER_USER/$WORKSPACE_PREFIX" ]]; then
echo "delete $ws"
if ! coder delete "$ws" -y; then
#do it twice as podman always throws an error at the first time
coder delete "$ws" -y
fi
fi
done
}

function updateWorkspace() {
local coderUser="$1"
local workspaceName="$2"
local qualifiedWorkspaceName="$coderUser/$workspaceName"
if ! coder stop "$qualifiedWorkspaceName" -y; then
#do it twice as podman always throws an error at the first time
coder stop "$qualifiedWorkspaceName" -y
fi
coder update "$qualifiedWorkspaceName"
}

function startTestWorkspace() {
local coderUser="$1"
local templateDir="$2"
local workspacePrefix="$3"
local templateName="$4"
local reuseTestWorkspace="$5"

local newWorkspaceName
if [ "$reuseTestWorkspace" = false ]; then
newWorkspaceName="$(generateUniqueWorkspaceName "$workspacePrefix")"
# do that before deleting others, so that i don't need to wait
createNewWorkspace "$templateName" "$newWorkspaceName" "$templateDir"
# trim prefix as the name of the workspace can also get trimmed
removeAllOtherWorkspaces "$coderUser" "${workspacePrefix:0:22}" "$newWorkspaceName"
else
newWorkspaceName="$workspacePrefix"
if ! doesWorkspaceExist "$coderUser" "$newWorkspaceName"; then
createNewWorkspace "$templateName" "$newWorkspaceName" "$templateDir"
else
updateWorkspace "$coderUser" "$newWorkspaceName"
fi
fi
}

function uploadToNexus() {
local fileToUpload="$1"
local fileNameNexus="${fileToUpload##*/}"
local templateName="$2"
local releaseVersion="$3"
local nexusUrl="${4:-https://ecosystem.cloudogu.com/nexus/repository/itz-bund/coder}"
set +x #disable command printing because of the password
curl --progress-bar -u "$(cat secrets/nexus-user):$(cat secrets/nexus-pw)" --upload-file "$fileToUpload" \
"$nexusUrl/$templateName/$releaseVersion/$fileNameNexus"
set -x
}
Loading

0 comments on commit 5f56792

Please sign in to comment.