-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
87 lines (72 loc) · 2.32 KB
/
install.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
#!/bin/bash
# Check if Python 3 is installed
PYTHON_EXEC="/usr/bin/python3"
if ! command -v $PYTHON_EXEC &> /dev/null
then
echo "Python 3 could not be found. Please install Python 3 before running this script."
exit 1
fi
# Check if Python 3 is installed
GIT_EXEC="/usr/bin/git"
if ! command -v $GIT_EXEC &> /dev/null
then
echo "Git could not be found. Please install Git before running this script."
exit 1
fi
# Define variables
SERVICE_NAME="xmarterunner"
REPO_PATH="/opt/xmarterunner"
VENV_PATH="$REPO_PATH/venv"
MAIN_SCRIPT="$REPO_PATH/main.py"
USER="xmarterunner"
USER_HOME="/home/$USER"
# Step 1: Create the system user if it doesn't exist
if ! id -u "$USER" &>/dev/null; then
echo "Creating system user $USER..."
useradd -r -m -d "$USER_HOME" -s /usr/sbin/nologin "$USER"
echo "System user $USER created with home directory $USER_HOME."
else
echo "System user $USER already exists."
fi
# Step 1: Clone the repository (if necessary) or update it
if [ ! -d "$REPO_PATH" ]; then
echo "Cloning the repository..."
git clone https://git.ccfe.ac.uk/marte21/public/xmarte-runner.git "$REPO_PATH"
else
echo "Updating the repository..."
cd "$REPO_PATH" && git pull
fi
pip install venv --exists-action=w
# Step 2: Set up the Python virtual environment
if [ ! -d "$VENV_PATH" ]; then
echo "Creating Python virtual environment..."
$PYTHON_EXEC -m venv "$VENV_PATH"
fi
# Step 3: Install Python dependencies if not already installed
echo "Installing Python dependencies..."
source "$VENV_PATH/bin/activate"
# Check if requirements need to be installed or updated
pip install --upgrade pip
pip install --upgrade $REPO_PATH --exists-action=w
# Step 3: Set up the systemd service file
echo "Creating systemd service file..."
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
cat <<EOT > $SERVICE_FILE
[Unit]
Description=XMARTe Runner
After=network.target
[Service]
User=root
WorkingDirectory=$REPO_PATH
ExecStart=$VENV_PATH/bin/python3 -m xmarterunner
Restart=always
Environment="PYTHONUNBUFFERED=1"
[Install]
WantedBy=multi-user.target
EOT
# Step 4: Reload systemd, enable and start the service
echo "Reloading systemd, enabling, and starting the service..."
systemctl daemon-reload
systemctl enable $SERVICE_NAME
systemctl start $SERVICE_NAME
echo "Service $SERVICE_NAME has been installed and started."