Skip to content

Commit

Permalink
optimize cloud deploy scripts (#4070)
Browse files Browse the repository at this point in the history
* optimize cloud deploy scripts

* optimize cloud deploy scripts

* optimize wait mongo ready prompt
  • Loading branch information
bxy4543 authored Oct 11, 2023
1 parent c79c1b2 commit 15a4828
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 35 deletions.
1 change: 0 additions & 1 deletion deploy/cloud/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cl
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-terminal-frontend:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-applaunchpad-frontend:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-dbprovider-frontend:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-resources-metering-controller:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-resources-controller:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-account-controller:latest
sealos pull --policy=always --platform=linux/"${ARCH}" ghcr.io/labring/sealos-cloud-costcenter-frontend:latest
Expand Down
17 changes: 11 additions & 6 deletions deploy/cloud/scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ function gen_mongodbUri() {
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)" ]; do
sleep 3
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
Expand Down Expand Up @@ -95,10 +104,6 @@ function sealos_run_controller {
sealos run tars/monitoring.tar \
--env MONGO_URI="$mongodbUri" --env DEFAULT_NAMESPACE="resources-system"

# run resources metering controller
sealos run tars/metering.tar \
--env MONGO_URI="$mongodbUri" --env DEFAULT_NAMESPACE="resources-system"

# run account controller
sealos run tars/account.tar \
--env MONGO_URI="$mongodbUri" \
Expand Down
166 changes: 138 additions & 28 deletions scripts/cloud/install.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,99 @@
#!/bin/bash


set -e

# Configurations
CLOUD_DIR="/root/.sealos/cloud"
SEALOS_VERSION="v4.3.3"
# TODO add support for multiple cloud versions
SEALOS_VERSION="v4.3.4"
CLOUD_VERSION="v0.0.1"

# Define English and Chinese prompts
declare -A PROMPTS_EN PROMPTS_CN

PROMPTS_EN=(
["install_sealos"]="Sealos CLI is not installed. Do you want to install it now? (y/n): "
["input_master_ips"]="Please enter Master IPs (comma separated, at least one required): "
["invalid_ips"]="Invalid IPs or no IPs provided. Please try again."
["input_node_ips"]="Please enter Node IPs (comma separated, leave empty if none): "
["pod_subnet"]="Please enter pod subnet (default: 100.64.0.0/10): "
["service_subnet"]="Please enter service subnet (default: 10.96.0.0/22): "
["cloud_domain"]="Please enter cloud domain: "
["cloud_port"]="Please enter cloud port (default: 443): "
["input_certificate"]="Do you want to input a certificate? (y/n): "
["certificate_path"]="Please input the certificate path: "
["private_key_path"]="Please input the private key path: "
["choose_language"]="Choose language / 选择语言:"
["enter_choice"]="Enter your choice (1/2): "
["k8s_installation"]="Installing Kubernetes cluster."
["ingress_installation"]="Installing ingress-nginx-controller and kubeblocks."
["patching_ingress"]="Patching ingress-nginx-controller tolerations to allow it to run on master node. If you don't want it to run on master node, please skip this step."
["installing_cloud"]="Installing sealos cloud."
["avx_not_supported"]="CPU does not support AVX instructions"
["ssh_private_key"]="Please configure the ssh private key path, press Enter to use the default value '/root/.ssh/id_rsa' "
["ssh_password"]="Please enter the ssh password, press Enter to skip\n"
)

PROMPTS_CN=(
["install_sealos"]="Sealos CLI没有安装,是否安装?(y/n): "
["input_master_ips"]="请输入Master IPs (多个master节点使用逗号分隔, 例:192.168.0.1,192.168.0.2,192.168.0.3) \n"
["invalid_ips"]="IP无效或没有提供IP,请再试一次。"
["input_node_ips"]="请输入Node IPs (多个node节点使用逗号分隔,可跳过): "
["pod_subnet"]="请输入pod子网 (回车使用默认值: 100.64.0.0/10): "
["service_subnet"]="请输入service子网 (回车使用默认值: 10.96.0.0/22): "
["cloud_domain"]="请输入云域名:(例:127.0.0.1.nip.io) \n "
["cloud_port"]="请输入云端口 (回车使用默认值: 443): "
["input_certificate"]="您要输入证书吗?(y/n): "
["certificate_path"]="请输入证书路径: "
["private_key_path"]="请输入私钥路径: "
["choose_language"]="选择语言:"
["enter_choice"]="请输入您的选择 (1/2): "
["k8s_installation"]="正在安装Kubernetes集群。"
["ingress_installation"]="正在安装ingress-nginx-controller和kubeblocks。"
["patching_ingress"]="正在修改ingress-nginx-controller的容忍度,以允许它在主节点上运行。如果您不希望它在主节点上运行,请跳过此步骤。"
["installing_cloud"]="正在安装sealos cloud。"
["avx_not_supported"]="CPU不支持AVX指令"
["ssh_private_key"]="如需免密登录请配置ssh私钥路径,回车使用默认值'/root/.ssh/id_rsa' "
["ssh_password"]="请输入ssh密码,配置免密登录可回车跳过\n"
)

# Choose Language
get_prompt() {
local key="$1"
if [[ $LANGUAGE == "CN" ]]; then
echo -e "${PROMPTS_CN[$key]}"
else
echo -e "${PROMPTS_EN[$key]}"
fi
}

get_prompt "choose_language"
echo "1. English"
echo "2. 中文"
read -p "$(get_prompt "enter_choice")" lang_choice

if [[ $lang_choice == "2" ]]; then
LANGUAGE="CN"
else
LANGUAGE="EN"
fi

#TODO check if CPU supports AVX instructions
#precheck() {
# cat /proc/cpuinfo | grep avx
# if [ $? -ne 0 ]; then
# get_prompt "avx_not_supported"
# exit 1
# fi
#}

# Initialization
init() {
mkdir -p $CLOUD_DIR

# Check for sealos CLI
if ! command -v sealos &> /dev/null; then
echo "Sealos CLI is not installed."
read -p "Do you want to install it now? (y/n): " installChoice
get_prompt "install_sealos"
read -p " " installChoice
if [[ $installChoice == "y" || $installChoice == "Y" ]]; then
curl -sfL https://raw.githubusercontent.com/labring/sealos/${SEALOS_VERSION}/scripts/install.sh |
sh -s ${SEALOS_VERSION} labring/sealos
Expand All @@ -28,29 +106,51 @@ init() {
fi
}

# Gather user input
collect_input() {
# Utility function to validate IP address
validate_ips() {
local ips="$1"
for ip in $(echo "$ips" | tr ',' ' '); do
if ! [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
return 1
fi
done
return 0
}

# Master and Node IPs
read -p "Please enter Master IPs (comma separated, at least one required): " masterIps
while [[ -z "$masterIps" ]]; do
read -p "At least one Master IP is required. Please try again: " masterIps
while :; do
read -p "$(get_prompt "input_master_ips")" masterIps
if validate_ips "$masterIps" && [[ ! -z "$masterIps" ]]; then
break
else
get_prompt "invalid_ips"
fi
done
read -p "Please enter Node IPs (comma separated, leave empty if none): " nodeIps

# Cluster settings
read -p "Please enter pod subnet (default: 100.64.0.0/10): " podCidr
read -p "Please enter service subnet (default: 10.96.0.0/22): " serviceCidr
read -p "Please enter cloud domain: " cloudDomain

# Certificate handling
read -p "Do you want to input a certificate? (y/n): " inputCert
while :; do
read -p "$(get_prompt "input_node_ips")" nodeIps
if validate_ips "$nodeIps"; then
break
else
get_prompt "invalid_ips"
fi
done
read -p "$(get_prompt "ssh_private_key")" ssh_private_key
if [[ -z "$ssh_private_key" ]]; then
ssh_private_key="${HOME}/.ssh/id_rsa"
fi
read -p "$(get_prompt "ssh_password")" ssh_password
read -p "$(get_prompt "pod_subnet")" podCidr
read -p "$(get_prompt "service_subnet")" serviceCidr
read -p "$(get_prompt "cloud_domain")" cloudDomain
read -p "$(get_prompt "cloud_port")" cloudPort
read -p "$(get_prompt "input_certificate")" inputCert
if [[ $inputCert == "y" || $inputCert == "Y" ]]; then
read -p "Please input the certificate path: " certPath
read -p "Please input the private key path: " keyPath
read -p "$(get_prompt "certificate_path")" certPath
read -p "$(get_prompt "private_key_path")" keyPath
fi
}

# Prepare configurations
prepare_configs() {
if [[ $inputCert == "y" || $inputCert == "Y" ]]; then
# Convert certificate and key to base64
Expand Down Expand Up @@ -100,7 +200,10 @@ spec:
labring/cilium:v1.12.14\
labring/cert-manager:v1.8.0\
labring/openebs:v3.4.0\
--masters $masterIps"
--masters $masterIps\
--pk=${ssh_private_key}\
--passwd=${ssh_password}\
"

if [ -n "$nodeIps" ]; then
sealos_gen_cmd+=" --nodes $nodeIps"
Expand All @@ -113,28 +216,29 @@ spec:
sed -i "s|10.96.0.0/22|${serviceCidr:-10.96.0.0/22}|g" $CLOUD_DIR/Clusterfile
}

# Execute commands based on collected input and prepared configs
execute_commands() {
echo "Installing Kubernetes cluster."
get_prompt "k8s_installation"
sealos apply -f $CLOUD_DIR/Clusterfile

echo "Installing ingress-nginx-controller and kubeblocks."
get_prompt "ingress_installation"
sealos run docker.io/labring/kubernetes-reflector:v7.0.151\
docker.io/labring/ingress-nginx:v1.5.1\
docker.io/labring/kubeblocks:v0.6.2\
--config-file $CLOUD_DIR/ingress-nginx-config.yaml

echo "Patching ingress-nginx-controller tolerations to allow it to run on master node. If you don't want it to run on master node, please skip this step."
get_prompt "patching_ingress"
kubectl -n ingress-nginx patch ds ingress-nginx-controller -p '{"spec":{"template":{"spec":{"tolerations":[{"key":"node-role.kubernetes.io/control-plane","operator":"Exists","effect":"NoSchedule"}]}}}}'

echo "Installing sealos cloud."
get_prompt "installing_cloud"
if [[ $inputCert == "y" || $inputCert == "Y" ]]; then
sealos run docker.io/labring/sealos-cloud:latest\
--env cloudDomain="$cloudDomain"\
--env cloudPort="${cloudPort:-443}"\
--config-file $CLOUD_DIR/tls-secret.yaml
else
sealos run docker.io/labring/sealos-cloud:latest\
--env cloudDomain="$cloudDomain"
--env cloudDomain="$cloudDomain"\
--env cloudPort="${cloudPort:-443}"
fi
}

Expand All @@ -143,3 +247,9 @@ init
collect_input
prepare_configs
execute_commands

GREEN='\033[0;32m'
BOLD='\033[1m'
RESET='\033[0m'

echo -e "${BOLD}Sealos cloud login info:${RESET}\nCloud Version: ${GREEN}${CLOUD_VERSION}${RESET}\nURL: ${GREEN}https://$cloudDomain:$cloudPort${RESET}\nadmin Username: ${GREEN}admin${RESET}\nadmin Password: ${GREEN}sealos2023${RESET}"

0 comments on commit 15a4828

Please sign in to comment.