Compare commits
3 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 79864ace88 | |||
| a2140b5ea8 | |||
| fa9fe6095b |
4 changed files with 206 additions and 320 deletions
212
.github/workflows/build.yml
vendored
212
.github/workflows/build.yml
vendored
|
|
@ -8,199 +8,131 @@ on:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Cross-compile for Raspberry Pi
|
name: Build multi-arch Raspberry Pi binaries
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '1.20'
|
go-version: "1.22"
|
||||||
|
|
||||||
- name: Build for ARM (Pi Zero, Pi 1 - ARMv6)
|
- name: Build for ARMv6 (Pi Zero / Pi 1)
|
||||||
run: |
|
run: |
|
||||||
GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -o wol-server-arm6
|
GOOS=linux GOARCH=arm GOARM=6 \
|
||||||
|
go build -ldflags="-s -w" -o wol-server-armv6
|
||||||
|
|
||||||
|
- name: Build for ARM64 (Pi Zero 2 / Pi 3 / Pi 4 / Pi 5)
|
||||||
|
run: |
|
||||||
|
GOOS=linux GOARCH=arm64 \
|
||||||
|
go build -ldflags="-s -w" -o wol-server-arm64
|
||||||
|
|
||||||
- name: Create systemd service file
|
- name: Create systemd service file
|
||||||
run: |
|
run: |
|
||||||
cat > wol-server.service << 'EOL'
|
cat > wol-server.service << 'EOF'
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=WOL Server Go Application
|
Description=WOL Server Go Application
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
User=pi
|
Type=simple
|
||||||
WorkingDirectory=/home/pi/wol-server
|
User=loke
|
||||||
ExecStart=/home/pi/wol-server/wol-server
|
WorkingDirectory=/home/loke/wol-server
|
||||||
|
ExecStart=/home/loke/wol-server/wol-server
|
||||||
Restart=always
|
Restart=always
|
||||||
|
RestartSec=3
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOL
|
EOF
|
||||||
|
|
||||||
- name: Create deployment script
|
- name: Create install script
|
||||||
run: |
|
run: |
|
||||||
cat > install.sh << 'EOL'
|
cat > install.sh << 'EOF'
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Installation directory
|
INSTALL_DIR="$HOME/wol-server"
|
||||||
INSTALL_DIR=~/wol-server
|
|
||||||
|
|
||||||
echo "Creating installation directory..."
|
echo "Creating installation directory..."
|
||||||
mkdir -p $INSTALL_DIR
|
mkdir -p "$INSTALL_DIR/templates"
|
||||||
mkdir -p $INSTALL_DIR/templates
|
|
||||||
|
|
||||||
echo "Installing application..."
|
ARCH=$(uname -m)
|
||||||
cp wol-server-arm6 $INSTALL_DIR/wol-server
|
case "$ARCH" in
|
||||||
chmod +x $INSTALL_DIR/wol-server
|
armv6l|armv7l)
|
||||||
|
BIN="wol-server-armv6"
|
||||||
|
;;
|
||||||
|
aarch64)
|
||||||
|
BIN="wol-server-arm64"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unsupported architecture: $ARCH"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
echo "Installing template files..."
|
echo "Detected architecture: $ARCH"
|
||||||
cp -r templates/* $INSTALL_DIR/templates/
|
echo "Using binary: $BIN"
|
||||||
|
|
||||||
echo "Installing system service..."
|
cp "$BIN" "$INSTALL_DIR/wol-server"
|
||||||
|
chmod +x "$INSTALL_DIR/wol-server"
|
||||||
|
|
||||||
|
echo "Installing templates..."
|
||||||
|
cp -r templates/* "$INSTALL_DIR/templates/"
|
||||||
|
|
||||||
|
echo "Installing systemd service..."
|
||||||
sudo cp wol-server.service /etc/systemd/system/
|
sudo cp wol-server.service /etc/systemd/system/
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable wol-server
|
sudo systemctl enable wol-server
|
||||||
|
|
||||||
# Install required dependencies
|
|
||||||
echo "Installing dependencies..."
|
echo "Installing dependencies..."
|
||||||
sudo apt-get update -qq
|
sudo apt-get update -qq
|
||||||
sudo apt-get install -y wakeonlan sshpass
|
sudo apt-get install -y wakeonlan sshpass
|
||||||
|
|
||||||
# Start the service
|
|
||||||
echo "Starting service..."
|
echo "Starting service..."
|
||||||
sudo systemctl restart wol-server
|
sudo systemctl restart wol-server
|
||||||
|
|
||||||
echo "==========================================="
|
echo "==========================================="
|
||||||
echo "Installation complete!"
|
echo "WOL Server installed successfully!"
|
||||||
echo "The WOL server is now running at http://$(hostname -I | awk '{print $1}'):8080"
|
echo "URL: http://$(hostname -I | awk '{print $1}'):8080"
|
||||||
echo "==========================================="
|
echo "==========================================="
|
||||||
EOL
|
EOF
|
||||||
|
|
||||||
chmod +x install.sh
|
chmod +x install.sh
|
||||||
|
|
||||||
- name: Create README with installation instructions
|
- name: Create release package
|
||||||
run: |
|
run: |
|
||||||
cat > INSTALL.md << 'EOL'
|
|
||||||
# WOL Server Installation Guide
|
|
||||||
|
|
||||||
This guide will help you install the Wake-on-LAN server on your Raspberry Pi.
|
|
||||||
|
|
||||||
## Prerequisites
|
|
||||||
|
|
||||||
- Raspberry Pi running Raspberry Pi OS (Raspbian)
|
|
||||||
- SSH access to your Pi
|
|
||||||
- SCP or SFTP capability to transfer files
|
|
||||||
|
|
||||||
## Installation Steps
|
|
||||||
|
|
||||||
### 1. Transfer Files to Raspberry Pi
|
|
||||||
|
|
||||||
**Option 1: Using SCP from your computer**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Replace with your Pi's IP address
|
|
||||||
PI_IP=192.168.1.100
|
|
||||||
|
|
||||||
# Transfer the installation package
|
|
||||||
scp wol-server.tar.gz pi@$PI_IP:~/
|
|
||||||
```
|
|
||||||
|
|
||||||
**Option 2: Using SFTP client**
|
|
||||||
|
|
||||||
Use a tool like FileZilla, WinSCP, or Cyberduck to transfer the `wol-server.tar.gz` file to your Raspberry Pi.
|
|
||||||
|
|
||||||
### 2. SSH into your Raspberry Pi
|
|
||||||
|
|
||||||
```bash
|
|
||||||
ssh pi@192.168.1.100
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Extract and Install
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Navigate to home directory
|
|
||||||
cd ~
|
|
||||||
|
|
||||||
# Extract the archive
|
|
||||||
tar -xzf wol-server.tar.gz
|
|
||||||
|
|
||||||
# Run the installation script
|
|
||||||
./install.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4. Test the Installation
|
|
||||||
|
|
||||||
Open a web browser and navigate to:
|
|
||||||
```
|
|
||||||
http://[your-pi-ip]:8080
|
|
||||||
```
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
If the service fails to start, check the logs:
|
|
||||||
```bash
|
|
||||||
sudo systemctl status wol-server
|
|
||||||
```
|
|
||||||
|
|
||||||
If template errors occur, ensure the template files were copied correctly:
|
|
||||||
```bash
|
|
||||||
ls -la ~/wol-server/templates/
|
|
||||||
```
|
|
||||||
|
|
||||||
## Manual Installation (if needed)
|
|
||||||
|
|
||||||
If you encounter issues with the automated install:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Create directories
|
|
||||||
mkdir -p ~/wol-server/templates
|
|
||||||
|
|
||||||
# Copy files manually
|
|
||||||
cp wol-server-arm6 ~/wol-server/wol-server
|
|
||||||
chmod +x ~/wol-server/wol-server
|
|
||||||
cp templates/* ~/wol-server/templates/
|
|
||||||
sudo cp wol-server.service /etc/systemd/system/
|
|
||||||
|
|
||||||
# Install dependencies
|
|
||||||
sudo apt-get update
|
|
||||||
sudo apt-get install -y wakeonlan sshpass
|
|
||||||
|
|
||||||
# Enable and start service
|
|
||||||
sudo systemctl daemon-reload
|
|
||||||
sudo systemctl enable wol-server
|
|
||||||
sudo systemctl start wol-server
|
|
||||||
```
|
|
||||||
EOL
|
|
||||||
|
|
||||||
- name: Create all-in-one package
|
|
||||||
run: |
|
|
||||||
# Create a single package with everything needed
|
|
||||||
mkdir -p package
|
mkdir -p package
|
||||||
cp wol-server-arm6 package/
|
cp wol-server-armv6 package/
|
||||||
|
cp wol-server-arm64 package/
|
||||||
cp wol-server.service package/
|
cp wol-server.service package/
|
||||||
cp install.sh package/
|
cp install.sh package/
|
||||||
cp -r templates package/
|
cp -r templates package/
|
||||||
cp INSTALL.md package/
|
|
||||||
|
|
||||||
# Create the tarball
|
|
||||||
tar -czf wol-server.tar.gz -C package .
|
tar -czf wol-server.tar.gz -C package .
|
||||||
|
|
||||||
- name: Create Release
|
- name: Create Forgejo Release
|
||||||
id: create_release
|
|
||||||
uses: softprops/action-gh-release@v1
|
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
with:
|
run: |
|
||||||
tag_name: v${{ github.run_number }}
|
TAG="v${{ github.run_number }}"
|
||||||
name: Release v${{ github.run_number }}
|
API_URL="${{ github.server_url }}/api/v1"
|
||||||
draft: false
|
REPO="${{ github.repository }}"
|
||||||
prerelease: false
|
|
||||||
files: |
|
# Create release
|
||||||
wol-server.tar.gz
|
RELEASE_ID=$(curl -s -X POST \
|
||||||
INSTALL.md
|
-H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \
|
||||||
env:
|
-H "Content-Type: application/json" \
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
"${API_URL}/repos/${REPO}/releases" \
|
||||||
|
-d "{\"tag_name\": \"${TAG}\", \"name\": \"Release ${TAG}\", \"draft\": false, \"prerelease\": false}" \
|
||||||
|
| grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||||
|
|
||||||
|
# Upload asset
|
||||||
|
curl -s -X POST \
|
||||||
|
-H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \
|
||||||
|
"${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=wol-server.tar.gz" \
|
||||||
|
-F "attachment=@wol-server.tar.gz" > /dev/null
|
||||||
|
|
||||||
|
echo "Release ${TAG} created with asset wol-server.tar.gz"
|
||||||
|
|
|
||||||
68
main.go
68
main.go
|
|
@ -296,21 +296,19 @@ func verifyScheduleConfig() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Immediately check if we need to boot based on the current time
|
// Immediately check if we need to boot ONLY at exact start time
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
currentTimeStr := now.Format("15:04")
|
currentTimeStr := now.Format("15:04")
|
||||||
|
|
||||||
// Check if we're within the schedule window (between start and end time)
|
// Check ONLY if current time EXACTLY matches start time
|
||||||
if currentTimeStr == scheduleConfig.StartTime {
|
if currentTimeStr == scheduleConfig.StartTime && ShouldRunToday(now) {
|
||||||
log.Printf("STARTUP MATCH: Current time %s matches start time, attempting boot", currentTimeStr)
|
log.Printf("STARTUP MATCH: Current time %s matches start time EXACTLY, attempting boot", currentTimeStr)
|
||||||
if !isServerOnline() {
|
if !isServerOnline() {
|
||||||
sendWakeOnLAN()
|
sendWakeOnLAN()
|
||||||
}
|
// Mark that the server was started by the scheduler
|
||||||
} else if currentTimeStr > scheduleConfig.StartTime && currentTimeStr < scheduleConfig.EndTime {
|
scheduleConfig.StartedBySchedule = true
|
||||||
log.Printf("STARTUP CHECK: Current time %s is within schedule window, checking server", currentTimeStr)
|
scheduleConfig.LastRun = now.Format(time.RFC3339)
|
||||||
if !isServerOnline() {
|
UpdateScheduleConfig(scheduleConfig)
|
||||||
log.Printf("Server should be online based on schedule - attempting boot")
|
|
||||||
sendWakeOnLAN()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -337,28 +335,21 @@ func verifyScheduleConfig() {
|
||||||
func runScheduleChecker() {
|
func runScheduleChecker() {
|
||||||
// Define the checkScheduleOnce function
|
// Define the checkScheduleOnce function
|
||||||
checkScheduleOnce := func() {
|
checkScheduleOnce := func() {
|
||||||
// Check if server should be on according to schedule
|
// Only check exact times for schedule actions, don't use window logic
|
||||||
shouldBeOn := CheckSchedule()
|
now := time.Now()
|
||||||
|
currentTimeStr := now.Format("15:04")
|
||||||
serverIsOn := isServerOnline()
|
serverIsOn := isServerOnline()
|
||||||
|
|
||||||
// Log schedule status (debug level)
|
// Log schedule status (debug level)
|
||||||
if scheduleConfig.Enabled {
|
if scheduleConfig.Enabled {
|
||||||
log.Printf("Schedule check: Window active=%v, Server online=%v", shouldBeOn, serverIsOn)
|
log.Printf("Schedule check: Current=%s, Start=%s, End=%s, LastRun=%s",
|
||||||
|
currentTimeStr, scheduleConfig.StartTime, scheduleConfig.EndTime, scheduleConfig.LastRun)
|
||||||
|
|
||||||
// Force check the current time against the schedule time
|
// Only act at exact start or end times
|
||||||
now := time.Now()
|
// EXACT START TIME MATCH - Try to boot server
|
||||||
currentTimeStr := now.Format("15:04")
|
if currentTimeStr == scheduleConfig.StartTime && !serverIsOn && ShouldRunToday(now) {
|
||||||
if currentTimeStr == scheduleConfig.StartTime {
|
log.Println("EXACT START TIME: Initiating boot sequence...")
|
||||||
log.Printf("EXACT TIME MATCH! Current time %s equals start time", currentTimeStr)
|
|
||||||
shouldBeOn = true
|
|
||||||
} else if currentTimeStr == scheduleConfig.EndTime {
|
|
||||||
log.Printf("EXACT END TIME MATCH! Current time %s equals end time", currentTimeStr)
|
|
||||||
shouldBeOn = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if shouldBeOn && !serverIsOn {
|
|
||||||
log.Println("Schedule: Server should be on but is offline. BOOT ATTEMPT INITIATED...")
|
|
||||||
// Try multiple times to boot with small delays between attempts
|
// Try multiple times to boot with small delays between attempts
|
||||||
for attempt := 1; attempt <= 3; attempt++ {
|
for attempt := 1; attempt <= 3; attempt++ {
|
||||||
log.Printf("Boot attempt %d/3", attempt)
|
log.Printf("Boot attempt %d/3", attempt)
|
||||||
|
|
@ -367,6 +358,10 @@ func runScheduleChecker() {
|
||||||
log.Printf("Error booting server from schedule: %v", err)
|
log.Printf("Error booting server from schedule: %v", err)
|
||||||
} else {
|
} else {
|
||||||
log.Println("Schedule: Boot command sent successfully")
|
log.Println("Schedule: Boot command sent successfully")
|
||||||
|
// Mark that server was started by scheduler
|
||||||
|
scheduleConfig.StartedBySchedule = true
|
||||||
|
scheduleConfig.LastRun = now.Format(time.RFC3339)
|
||||||
|
UpdateScheduleConfig(scheduleConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if server came online
|
// Check if server came online
|
||||||
|
|
@ -381,10 +376,11 @@ func runScheduleChecker() {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !shouldBeOn && serverIsOn {
|
// EXACT END TIME MATCH - Try to shutdown server
|
||||||
|
} else if currentTimeStr == scheduleConfig.EndTime && serverIsOn {
|
||||||
// Check if auto-shutdown is enabled
|
// Check if auto-shutdown is enabled
|
||||||
if scheduleConfig.AutoShutdown && shutdownPassword != "" && scheduleConfig.StartedBySchedule {
|
if scheduleConfig.AutoShutdown && shutdownPassword != "" && scheduleConfig.StartedBySchedule {
|
||||||
log.Println("Schedule: Server is on outside of scheduled window - attempting auto-shutdown")
|
log.Println("EXACT END TIME: Attempting auto-shutdown")
|
||||||
|
|
||||||
// Try multiple times to shut down the server
|
// Try multiple times to shut down the server
|
||||||
var shutdownSuccessful bool
|
var shutdownSuccessful bool
|
||||||
|
|
@ -406,19 +402,21 @@ func runScheduleChecker() {
|
||||||
if !shutdownSuccessful {
|
if !shutdownSuccessful {
|
||||||
log.Printf("All auto shutdown attempts failed")
|
log.Printf("All auto shutdown attempts failed")
|
||||||
}
|
}
|
||||||
} else if !scheduleConfig.StartedBySchedule {
|
}
|
||||||
// Server wasn't started by scheduler
|
|
||||||
log.Println("Schedule: Server is on outside of scheduled window but was not started by scheduler")
|
|
||||||
} else {
|
} else {
|
||||||
// Just log the situation when auto-shutdown is not enabled
|
// No action at non-exact times, just log status
|
||||||
log.Println("Schedule: Server is on outside of scheduled window")
|
if serverIsOn && scheduleConfig.StartedBySchedule && currentTimeStr > scheduleConfig.EndTime {
|
||||||
|
log.Printf("Server is still online after end time %s - waiting for next exact end time match", scheduleConfig.EndTime)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update last run timestamp if we're exiting the window
|
// Update last run timestamp if we've passed the end time
|
||||||
// This helps track when the schedule was last active
|
// This helps track when the schedule was last active
|
||||||
currentConfig := GetScheduleConfig()
|
currentConfig := GetScheduleConfig()
|
||||||
if currentConfig.Enabled && !shouldBeOn && currentConfig.LastRun != "" {
|
nowTime := time.Now()
|
||||||
|
currentTimeString := nowTime.Format("15:04")
|
||||||
|
if currentConfig.Enabled && currentTimeString > currentConfig.EndTime && currentConfig.LastRun != "" {
|
||||||
lastRun, err := time.Parse(time.RFC3339, currentConfig.LastRun)
|
lastRun, err := time.Parse(time.RFC3339, currentConfig.LastRun)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// If it's been more than a day since the last update, reset the timestamp
|
// If it's been more than a day since the last update, reset the timestamp
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"enabled": false,
|
"enabled": true,
|
||||||
"startTime": "",
|
"startTime": "13:46",
|
||||||
"endTime": "",
|
"endTime": "13:49",
|
||||||
"frequency": "daily",
|
"frequency": "daily",
|
||||||
"lastRun": "",
|
"lastRun": "2025-09-05T13:46:41+02:00",
|
||||||
"autoShutdown": false,
|
"autoShutdown": true,
|
||||||
"startedBySchedule": false
|
"startedBySchedule": true
|
||||||
}
|
}
|
||||||
92
utils.go
92
utils.go
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -231,7 +230,7 @@ func CheckSchedule() (shouldBeOn bool) {
|
||||||
|
|
||||||
// Check if the schedule should run today based on frequency
|
// Check if the schedule should run today based on frequency
|
||||||
// Check if we're in the schedule window
|
// Check if we're in the schedule window
|
||||||
if !shouldRunToday(now) {
|
if !ShouldRunToday(now) {
|
||||||
log.Printf("Schedule is active but not set to run today based on frequency: %s", scheduleConfig.Frequency)
|
log.Printf("Schedule is active but not set to run today based on frequency: %s", scheduleConfig.Frequency)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -282,7 +281,7 @@ func CheckSchedule() (shouldBeOn bool) {
|
||||||
if scheduleConfig.AutoShutdown && shutdownPassword != "" && isServerOnline() {
|
if scheduleConfig.AutoShutdown && shutdownPassword != "" && isServerOnline() {
|
||||||
// Only shut down if the server was started by the scheduler
|
// Only shut down if the server was started by the scheduler
|
||||||
if scheduleConfig.StartedBySchedule {
|
if scheduleConfig.StartedBySchedule {
|
||||||
log.Printf("Auto shutdown is enabled - attempting to shut down server")
|
log.Printf("Auto shutdown is enabled - attempting to shut down server at end time")
|
||||||
|
|
||||||
// Try up to 3 times to shut down the server
|
// Try up to 3 times to shut down the server
|
||||||
var shutdownSuccessful bool
|
var shutdownSuccessful bool
|
||||||
|
|
@ -309,8 +308,13 @@ func CheckSchedule() (shouldBeOn bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Otherwise check if we're within the window with a small buffer (30 seconds)
|
// ONLY consider the server should be on at EXACT start time or EXACT end time
|
||||||
shouldBeOn = (now.After(startTime.Add(-30*time.Second)) && now.Before(endTime))
|
shouldBeOn = (currentTimeStr == scheduleConfig.StartTime)
|
||||||
|
|
||||||
|
// Log that we're waiting for exact times for actions
|
||||||
|
if currentTimeStr != scheduleConfig.StartTime && currentTimeStr != scheduleConfig.EndTime {
|
||||||
|
log.Printf("Not at exact schedule times - no action needed until exact start/end time")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Schedule window check: Current=%v, Start=%v, End=%v, ShouldBeOn=%v",
|
log.Printf("Schedule window check: Current=%v, Start=%v, End=%v, ShouldBeOn=%v",
|
||||||
|
|
@ -321,39 +325,25 @@ func CheckSchedule() (shouldBeOn bool) {
|
||||||
log.Printf("EXACT END TIME MATCH! Current time %s equals end time - schedule window should close", currentTimeStr)
|
log.Printf("EXACT END TIME MATCH! Current time %s equals end time - schedule window should close", currentTimeStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're in the schedule window, update the LastRun and attempt to boot the server
|
// If we're at exact start time, update the LastRun timestamp
|
||||||
if shouldBeOn {
|
if currentTimeStr == scheduleConfig.StartTime && ShouldRunToday(now) {
|
||||||
// Only update LastRun if it hasn't been set yet for this window
|
// We only track that we've seen the start time
|
||||||
if scheduleConfig.LastRun == "" {
|
log.Printf("Exact start time reached - marking schedule run")
|
||||||
log.Printf("Entering scheduled backup window, updating LastRun timestamp")
|
|
||||||
|
// Don't automatically boot the server here - let the main scheduler handle it
|
||||||
|
// We're just updating state information
|
||||||
scheduleConfig.LastRun = now.Format(time.RFC3339)
|
scheduleConfig.LastRun = now.Format(time.RFC3339)
|
||||||
if err := saveScheduleConfig(); err != nil {
|
if err := saveScheduleConfig(); err != nil {
|
||||||
log.Printf("Warning: Failed to save schedule config: %v", err)
|
log.Printf("Warning: Failed to save schedule config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force a server boot attempt on window start
|
|
||||||
if !isServerOnline() {
|
|
||||||
log.Printf("Schedule window started - attempting to boot server")
|
|
||||||
if err := sendWakeOnLAN(); err != nil {
|
|
||||||
log.Printf("Schedule boot failed: %v", err)
|
|
||||||
} else {
|
|
||||||
// Mark that server was started by scheduler
|
|
||||||
scheduleConfig.StartedBySchedule = true
|
|
||||||
if err := saveScheduleConfig(); err != nil {
|
|
||||||
log.Printf("Failed to save schedule config: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return shouldBeOn
|
return shouldBeOn
|
||||||
}
|
}
|
||||||
|
|
||||||
// shouldRunToday checks if the schedule should run today based on frequency
|
// ShouldRunToday checks if the schedule should run today based on frequency
|
||||||
func shouldRunToday(now time.Time) bool {
|
func ShouldRunToday(now time.Time) bool {
|
||||||
// Special case: if we're within the current window (between start and end time),
|
// We no longer check for windows - we only check at exact times
|
||||||
// we should consider the schedule to be active regardless of LastRun
|
|
||||||
currentTimeStr := now.Format("15:04")
|
currentTimeStr := now.Format("15:04")
|
||||||
today := now.Format("2006-01-02")
|
today := now.Format("2006-01-02")
|
||||||
|
|
||||||
|
|
@ -366,9 +356,9 @@ func shouldRunToday(now time.Time) bool {
|
||||||
endTime = endTime.AddDate(0, 0, 1)
|
endTime = endTime.AddDate(0, 0, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're currently in the window, allow it to run
|
// Only log that we're at an exact schedule time
|
||||||
if now.After(startTime) && now.Before(endTime) {
|
if currentTimeStr == scheduleConfig.StartTime {
|
||||||
log.Println("Currently within today's schedule window - schedule should be active")
|
log.Println("Currently at exact start time - schedule should be active")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -389,10 +379,11 @@ func shouldRunToday(now time.Time) bool {
|
||||||
// Don't allow running multiple times on the same day unless
|
// Don't allow running multiple times on the same day unless
|
||||||
// it's been reset explicitly (LastRun set to empty)
|
// it's been reset explicitly (LastRun set to empty)
|
||||||
if lastRun.Year() == now.Year() && lastRun.YearDay() == now.YearDay() {
|
if lastRun.Year() == now.Year() && lastRun.YearDay() == now.YearDay() {
|
||||||
// Check if we've passed the end time today - if so, we can reset for tomorrow
|
// Check if we've passed the end time today - if so, we can reset for next run
|
||||||
if scheduleConfig.EndTime != "" && currentTimeStr > scheduleConfig.EndTime {
|
if scheduleConfig.EndTime != "" && currentTimeStr > scheduleConfig.EndTime {
|
||||||
log.Println("Current time is after end time - resetting for next run")
|
log.Println("Current time is after end time - resetting for next run")
|
||||||
scheduleConfig.LastRun = ""
|
scheduleConfig.LastRun = ""
|
||||||
|
scheduleConfig.StartedBySchedule = false // Reset this flag too
|
||||||
saveScheduleConfig()
|
saveScheduleConfig()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -554,18 +545,6 @@ func shutdownServer(password string) error {
|
||||||
log.Println("Using sshpass for authentication")
|
log.Println("Using sshpass for authentication")
|
||||||
log.Printf("Password being used: %s", password)
|
log.Printf("Password being used: %s", password)
|
||||||
|
|
||||||
// Create the command
|
|
||||||
cmdArgs := []string{
|
|
||||||
"sshpass", "-p", password, "ssh",
|
|
||||||
"-o", "StrictHostKeyChecking=no",
|
|
||||||
"-o", "UserKnownHostsFile=/dev/null",
|
|
||||||
"-o", "LogLevel=ERROR",
|
|
||||||
"-o", "ConnectTimeout=10",
|
|
||||||
fmt.Sprintf("%s@%s", serverUser, serverName),
|
|
||||||
"sudo", "-S", "shutdown", "-h", "now",
|
|
||||||
}
|
|
||||||
log.Printf("Full command: %s", strings.Join(cmdArgs, " "))
|
|
||||||
|
|
||||||
// Add more SSH options to handle potential issues
|
// Add more SSH options to handle potential issues
|
||||||
cmd := exec.Command("sshpass", "-p", password, "ssh",
|
cmd := exec.Command("sshpass", "-p", password, "ssh",
|
||||||
"-o", "StrictHostKeyChecking=no",
|
"-o", "StrictHostKeyChecking=no",
|
||||||
|
|
@ -593,18 +572,6 @@ func shutdownServer(password string) error {
|
||||||
log.Println("Trying direct SSH with password via stdin")
|
log.Println("Trying direct SSH with password via stdin")
|
||||||
log.Printf("Password being used: %s", password)
|
log.Printf("Password being used: %s", password)
|
||||||
|
|
||||||
// Create command args
|
|
||||||
cmdArgs := []string{
|
|
||||||
"ssh",
|
|
||||||
"-o", "StrictHostKeyChecking=no",
|
|
||||||
"-o", "UserKnownHostsFile=/dev/null",
|
|
||||||
"-o", "LogLevel=ERROR",
|
|
||||||
"-o", "ConnectTimeout=10",
|
|
||||||
fmt.Sprintf("%s@%s", serverUser, serverName),
|
|
||||||
"sudo", "-S", "shutdown", "-h", "now",
|
|
||||||
}
|
|
||||||
log.Printf("Full command: %s", strings.Join(cmdArgs, " "))
|
|
||||||
|
|
||||||
cmd := exec.Command("ssh",
|
cmd := exec.Command("ssh",
|
||||||
"-o", "StrictHostKeyChecking=no",
|
"-o", "StrictHostKeyChecking=no",
|
||||||
"-o", "UserKnownHostsFile=/dev/null",
|
"-o", "UserKnownHostsFile=/dev/null",
|
||||||
|
|
@ -629,17 +596,6 @@ func shutdownServer(password string) error {
|
||||||
log.Println("Trying simpler shutdown command as fallback")
|
log.Println("Trying simpler shutdown command as fallback")
|
||||||
log.Printf("Password being used: %s", password)
|
log.Printf("Password being used: %s", password)
|
||||||
|
|
||||||
// Create command args
|
|
||||||
cmdArgs = []string{
|
|
||||||
"ssh",
|
|
||||||
"-o", "StrictHostKeyChecking=no",
|
|
||||||
"-o", "UserKnownHostsFile=/dev/null",
|
|
||||||
"-o", "LogLevel=ERROR",
|
|
||||||
fmt.Sprintf("%s@%s", serverUser, serverName),
|
|
||||||
"sudo", "shutdown", "now",
|
|
||||||
}
|
|
||||||
log.Printf("Full command: %s", strings.Join(cmdArgs, " "))
|
|
||||||
|
|
||||||
cmd = exec.Command("ssh",
|
cmd = exec.Command("ssh",
|
||||||
"-o", "StrictHostKeyChecking=no",
|
"-o", "StrictHostKeyChecking=no",
|
||||||
"-o", "UserKnownHostsFile=/dev/null",
|
"-o", "UserKnownHostsFile=/dev/null",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue