Skip to content

Commit

Permalink
add versioning to service files
Browse files Browse the repository at this point in the history
  • Loading branch information
bdamokos committed Dec 21, 2024
1 parent f2adeb3 commit 776a237
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/service/bluetooth-serial.service
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Unit]
# Version: 0.0.1 (2024-12-21) # AUTO-INCREMENT
Description=Bluetooth Serial Port Service
After=bluetooth.target
Requires=bluetooth.target
Expand Down
1 change: 1 addition & 0 deletions docs/service/display.service.docker.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Unit]
# Version: 0.0.1 (2024-12-21) # AUTO-INCREMENT
Description=E-Paper Display Service (Docker Version)
After=network.target docker.service
Requires=docker.service
Expand Down
1 change: 1 addition & 0 deletions docs/service/display.service.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Unit]
# Version: 0.0.1 (2024-12-21) # AUTO-INCREMENT
Description=E-Paper Display Service
After=network.target
StartLimitIntervalSec=0
Expand Down
1 change: 1 addition & 0 deletions docs/service/display.service.remote_server.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Unit]
# Version: 0.0.1 (2024-12-21) # AUTO-INCREMENT
Description=E-Paper Display Service
After=network.target
StartLimitIntervalSec=0
Expand Down
101 changes: 95 additions & 6 deletions docs/service/start_display.sh.docker.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,87 @@
# chmod +x ~/start_display.sh
echo "----------------------------------------"
echo "Starting display service script (Docker Version)"
echo "Version: 0.0.1 (2024-12-04)" # AUTO-INCREMENT
echo "Version: 0.0.2 (2024-12-21)" # AUTO-INCREMENT
echo "----------------------------------------"
echo "MIT License - Copyright (c) 2024 Bence Damokos"
echo "----------------------------------------"

# Function to check and update a service file if needed
check_service_file() {
local example_file=$1
local service_name
local installed_file
local needs_username_change=false
local current_user=$(whoami)

# Determine if this is an example file that needs username adjustment
if [[ "$example_file" == *.example ]]; then
service_name=$(basename "$example_file" .example)
needs_username_change=true
else
service_name=$(basename "$example_file")
fi
installed_file="/etc/systemd/system/$service_name"

echo "Checking $service_name..."
if [ -f "$example_file" ]; then
# Get current version from installed service file
INSTALLED_VERSION=""
if [ -f "$installed_file" ]; then
INSTALLED_VERSION=$(grep "^# Version: " "$installed_file" | cut -d' ' -f3)

# Get new version from example file
NEW_VERSION=$(grep "^# Version: " "$example_file" | cut -d' ' -f3)

if [ "$INSTALLED_VERSION" != "$NEW_VERSION" ]; then
echo "Service file $service_name needs to be updated (Current: ${INSTALLED_VERSION:-none}, New: $NEW_VERSION)"
if [ "$needs_username_change" = true ]; then
echo "Updating service file with username $current_user..."
# Create a temporary file with username replaced
local temp_file=$(mktemp)
sed "s|/home/pi|/home/$current_user|g" "$example_file" > "$temp_file"
sed -i "s|User=pi|User=$current_user|g" "$temp_file"

# Copy the modified file to systemd
sudo cp "$temp_file" "$installed_file"
rm "$temp_file"

sudo systemctl daemon-reload
echo "Service file updated and daemon reloaded."
else
echo "Updating service file..."
sudo cp "$example_file" "$installed_file"
sudo systemctl daemon-reload
echo "Service file updated and daemon reloaded."
fi
else
echo "Service file $service_name is up to date."
fi
else
if [ "$needs_username_change" = true ]; then
echo "Service $service_name is not installed."
echo "Creating service file with username $current_user..."
# Create a temporary file with username replaced
local temp_file=$(mktemp)
sed "s|/home/pi|/home/$current_user|g" "$example_file" > "$temp_file"
sed -i "s|User=pi|User=$current_user|g" "$temp_file"

# Copy the modified file to systemd
sudo cp "$temp_file" "$installed_file"
rm "$temp_file"

sudo systemctl daemon-reload
echo "Service file created and daemon reloaded."
echo "To enable the service, run: sudo systemctl enable $service_name"
echo "To start the service, run: sudo systemctl start $service_name"
else
echo "Warning: $service_name is not installed. If you want to use this feature, please install it first."
echo "Available version: $(grep "^# Version: " "$example_file" | cut -d' ' -f3)"
fi
fi
fi
}

