Files
drb-core-client/install.sh
2025-07-13 01:03:58 -04:00

197 lines
7.1 KiB
Bash

#!/bin/bash
# Define ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m' # Added yellow for warnings/info
NC='\033[0m' # No Color - resets text to default color
install_prereqs() {
if id -Gn $(whoami) | grep -qw docker; then
echo -e "${GREEN}Prerequisites Already Installed${NC}"
else
echo -e "${GREEN}Installing Prerequisites...${NC}"
sudo apt-get update -y
sudo apt-get install docker.io -y
sudo usermod -aG docker $(whoami)
sudo su $(whoami)
fi
}
run_discord_bot() {
local drb_container_name="drb-client-discord-bot"
# Stop and remove existing container if it's running/exists
if docker ps -a --format '{{.Names}}' | grep -q "^${drb_container_name}$"; then
echo -e "${YELLOW}Container '${drb_container_name}' already exists. Stopping and removing it...${NC}"
docker stop "${drb_container_name}" &> /dev/null
docker rm "${drb_container_name}" &> /dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}Warning: Could not stop/remove existing container '${drb_container_name}'. It might not be running.${NC}"
fi
fi
mkdir -p $(pwd)/configs
echo -e "${GREEN}Installing the discord bot...${NC}"
docker pull git.vpn.cusano.net/logan/drb-client-discord-bot/drb-client-discord-bot:stable
docker run -d --privileged \
-v /dev:/dev \
-v $(pwd)/configs:/configs \
--name "${drb_container_name}" \
--network=host \
--restart unless-stopped \
git.vpn.cusano.net/logan/drb-client-discord-bot/drb-client-discord-bot:stable
}
create_config_json() {
echo -e "${GREEN}Creating config.json file...${NC}"
# Define the JSON content directly as a multi-line string
local config_content='{
"SERVER_WS_URI": "ws://drb-sock.vpn.cusano.net",
"SERVER_API_URL": "https://drb-api.vpn.cusano.net",
"CLIENT_API_URL": "http://localhost:8001",
"nickname": ""
}'
# Create data folder if it doesn't exist
mkdir -p $(pwd)/data
# Write the content to config.json
echo "$config_content" > $(pwd)/data/config.json
# Check if the file was successfully created
if [ -f "$(pwd)/data/config.json" ]; then
echo -e "${GREEN}Successfully created 'config.json'.${NC}"
echo -e "${GREEN}Content of config.json:${NC}"
cat $(pwd)/data/config.json
else
echo -e "${RED}Error: Failed to create 'config.json'.${NC}"
exit 1 # Exit with an error code if file creation fails
fi
}
start_docker_container() {
local container_name="drb-client-app-container" # A unique name for your container
local image_name="drb-client-app" # From your Makefile
# Check to make sure the repo is up to date
git pull
echo -e "${GREEN}Building Docker image '${image_name}'...${NC}"
# Build the Docker image from the current directory
if ! docker build -t "${image_name}" .; then
echo -e "${RED}Error: Failed to build Docker image '${image_name}'. Please check your Dockerfile and context.${NC}"
exit 1
fi
echo -e "${GREEN}Docker image '${image_name}' built successfully.${NC}"
echo -e "${GREEN}Starting Docker container...${NC}"
# Stop and remove existing container if it's running/exists
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
echo -e "${YELLOW}Container '${container_name}' already exists. Stopping and removing it...${NC}"
docker stop "${container_name}" &> /dev/null
docker rm "${container_name}" &> /dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}Warning: Could not stop/remove existing container '${container_name}'. It might not be running.${NC}"
fi
fi
echo -e "${GREEN}Starting new container '${container_name}' from image '${image_name}' with restart policy 'unless-stopped'.${NC}"
# Run the container
docker run -d \
--name "${container_name}" \
--restart unless-stopped \
-v "$(pwd)/data":/data \
--network=host \
"${image_name}"
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
echo -e "${GREEN}Docker container '${container_name}' started successfully in detached mode.${NC}"
echo -e "${GREEN}It is configured to restart automatically on daemon startup or if it exits (unless stopped manually).${NC}"
echo -e "${GREEN}You can check its status with: docker ps -f name=${container_name}${NC}"
echo -e "${GREEN}You can view its logs with: docker logs -f ${container_name}${NC}"
else
echo -e "${RED}Error: Failed to start Docker container '${container_name}'.${NC}"
exit 1
fi
}
# --- Main script execution ---
# Default flags
RUN_BOTH_DOCKERS=false
RUN_DISCORD_DOCKER=false
RUN_CLIENT_DOCKER=false
FULL_INSTALL=true
# Parse command-line arguments
# We use a while loop with a case statement to handle different options.
# getopts is for short options (e.g., -r)
# For long options (e.g., --run), we'll handle them manually or use a more advanced parser.
while [[ "$#" -gt 0 ]]; do
case "$1" in
-r|--run)
RUN_BOTH_DOCKERS=true
FULL_INSTALL=false # If -r is passed, we don't do a full install
shift # Move to next argument
;;
-c|--client)
RUN_CLIENT_DOCKER=true
FULL_INSTALL=false
shift
;;
-d|--discord|-l)
RUN_DISCORD_DOCKER=true
FULL_INSTALL=false
shift
;;
*)
echo "Unknown parameter passed: $1"
shift
;;
esac
done
if [ "$FULL_INSTALL" = true ]; then
echo -e "${GREEN}--- Starting full installation and setup ---${NC}"
# Install PreReqs
install_prereqs
# Create config.json
create_config_json
# Build and Start Docker container
start_docker_container
# Download/update and run the drb discord bot
run_discord_bot
echo -e "${GREEN}--- All installation and startup steps finished ---${NC}"
elif [ "$RUN_BOTH_DOCKERS" = true ]; then
# Build and run the DRB client container
echo -e "${GREEN}--- Starting DRB Client Docker container ---${NC}"
start_docker_container
# Download/update and run the DRB Discord Bot container
echo -e "${GREEN}--- Starting DRB Listener Docker container ---${NC}"
run_discord_bot
echo -e "${GREEN}--- Docker containers startup finished ---${NC}"
elif [ "$RUN_DISCORD_DOCKER" = true ]; then
# Download/update and run the DRB Discord Bot container
echo -e "${GREEN}--- Starting DRB Listener Docker container ---${NC}"
run_discord_bot
echo -e "${GREEN}--- Discord Docker container startup finished ---${NC}"
elif [ "$RUN_CLIENT_DOCKER" = true ]; then
# Build and run the DRB client container
echo -e "${GREEN}--- Starting DRB Client Docker container ---${NC}"
start_docker_container
echo -e "${GREEN}--- Client Docker container startup finished ---${NC}"
else
# This case should ideally not be hit if flags are handled correctly,
# but it's here for completeness.
echo -e "${YELLOW}---No valid operation specified. Exiting.${NC}"
exit 1
fi