Files
Kubernetes-Build/task
T

388 lines
10 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=42
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 ' ' '-')|"
# Print table rows
printf "$COLUMN" "version" "" "Show version of required tools."
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."
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" "install-chart" "[name]" "Install Helm Chart release."
printf "$COLUMN" "upgrade-chart" "[name]" "Upgrade Helm Chart release."
printf "$COLUMN" "uninstall-release" "[name]" "Uninstall Helm release."
printf "$COLUMN" "forward-odoo" "" "Forward odoo port."
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."
printf "$COLUMN" "forward-ingress-nginx" "" "Forward ingress-nginx port."
printf "$COLUMN" "forward-haproxy-ingress" "" "Forward haproxy-ingress port."
printf "$COLUMN" "logs-haproxy-ingress" "" "Show haproxy-ingress logs."
printf "$COLUMN" "test-k8s" "[clean]" "Test Kubernetes environment."
printf "$COLUMN" "template" "" "Template all charts with Helm."
printf "$COLUMN" "logs" "" "Show pod logs."
printf "$COLUMN" "list" "" "List Helm deployments."
printf "$COLUMN" "list-pods" "" "List all pods."
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."
printf "$COLUMN" "commit-with-llm" "" "Commit with llm generated commit message."
printf "$COLUMN" "update-with-llm" "[path][prompt]" "Update files with LLM prompt."
echo
}
# Import commands
[ -f ~/taskfile.build/bin/commit-with-llm ] && source ~/taskfile.build/bin/commit-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 odoo.cloud.local' /etc/hosts || echo "127.0.0.1 odoo.cloud.local" | sudo tee -a /etc/hosts > /dev/null
}
# Docs Commands
function dev-vitepress() {
pnpm run docs:dev
}
function build-vitepress() {
pnpm run docs:build
}
function preview-vitepress() {
pnpm run docs:preview
}
# Kubernetes 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 install-chart() {
if test -z "$1"; then
echo "\$1 is empty."
exit 1;
fi
echo "Install dependencies."
helm dependency build "$1"
echo "Install chart $1 as release $1."
helm install "$1" "$1"
sleep 8
echo "Get pods with name $1:"
kubectl get pods -o=name --all-namespaces | grep "$1"
POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "$1")
echo -e "\nWait for $POD_ID pods to be running:"
kubectl wait --for=condition=Ready "$POD_ID" --timeout=120s
}
function upgrade-chart() {
if test -z "$1"; then
echo "\$1 is empty."
exit 1;
fi
echo "Upgrade release $1 with helm."
helm upgrade "$1" "$1"
}
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: ingress.haproxy.org/ingress-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() {
if test -z "$1"; then
echo "\$1 is empty."
exit 1;
fi
echo "Uninstall release $1."
helm uninstall "$1"
}
function test-k8s() {
if [ "$1" = "clean" ]; then
echo "Cleanup environment"
delete-kind
else
start-kind
add-repo
install-chart odoo
fi
}
# Kubectl Commands
function logs() {
POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "odoo")
kubectl logs "$POD_ID"
}
function list-pods() {
kubectl get pods
}
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
}
function logs-haproxy-ingress() {
POD_ID=$(kubectl get pods -o=name -n haproxy-ingress | grep "haproxy-ingress")
kubectl logs "$POD_ID" -n haproxy-ingress
}
# Helm Repo Commands
function template() {
echo "Template charts into manifest.yaml."
helm template odoo odoo > manifest.yaml
helm template postgres postgres >> manifest.yaml
}
function list() {
helm list
}
function package-repo() {
echo "Package Helm Charts."
helm package postgres
cp postgres-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