first commit

This commit is contained in:
stephan
2025-06-29 07:39:33 +02:00
commit 4ed4d69b21
20 changed files with 2754 additions and 0 deletions

171
deploy.sh Executable file
View File

@ -0,0 +1,171 @@
#!/bin/bash
# Deployment script for Wilcon Client Dashboard
# --- Configuration ---
AGENT_USER="agent_user"
SERVICE_NAME="wilcon-agent"
BACKEND_PORT="8080"
BACKEND_SCRIPT="wilcon.py"
FRONTEND_FILE="index.html"
# --- Functions ---
usage() {
echo "Usage: $0 <target_user> <target_ip> [target_path]"
echo " <target_user>: Username on the target device (e.g., 'ubuntu', 'pi')."
echo " <target_ip>: IP address or hostname of the target device."
echo " [target_path]: Optional. Full path on the target device where the project will be deployed. "
echo " Defaults to '/opt/wilcon_agent'."
exit 1
}
setup_ssh_key() {
echo "--- Setting up SSH Key for passwordless access ---"
# Check if ssh-keygen exists
if ! command -v ssh-keygen &>/dev/null; then
echo "Error: ssh-keygen not found. Please install OpenSSH client utilities."
exit 1
fi
# Check if ssh-copy-id exists
if ! command -v ssh-copy-id &>/dev/null; then
echo "Error: ssh-copy-id not found. Please install OpenSSH client utilities (often in 'openssh-client' or 'ssh-client' package)."
exit 1
fi
# Check if SSH key pair exists
if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
echo " SSH key pair not found. Generating new SSH key..."
ssh-keygen -t rsa -b 4096 -f "$HOME/.ssh/id_rsa" -N "" # -N "" for no passphrase
if [ $? -ne 0 ]; then
echo "Error: Failed to generate SSH key. Aborting."
exit 1
fi
echo " Local SSH key generated."
else
echo " Local SSH key pair already exists."
fi
# Check if the key is already authorized on the target device, and copy it if not.
if ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$TARGET_USER@$TARGET_IP" exit 2>/dev/null; then
echo " SSH key is already authorized on the target device. Skipping copy."
else
echo " Copying public key to $TARGET_USER@$TARGET_IP... (You may be prompted for the password for '$TARGET_USER' on '$TARGET_IP')"
ssh-copy-id "$TARGET_USER@$TARGET_IP"
if [ $? -ne 0 ]; then
echo "Error: Failed to copy SSH key. Please ensure SSH is enabled and the password is correct."
exit 1
fi
echo " SSH key copied successfully."
fi
echo ""
}
check_target_online() {
echo "Checking if target device $TARGET_IP is online..."
# Ping the target IP 3 times with a 1-second timeout per ping
if ping -c 3 -W 1 "$TARGET_IP" &>/dev/null; then
echo " Target device is online."
else
echo "Error: Target device $TARGET_IP appears to be offline or unreachable."
echo "Please ensure the device is powered on, connected to the network, and accessible via SSH."
exit 1
fi
echo ""
}
# --- Main Script ---
# Check arguments
if [ -z "$1" ] || [ -z "$2" ]; then
usage
fi
TARGET_USER="$1"
TARGET_IP="$2"
DEFAULT_TARGET_PATH="/opt/wilcon_agent"
TARGET_PATH="${3:-$DEFAULT_TARGET_PATH}"
echo "--- Starting Deployment ---"
echo "Target User: $TARGET_USER"
echo "Target IP: $TARGET_IP"
echo "Target Path: $TARGET_PATH"
echo ""
# Set up SSH key for passwordless access
setup_ssh_key
# Check if the target device is online before proceeding
check_target_online
# 1. Generate requirements.txt on the local machine
echo "1. Generating requirements.txt..."
if [ -d "venv" ]; then
source venv/bin/activate
pip freeze >requirements.txt
deactivate
else
echo "Warning: 'venv' not found. Assuming dependencies are globally installed or already in requirements.txt."
pip freeze >requirements.txt 2>/dev/null || echo "Could not generate requirements.txt. Ensure pip is installed."
fi
if [ ! -f "requirements.txt" ]; then
echo "Error: requirements.txt could not be created. Aborting."
exit 1
fi
echo " requirements.txt generated."
echo ""
# 2. Prepare target directory on the remote host
echo "2. Preparing target directory on $TARGET_IP..."
echo " This may ask for the sudo password for '$TARGET_USER' to create/access '$TARGET_PATH'."
REMOTE_DIR_PREP_COMMAND="
set -e
echo 'Remote: Attempting to create directory $TARGET_PATH...'
sudo mkdir -p '$TARGET_PATH'
echo 'Remote: Attempting to set ownership for $TARGET_PATH...'
sudo chown -R $TARGET_USER:\$(id -gn $TARGET_USER) '$TARGET_PATH'
echo 'Remote: Directory preparation commands completed.'
"
if ! ssh -t "$TARGET_USER@$TARGET_IP" "$REMOTE_DIR_PREP_COMMAND"; then
echo "Error: Failed to create or set permissions for '$TARGET_PATH' on the target device."
echo "Please ensure user '$TARGET_USER' has sudo privileges and the path is correct."
exit 1
fi
echo " Target directory prepared successfully."
echo ""
# 3. Transfer project files to the target device
echo "3. Transferring project files to $TARGET_USER@$TARGET_IP:$TARGET_PATH..."
# Using tar to pipe files over SSH, which is more robust than 'scp -r .'
tar cf - --exclude=venv . | ssh "$TARGET_USER@$TARGET_IP" "
set -e
echo 'Remote: Navigating to $TARGET_PATH...'
cd '$TARGET_PATH'
echo 'Remote: Starting file extraction...'
tar xf -
echo 'Remote: File extraction completed.'
"
if [ $? -ne 0 ]; then
echo "Error: File transfer failed. Check SSH connectivity, permissions, and if 'tar' is installed on both systems."
exit 1
fi
echo " Files transferred successfully."
echo ""
# 4. Make installation script executable on the target device
echo "4. Making installation script executable on target..."
ssh "$TARGET_USER@$TARGET_IP" "chmod +x '$TARGET_PATH/install_service.sh'"
if [ $? -ne 0 ]; then
echo "Error: Failed to make install_service.sh executable."
exit 1
fi
echo " Installation script is now executable."
echo ""
echo "--- Deployment Complete! ---"
echo "Project files have been successfully deployed to $TARGET_IP:$TARGET_PATH"
echo ""
echo "Next Step: Log in to the target device and run the installation script:"
echo " ssh $TARGET_USER@$TARGET_IP"
echo " cd $TARGET_PATH"
echo " sudo ./install_service.sh"