Files
nupst/test/manualdocker/01-setup-v3-container.sh

149 lines
4.6 KiB
Bash
Raw Normal View History

#!/bin/bash
#
# Setup Docker container with systemd and install NUPST v3
# This creates a container from commit 806f81c6a057a2a5da586b96a231d391f12eb1bb (v3)
#
set -e
CONTAINER_NAME="nupst-test-v3"
V3_COMMIT="806f81c6a057a2a5da586b96a231d391f12eb1bb"
echo "================================================"
echo " NUPST v3 Test Container Setup"
echo "================================================"
echo ""
# Check if container already exists
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "⚠️ Container ${CONTAINER_NAME} already exists"
read -p "Remove and recreate? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "→ Stopping and removing existing container..."
docker stop ${CONTAINER_NAME} 2>/dev/null || true
docker rm ${CONTAINER_NAME} 2>/dev/null || true
else
echo "Exiting. Remove manually with: docker rm -f ${CONTAINER_NAME}"
exit 1
fi
fi
echo "→ Creating Docker container (will install systemd)..."
docker run -d \
--name ${CONTAINER_NAME} \
--privileged \
--cgroupns=host \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw \
ubuntu:22.04 \
/bin/bash -c "apt-get update && apt-get install -y systemd systemd-sysv && exec /sbin/init"
echo "→ Waiting for systemd to initialize..."
sleep 10
echo "→ Waiting for dpkg lock to be released..."
docker exec ${CONTAINER_NAME} bash -c "
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
echo ' Waiting for dpkg lock...'
sleep 2
done
echo ' dpkg lock released'
"
echo "→ Installing prerequisites in container..."
docker exec ${CONTAINER_NAME} bash -c "
apt-get update -qq
apt-get install -y -qq git curl sudo jq
"
echo "→ Cloning NUPST v3 (commit ${V3_COMMIT})..."
docker exec ${CONTAINER_NAME} bash -c "
cd /opt
git clone https://code.foss.global/serve.zone/nupst.git
cd nupst
git checkout ${V3_COMMIT}
echo 'Checked out commit:'
git log -1 --oneline
"
echo "→ Running NUPST v3 installation directly (bypassing install.sh auto-update)..."
docker exec ${CONTAINER_NAME} bash -c "
cd /opt/nupst
# Run setup.sh directly to avoid install.sh trying to update to v4
bash setup.sh -y
"
echo "→ Creating NUPST configuration using real UPS data from .nogit/env.json..."
# Check if .nogit/env.json exists
if [ ! -f "../../.nogit/env.json" ]; then
echo "❌ Error: .nogit/env.json not found"
echo "This file contains test UPS credentials and is required for testing"
exit 1
fi
# Read UPS data from .nogit/env.json and create v3 config
docker exec ${CONTAINER_NAME} bash -c "mkdir -p /etc/nupst"
# Generate config from .nogit/env.json using jq
cat ../../.nogit/env.json | jq -r '
{
"upsList": [
{
"id": "test-ups-v1",
"name": "Test UPS (SNMP v1)",
"host": .testConfigV1.snmp.host,
"port": .testConfigV1.snmp.port,
"community": .testConfigV1.snmp.community,
"version": (.testConfigV1.snmp.version | tostring),
"batteryLowOID": "1.3.6.1.4.1.935.1.1.1.3.3.1.0",
"onBatteryOID": "1.3.6.1.4.1.935.1.1.1.3.3.2.0",
"shutdownCommand": "echo \"Shutdown triggered for test-ups-v1\""
},
{
"id": "test-ups-v3",
"name": "Test UPS (SNMP v3)",
"host": .testConfigV3.snmp.host,
"port": .testConfigV3.snmp.port,
"version": (.testConfigV3.snmp.version | tostring),
"securityLevel": .testConfigV3.snmp.securityLevel,
"username": .testConfigV3.snmp.username,
"authProtocol": .testConfigV3.snmp.authProtocol,
"authKey": .testConfigV3.snmp.authKey,
"batteryLowOID": "1.3.6.1.4.1.935.1.1.1.3.3.1.0",
"onBatteryOID": "1.3.6.1.4.1.935.1.1.1.3.3.2.0",
"shutdownCommand": "echo \"Shutdown triggered for test-ups-v3\""
}
],
"groups": []
}' | docker exec -i ${CONTAINER_NAME} tee /etc/nupst/config.json > /dev/null
echo " ✓ Real UPS config created at /etc/nupst/config.json (from .nogit/env.json)"
echo "→ Enabling NUPST systemd service..."
docker exec ${CONTAINER_NAME} bash -c "
nupst enable
"
echo "→ Starting NUPST service..."
docker exec ${CONTAINER_NAME} bash -c "
nupst start
"
echo ""
echo "================================================"
echo " ✓ NUPST v3 Container Ready"
echo "================================================"
echo ""
echo "Container name: ${CONTAINER_NAME}"
echo "NUPST version: v3 (commit ${V3_COMMIT})"
echo ""
echo "Useful commands:"
echo " docker exec -it ${CONTAINER_NAME} bash"
echo " docker exec ${CONTAINER_NAME} systemctl status nupst"
echo " docker exec ${CONTAINER_NAME} nupst --version"
echo " docker stop ${CONTAINER_NAME}"
echo " docker start ${CONTAINER_NAME}"
echo " docker rm -f ${CONTAINER_NAME}"
echo ""