feat: add a start docker script

Adds a start docker script so that we have required values like OSS_JWT_SECRET at runtime
This commit is contained in:
Abhishek Kumar 2026-06-09 11:18:20 +05:30
parent 49e68b49d5
commit bdf43df307
7 changed files with 258 additions and 20 deletions

95
scripts/start_docker.ps1 Normal file
View file

@ -0,0 +1,95 @@
$ErrorActionPreference = 'Stop'
$EnvFile = '.env'
$Registry = if ([string]::IsNullOrEmpty($env:REGISTRY)) { 'ghcr.io/dograh-hq' } else { $env:REGISTRY }
$EnableTelemetry = if ([string]::IsNullOrEmpty($env:ENABLE_TELEMETRY)) { 'true' } else { $env:ENABLE_TELEMETRY }
$Utf8NoBom = [System.Text.UTF8Encoding]::new($false)
function New-HexSecret {
$bytes = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
return -join ($bytes | ForEach-Object { $_.ToString('x2') })
}
function Get-DotEnvValue {
param(
[string]$Path,
[string]$Key
)
if (-not (Test-Path $Path)) {
return $null
}
$resolvedPath = (Resolve-Path $Path).Path
foreach ($line in [System.IO.File]::ReadLines($resolvedPath)) {
if ($line.StartsWith("$Key=")) {
return $line.Substring($Key.Length + 1)
}
}
return $null
}
function Set-DotEnvValue {
param(
[string]$Path,
[string]$Key,
[string]$Value
)
$lines = New-Object System.Collections.Generic.List[string]
$updated = $false
if (Test-Path $Path) {
$resolvedPath = (Resolve-Path $Path).Path
foreach ($line in [System.IO.File]::ReadLines($resolvedPath)) {
if ($line.StartsWith("$Key=")) {
$lines.Add("$Key=$Value")
$updated = $true
} else {
$lines.Add($line)
}
}
}
if (-not $updated) {
$lines.Add("$Key=$Value")
}
[System.IO.File]::WriteAllLines((Join-Path (Get-Location) $Path), $lines, $Utf8NoBom)
}
if (-not (Test-Path 'docker-compose.yaml')) {
Write-Error 'docker-compose.yaml not found. Download it first, then re-run this script.'
exit 1
}
$existingSecret = Get-DotEnvValue -Path $EnvFile -Key 'OSS_JWT_SECRET'
if ([string]::IsNullOrEmpty($existingSecret)) {
Set-DotEnvValue -Path $EnvFile -Key 'OSS_JWT_SECRET' -Value (New-HexSecret)
Write-Host "Created OSS_JWT_SECRET in $EnvFile."
} else {
Write-Host "OSS_JWT_SECRET is already set in $EnvFile."
}
Write-Host ''
Write-Host "Docker registry: $Registry"
Write-Host "Telemetry enabled: $EnableTelemetry"
Write-Host ''
Write-Host 'This will run:'
Write-Host " `$env:REGISTRY = '$Registry'; `$env:ENABLE_TELEMETRY = '$EnableTelemetry'; docker compose up --pull always"
Write-Host ''
$answer = Read-Host 'Start Dograh now? [Y/n]'
if ($answer -match '^[Nn]') {
Write-Host 'Dograh was not started.'
exit 0
}
$env:REGISTRY = $Registry
$env:ENABLE_TELEMETRY = $EnableTelemetry
docker compose up --pull always
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

108
scripts/start_docker.sh Executable file
View file

@ -0,0 +1,108 @@
#!/usr/bin/env bash
set -e
ENV_FILE=".env"
REGISTRY="${REGISTRY:-ghcr.io/dograh-hq}"
ENABLE_TELEMETRY="${ENABLE_TELEMETRY:-true}"
fail() {
echo "Error: $*" >&2
exit 1
}
generate_secret() {
if command -v python3 >/dev/null 2>&1 && python3 -c 'import secrets; print(secrets.token_hex(32))'; then
return
fi
if command -v openssl >/dev/null 2>&1 && openssl rand -hex 32; then
return
fi
if [[ -r /dev/urandom ]] && command -v od >/dev/null 2>&1 && command -v tr >/dev/null 2>&1 && od -An -N32 -tx1 /dev/urandom | tr -d ' \n'; then
return
fi
fail "Could not generate OSS_JWT_SECRET. Install python3 or openssl, or set OSS_JWT_SECRET manually in .env."
}
dotenv_value() {
local key=$1
local line
[[ -f "$ENV_FILE" ]] || return 1
while IFS= read -r line || [[ -n "$line" ]]; do
case "$line" in
"$key"=*)
printf '%s\n' "${line#*=}"
return 0
;;
esac
done < "$ENV_FILE"
return 1
}
set_dotenv_value() {
local key=$1
local value=$2
local tmp_file="${ENV_FILE}.tmp.$$"
local line
local updated=false
if [[ -f "$ENV_FILE" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
case "$line" in
"$key"=*)
printf '%s=%s\n' "$key" "$value"
updated=true
;;
*)
printf '%s\n' "$line"
;;
esac
done < "$ENV_FILE" > "$tmp_file"
if [[ "$updated" != "true" ]]; then
printf '%s=%s\n' "$key" "$value" >> "$tmp_file"
fi
mv "$tmp_file" "$ENV_FILE"
else
printf '%s=%s\n' "$key" "$value" > "$ENV_FILE"
fi
}
[[ -f docker-compose.yaml ]] || fail "docker-compose.yaml not found. Download it first, then re-run this script."
existing_secret="$(dotenv_value OSS_JWT_SECRET || true)"
if [[ -z "$existing_secret" ]]; then
set_dotenv_value OSS_JWT_SECRET "$(generate_secret)"
echo "Created OSS_JWT_SECRET in $ENV_FILE."
else
echo "OSS_JWT_SECRET is already set in $ENV_FILE."
fi
echo ""
echo "Docker registry: $REGISTRY"
echo "Telemetry enabled: $ENABLE_TELEMETRY"
echo ""
echo "This will run:"
echo " REGISTRY=$REGISTRY ENABLE_TELEMETRY=$ENABLE_TELEMETRY docker compose up --pull always"
echo ""
if [[ ! -t 0 ]]; then
echo "Run the command above from an interactive shell to start Dograh."
exit 0
fi
read -r -p "Start Dograh now? [Y/n]: " answer
case "$answer" in
[Nn]*)
echo "Dograh was not started."
exit 0
;;
esac
REGISTRY="$REGISTRY" ENABLE_TELEMETRY="$ENABLE_TELEMETRY" docker compose up --pull always