782 lines
24 KiB
Bash
Executable File
782 lines
24 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
help() {
|
|
echo
|
|
echo 'task <command> [options]'
|
|
echo
|
|
echo 'commands:'
|
|
echo
|
|
|
|
local -a rows
|
|
local max_cmd=7 max_opt=6 max_desc=11
|
|
|
|
rows+=("Command|Option|Description")
|
|
|
|
# Env
|
|
rows+=("version||Show version of required tools.")
|
|
rows+=("info||Show info about env.")
|
|
rows+=("template-dotenv||Generate .env from .env.template.")
|
|
rows+=("export-kubeconfig|[user][path]|Export kubeconfig file for service account.")
|
|
rows+=("generate-kubconfig|[name]|Generate kubeconfig for deploymentUpdater.")
|
|
rows+=("load-kubeconfig-from-keepass|[ctx]|Load kubeconfig file from KeePass.")
|
|
rows+=("save-kubeconfig-to-keepass|[ctx]|Save kubeconfig file in KeePass.")
|
|
# Docs
|
|
rows+=("install-vitepress||Install Node dependencies.")
|
|
rows+=("docs||Generate Helm chart readmes.")
|
|
rows+=("lint||Lint Helm charts.")
|
|
rows+=("dev-vitepress||Start VitePress dev server.")
|
|
rows+=("build-vitepress||Build site with VitePress.")
|
|
rows+=("serve-vitepress||Serve VitePress build.")
|
|
# Host
|
|
rows+=("start||Start kind Kubernetes cluster.")
|
|
rows+=("start-and-prepare||Start and prepare Kubernetes cluster.")
|
|
rows+=("load-image|[tag]|Load Docker image into kind cluster.")
|
|
rows+=("delete-kind||Delete kind Kubernetes cluster.")
|
|
rows+=("setup-hosts||Update hosts file.")
|
|
rows+=("forward|[chart]|Forward port.")
|
|
# Chart
|
|
rows+=("list-charts||List local Helm charts.")
|
|
rows+=("list-external-charts||List external Helm charts.")
|
|
rows+=("list-cluster||List clusters.")
|
|
rows+=("list-context||List kubeconfig files.")
|
|
rows+=("switch-context||Switch kubeconfig file.")
|
|
rows+=("show-context||Show active kubeconfig.")
|
|
rows+=("show-cluster||Show active cluster.")
|
|
rows+=("get-context-alias||Get alias of current context.")
|
|
rows+=("update-dependencies|[chart]|Update dependencies for a chart.")
|
|
rows+=("install-chart|[chart][values]|Install Helm chart.")
|
|
rows+=("upgrade-release|[chart][values]|Upgrade Helm release.")
|
|
rows+=("uninstall-release|[release]|Uninstall Helm release.")
|
|
# kubectl
|
|
rows+=("get-secrets|[chart]|Show secrets for chart.")
|
|
rows+=("exec-bash|[chart]|Open bash shell in container.")
|
|
rows+=("logs|[grep]|Select pods with grep and show logs.")
|
|
rows+=("list||List Helm deployments.")
|
|
rows+=("get-pods||List all pods.")
|
|
rows+=("login-oc||Refresh the auth token for OpenShift.")
|
|
rows+=("run-job|[name]|Make a copy and unsuspends the job.")
|
|
# Helm
|
|
rows+=("add-repos||Add the required Helm repos.")
|
|
rows+=("template-manifest|[chart][values]|Template all charts with Helm.")
|
|
rows+=("package-repo|[chart]|Package Helm charts.")
|
|
rows+=("index-repo||Index Helm chart.")
|
|
rows+=("remove-repo||Remove the kubernetes-build repo.")
|
|
rows+=("commit||Commit .tgz files.")
|
|
# LLM
|
|
rows+=("commit-with-llm||Commit with llm generated commit message.")
|
|
rows+=("update-with-llm|[glob][prompt]|Feed files matching glob to LLM and apply changes.")
|
|
# Test
|
|
rows+=("test-chart-clusterIssuer||Test cluster issuer.")
|
|
rows+=("test-chart-odoo|[cleanup]|Setup Odoo chart and dependencies.")
|
|
rows+=("test-chart-nextcloud|[cleanup]|Setup Nextcloud chart and dependencies.")
|
|
|
|
for row in "${rows[@]}"; do
|
|
IFS='|' read -r cmd opt desc <<< "$row"
|
|
(( ${#cmd} > max_cmd )) && max_cmd=${#cmd}
|
|
(( ${#opt} > max_opt )) && max_opt=${#opt}
|
|
(( ${#desc} > max_desc )) && max_desc=${#desc}
|
|
done
|
|
|
|
printf '| %-*s | %-*s | %-*s |\n' \
|
|
"$max_cmd" "Command" \
|
|
"$max_opt" "Option" \
|
|
"$max_desc" "Description"
|
|
|
|
printf '|-%*s-|-%*s-|-%*s-|\n' \
|
|
"$max_cmd" "$(printf '%*s' "$max_cmd" '' | tr ' ' '-')" \
|
|
"$max_opt" "$(printf '%*s' "$max_opt" '' | tr ' ' '-')" \
|
|
"$max_desc" "$(printf '%*s' "$max_desc" '' | tr ' ' '-')"
|
|
|
|
for row in "${rows[@]:1}"; do
|
|
IFS='|' read -r cmd opt desc <<< "$row"
|
|
printf '| %-*s | %-*s | %-*s |\n' \
|
|
"$max_cmd" "$cmd" \
|
|
"$max_opt" "$opt" \
|
|
"$max_desc" "$desc"
|
|
done
|
|
}
|
|
|
|
# Default vars
|
|
|
|
OC_PROJECT=${OC_PROJECT:="odoo"}
|
|
CONTAINER_REGISTRY=${CONTAINER_REGISTRY:="mintsystem/"}
|
|
CONTAINER_CONFIG="$HOME/.docker/$CONTAINER_REGISTRY"
|
|
|
|
# Dynamic vars
|
|
|
|
pass_entry=$(echo "env/${PWD##*/}" | tr '[:upper:]' '[:lower:]')
|
|
keepass_entry=$(echo "env/${PWD##*/}" | tr '[:upper:]' '[:lower:]')
|
|
|
|
get-namespace() {
|
|
if kubectl-ctx &> /dev/null; then
|
|
echo $(kubectl-ctx -c 2> /dev/null)
|
|
fi
|
|
}
|
|
namespace="$(get-namespace)"
|
|
|
|
get-context-alias() {
|
|
local alias="$(basename "$(readlink "$HOME/.kube/config" 2>/dev/null)" 2>/dev/null | cut -d '.' -f 2)"
|
|
echo "${alias:-loc}"
|
|
}
|
|
context_alias="$(get-context-alias)"
|
|
|
|
# Import commands
|
|
|
|
clone-taskfile(){
|
|
if [[ ! -d "$HOME/taskfile.build" ]]; then
|
|
echo -e "\033[38;5;214mGit\033[0m: Clone taskfile repo"
|
|
git clone https://git.taskfile.build "$HOME/taskfile.build"
|
|
else
|
|
echo -e "\033[38;5;214mGit\033[0m: Pull taskfile repo"
|
|
git -C "$HOME/taskfile.build" pull
|
|
fi
|
|
}
|
|
|
|
if [[ -d "$HOME/taskfile.build/bin" ]]; then
|
|
for file in "$HOME/taskfile.build/bin/"*; do
|
|
if [[ -f "$file" ]]; then
|
|
source "$file"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Env Commands
|
|
|
|
version() {
|
|
kind version
|
|
helm version
|
|
}
|
|
|
|
info() {
|
|
echo "OpenShift-Project: $OC_PROJECT"
|
|
echo "Context-Alias: $(get-context-alias)"
|
|
echo "Namespace: $namespace"
|
|
}
|
|
|
|
install-vitepress() {
|
|
pnpm install
|
|
}
|
|
|
|
setup-hosts() {
|
|
echo 'Adding entries to /etc/hosts ...'
|
|
# Cluster
|
|
grep -qxF '127.0.0.1 knd.local' /etc/hosts || echo '127.0.0.1 knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Odoo
|
|
grep -qxF '127.0.0.1 odoo.local' /etc/hosts || echo '127.0.0.1 odoo.local' | sudo tee -a /etc/hosts > /dev/null
|
|
grep -qxF '127.0.0.1 odoo.knd.local' /etc/hosts || echo '127.0.0.1 odoo.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
grep -qxF '127.0.0.1 staging.odoo.knd.local' /etc/hosts || echo '127.0.0.1 staging.odoo.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Odoo with different namespace
|
|
grep -qxF '127.0.0.1 restic.local' /etc/hosts || echo '127.0.0.1 restic.local' | sudo tee -a /etc/hosts > /dev/null
|
|
grep -qxF '127.0.0.1 restic.knd.local' /etc/hosts || echo '127.0.0.1 restic.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
grep -qxF '127.0.0.1 staging.restic.knd.local' /etc/hosts || echo '127.0.0.1 staging.restic.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Nextcloud
|
|
grep -qxF '127.0.0.1 nextcloud.local' /etc/hosts || echo '127.0.0.1 nextcloud.local' | sudo tee -a /etc/hosts > /dev/null
|
|
grep -qxF '127.0.0.1 nextcloud.knd.local' /etc/hosts || echo '127.0.0.1 nextcloud.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Collabora
|
|
grep -qxF '127.0.0.1 doc.nextcloud.knd.local' /etc/hosts || echo '127.0.0.1 doc.nextcloud.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Hugo
|
|
grep -qxF '127.0.0.1 hugo.knd.local' /etc/hosts || echo '127.0.0.1 hugo.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
# Vuepress
|
|
grep -qxF '127.0.0.1 vuepress.knd.local' /etc/hosts || echo '127.0.0.1 vuepress.knd.local' | sudo tee -a /etc/hosts > /dev/null
|
|
}
|
|
|
|
test-chart-odoo() {
|
|
if [[ "$1" = 'cleanup' ]]; then
|
|
echo 'Cleanup environment'
|
|
delete-kind
|
|
else
|
|
start-and-prepare
|
|
|
|
update-dependencies odoo
|
|
install-chart odoo test_values/odoo.knd.local.yaml
|
|
kubectl wait --for=condition=Available --timeout=300s deployments/odoo-odoo
|
|
fi
|
|
}
|
|
|
|
test-chart-nextcloud() {
|
|
if [[ "$1" = 'cleanup' ]]; then
|
|
echo 'Cleanup environment'
|
|
delete-kind
|
|
else
|
|
start
|
|
add-repos
|
|
info
|
|
# install-chart haproxy-ingress
|
|
# kubectl wait --namespace haproxy-ingress --for=condition=Available --timeout=60s deployments/haproxy-ingress
|
|
install-chart ingress-nginx
|
|
kubectl wait --namespace ingress-nginx --for=condition=Available --timeout=60s deployments/ingress-nginx-controller
|
|
install-chart cnpg
|
|
kubectl wait --namespace cnpg-system --for=condition=Available --timeout=60s deployments/cnpg-cloudnative-pg
|
|
kubectl create secret generic nextcloud-creds --from-literal=NEXTCLOUD_ADMIN_USER="test" --from-literal=NEXTCLOUD_ADMIN_PASSWORD="test"
|
|
install-chart nextcloud test_values/nextcloud.knd.local.yaml
|
|
fi
|
|
}
|
|
|
|
export-kubeconfig() {
|