-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
198 lines (159 loc) · 5.75 KB
/
install.sh
File metadata and controls
198 lines (159 loc) · 5.75 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
#
# Gitbacker installer — self-hosted git backup tool
#
# bash -c "$(curl -fsSL https://gitbacker.com/install.sh)"
#
# Or:
# curl -fsSL https://gitbacker.com/install.sh | bash
#
# What this does:
# 1. Checks for Docker and Docker Compose
# 2. Prompts for install directory (default: ./gitbacker in current folder)
# 3. Generates a random JWT secret
# 4. Pulls images and starts all services
# 5. Seeds the admin account
# 6. Prints the URL to access Gitbacker
#
set -euo pipefail
main() {
REPO="https://raw.githubusercontent.com/gitbckr/gitbacker/main"
VERSION="${GITBACKER_VERSION:-latest}"
# --- Helpers ---
info() { printf "\033[1;34m==>\033[0m %s\n" "$*"; }
ok() { printf "\033[1;32m==>\033[0m %s\n" "$*"; }
warn() { printf "\033[1;33m==>\033[0m %s\n" "$*"; }
fail() { printf "\033[1;31m==>\033[0m %s\n" "$*" >&2; exit 1; }
prompt() {
# Read from /dev/tty so prompts work even under `curl | bash`.
local prompt_text="$1" default="${2:-}" reply
if [ -r /dev/tty ]; then
printf "%s " "$prompt_text" > /dev/tty
read -r reply < /dev/tty || reply=""
printf "%s" "${reply:-$default}"
else
printf "%s" "$default"
fi
}
choose_install_dir() {
# Priority:
# 1. GITBACKER_DIR env var (scripted/CI use — no prompt)
# 2. Interactive: default to $PWD/gitbacker, let user confirm or override
# 3. Non-interactive fallback: $PWD/gitbacker
if [ -n "${GITBACKER_DIR:-}" ]; then
printf "%s" "$GITBACKER_DIR"
return
fi
local default="$PWD/gitbacker"
if [ ! -r /dev/tty ]; then
printf "%s" "$default"
return
fi
local answer
answer=$(prompt "Install Gitbacker to $default? [Y/n]" "y")
case "$answer" in
""|[yY]|[yY][eE][sS])
printf "%s" "$default"
;;
*)
local custom
custom=$(prompt "Enter install path:" "$default")
# Expand leading ~ manually (shell doesn't expand from read)
custom="${custom/#\~/$HOME}"
printf "%s" "${custom:-$default}"
;;
esac
}
check_command() {
command -v "$1" >/dev/null 2>&1 || fail "$1 is required but not installed. See https://docs.docker.com/get-docker/"
}
random_secret() {
head -c 32 /dev/urandom | base64 | tr -d '/+=' | head -c 43
}
wait_for_healthy() {
local url="$1" retries="${2:-30}" delay="${3:-2}"
info "Waiting for API to be ready..."
for i in $(seq 1 "$retries"); do
if curl -sf "$url" >/dev/null 2>&1; then
return 0
fi
sleep "$delay"
done
fail "API did not become healthy after $((retries * delay))s"
}
# --- Preflight checks ---
info "Checking prerequisites..."
check_command docker
docker compose version >/dev/null 2>&1 || fail "Docker Compose V2 is required (docker compose, not docker-compose)"
check_command curl
ok "Docker and Docker Compose found"
# --- Install directory ---
INSTALL_DIR=$(choose_install_dir)
info "Using install directory: $INSTALL_DIR"
# If an existing install is found (either compose.yml OR .env present),
# take the update path. Never regenerate secrets on top of an existing install.
if [ -d "$INSTALL_DIR" ] && { [ -f "$INSTALL_DIR/docker-compose.yml" ] || [ -f "$INSTALL_DIR/.env" ]; }; then
info "Existing installation detected at $INSTALL_DIR — updating in place"
cd "$INSTALL_DIR"
# Refresh compose file to pick up any new services/env vars/volumes.
# .env and .admin-credentials are preserved (never overwritten).
info "Refreshing docker-compose.yml..."
curl -fsSL "$REPO/docker-compose.yml" -o docker-compose.yml
info "Pulling latest images..."
VERSION="$VERSION" docker compose pull </dev/null
info "Restarting services..."
VERSION="$VERSION" docker compose up -d </dev/null
ok "Gitbacker updated and running at http://localhost:3000"
if [ -f ".admin-credentials" ]; then
echo ""
echo " Your admin credentials are in: $INSTALL_DIR/.admin-credentials"
fi
exit 0
fi
info "Installing to $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"
# --- Create data directories ---
mkdir -p "${INSTALL_DIR}/data/postgres" "${INSTALL_DIR}/data/redis" "${INSTALL_DIR}/data/backups"
# --- Download configuration ---
info "Downloading configuration..."
curl -fsSL "$REPO/docker-compose.yml" -o docker-compose.yml
curl -fsSL "$REPO/.env.example" -o .env
# --- Patch .env for production ---
JWT_SECRET=$(random_secret)
ADMIN_PASSWORD=$(random_secret | head -c 16)
sed -i.bak "s|^JWT_SECRET=.*|JWT_SECRET=$JWT_SECRET|" .env
sed -i.bak "s|^ENVIRONMENT=.*|ENVIRONMENT=production|" .env
rm -f .env.bak
cat > .admin-credentials <<CREDS
Gitbacker Admin Credentials
============================
URL: http://localhost:3000
Email: admin@gitbacker.local
Password: $ADMIN_PASSWORD
Store this securely and delete this file.
CREDS
chmod 600 .admin-credentials
ok "Generated secrets and admin credentials"
# --- Start ---
info "Pulling images (this may take a minute)..."
VERSION="$VERSION" docker compose pull --quiet </dev/null
info "Starting Gitbacker..."
VERSION="$VERSION" docker compose up -d </dev/null
# --- Seed admin ---
wait_for_healthy "http://localhost:8000/api/health"
info "Creating admin account..."
docker compose exec -T -e ADMIN_PASSWORD="$ADMIN_PASSWORD" api python seed_admin.py
ok "Gitbacker is running!"
echo ""
echo " Open: http://localhost:3000"
echo " Email: admin@gitbacker.local"
echo " Password: $ADMIN_PASSWORD"
echo ""
echo " Credentials saved to: $INSTALL_DIR/.admin-credentials"
echo ""
echo " To stop: cd $INSTALL_DIR && docker compose down"
echo " To update: bash -c \"\$(curl -fsSL https://gitbacker.com/install.sh)\""
echo ""
}
main "$@"