-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
executable file
·174 lines (146 loc) · 5.22 KB
/
startup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# Exit on error
set -e
# Define default project name
SERVICE_NAME="tor-backend"
BACKEND_PROFILE="backend"
FRONTEND_PROFILE="frontend"
MONITOR_PROFILE="monitoring"
VENV_DIR="venv"
domains=()
# Function to check if a container is healthy
check_health() {
local service="$1"
local retries=30
local count
local service_status
echo -e "\nService ${service} has ${REPLICAS} replicas."
echo -e "Checking health of service ${service}, waiting for all replicas to become healthy...\n"
for instance in $(seq 1 "${REPLICAS}"); do
count=0
service_status="starting"
while [ "$service_status" != "healthy" ] && [ $count -lt $retries ]; do
service_status=$(docker inspect --format='{{.State.Health.Status}}' "${PROJECT_NAME}-${service}-${instance}")
if [ "$service_status" = "healthy" ]; then
echo "Service ${service}-${instance} is healthy."
else
echo "Waiting for ${service}-${instance} to become healthy..."
sleep 10
count=$((count + 1))
fi
done
if [ "$service_status" != "healthy" ]; then
echo "Service ${service}-${instance} failed to become healthy after $((retries * 10)) seconds."
exit 1
fi
done
}
# Load environment variables from .env file
if [ -f .env ]; then
while IFS='=' read -r key value; do
if [[ $key != \#* && $key != "" ]]; then
export "$key"="$value"
echo "Loaded $key=$value"
fi
done <.env
else
echo ".env file not found"
fi
# Check if critical variables are set
if [ -z "$PROJECT_NAME" ] || [ -z "$BACKEND_PROFILE" ] || [ -z "$FRONTEND_PROFILE" ] || [ -z "$REPLICAS" ]; then
echo "Critical variables are not set."
exit 1
fi
# Check if the virtual environment directory exists
if [ ! -d "$VENV_DIR" ]; then
echo "Virtual environment not found. Please run 'make install' to set up the virtual environment."
exit 1
fi
# Activate the virtual environment
source "$VENV_DIR/bin/activate"
# Check if the script is run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root. Please use sudo."
exit 1
fi
# Check if the directory exists
if [ ! -d "$DIRECTORY" ]; then
echo "Directory does not exist: $DIRECTORY"
echo "Attempting to create directory..."
mkdir -p "$DIRECTORY" &&
echo "Directory created successfully." ||
echo "Failed to create directory."
else
echo "Directory already exists: $DIRECTORY"
fi
# Get .onion hostname
if [ -f "${DIRECTORY}/hostname" ]; then
echo "File exists: ${DIRECTORY}/hostname"
hostname_value=$(sudo cat "${DIRECTORY}/hostname")
else
echo "File does not exist: ${DIRECTORY}/hostname"
echo "Unable to proceed without hostname file."
exit 1
fi
# Generate the ob_config file
python3 ./scripts/config_generator.py ob_config --master_onion_address "${hostname_value}"
echo "Setting up directories..."
# Set permissions and ownership for the directory
if [ -d "$DIRECTORY" ]; then
chmod -R 700 "$DIRECTORY"
chown -R 100:100 "$DIRECTORY"
else
echo "Directory $DIRECTORY does not exist."
exit 1
fi
echo "Running docker-compose..."
if command -v docker-compose >/dev/null 2>&1; then
docker-compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$BACKEND_PROFILE" up \
-d --build --force-recreate
check_health "$SERVICE_NAME"
elif command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
docker compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$BACKEND_PROFILE" up \
-d --build --force-recreate
check_health "$SERVICE_NAME"
else
echo "docker-compose is not installed."
exit 1
fi
for instance in $(seq 1 "$REPLICAS"); do
container_name="${PROJECT_NAME}-${SERVICE_NAME}-${instance}"
domain=$(./scripts/domain.sh "$container_name")
domains+=("$domain")
done
# Generate the frontend configuration file.
python ./scripts/config_generator.py config --log_level "$LOG_LEVEL" --log_location "$LOG_LOCATION" --domains "${domains[@]}" --key_path "$KEY_LOCATION"
# Generate the monitoring configuration file.
python ./scripts/config_generator.py monitor_config --master_onion_address "${hostname_value}"
if command -v docker-compose >/dev/null 2>&1; then
docker-compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$FRONTEND_PROFILE" up \
-d --build --force-recreate
docker-compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$MONITOR_PROFILE" up \
-d --build --force-recreate
elif command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
docker compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$FRONTEND_PROFILE" up \
-d --build --force-recreate
docker compose -p "$PROJECT_NAME" \
-f docker-compose-onionbalance.yaml \
--profile "$MONITOR_PROFILE" up \
-d --build --force-recreate
else
echo "docker-compose is not installed."
exit 1
fi
# Deactivate the virtual environment
deactivate
echo -e "\nStartup script completed."