-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdev-run.sh
More file actions
executable file
·176 lines (150 loc) · 6.1 KB
/
Copy pathdev-run.sh
File metadata and controls
executable file
·176 lines (150 loc) · 6.1 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Developer startup script with continuous logs
#
# Container runtime: auto-detected (podman preferred, falls back to docker).
# Override with DOCKER=docker ./dev-run.sh if needed.
# When using podman, podman-compose is auto-installed into the venv if missing.
if [ -z "$DOCKER" ]; then
if command -v podman >/dev/null 2>&1; then
DOCKER="podman"
else
DOCKER="docker"
fi
fi
COMPOSE="${COMPOSE:-${DOCKER} compose}"
# Locate the venv — worktrees share the main repo's venv, not a local one.
_git_common=$(git rev-parse --git-common-dir 2>/dev/null)
_main_root=$(cd "$(dirname "$_git_common")" && pwd)
if [ -f ".venv/bin/pip" ]; then
VENV_BIN="$(pwd)/.venv/bin"
elif [ -f "$_main_root/.venv/bin/pip" ]; then
VENV_BIN="$_main_root/.venv/bin"
else
VENV_BIN=""
fi
# podman-compose uses a different binary; auto-install into the venv if missing.
if [ "$DOCKER" = "podman" ]; then
if [ -n "$VENV_BIN" ] && [ ! -f "$VENV_BIN/podman-compose" ]; then
echo "Installing podman-compose into venv..."
"$VENV_BIN/pip" install --quiet podman-compose
fi
if [ -n "$VENV_BIN" ] && [ -f "$VENV_BIN/podman-compose" ]; then
COMPOSE="$VENV_BIN/podman-compose"
elif command -v podman-compose >/dev/null 2>&1; then
COMPOSE="podman-compose"
else
echo "Error: podman-compose not available and no venv found to install it."
echo "Run: pip install podman-compose"
exit 1
fi
fi
echo "==== BESS Manager Development Environment Setup ===="
echo "Container runtime: $DOCKER"
# Derive a unique project name from the directory so multiple worktrees
# can run side-by-side without container name conflicts.
export COMPOSE_PROJECT_NAME="bess-$(basename "$(pwd)")"
echo "Compose project: $COMPOSE_PROJECT_NAME"
# Derive stable ports from the directory name so each worktree gets a
# predictable, unique port. The hash maps any directory name into the
# 8080-8179 range (backend) with frontend at +1000.
# Override via BESS_DEV_PORT / BESS_FRONTEND_PORT in .env if needed.
if [ -z "$BESS_DEV_PORT" ]; then
_hash=$(printf '%s' "$(basename "$(pwd)")" | cksum | awk '{print $1}')
export BESS_DEV_PORT=$(( 8080 + _hash % 100 ))
export BESS_FRONTEND_PORT=$(( BESS_DEV_PORT + 1000 ))
fi
export BESS_FRONTEND_PORT="${BESS_FRONTEND_PORT:-$(( BESS_DEV_PORT + 1000 ))}"
# Verify container runtime is available
if ! $DOCKER info > /dev/null 2>&1; then
echo "Error: $DOCKER is not running."
echo "Please start $DOCKER and try again."
exit 1
fi
# Check if .env exists, if not prompt the user
if [ ! -f .env ]; then
echo "Error: .env file not found."
echo "Please create a .env file with HA_URL and HA_TOKEN defined."
exit 1
fi
# Export environment variables from .env file (excluding comments and empty lines)
echo "Loading environment variables from .env..."
set -a # automatically export all variables
source <(grep -v '^#' .env | grep -v '^$' | sed 's/^\s*//')
set +a # stop automatically exporting
# Display which HA instance we're connecting to
echo "Connecting to Home Assistant at: $HA_URL"
# Check if token is still the default
if [[ "$HA_TOKEN" == "your_long_lived_access_token_here" ]]; then
echo "Please edit .env file to add your Home Assistant token."
exit 1
fi
# Ensure requirements.txt exists with needed packages
echo "Checking requirements.txt..."
if [ ! -f backend/requirements.txt ]; then
echo "Please create requirements.txt in backend directory..."
exit 1
fi
# Extract options from config.yaml to backend/dev-options.json for development
# Note: InfluxDB credentials are passed as environment variables, not in options.json
echo "Extracting development options from config.yaml..."
if [ -f config.yaml ] && [ -n "$VENV_BIN" ]; then
"$VENV_BIN/python" << 'EOF'
import yaml
import json
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
options = config.get('options', {})
# Remove InfluxDB from options - credentials come from environment variables
if 'influxdb' in options:
del options['influxdb']
print(" → InfluxDB credentials will be loaded from environment variables")
with open('backend/dev-options.json', 'w') as f:
json.dump(options, f, indent=2)
print("✓ Created backend/dev-options.json (simulates /data/options.json in HA)")
EOF
else
echo "Warning: config.yaml not found in root directory"
fi
# Create dev settings from example template if missing (file is gitignored)
if [ ! -f backend/dev-bess-settings.json ]; then
cp backend/dev-bess-settings.json.example backend/dev-bess-settings.json
echo "✓ Created backend/dev-bess-settings.json from example template"
fi
# Pass host timezone to containers so log timestamps use local time
export TZ=${TZ:-Europe/Stockholm}
echo "Stopping any existing containers for this project..."
$COMPOSE down --remove-orphans 2>/dev/null
# Also remove any stale container with the target name (e.g. left over
# from a previous run or an older compose config without project naming).
CONTAINER_NAME="${COMPOSE_PROJECT_NAME}-dev"
if $DOCKER ps -a --format '{{.Names}}' | grep -qx "$CONTAINER_NAME"; then
echo "Removing stale container: $CONTAINER_NAME"
$DOCKER rm -f "$CONTAINER_NAME" >/dev/null
fi
# Only rebuild frontend if source files changed since last build
FRONTEND_BUILD="frontend/dist"
if [ ! -d "$FRONTEND_BUILD" ] || [ -n "$(find frontend/src frontend/index.html frontend/vite.config.ts frontend/package.json -newer "$FRONTEND_BUILD" 2>/dev/null)" ]; then
echo "Building frontend (changes detected)..."
(cd frontend && npm run build)
else
echo "Frontend unchanged, skipping build."
fi
echo "Building and starting development container..."
$COMPOSE up --build -d
echo "Waiting for BESS to start (following logs)..."
echo ""
print_banner() {
echo ""
echo "========================================================"
echo " BESS Manager running at: http://localhost:${BESS_DEV_PORT}"
echo "========================================================"
echo ""
}
trap 'print_banner; exit 0' INT
# Stream logs; print the banner once BESS is fully started (Uvicorn ready).
$COMPOSE logs -f --no-log-prefix 2>&1 | while IFS= read -r line; do
echo "$line"
if [[ "$line" == *"Uvicorn running on"* ]]; then
print_banner
fi
done