mirror of
https://github.com/harivansh-afk/betterNAS.git
synced 2026-04-15 09:01:13 +00:00
prepare runtime loop
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
parent
e75b3f98a6
commit
f754a217f4
12 changed files with 386 additions and 44 deletions
|
|
@ -4,10 +4,11 @@ set -euo pipefail
|
|||
|
||||
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
env_file="$repo_root/.env.agent"
|
||||
example_env_file="$repo_root/.env.agent.example"
|
||||
|
||||
if [[ ! -f "$env_file" ]]; then
|
||||
cp "$example_env_file" "$env_file"
|
||||
# shellcheck disable=SC1091
|
||||
source "$repo_root/scripts/lib/agent-env.sh"
|
||||
betternas_write_agent_env_file "$env_file" "$repo_root"
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC1091
|
||||
|
|
|
|||
|
|
@ -51,8 +51,8 @@ if ! nextcloud_is_installed; then
|
|||
--database-host db \
|
||||
--database-user nextcloud \
|
||||
--database-pass nextcloud \
|
||||
--admin-user admin \
|
||||
--admin-pass admin \
|
||||
--admin-user "$NEXTCLOUD_ADMIN_USER" \
|
||||
--admin-pass "$NEXTCLOUD_ADMIN_PASSWORD" \
|
||||
--data-dir /var/www/html/data
|
||||
|
||||
if ! nextcloud_is_installed; then
|
||||
|
|
|
|||
139
scripts/lib/agent-env.sh
Normal file
139
scripts/lib/agent-env.sh
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
betternas_default_clone_name() {
|
||||
local repo_path="${1-}"
|
||||
local repo_name
|
||||
|
||||
repo_name="$(basename "$repo_path")"
|
||||
|
||||
case "$repo_name" in
|
||||
betterNAS)
|
||||
printf '%s' "betternas-main"
|
||||
;;
|
||||
*)
|
||||
printf '%s' "$repo_name"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
betternas_default_compose_project_seed() {
|
||||
local repo_path="${1-}"
|
||||
local clone_name="${2-}"
|
||||
|
||||
if [[ -n "$clone_name" ]]; then
|
||||
printf '%s' "$clone_name"
|
||||
return 0
|
||||
fi
|
||||
|
||||
betternas_default_clone_name "$repo_path"
|
||||
}
|
||||
|
||||
betternas_normalize_compose_project_name() {
|
||||
local raw_value="${1-}"
|
||||
local value
|
||||
|
||||
value="${raw_value,,}"
|
||||
value="$(
|
||||
printf '%s' "$value" | sed -E \
|
||||
-e 's/[^a-z0-9_-]+/-/g' \
|
||||
-e 's/^[^a-z0-9]+//' \
|
||||
-e 's/[-_]+$//' \
|
||||
-e 's/-+/-/g'
|
||||
)"
|
||||
|
||||
if [[ -z "$value" ]]; then
|
||||
printf 'Unable to derive a valid Docker Compose project name from %q.\n' "$raw_value" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
betternas_resolve_compose_project_name() {
|
||||
local repo_path="${1-}"
|
||||
local override="${2-}"
|
||||
local clone_name="${3-}"
|
||||
local seed
|
||||
|
||||
if [[ -n "$override" ]]; then
|
||||
seed="$override"
|
||||
else
|
||||
seed="$(betternas_default_compose_project_seed "$repo_path" "$clone_name")"
|
||||
fi
|
||||
|
||||
betternas_normalize_compose_project_name "$seed"
|
||||
}
|
||||
|
||||
betternas_default_ports() {
|
||||
local repo_path="${1-}"
|
||||
local clone_name="${2-}"
|
||||
local hash
|
||||
local slot
|
||||
local base
|
||||
|
||||
if [[ -z "$clone_name" ]]; then
|
||||
clone_name="$(betternas_default_clone_name "$repo_path")"
|
||||
fi
|
||||
|
||||
case "$clone_name" in
|
||||
betternas-main)
|
||||
printf '%s %s %s\n' "8080" "3090" "3001"
|
||||
return 0
|
||||
;;
|
||||
betterNAS-runtime)
|
||||
printf '%s %s %s\n' "41080" "41090" "41001"
|
||||
return 0
|
||||
;;
|
||||
betterNAS-control)
|
||||
printf '%s %s %s\n' "42080" "42090" "42001"
|
||||
return 0
|
||||
;;
|
||||
betterNAS-node)
|
||||
printf '%s %s %s\n' "43080" "43090" "43001"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
|
||||
hash="$(printf '%s' "$repo_path" | cksum | awk '{print $1}')"
|
||||
slot=$((hash % 1000))
|
||||
base=$((45000 + slot * 20))
|
||||
|
||||
printf '%s %s %s\n' "$base" "$((base + 10))" "$((base + 1))"
|
||||
}
|
||||
|
||||
betternas_write_env_assignment() {
|
||||
local key="${1-}"
|
||||
local value="${2-}"
|
||||
|
||||
printf '%s=%q\n' "$key" "$value"
|
||||
}
|
||||
|
||||
betternas_write_agent_env_file() {
|
||||
local env_path="${1-}"
|
||||
local repo_path="${2-}"
|
||||
local clone_name
|
||||
local compose_project_name
|
||||
local nextcloud_port
|
||||
local node_agent_port
|
||||
local control_plane_port
|
||||
|
||||
clone_name="$(betternas_default_clone_name "$repo_path")"
|
||||
compose_project_name="$(betternas_resolve_compose_project_name "$repo_path" "" "$clone_name")"
|
||||
read -r nextcloud_port node_agent_port control_plane_port <<<"$(betternas_default_ports "$repo_path" "$clone_name")"
|
||||
|
||||
{
|
||||
echo "# Generated by pnpm agent:bootstrap"
|
||||
betternas_write_env_assignment "BETTERNAS_CLONE_NAME" "$clone_name"
|
||||
betternas_write_env_assignment "COMPOSE_PROJECT_NAME" "$compose_project_name"
|
||||
betternas_write_env_assignment "BETTERNAS_CONTROL_PLANE_PORT" "$control_plane_port"
|
||||
betternas_write_env_assignment "BETTERNAS_NODE_AGENT_PORT" "$node_agent_port"
|
||||
betternas_write_env_assignment "BETTERNAS_NEXTCLOUD_PORT" "$nextcloud_port"
|
||||
betternas_write_env_assignment "BETTERNAS_EXPORT_PATH" ".state/${clone_name}/export"
|
||||
betternas_write_env_assignment "BETTERNAS_VERSION" "local-dev"
|
||||
betternas_write_env_assignment "BETTERNAS_NODE_DIRECT_ADDRESS" "http://localhost:${node_agent_port}"
|
||||
betternas_write_env_assignment "BETTERNAS_EXAMPLE_MOUNT_URL" "http://localhost:${node_agent_port}/dav/"
|
||||
betternas_write_env_assignment "NEXTCLOUD_BASE_URL" "http://localhost:${nextcloud_port}"
|
||||
betternas_write_env_assignment "NEXTCLOUD_ADMIN_USER" "admin"
|
||||
betternas_write_env_assignment "NEXTCLOUD_ADMIN_PASSWORD" "admin"
|
||||
} >"$env_path"
|
||||
}
|
||||
|
|
@ -7,6 +7,9 @@ compose_file="$repo_root/infra/docker/compose.dev.yml"
|
|||
default_env_file="$repo_root/.env.agent"
|
||||
env_file="${BETTERNAS_ENV_FILE:-$default_env_file}"
|
||||
|
||||
# shellcheck disable=SC1091
|
||||
source "$repo_root/scripts/lib/agent-env.sh"
|
||||
|
||||
if [[ -f "$env_file" ]]; then
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
|
|
@ -14,11 +17,19 @@ if [[ -f "$env_file" ]]; then
|
|||
set +a
|
||||
fi
|
||||
|
||||
: "${BETTERNAS_CLONE_NAME:=betternas-main}"
|
||||
: "${COMPOSE_PROJECT_NAME:=betternas-${BETTERNAS_CLONE_NAME}}"
|
||||
: "${BETTERNAS_CONTROL_PLANE_PORT:=3001}"
|
||||
: "${BETTERNAS_NODE_AGENT_PORT:=3090}"
|
||||
: "${BETTERNAS_NEXTCLOUD_PORT:=8080}"
|
||||
if [[ -z "${BETTERNAS_CLONE_NAME:-}" ]]; then
|
||||
BETTERNAS_CLONE_NAME="$(betternas_default_clone_name "$repo_root")"
|
||||
fi
|
||||
|
||||
COMPOSE_PROJECT_NAME="$(
|
||||
betternas_resolve_compose_project_name "$repo_root" "${COMPOSE_PROJECT_NAME:-}" "$BETTERNAS_CLONE_NAME"
|
||||
)"
|
||||
|
||||
read -r default_nextcloud_port default_node_agent_port default_control_plane_port <<<"$(betternas_default_ports "$repo_root" "$BETTERNAS_CLONE_NAME")"
|
||||
|
||||
: "${BETTERNAS_CONTROL_PLANE_PORT:=$default_control_plane_port}"
|
||||
: "${BETTERNAS_NODE_AGENT_PORT:=$default_node_agent_port}"
|
||||
: "${BETTERNAS_NEXTCLOUD_PORT:=$default_nextcloud_port}"
|
||||
: "${BETTERNAS_VERSION:=local-dev}"
|
||||
: "${NEXTCLOUD_ADMIN_USER:=admin}"
|
||||
: "${NEXTCLOUD_ADMIN_PASSWORD:=admin}"
|
||||
|
|
|
|||
|
|
@ -7,34 +7,16 @@ parent_dir="$(cd "$repo_root/.." && pwd)"
|
|||
repo_name="$(basename "$repo_root")"
|
||||
sync_clone_script="$repo_root/scripts/sync-clone"
|
||||
|
||||
declare -A clone_ports=(
|
||||
["betterNAS-runtime"]="41080 41090 41001"
|
||||
["betterNAS-control"]="42080 42090 42001"
|
||||
["betterNAS-node"]="43080 43090 43001"
|
||||
)
|
||||
# shellcheck disable=SC1091
|
||||
source "$repo_root/scripts/lib/agent-env.sh"
|
||||
|
||||
clone_names=("betterNAS-runtime" "betterNAS-control" "betterNAS-node")
|
||||
|
||||
for clone_name in "${clone_names[@]}"; do
|
||||
clone_dir="$parent_dir/$clone_name"
|
||||
|
||||
"$sync_clone_script" "$clone_dir"
|
||||
|
||||
read -r nextcloud_port node_agent_port control_plane_port <<<"${clone_ports[$clone_name]}"
|
||||
|
||||
cat >"$clone_dir/.env.agent" <<EOF
|
||||
BETTERNAS_CLONE_NAME=${clone_name}
|
||||
COMPOSE_PROJECT_NAME=${clone_name}
|
||||
BETTERNAS_NEXTCLOUD_PORT=${nextcloud_port}
|
||||
BETTERNAS_NODE_AGENT_PORT=${node_agent_port}
|
||||
BETTERNAS_CONTROL_PLANE_PORT=${control_plane_port}
|
||||
BETTERNAS_EXPORT_PATH=.state/${clone_name}/export
|
||||
BETTERNAS_VERSION=local-dev
|
||||
BETTERNAS_NODE_DIRECT_ADDRESS=http://localhost:${node_agent_port}
|
||||
BETTERNAS_EXAMPLE_MOUNT_URL=http://localhost:${node_agent_port}/dav/
|
||||
NEXTCLOUD_BASE_URL=http://localhost:${nextcloud_port}
|
||||
NEXTCLOUD_ADMIN_USER=admin
|
||||
NEXTCLOUD_ADMIN_PASSWORD=admin
|
||||
EOF
|
||||
betternas_write_agent_env_file "$clone_dir/.env.agent" "$clone_dir"
|
||||
done
|
||||
|
||||
cat <<EOF
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue