-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproduction-run.sh
93 lines (78 loc) · 2.45 KB
/
production-run.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
#!/bin/bash
# Detect OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
OS="linux"
else
echo "Unsupported OS. Exiting..."
exit 1
fi
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load environment variables from .env file in the script's directory
set -a
source "$SCRIPT_DIR/.env"
set +a
SERVICE_PATH="/etc/systemd/system/fullstackinvite.service"
SERVICE_NAME="fullstackinvite.service"
USER=$(whoami)
WORKING_DIR="$SCRIPT_DIR"
ENV_PATH="$WORKING_DIR/venv/bin"
PORT=${OPEN_PORT:-5000}
# Function to create the service file
create_service_file() {
cat <<EOF > $SERVICE_PATH
[Unit]
Description=Gunicorn instance to serve Full Stack Invite
After=network.target
[Service]
User=$USER
Group=www-data
WorkingDirectory=$WORKING_DIR
Environment="PATH=$ENV_PATH"
ExecStart=$ENV_PATH/gunicorn --workers 3 --bind 127.0.0.1:$PORT app:app
[Install]
WantedBy=multi-user.target
EOF
}
# Check if the service file exists
if [ -f "$SERVICE_PATH" ]; then
echo "$SERVICE_NAME exists. Checking for updates..."
# Check and update the port, username, and working directory if needed
NEED_UPDATE=false
if ! grep -q "ExecStart=$ENV_PATH/gunicorn --workers 3 --bind 127.0.0.1:$PORT app:app" "$SERVICE_PATH"; then
echo "Port mismatch. Updating service file..."
NEED_UPDATE=true
fi
if ! grep -q "User=$USER" "$SERVICE_PATH"; then
echo "Username mismatch. Updating service file..."
NEED_UPDATE=true
fi
if ! grep -q "WorkingDirectory=$WORKING_DIR" "$SERVICE_PATH"; then
echo "Working directory mismatch. Updating service file..."
NEED_UPDATE=true
fi
if [ "$NEED_UPDATE" = true ]; then
echo "Updating $SERVICE_NAME..."
create_service_file
sudo systemctl daemon-reload
sudo systemctl restart $SERVICE_NAME
else
echo "$SERVICE_NAME is up-to-date."
fi
else
echo "$SERVICE_NAME does not exist. Creating service file..."
create_service_file
sudo systemctl daemon-reload
sudo systemctl start $SERVICE_NAME
sudo systemctl enable $SERVICE_NAME
fi
# Ensure the service is enabled and started
if ! sudo systemctl is-enabled --quiet $SERVICE_NAME; then
echo "Enabling $SERVICE_NAME..."
sudo systemctl enable $SERVICE_NAME
fi
if ! sudo systemctl is-active --quiet $SERVICE_NAME; then
echo "Starting $SERVICE_NAME..."
sudo systemctl start $SERVICE_NAME
fi
echo "$SERVICE_NAME setup is complete."