File size: 2,000 Bytes
5555825
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# !/bin/bash

# Make sure that the script is run as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Token is first script parameter
if [ -z "$1" ]; then
    echo "No HF_TOKEN provided. Please provide the token as the first argument."
    exit 1
fi

# Get the authorized_keys file from GitHub and replace the existing one
curl -sS -o /home/student-admin/.ssh/authorized_keys https://raw.githubusercontent.com/Badrivishal/xkcd_finder/refs/heads/main/deployment_scripts/authorized_keys
chown student-admin:student-admin /home/student-admin/.ssh/authorized_keys
chmod 600 /home/student-admin/.ssh/authorized_keys
echo "Updated authorized_keys file for student-admin user."

# Clone the repository if it doesn't exist, otherwise pull the latest changes
REPO_DIR="/home/student-admin/xkcd_finder"
if [ ! -d "$REPO_DIR" ]; then
    git clone https://github.com/Badrivishal/xkcd_finder.git "$REPO_DIR"
else
    git -C "$REPO_DIR" pull
fi

# Install python virtual environment
apt-get update -y
apt-get install -y python3-venv
echo "Installed python3-venv package."

# Set up and activate the virtual environment, then install dependencies
cd "$REPO_DIR"
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
echo "Set up virtual environment and installed dependencies."

# Set up systemd service 
SERVICE_FILE="/etc/systemd/system/xkcd_finder.service"
cat <<EOL > $SERVICE_FILE
[Unit]
Description=xkcd Finder Service
After=network.target

[Service]
WorkingDirectory=$REPO_DIR
Environment="HF_TOKEN=$1"
ExecStart=$REPO_DIR/venv/bin/python app.py
Restart=always

[Install]
WantedBy=multi-user.target
EOL

systemctl daemon-reload
# Check if the service already exists and stop it if it does
if systemctl is-active --quiet xkcd_finder.service; then
    systemctl stop xkcd_finder.service
fi
systemctl enable xkcd_finder.service
systemctl start xkcd_finder.service
echo "Set up and started xkcd_finder systemd service."