Files
Kubernetes-Build/task
T
2025-03-13 17:02:26 +01:00

232 lines
5.8 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=20
OPT_WIDTH=14
DESC_WIDTH=36
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" "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" "apply-odoo" "" "Apply the Odoo K8s manifests."
printf "$COLUMN" "forward-odoo" "" "Forward Odoo port."
printf "$COLUMN" "test-k8s" "" "Test Kubernetes environment."
printf "$COLUMN" "install" "" "Setup the local environment."
printf "$COLUMN" "version" "" "Show version of required tools."
printf "$COLUMN" "update-with-llm" "[path][prompt]" "Update files with LLM prompt."
printf "$COLUMN" "package" "" "Package Helm Chart."
printf "$COLUMN" "index" "" "Index Helm Chart."
printf "$COLUMN" "publish" "" "Copy Helm Chart packages and repo index."
echo
}
# Env Commands
function version() {
minikube version
kind version
helm version
}
function install() {
pnpm install
}
# 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 apply-odoo() {
echo "Install release odoo with Helm."
helm install odoo .
echo -e "\nWait until resources are ready."
sleep 8
ODOO_POD=$(kubectl get pods -o=name --all-namespaces | grep odoo)
POSTGRES_POD=$(kubectl get pods -o=name --all-namespaces | grep postgres)
echo -e "\nWait for $ODOO_POD and $POSTGRES_POD pods to be running:"
kubectl wait --for=condition=Ready "$ODOO_POD" "$POSTGRES_POD" --timeout=120s
}
function test-k8s() {
start-kind
apply-odoo
}
function forward-odoo() {
echo "Open http://localhost:8069 url in your browser."
kubectl port-forward service/odoo 8069:8069
}
# Helm Commands
function package() {
echo "Package Helm Chart."
helm package .
}
function index() {
echo "Update index.yaml"
helm repo index .
}
function publish() {
build-vitepress
cp *.tgz .vitepress/dist
cp index.yaml .vitepress/dist
}
# 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