Files
oc-k8s/oc-k8s.sh

267 lines
6.8 KiB
Bash
Executable File

#!/bin/bash
REPOS=(
"oc-auth"
"oc-catalog"
"oc-datacenter"
"oc-front"
"oc-monitord"
"oc-peer"
"oc-shared"
"oc-scheduler"
"oc-schedulerd"
"oc-workflow"
"oc-workspace"
)
# values template
main_create_values() {
set -euo pipefail
if [[ -z "${1:-}" ]]; then
echo "Error: No RELEASE PROVIDED."
main_help_values
exit 1
fi
TEMPLATE_FILE=./opencloud/values.yaml.template
ENV_FILE=${2:-}
OUTPUT_FILE="./opencloud/values/$1-values.yaml"
# Load environment variables from env file
if [[ -f "$ENV_FILE" ]]; then
set -a
source "$ENV_FILE"
set +a
fi
# Process the template
awk '
{
line = $0
# match ${VAR:-default} patterns
while (match(line, /\$\{([A-Za-z_][A-Za-z0-9_]*):-([^}]+)\}/, arr)) {
varname = arr[1]
defaultval = arr[2]
# get environment value or default
cmd = "bash -c '\''echo ${" varname ":-" defaultval "}'\''"
cmd | getline value
close(cmd)
line = substr(line, 1, RSTART-1) value substr(line, RSTART+RLENGTH)
}
print line
}' "$TEMPLATE_FILE" > "$OUTPUT_FILE"
echo "Rendered $OUTPUT_FILE from $TEMPLATE_FILE using $ENV_FILE"
}
# HELM SERVICE
main_create_helm() {
main_delete_helm "${1:-dev}" | true
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
helm install ${RELEASE_NAME} opencloud -n ${RELEASE_NAMESPACE} --create-namespace -f opencloud/${RELEASE_NAME}-values.yaml
kind get kubeconfig --name opencloud > ./deployed_config
kind export logs ./kind-logs
}
main_upgrade_helm() {
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
helm upgrade ${RELEASE_NAME} opencloud -n ${RELEASE_NAMESPACE} --create-namespace -f opencloud/${RELEASE_NAME}-values.yaml
}
main_delete_helm() {
RELEASE_NAME=${1:-dev}
RELEASE_NAMESPACE=${1:-dev}
helm uninstall ${RELEASE_NAME} -n ${RELEASE_NAMESPACE}
export KUBECONFIG=$(realpath ~/.kube/config)
}
# CLUSTER SERVICE
build_service() {
local repo_url="https://cloud.o-forge.io/core/$1.git"
local branch=${2:-main}
local target=${3:-all}
local repo_name=$(basename "$repo_url" .git)
echo "Processing repository: $repo_name"
if [ ! -d "$1" ]; then
echo "Cloning repository: $repo_name"
git clone "$repo_url"
if [ $? -ne 0 ]; then
echo "Error cloning $repo_url"
exit 1
fi
fi
echo "Repository '$repo_name' now exists. Pulling latest changes..."
cd "$repo_name" && git checkout $branch && git pull
echo "Running 'make $target' in $repo_name"
export HOST="${2:-http://beta.opencloud.com/}" && make "$target"
if [ $? -ne 0 ]; then
echo "Error: make $target failed in $dir"
exit 1
fi
cd ..
}
main_build_services() {
branch=${1:-main}
target=${2:-all}
cd ..
# Iterate through each repository in the list
for repo in "${REPOS[@]}"; do
build_service "$repo" "$branch" "$target"
done
echo "All repositories processed successfully."
}
# CLUSTER CONTROLLER
main_delete_cluster() {
kind delete cluster --name opencloud | true
}
main_create_cluster() {
main_delete_cluster | true
cat <<EOF | kind create cluster --name opencloud --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 30950
hostPort: 80
protocol: TCP
- containerPort: 30951
hostPort: 443
protocol: TCP
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."locahost:5000"]
endpoint = ["http://dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
endpoint = ["http://dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."dev-docker-registry-ui-registry-server.opencloud.svc.cluster.local:5000".tls]
insecure_skip_verify = true
cert_file = ""
key_file = ""
ca_file = ""
EOF
echo "[WARNING] New cluster shoulw be merged into your current config !"
kind get kubeconfig --name opencloud > /tmp/kind-opencloud.kubeconfig
KUBECONFIG=~/.kube/config:/tmp/kind-opencloud.kubeconfig kubectl config view --flatten > ~/.kube/config # fusionnate clusters config.
kubectl config get-contexts
kubectl config use-context kind-opencloud
}
main_help_values() {
echo "
Cluster commands: oc-k8s <action> values
create - Create a new values release yaml
help - Show this help message
Usage:
oc-k8s create values [release] [env_file (optionnal)]
release - Release values name (required)
env_file - env to map (optionnal)
oc-k8s help values
"
}
main_help_cluster() {
echo "
Cluster commands: oc-k8s <action> cluster
create - Create a new kind cluster named 'opencloud'
delete - Delete the kind cluster named 'opencloud'
help - Show this help message
Usage:
oc-k8s create cluster
oc-k8s delete cluster
oc-k8s help cluster
"
}
main_help_services() {
echo "
Service commands: oc-k8s <action> services
build - Build all opencloud services
help - Show this help message
Usage:
oc-k8s build services [branch] [target]
branch - Git branch to build (default: main)
target - make target (default: all)
oc-k8s help services
"
}
main_help_helm() {
echo "
Helm commands: oc-k8s <action> helm
create - Install a helm release for the given environment (default: dev)
delete - Uninstall a helm release for the given environment (default: dev)
help - Show this help message
Usage:
oc-k8s create helm [env]
dev - environnement selected (default: dev)
oc-k8s upgrade helm [env]
dev - environnement selected (default: dev)
oc-k8s delete helm [env]
dev - environnement selected (default: dev)
oc-k8sh help helm
"
}
main_help_all() {
echo "
Main commands: oc-k8s <action>
start - Start opencloud k8s
stop - Stop opencloud k8s
Usage:
oc-k8s start [args]
oc-k8s stop [args]
"
main_help_cluster
main_help_services
main_help_helm
main_help_values
}
main_start() {
sudo /etc/init.d/apache2 stop
sudo nginx -s stop
main_create_cluster "${@:1}"
main_build_services "${@:1}"
main_create_helm "${@:1}"
}
main_stop() {
main_delete_helm "${@:1}" | true
main_delete_cluster "${@:1}" | true
}
if declare -f main_${1} > /dev/null; then
main_${1} "${@:2}"
elif declare -f main_${1}_${2} > /dev/null; then
main_${1}_${2} "${@:3}"
else
echo "Function does not exist"
main_help_all
fi