Atualizar install.sh

This commit is contained in:
wallaceosmar 2024-11-23 23:25:33 +00:00
parent 7b74b441ff
commit 03c8cf4d07

View File

@ -1,10 +1,15 @@
#!/bin/bash #!/bin/bash
# URLs for configuration files # URLs for configuration files
DOCKER_COMPOSE_URL="https://git.masscivicdynamic.com/wallaceosmar/nexo-cloud-scripts/raw/branch/main/docker-compose.yaml" DOCKER_COMPOSE_URL="https://git.masscivicdynamic.com/masscivicdynamic/nexo-cloud/raw/branch/main/script/docker-compose.yml"
KUBERNETES_DEPLOY_URL="https://git.masscivicdynamic.com/wallaceosmar/nexo-cloud-scripts/raw/branch/main/kube-deploy.yaml" KUBERNETES_DEPLOY_URL="https://git.masscivicdynamic.com/masscivicdynamic/nexo-cloud/raw/branch/main/script/kube-deploy.yaml"
ENV_FILE="/data/nexo-cloud/.env" ENV_FILE="/data/nexo-cloud/.env"
DATA_DIR="/data/nexo-cloud" DATA_DIR="/data/nexo-cloud"
SSL_DIR="$DATA_DIR/ssh"
KUBE_DIR="$DATA_DIR/kubernetes"
SSL_CERT="$SSL_DIR/server.crt"
SSL_KEY="$SSL_DIR/server.key"
KUBECONFIG_FILE="$KUBE_DIR/config"
# Minimum disk space requirements (in GB) # Minimum disk space requirements (in GB)
MIN_TOTAL_DISK_SPACE=30 MIN_TOTAL_DISK_SPACE=30
@ -20,12 +25,25 @@ generate_random_password() {
echo "$(openssl rand -base64 16)" echo "$(openssl rand -base64 16)"
} }
# Function to generate a new APP_KEY # Function to generate an APP_KEY
generate_app_key() { generate_app_key() {
echo "$(openssl rand -base64 32)" echo "$(openssl rand -base64 32)"
} }
# Function to check disk space requirements # Function to generate SSL certificate
generate_ssl_certificate() {
if [ ! -f "$SSL_CERT" ] || [ ! -f "$SSL_KEY" ]; then
echo "Generating SSL certificate..."
mkdir -p "$SSL_DIR"
openssl req -newkey rsa:2048 -nodes -keyout "$SSL_KEY" \
-x509 -days 365 -out "$SSL_CERT" -subj "/CN=NexoCloud"
echo "SSL certificate generated and stored in $SSL_DIR."
else
echo "SSL certificate already exists in $SSL_DIR."
fi
}
# Function to check disk space
check_disk_space() { check_disk_space() {
local total_space=$(df --output=size "$DATA_DIR" | tail -n 1 | awk '{print $1}') local total_space=$(df --output=size "$DATA_DIR" | tail -n 1 | awk '{print $1}')
local available_space=$(df --output=avail "$DATA_DIR" | tail -n 1 | awk '{print $1}') local available_space=$(df --output=avail "$DATA_DIR" | tail -n 1 | awk '{print $1}')
@ -43,37 +61,73 @@ check_disk_space() {
fi fi
} }
# Function to create the folder if it doesn't exist # Function to create the data directory and subdirectories if they don't exist
ensure_data_directory() { ensure_data_directory() {
if [ ! -d "$DATA_DIR" ]; then if [ ! -d "$DATA_DIR" ]; then
echo "Creating directory $DATA_DIR..." echo "Creating data directory at $DATA_DIR..."
mkdir -p "$DATA_DIR" mkdir -p "$DATA_DIR"
mkdir -p "$DATA_DIR/{logs,ssh,applications,databases,services,backups}" fi
echo "Directory $DATA_DIR created successfully."
# Create subdirectories
for subdir in logs ssh applications databases services backups kubernetes; do
if [ ! -d "$DATA_DIR/$subdir" ]; then
echo "Creating subdirectory $subdir in $DATA_DIR..."
mkdir -p "$DATA_DIR/$subdir"
fi
done
echo "Directories created successfully."
}
# Function to copy kubeconfig file if Kubernetes is installed
copy_kubeconfig_file() {
if command_exists kubectl; then
echo "Kubernetes detected. Checking kubeconfig..."
if [ -f "$HOME/.kube/config" ]; then
mkdir -p "$KUBE_DIR"
cp "$HOME/.kube/config" "$KUBECONFIG_FILE"
echo "Kubeconfig file copied to $KUBECONFIG_FILE."
else
echo "Kubeconfig file not found in $HOME/.kube/config."
fi
elif command_exists k3s; then
echo "K3s detected. Checking kubeconfig..."
if [ -f "/etc/rancher/k3s/k3s.yaml" ]; then
mkdir -p "$KUBE_DIR"
cp "/etc/rancher/k3s/k3s.yaml" "$KUBECONFIG_FILE"
echo "Kubeconfig file copied to $KUBECONFIG_FILE."
else
echo "K3s kubeconfig file not found in /etc/rancher/k3s/k3s.yaml."
fi
else
echo "No Kubernetes or K3s installation detected. Skipping kubeconfig copy."
fi fi
} }
# Function to handle the .env file # Function to handle the .env file
handle_env_file() { handle_env_file() {
if [ ! -f "$ENV_FILE" ]; then if [ ! -f "$ENV_FILE" ]; then
echo "No .env file found. Creating a new one." echo "Environment file not found. Creating a new one."
touch "$ENV_FILE" touch "$ENV_FILE"
fi fi
# Generate and append variables APP_KEY=$(generate_app_key)
APP_KEY=base64:$(generate_app_key)
APP_NAME="NexoCloud"
DB_USERNAME="nexo-cloud" DB_USERNAME="nexo-cloud"
DB_PASSWORD=$(generate_random_password) DB_PASSWORD=$(generate_random_password)
REDIS_PASSWORD=$(generate_random_password) REDIS_PASSWORD=$(generate_random_password)
# Set APP_NAME # Ask if user wants to change APP_NAME
if ! grep -q "^APP_NAME=" "$ENV_FILE"; then read -p "Do you want to change the APP_NAME? (y/n) [default: NexoCloud]: " change_app_name
echo "APP_NAME=$APP_NAME" >> "$ENV_FILE" if [ "$change_app_name" == "y" ] || [ "$change_app_name" == "Y" ]; then
echo "Default APP_NAME set to $APP_NAME." read -p "Enter the new APP_NAME: " new_app_name
APP_NAME="$new_app_name"
else
APP_NAME="NexoCloud"
fi fi
# Handle APP_PREVIOUS_KEYS and APP_KEY # Update environment variables
sed -i '/^APP_NAME=/d' "$ENV_FILE"
echo "APP_NAME=$APP_NAME" >> "$ENV_FILE"
if grep -q "^APP_KEY=" "$ENV_FILE"; then if grep -q "^APP_KEY=" "$ENV_FILE"; then
PREVIOUS_KEYS=$(grep "^APP_PREVIOUS_KEYS=" "$ENV_FILE" | cut -d'=' -f2) PREVIOUS_KEYS=$(grep "^APP_PREVIOUS_KEYS=" "$ENV_FILE" | cut -d'=' -f2)
NEW_PREVIOUS_KEYS="${PREVIOUS_KEYS},$(grep "^APP_KEY=" "$ENV_FILE" | cut -d'=' -f2)" NEW_PREVIOUS_KEYS="${PREVIOUS_KEYS},$(grep "^APP_KEY=" "$ENV_FILE" | cut -d'=' -f2)"
@ -85,98 +139,101 @@ handle_env_file() {
sed -i '/^APP_KEY=/d' "$ENV_FILE" sed -i '/^APP_KEY=/d' "$ENV_FILE"
echo "APP_KEY=$APP_KEY" >> "$ENV_FILE" echo "APP_KEY=$APP_KEY" >> "$ENV_FILE"
# Set DB_USERNAME if not present
if ! grep -q "^DB_USERNAME=" "$ENV_FILE"; then if ! grep -q "^DB_USERNAME=" "$ENV_FILE"; then
echo "DB_USERNAME=$DB_USERNAME" >> "$ENV_FILE" echo "DB_USERNAME=$DB_USERNAME" >> "$ENV_FILE"
echo "Default DB_USERNAME set to $DB_USERNAME."
fi fi
# Set DB_PASSWORD if not present
if ! grep -q "^DB_PASSWORD=" "$ENV_FILE"; then if ! grep -q "^DB_PASSWORD=" "$ENV_FILE"; then
echo "DB_PASSWORD=$DB_PASSWORD" >> "$ENV_FILE" echo "DB_PASSWORD=$DB_PASSWORD" >> "$ENV_FILE"
echo "Generated DB_PASSWORD."
fi fi
# Set REDIS_PASSWORD if not present
if ! grep -q "^REDIS_PASSWORD=" "$ENV_FILE"; then if ! grep -q "^REDIS_PASSWORD=" "$ENV_FILE"; then
echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> "$ENV_FILE" echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> "$ENV_FILE"
echo "Generated REDIS_PASSWORD."
fi fi
echo "Environment variables set up in $ENV_FILE." echo "Environment variables configured in $ENV_FILE."
} }
# Function to load the .env file # Function to set up Docker
load_env_file() { setup_docker() {
if [ -f "$ENV_FILE" ]; then docker swarm init
export $(grep -v '^#' "$ENV_FILE" | xargs) curl -o "$DATA_DIR/docker-compose.yml" "$DOCKER_COMPOSE_URL"
echo "Environment variables loaded from $ENV_FILE." docker-compose -f "$DATA_DIR/docker-compose.yml" up -d
else }
echo "No .env file found. Exiting."
exit 1 # Function to deploy Kubernetes application
deploy_kubernetes() {
curl -o "$DATA_DIR/kubernetes/kube-deploy.yaml" "$KUBERNETES_DEPLOY_URL"
kubectl apply -f "$DATA_DIR/kubernetes/kube-deploy.yaml"
}
# Main deployment logic
deploy_application() {
echo "Deploying the application..."
if [ "$1" == "docker" ]; then
setup_docker
elif [ "$1" == "kubernetes" ]; then
deploy_kubernetes
fi fi
echo "Deployment completed."
} }
# Main installation logic # Function to handle installation or restart
install_application() { install_application() {
ensure_data_directory echo "Do you want to:"
check_disk_space echo "1) Install a fresh application"
handle_env_file echo "2) Restart the deployment"
load_env_file read -p "Enter your choice (1-2): " deployment_choice
if command_exists docker; then if [ "$deployment_choice" == "1" ]; then
echo "Docker detected. Proceeding with Docker setup..." ensure_data_directory
curl -o docker-compose.yml $DOCKER_COMPOSE_URL check_disk_space
echo "docker-compose.yml downloaded successfully." handle_env_file
echo "Running Docker Compose with the environment variables..." generate_ssl_certificate
docker compose --env-file "$ENV_FILE" up -d
elif command_exists kubectl || command_exists k3s; then echo "Choose the platform to install or manage:"
echo "K3s or Kubernetes detected. Proceeding with Kubernetes setup..." echo "1) Docker"
curl -o kubernetes-deploy.yaml $KUBERNETES_DEPLOY_URL echo "2) Kubernetes (K8s)"
echo "kubernets-deploy.yaml downloaded successfully." echo "3) K3s (Lightweight Kubernetes)"
echo "Applying Kubernetes deployment..." read -p "Enter your choice (1-3): " platform_choice
kubectl apply -f kubernetes-deploy.yaml
else case $platform_choice in
echo "Neither Docker nor Kubernetes is installed." 1)
read -r -p "Would you like to install Docker or K3s? (docker/k3s): " choice PLATFORM="docker"
case "$choice" in if ! command_exists docker; then
docker) echo "Installing Docker..."
install_docker curl -s https://get.docker.com | sh -s --
curl -o docker-compose.yml $DOCKER_COMPOSE_URL fi
echo "docker-compose.yml downloaded successfully."
echo "Running Docker Compose with the environment variables..."
docker compose --env-file "$ENV_FILE" up -d
;; ;;
k3s) 2)
install_k3s PLATFORM="kubernetes"
curl -o kubernetes-deploy.yaml $KUBERNETES_DEPLOY_URL if ! command_exists kubectl; then
echo "kubernets-deploy.yaml downloaded successfully." echo "Installing Kubernetes..."
echo "Applying Kubernetes deployment..." apt-get update && apt-get install -y kubeadm kubectl kubelet
kubectl apply -f kubernetes-deploy.yaml fi
copy_kubeconfig_file
;;
3)
PLATFORM="kubernetes"
if ! command_exists k3s; then
echo "Installing K3s..."
curl -sfL https://get.k3s.io | sh -
fi
copy_kubeconfig_file
;; ;;
*) *)
echo "Invalid choice. Exiting." echo "Invalid choice. Exiting."
exit 1 exit 1
;; ;;
esac esac
deploy_application "$PLATFORM"
elif [ "$deployment_choice" == "2" ]; then
echo "Restarting deployment..."
deploy_application "$PLATFORM"
else
echo "Invalid choice. Exiting."
exit 1
fi fi
} }
# Function to install Docker
install_docker() {
echo "Docker is not installed. Installing Docker..."
curl -fsSL https://get.docker.com | bash
sudo systemctl start docker
sudo systemctl enable docker
echo "Docker installed successfully."
}
# Function to install K3s
install_k3s() {
echo "K3s is not installed. Installing K3s..."
curl -sfL https://get.k3s.io | sh -
echo "K3s installed successfully."
}
# Run the installation process
install_application install_application