Files
Kubernetes-Build/task
T

471 lines
14 KiB
Bash
Executable File

#!/bin/bash
set -e
if [[ -a ".env" ]]; then
source .env
fi
function help() {
echo
echo "task <command> [options]"
echo
echo "commands:"
echo
# Define column widths
CMD_WIDTH=26
OPT_WIDTH=14
DESC_WIDTH=50
COLUMN="| %-${CMD_WIDTH}s | %-${OPT_WIDTH}s | %-${DESC_WIDTH}s |\n"
# Print table header
printf "$COLUMN" "Command" "Option" "Description"
echo "|$(printf '%*s' $((CMD_WIDTH + 2)) '' | tr ' ' '-')|$(printf '%*s' $((OPT_WIDTH + 2)) '' | tr ' ' '-')|$(printf '%*s' $((DESC_WIDTH + 2)) '' | tr ' ' '-')|"
# Env
printf "$COLUMN" "version" "" "Show version of required tools."
printf "$COLUMN" "test-k8s" "[clean]" "Test Kubernetes environment."
# Docs
printf "$COLUMN" "install" "" "Install node dependencies."
printf "$COLUMN" "dev-vitepress" "" "Start VitePress dev server."
printf "$COLUMN" "build-vitepress" "" "Build site with VitePress."
printf "$COLUMN" "preview-vitepress" "" "Server VitePress build."
# Host
printf "$COLUMN" "start-kind" "" "Start kind Kubernetes cluster."
printf "$COLUMN" "delete-kind" "" "Delete kind Kubernetes cluster."
printf "$COLUMN" "start-minikube" "" "Start minikube Kubernetes cluster."
printf "$COLUMN" "add-repos" "" "Add the selected repos."
printf "$COLUMN" "setup-hosts" "" "Update hosts file."
printf "$COLUMN" "forward-odoo" "" "Forward odoo port."
printf "$COLUMN" "forward-ingress-nginx" "" "Forward ingress-nginx port."
printf "$COLUMN" "forward-haproxy-ingress" "" "Forward haproxy-ingress port."
# Chart, Release and Forwarding
printf "$COLUMN" "install-chart-odoo" "" "Install Odoo Chart."
printf "$COLUMN" "install-chart-postgres" "" "Install Postgres Chart."
printf "$COLUMN" "install-chart-vshnpostgres" "" "Install vshnPostgres Chart."
printf "$COLUMN" "install-chart-odoo-appuio" "" "Install Odoo Chart to APPUiO."
printf "$COLUMN" "upgrade-chart-odoo" "" "Upgrade Odoo Chart release."
printf "$COLUMN" "upgrade-chart-postgres" "" "Upgrade Postgres Chart release."
printf "$COLUMN" "upgrade-chart-vshnpostgres" "" "Upgrade Postgres Chart release."
printf "$COLUMN" "upgrade-chart-odoo-appuio" "" "Upgrade Odoo Chart release for AppUiO."
printf "$COLUMN" "uninstall-release" "" "Uninstall Odoo Helm release."
printf "$COLUMN" "install-ingress-nginx" "" "Install ingress-nginx controller."
printf "$COLUMN" "install-haproxy-ingress" "" "Install haproxy ingress controller."
printf "$COLUMN" "uninstall-ingress-nginx" "" "Install ingress-nginx controller."
printf "$COLUMN" "uninstall-haproxy-ingress" "" "Install haproxy ingress controller."
# kubectl
printf "$COLUMN" "logs-odoo" "" "Show pod logs."
printf "$COLUMN" "logs-haproxy-ingress" "" "Show haproxy-ingress logs."
printf "$COLUMN" "logs-ingress-nginx" "" "Show ingress-nginx logs."
printf "$COLUMN" "list" "" "List Helm deployments."
printf "$COLUMN" "list-pods" "" "List all pods."
printf "$COLUMN" "login-oc" "" "Refresh the auth token for OpenShift."
# Helm
printf "$COLUMN" "template" "" "Template all charts with Helm."
printf "$COLUMN" "template-appuio" "" "Template all charts for APPUiO."
printf "$COLUMN" "package-repo" "" "Package Helm Charts."
printf "$COLUMN" "index-repo" "" "Index Helm Chart."
printf "$COLUMN" "publish-repo" "" "Copy Helm Chart files to dist."
printf "$COLUMN" "remove-repo" "" "Remove the kubernetes-build repo."
printf "$COLUMN" "install-repo" "[name]" "Install Chart from kubernetes-build repo."
# LLM
printf "$COLUMN" "commit-with-llm" "" "Commit with llm generated commit message."
printf "$COLUMN" "update-with-llm" "[glob][prompt]" "Feed files matching glob to LLM and apply changes."
}
# Default vars
OC_PROJECT=${OC_PROJECT:="odoo"}
NAMESPACE=${NAMESPACE:="odoo"}
RELEASE_NAME=${RELEASE_NAME:="odoo"}
# Import commands
[ -f ~/taskfile.build/bin/commit-with-llm ] && source ~/taskfile.build/bin/commit-with-llm
[ -f ~/taskfile.build/bin/update-with-llm ] && source ~/taskfile.build/bin/update-with-llm
# Env Commands
function version() {
minikube version
kind version
helm version
}
function install() {
pnpm install
}
function setup-hosts() {
echo "Adding entries to /etc/hosts ..."
grep -qxF '127.0.0.1 cloud.local' /etc/hosts || echo "127.0.0.1 cloud.local" | sudo tee -a /etc/hosts > /dev/null
grep -qxF "127.0.0.1 $NAMESPACE.cloud.local" /etc/hosts || echo "127.0.0.1 $NAMESPACE.cloud.local" | sudo tee -a /etc/hosts > /dev/null
grep -qxF "127.0.0.1 restic.cloud.local" /etc/hosts || echo "127.0.0.1 restic.cloud.local" | sudo tee -a /etc/hosts > /dev/null
}
function test-k8s() {
if [ "$1" = "clean" ]; then
echo "Cleanup environment"
delete-kind
else
start-kind
add-repos
install-chart-odoo
install-ingress-nginx
fi
}
# Docs Commands
function dev-vitepress() {
cp README.md index.md
pnpm run docs:dev
}
function build-vitepress() {
cp README.md index.md
pnpm run docs:build
}
function preview-vitepress() {
pnpm run docs:preview
}
# Host Commands
function start-kind() {
echo "Start kind."
kind create cluster
}
function delete-kind() {
echo "Delete kind."
kind delete cluster
}
function start-minikube() {
echo "Start minikube."
minikube start
}
function forward-odoo() {
PORT=8080
echo "Open http://localhost:$PORT url in your browser."
kubectl port-forward service/odoo $PORT:8069
}
function forward-ingress-nginx() {
PORT=8080
echo "Open http://odoo.cloud.local:$PORT url in your browser."
kubectl port-forward -n ingress-nginx svc/ingress-nginx-controller $PORT:80
}
function forward-haproxy-ingress() {
PORT=8080
echo "Open http://odoo.cloud.local:$PORT url in your browser."
kubectl port-forward -n haproxy-ingress svc/haproxy-ingress $PORT:80
}
# Helm, Release Commands
function install-chart-postgres() {
echo "Install chart postgres as release $RELEASE_NAME."
helm install "$RELEASE_NAME" postgres \
--set postgres.client.enabled=true
}
function install-chart-odoo() {
echo "Install dependencies."
helm dependency build odoo
echo "Install odoo chart as release $RELEASE_NAME."
helm install "$RELEASE_NAME" odoo
sleep 8
echo "Get pods with name $1:"
kubectl get pods -o=name --all-namespaces | grep "odoo"
POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "odoo")
echo -e "\nWait for $POD_ID pods to be running:"
kubectl wait --for=condition=Ready "$POD_ID" --timeout=120s
}
function install-chart-vshnpostgres() {
echo "Set oc project to $OC_PROJECT."
oc project "$OC_PROJECT"
echo "Install vshnPostgres chart as release odoo with APPUiO-specific values."
helm install "$RELEASE_NAME" vshnPostgres \
--set vshnPostgres.enabled=true \
--set vshnPostgres.client.enabled=true
}
function install-chart-odoo-appuio() {
echo "Set oc project to $OC_PROJECT."
oc project "$OC_PROJECT"
echo "Install dependencies."
helm dependency build odoo
echo "Install odoo chart as release $RELEASE_NAME with APPUiO-specific values."
helm install "$RELEASE_NAME" odoo \
--set ingress.host="$NAMESPACE.exo.mintcloud.ch" \
--set ingress.className=haproxy \
--set ingress.secure=true \
--set vshnPostgres.enabled=true \
--set vshnPostgres.client.enabled=true \
--set postgres.enabled=false
}
function upgrade-chart-odoo() {
echo "Update dependencies."
helm dependency update odoo
echo "Upgrade odoo chart for release $RELEASE_NAME."
helm upgrade "$RELEASE_NAME" odoo
}
function upgrade-chart-postgres() {
echo "Upgrade postgres chart for release $RELEASE_NAME."
helm upgrade "$RELEASE_NAME" postgres
}
function upgrade-chart-vshnpostgres() {
echo "Upgrade vshnPostgres chart for release $RELEASE_NAME."
helm upgrade "$RELEASE_NAME" vshnPostgres \
--set vshnPostgres.enabled=true \
--set vshnPostgres.client.enabled=true
}
function upgrade-chart-odoo-appuio() {
echo "Upgrade odoo chart for release $RELEASE_NAME with APPUiO-specific values."
helm upgrade "$RELEASE_NAME" odoo \
--set ingress.host="$NAMESPACE.exo.mintcloud.ch" \
--set ingress.className=haproxy \
--set ingress.secure=true \
--set vshnPostgres.enabled=true \
--set vshnPostgres.client.enabled=true \
--set postgres.enabled=false
}
function install-ingress-nginx() {
helm install ingress-nginx ingress-nginx/ingress-nginx \
--create-namespace --namespace ingress-nginx
}
function install-haproxy-ingress() {
kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: haproxy
spec:
controller: haproxy-ingress.github.io/controller
EOF
helm install haproxy-ingress haproxy-ingress/haproxy-ingress \
--create-namespace --namespace haproxy-ingress
}
function uninstall-ingress-nginx() {
helm uninstall ingress-nginx --namespace ingress-nginx
}
function uninstall-haproxy-ingress() {
helm uninstall haproxy-ingress --namespace haproxy-ingress
}
function uninstall-release() {
echo "Uninstall release $RELEASE_NAME."
helm uninstall "$RELEASE_NAME"
}
# kubectl Commands
function logs-odoo() {
POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "odoo")
kubectl logs "$POD_ID"
}
function list-pods() {
kubectl get pods
}
function logs-haproxy-ingress() {
POD_ID=$(kubectl get pods -o=name -n haproxy-ingress | grep "haproxy-ingress")
kubectl logs "$POD_ID" -n haproxy-ingress
}
function logs-ingress-nginx() {
POD_ID=$(kubectl get pods -o=name -n ingress-nginx | grep "ingress-nginx")
kubectl logs "$POD_ID" -n ingress-nginx
}
# Helm Commands
function template() {
echo "Template charts into manifest.yaml."
helm template "$RELEASE_NAME" odoo > manifest.yaml
}
function template-appuio() {
echo "Template charts into manifest.yaml."
helm template "$RELEASE_NAME" odoo \
--set ingress.host=$NAMESPACE.exo.mintcloud.ch \
--set ingress.className=haproxy \
--set ingress.secure=true \
--set vshnPostgres.enabled=true \
--set vshnPostgres.client.enabled=true \
--set postgres.enabled=false > manifest.yaml
}
function list() {
helm list
}
function package-repo() {
echo "Package Helm Charts."
index-repo
helm package postgres
helm package vshnPostgres
cp postgres-0.1.0.tgz odoo/charts/
cp vshnPostgres-0.1.0.tgz odoo/charts/
helm package odoo
}
function index-repo() {
echo "Update index.yaml"
helm repo index .
}
function publish-repo() {
build-vitepress
cp *.tgz .vitepress/dist
cp index.yaml .vitepress/dist
}
function add-repos(){
echo "Add the kubernetes-build repo."
helm repo add kubernetes-build https://kubernetes.build
echo "Add the ingress-nginx repo."
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
echo "Add the haproxy-ingress repo."
helm repo add haproxy-ingress https://haproxy-ingress.github.io/charts
}
function remove-repo(){
echo "Remove the kubernetes-build repo."
helm repo remove kubernetes-build
}
function install-repo(){
echo "Install release odoo from kubernetes-build repo."
helm install odoo kubernetes-build/odoo
}
# LLM Commands
function update-with-llm() {
if test -z "$1"; then
echo "\$1 is empty. Usage:";
help | grep "${FUNCNAME[0]}"
exit 1;
fi
# Get files from path
if [ -f "$1" ]; then
FILES="$1"
elif [ -d "$1" ]; then
FILES=$(find "$1" -type f \( -name "*.md" -o -name "*.yml" -o -name "*.yaml" -o -name "*.conf" -o -name "*.nginx" \) \
| grep -vFf <(git -C "$1" ls-files --ignored --exclude-standard --others))
else
echo "$1 is neither a file nor a directory."
exit 1
fi
echo -e "Loaded these files into prompt:\n\n$FILES\n"
# Prompt task description
if [ -z "$2" ]; then
read -p "Enter the task description: " TASK_DESCRIPTION
else
TASK_DESCRIPTION="$2"
fi
# Prepare the files content for prompt
FILE_CONTENTS=""
for FILE in $FILES; do
FILE_CONTENTS+="<<<$FILE>>>
$(cat "$FILE")
"
done
# Define prompt content
mkdir -p tmp
PROMPT_FILE="tmp/llm_update"
echo -e "\nWrite prompt to $PROMPT_FILE."
cat << EOF > "$PROMPT_FILE"
Look at the code files below and do the following:
$TASK_DESCRIPTION
Output all files that you need to change in full again, including your changes.
In the same format as I provide the files below. Under no circumstances output any other text,
no additional infos, no code formatting chars. Only the code in the given format.
Here are the files:
$FILE_CONTENTS
EOF
# Run the llm command
echo -e "Send prompt and wait for the response of the $LLM_MODEL LLM."
RESULT=$(cat "$PROMPT_FILE" | llm -m "$LLM_MODEL")
# Check if result is empty
if [ -z "$RESULT" ]; then
echo "No response from the model. Exiting."
exit 1
fi
# Save the result to a file
RESULT_FILE="tmp/llm_update_result"
echo "$RESULT" > "$RESULT_FILE"
echo -e "Saved response to $RESULT_FILE.\n"
# Show a preview of result file
less "$RESULT_FILE"
# Ask for confirmation before updating files
read -p "Do you want to apply these updates to the files? (y/n): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
exit 0
fi
# Parse the response from the file and update the files directly
echo -e "Parsing the response and updating files...\n"
CURRENT_FILE=""
while IFS= read -r LINE; do
if [[ $LINE =~ ^"<<<"(.*)">>>" ]]; then
CURRENT_FILE="${BASH_REMATCH[1]}"
echo "Update file $CURRENT_FILE."
> "$CURRENT_FILE"
elif [[ -n $CURRENT_FILE ]]; then
echo "$LINE" >> "$CURRENT_FILE"
fi
done < "$RESULT_FILE"
}
if declare -f "$1" > /dev/null; then
"$1" "${@:2}"
else
case "$1" in
*)
echo "Unknown command: $1"
help
exit 1
;;
esac
fi