Files
sealos/deploy/cloud/scripts/init.sh
T
2023-10-30 15:10:20 +08:00

212 lines
5.8 KiB
Bash

#!/bin/bash
set -e
cloudDomain="127.0.0.1.nip.io"
cloudPort=""
mongodbUri=""
tlsCrtPlaceholder="<tls-crt-placeholder>"
tlsKeyPlaceholder="<tls-key-placeholder>"
saltKey=""
function prepare {
# source .env
source etc/sealos/.env
# kubectl apply namespace, secret and mongodb
kubectl apply -f manifests/namespace.yaml
# apply notifications crd
kubectl apply -f manifests/notifications_crd.yaml
# gen mongodb uri
gen_mongodbUri
# gen saltKey if not set or not found in secret
gen_saltKey
# mutate desktop config
mutate_desktop_config
# create tls secret
create_tls_secret
}
function gen_mongodbUri() {
# if mongodbUri is empty then create mongodb and gen mongodb uri
if [ -z "$mongodbUri" ]; then
echo "no mongodb uri found, create mongodb and gen mongodb uri"
kubectl apply -f manifests/mongodb.yaml
echo "waiting for mongodb secret generated"
message="Waiting for MongoDB ready"
# if there is no sealos-mongodb-conn-credential secret then wait for mongodb ready
while [ -z "$(kubectl get secret -n sealos sealos-mongodb-conn-credential 2>/dev/null)" ]; do
echo -ne "\r$message \e[K"
sleep 0.5
echo -ne "\r$message . \e[K"
sleep 0.5
echo -ne "\r$message .. \e[K"
sleep 0.5
echo -ne "\r$message ...\e[K"
sleep 0.5
done
echo "mongodb secret has been generated successfully."
chmod +x scripts/gen-mongodb-uri.sh
mongodbUri=$(scripts/gen-mongodb-uri.sh)
fi
}
function gen_saltKey() {
password_salt=$(kubectl get secret desktop-frontend-secret -n sealos -o jsonpath="{.data.password_salt}" 2>/dev/null || true)
if [[ -z "$password_salt" ]]; then
saltKey=$(tr -dc 'a-z0-9' </dev/urandom | head -c64 | base64 -w 0)
else
saltKey=$password_salt
fi
}
function mutate_desktop_config() {
# mutate etc/sealos/desktop-config.yaml by using mongodb uri and two random base64 string
sed -i -e "s;<your-mongodb-uri-base64>;$(echo -n "${mongodbUri}/sealos-auth?authSource=admin" | base64 -w 0);" etc/sealos/desktop-config.yaml
sed -i -e "s;<your-jwt-secret-base64>;$(tr -cd 'a-z0-9' </dev/urandom | head -c64 | base64 -w 0);" etc/sealos/desktop-config.yaml
sed -i -e "s;<your-password-salt-base64>;$saltKey;" etc/sealos/desktop-config.yaml
}
function create_tls_secret {
if grep -q $tlsCrtPlaceholder manifests/tls-secret.yaml; then
echo "mock tls secret"
kubectl apply -f manifests/mock-cert.yaml
echo "mock tls cert has been created successfully."
else
echo "tls secret is already set"
kubectl apply -f manifests/tls-secret.yaml
fi
}
function sealos_run_controller {
# run user controller
sealos run tars/user.tar \
--env cloudDomain="$cloudDomain" \
--env apiserverPort="6443"
# run terminal controller
sealos run tars/terminal.tar \
--env cloudDomain="$cloudDomain" \
--env cloudPort="$cloudPort" \
--env userNamespace="user-system" \
--env wildcardCertSecretName="wildcard-cert" \
--env wildcardCertSecretNamespace="sealos-system"
# run app controller
sealos run tars/app.tar
# run resources monitoring controller
sealos run tars/monitoring.tar \
--env MONGO_URI="$mongodbUri" --env DEFAULT_NAMESPACE="resources-system"
# run account controller
sealos run tars/account.tar \
--env MONGO_URI="$mongodbUri" \
--env cloudDomain="$cloudDomain" \
--env cloudPort="$cloudPort" \
--env DEFAULT_NAMESPACE="account-system"
# run license controller
sealos run tars/license.tar \
--env MONGO_URI="$mongodbUri"
}
function sealos_authorize {
sealos run tars/job-init.tar
# wait for admin user create
echo "Waiting for admin user create"
while [ -z "$(kubectl get ns ns-admin 2>/dev/null)" ]; do
sleep 1
done
kubectl apply -f manifests/free-license.yaml
}
function sealos_run_frontend {
echo "run desktop frontend"
sealos run tars/frontend-desktop.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert" \
--env passwordEnabled="true" \
--config-file etc/sealos/desktop-config.yaml
# sealos authorize !!must run after sealos_run_controller frontend-desktop.tar and before sealos_run_frontend
sealos_authorize
echo "run applaunchpad frontend"
sealos run tars/frontend-applaunchpad.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert"
echo "run terminal frontend"
sealos run tars/frontend-terminal.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert"
echo "run dbprovider frontend"
sealos run tars/frontend-dbprovider.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert"
echo "run cost center frontend"
sealos run tars/frontend-costcenter.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert" \
--env transferEnabled="true" \
--env rechargeEnabled="false"
echo "run template frontend"
sealos run tars/frontend-template.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert"
echo "run license frontend"
sealos run tars/frontend-license.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert" \
--env licensePurchaseDomain="license.sealos.io"
echo "run cronjob frontend"
sealos run tars/frontend-cronjob.tar \
--env cloudDomain=$cloudDomain \
--env cloudPort="$cloudPort" \
--env certSecretName="wildcard-cert"
echo "run db monitoring"
sealos run tars/database-service.tar
}
function resource_exists {
kubectl get "$1" >/dev/null 2>&1
}
function install {
# gen mongodb uri and others
prepare
# sealos run controllers
sealos_run_controller
# sealos run frontends
sealos_run_frontend
}
install