# Sleep for 10 seconds to allow for network and Docker to be ready
sleep 10

Expand Down Expand Up @@ -46,7 +122,20 @@ fi
echo "Startup script is up to date."
echo "----------------------------------------"

# Step 3: Update brussels_transit and rebuild Docker image
# Step 3: Check and update service files if needed
echo "Checking service files..."
cd ~/display_programme/docs/service

# Check main display service (requires username adjustment)
check_service_file "display.service.docker.example"

# Check optional services (direct install)
check_service_file "bluetooth-serial.service"
check_service_file "webserial.service"

echo "----------------------------------------"

# Step 4: Update brussels_transit and rebuild Docker image
cd ~/brussels_transit
echo "Updating brussels_transit from git..."
git fetch -v origin main
Expand All @@ -58,13 +147,13 @@ echo "Building Docker image..."
docker build -t brussels_transit:latest .
echo "----------------------------------------"

# Step 4: Activate virtual environment (for display program only)
# Step 5: Activate virtual environment (for display program only)
echo "Activating virtual environment..."
source ~/display_env/bin/activate
echo "Virtual environment activated."
echo "----------------------------------------"

# Step 5: Install requirements (for display program only) if changed
# Step 6: Install requirements (for display program only) if changed
echo "Checking requirements..."
cd ~/display_programme
if [ ! -f ~/.display_requirements_version ] || \
Expand Down Expand Up @@ -106,7 +195,7 @@ cleanup() {
# Trap SIGTERM and SIGINT
trap cleanup SIGTERM SIGINT

# Step 6: Clean up any existing containers
# Step 7: Clean up any existing containers
echo "Cleaning up any existing brussels_transit containers..."
docker ps -q --filter "ancestor=brussels_transit:latest" | xargs -r docker stop

Expand All @@ -119,7 +208,7 @@ echo "----------------------------------------"
# Wait for backend to start
sleep 5

# Step 7: Start the display program
# Step 8: Start the display program
cd ~/display_programme
echo "Starting display program..."
python basic.py &
Expand Down
107 changes: 101 additions & 6 deletions docs/service/start_display.sh.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,87 @@
# chmod +x ~/start_display.sh
echo "----------------------------------------"
echo "Starting display service script"
echo "Version: 0.0.12 (2024-12-14)" # AUTO-INCREMENT
echo "Version: 0.0.13 (2024-12-21)" # AUTO-INCREMENT
echo "----------------------------------------"
echo "MIT License - Copyright (c) 2024 Bence Damokos"
echo "----------------------------------------"

# Function to check and update a service file if needed
check_service_file() {
local example_file=$1
local service_name
local installed_file
local needs_username_change=false
local current_user=$(whoami)

# Determine if this is an example file that needs username adjustment
if [[ "$example_file" == *.example ]]; then
service_name=$(basename "$example_file" .example)
needs_username_change=true
else
service_name=$(basename "$example_file")
fi
installed_file="/etc/systemd/system/$service_name"

echo "Checking $service_name..."
if [ -f "$example_file" ]; then
# Get current version from installed service file
INSTALLED_VERSION=""
if [ -f "$installed_file" ]; then
INSTALLED_VERSION=$(grep "^# Version: " "$installed_file" | cut -d' ' -f3)

# Get new version from example file
NEW_VERSION=$(grep "^# Version: " "$example_file" | cut -d' ' -f3)

if [ "$INSTALLED_VERSION" != "$NEW_VERSION" ]; then
echo "Service file $service_name needs to be updated (Current: ${INSTALLED_VERSION:-none}, New: $NEW_VERSION)"
if [ "$needs_username_change" = true ]; then
echo "Updating service file with username $current_user..."
# Create a temporary file with username replaced
local temp_file=$(mktemp)
sed "s|/home/pi|/home/$current_user|g" "$example_file" > "$temp_file"
sed -i "s|User=pi|User=$current_user|g" "$temp_file"

# Copy the modified file to systemd
sudo cp "$temp_file" "$installed_file"
rm "$temp_file"

sudo systemctl daemon-reload
echo "Service file updated and daemon reloaded."
else
echo "Updating service file..."
sudo cp "$example_file" "$installed_file"
sudo systemctl daemon-reload
echo "Service file updated and daemon reloaded."
fi
else
echo "Service file $service_name is up to date."
fi
else
if [ "$needs_username_change" = true ]; then
echo "Service $service_name is not installed."
echo "Creating service file with username $current_user..."
# Create a temporary file with username replaced
local temp_file=$(mktemp)
sed "s|/home/pi|/home/$current_user|g" "$example_file" > "$temp_file"
sed -i "s|User=pi|User=$current_user|g" "$temp_file"

# Copy the modified file to systemd
sudo cp "$temp_file" "$installed_file"
rm "$temp_file"

sudo systemctl daemon-reload
echo "Service file created and daemon reloaded."
echo "To enable the service, run: sudo systemctl enable $service_name"
echo "To start the service, run: sudo systemctl start $service_name"
else
echo "Warning: $service_name is not installed. If you want to use this feature, please install it first."
echo "Available version: $(grep "^# Version: " "$example_file" | cut -d' ' -f3)"
fi
fi
fi
}

# Sleep for 10 seconds to allow for network to be ready
sleep 10

Expand Down Expand Up @@ -45,7 +121,26 @@ fi
echo "Startup script is up to date."
echo "----------------------------------------"

# Step 3: Update brussels_transit
# Step 3: Check and update service files if needed
echo "Checking service files..."
cd ~/display_programme/docs/service

# Check main display service (requires username adjustment)
check_service_file "display.service.example"

# Check Docker display service (requires username adjustment)
check_service_file "display.service.docker.example"

# Check remote server display service (requires username adjustment)
check_service_file "display.service.remote_server.example"

# Check optional services (direct install)
check_service_file "bluetooth-serial.service"
check_service_file "webserial.service"

echo "----------------------------------------"

# Step 4: Update brussels_transit
cd ~/brussels_transit
echo "Updating brussels_transit from git..."
git fetch -v origin main
Expand All @@ -60,12 +155,12 @@ fi
echo "----------------------------------------"

echo "Activating virtual environment..."
# Step 4: Activate virtual environment
# Step 5: Activate virtual environment
source ~/display_env/bin/activate
echo "Virtual environment activated."
echo "----------------------------------------"

# Step 5: Install requirements if changed
# Step 6: Install requirements if changed
echo "Checking display_programme requirements..."
cd ~/display_programme
if [ ! -f ~/.display_requirements_version ] || \
Expand Down Expand Up @@ -119,7 +214,7 @@ cleanup() {
# Trap SIGTERM and SIGINT
trap cleanup SIGTERM SIGINT

# Step 6: Start the backend server
# Step 7: Start the backend server
cd ~/brussels_transit/app
echo "Starting backend server..."
python main.py &
Expand All @@ -130,7 +225,7 @@ echo "----------------------------------------"
# Wait for backend to start
sleep 5

# Step 7: Start the display program
# Step 8: Start the display program
cd ~/display_programme
echo "Starting display program..."
python basic.py &
Expand Down
Loading

0 comments on commit 776a237

Please sign in to comment.