Skip to content

Commit 0873179

Browse files
authored
One liner 2.0 (#142)
* new one liner * Update setup.sh * remove secrets generation * Change services restart to unless-stopped * Set default image tags to pre-release * change port * add test * dont duplicate tests * remove unused stuff
1 parent 30dec0f commit 0873179

3 files changed

Lines changed: 383 additions & 0 deletions

File tree

.github/workflows/test2.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Test setup script 2.0
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- "docker-compose2.0/**"
9+
- ".github/workflows/test2.yml"
10+
pull_request:
11+
branches:
12+
- main
13+
paths:
14+
- "docker-compose2.0/**"
15+
- ".github/workflows/test2.yml"
16+
17+
jobs:
18+
test:
19+
name: Test setup script 2.0
20+
runs-on: [self-hosted, Linux, X64]
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Login to GitHub container registry
26+
uses: docker/login-action@v2
27+
with:
28+
registry: ghcr.io
29+
username: ${{ github.actor }}
30+
password: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Create working directory
33+
run: mkdir temp
34+
35+
- name: Copy compose file
36+
run: cp docker-compose2.0/docker-compose.setup.yaml temp/docker-compose.yaml
37+
38+
- name: Run setup script
39+
working-directory: temp
40+
run: bash ../docker-compose2.0/setup.sh
41+
42+
- name: Wait for stack to be ready
43+
run: sleep 15s
44+
45+
- name: Test health endpoint
46+
run: curl -f http://localhost:8000/api/v1/health
47+
48+
- name: Stop compose stack
49+
if: always()
50+
working-directory: temp
51+
run: docker compose down -v
52+
53+
- name: Cleanup
54+
if: always()
55+
run: sudo rm -rf temp
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
core:
3+
restart: unless-stopped
4+
image: ghcr.io/defguard/defguard:${DEFGUARD_CORE_TAG:?DEFGUARD_CORE_TAG is required}
5+
env_file: .env
6+
environment:
7+
DEFGUARD_DB_HOST: db
8+
DEFGUARD_DB_PORT: 5432
9+
DEFGUARD_ADOPT_EDGE: "edge:50051"
10+
DEFGUARD_ADOPT_GATEWAY: "gateway:50066"
11+
depends_on:
12+
- db
13+
- edge
14+
- gateway
15+
ports:
16+
- "8000:8000"
17+
18+
edge:
19+
restart: unless-stopped
20+
image: ghcr.io/defguard/defguard-proxy:${DEFGUARD_PROXY_TAG:?DEFGUARD_PROXY_TAG is required}
21+
env_file: .env
22+
volumes:
23+
- ./.volumes/certs/edge:/etc/defguard/certs
24+
ports:
25+
- "8080:8080"
26+
27+
gateway:
28+
restart: unless-stopped
29+
image: ghcr.io/defguard/gateway:${DEFGUARD_GATEWAY_TAG:?DEFGUARD_GATEWAY_TAG is required}
30+
env_file: .env
31+
cap_add:
32+
- NET_ADMIN
33+
volumes:
34+
- ./.volumes/certs/gateway:/etc/defguard/certs
35+
environment:
36+
DEFGUARD_STATS_PERIOD: 10
37+
HEALTH_PORT: 55003
38+
ports:
39+
- "51820:51820/udp"
40+
41+
db:
42+
restart: unless-stopped
43+
image: postgres:18-alpine
44+
env_file: .env
45+
volumes:
46+
- ./.volumes/db:/var/lib/postgresql

docker-compose2.0/setup.sh

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
#!/usr/bin/env bash
2+
# shellcheck shell=bash
3+
set -euo pipefail
4+
5+
# Defguard setup script
6+
# Usage: bash <(curl -sSL https://raw.githubusercontent.com/defguard/deployment/main/docker-compose2.0/setup.sh)
7+
#
8+
# Options:
9+
# --dev use development images
10+
# --pre-release use pre-release images
11+
# --help show this help and exit
12+
13+
COMPOSE_FILE_URL="https://raw.githubusercontent.com/defguard/deployment/one-liner-2.0/docker-compose2.0/docker-compose.setup.yaml"
14+
COMPOSE_FILE="./docker-compose.yaml"
15+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || pwd)"
16+
COMPOSE_FILE_LOCAL="${SCRIPT_DIR}/docker-compose.yaml"
17+
18+
DEFGUARD_CORE_TAG="pre-release"
19+
DEFGUARD_PROXY_TAG="pre-release"
20+
DEFGUARD_GATEWAY_TAG="pre-release"
21+
IMAGE_MODE="pre-release"
22+
23+
check_character_support() {
24+
echo -e "$1" | grep -q "$1"
25+
}
26+
27+
init_term() {
28+
if check_character_support ""; then
29+
TXT_CHECK=""
30+
TXT_BEGIN=""
31+
TXT_SUB=""
32+
TXT_X=""
33+
else
34+
TXT_CHECK="+"
35+
TXT_BEGIN=">>"
36+
TXT_SUB=">"
37+
TXT_X="x"
38+
fi
39+
40+
if [[ $TERM == *"256"* ]]; then
41+
C_RED="\033[31m"
42+
C_GREEN="\033[32m"
43+
C_YELLOW="\033[33m"
44+
C_LRED="\033[91m"
45+
C_LGREEN="\033[92m"
46+
C_LYELLOW="\033[93m"
47+
C_LBLUE="\033[94m"
48+
C_BOLD="\033[1m"
49+
C_BG_GREY="\033[100m"
50+
C_END="\033[0m"
51+
else
52+
C_RED=""
53+
C_GREEN=""
54+
C_YELLOW=""
55+
C_LRED=""
56+
C_LGREEN=""
57+
C_LYELLOW=""
58+
C_LBLUE=""
59+
C_BOLD=""
60+
C_BG_GREY=""
61+
C_END=""
62+
fi
63+
}
64+
65+
info() { echo -e " ${TXT_BEGIN} $*"; }
66+
success() { echo -e " ${C_LGREEN}${TXT_CHECK}${C_END} $*"; }
67+
warn() { echo -e " ${C_LYELLOW}${TXT_X}${C_END} $*"; }
68+
error() { echo -e " ${C_LRED}${TXT_X}${C_END} $*" >&2; }
69+
die() { error "$*"; exit 1; }
70+
section() { echo -e "\n${C_BOLD}$*${C_END}\n"; }
71+
72+
print_header() {
73+
echo -e "${C_LBLUE}"
74+
cat << 'LOGO'
75+
#
76+
## #
77+
## ## # # ## #
78+
## ## # # # #
79+
# ## # #### # #### ##### #### # # #### ### #### #
80+
# ## ## # ## # ## # # # # # # # # # ##
81+
## ## # # ######## # # # # # # # # #
82+
# ## ## # # # ## # ##### # # ###### # # #
83+
# ## # # ## # # # # # # # # # # ##
84+
## ## #### # ##### # ####### #### # #### # # #### #
85+
## ## # # #
86+
## # #######
87+
#
88+
LOGO
89+
echo -e "${C_END}"
90+
echo "Defguard docker-compose 2.0 setup script"
91+
echo -e "Copyright ©2023-2026 ${C_BOLD}defguard sp. z o.o.${C_END} <${C_BG_GREY}${C_YELLOW}https://defguard.net/${C_END}>"
92+
echo
93+
}
94+
95+
usage() {
96+
echo "Usage: $(basename "$0") [OPTIONS]"
97+
echo
98+
echo "Available options:"
99+
echo " --dev use development images"
100+
echo " --pre-release use pre-release images"
101+
echo " --help show this help and exit"
102+
echo
103+
exit 0
104+
}
105+
106+
parse_args() {
107+
while [[ $# -gt 0 ]]; do
108+
case "$1" in
109+
--dev)
110+
IMAGE_MODE="dev"
111+
DEFGUARD_CORE_TAG="dev"
112+
DEFGUARD_PROXY_TAG="dev"
113+
DEFGUARD_GATEWAY_TAG="dev"
114+
shift ;;
115+
--pre-release)
116+
IMAGE_MODE="pre-release"
117+
DEFGUARD_CORE_TAG="pre-release"
118+
DEFGUARD_PROXY_TAG="pre-release"
119+
DEFGUARD_GATEWAY_TAG="pre-release"
120+
shift ;;
121+
--help|-h)
122+
usage ;;
123+
*)
124+
die "Unknown option: $1. Run with --help for usage." ;;
125+
esac
126+
done
127+
}
128+
129+
gen_secret() {
130+
if command -v openssl &>/dev/null; then
131+
openssl rand -hex 32
132+
else
133+
tr -dc 'a-f0-9' </dev/urandom 2>/dev/null | head -c 64
134+
fi
135+
}
136+
137+
get_host_ip() {
138+
local ip=""
139+
if command -v ip &>/dev/null; then
140+
ip=$(ip route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="src") print $(i+1); exit}')
141+
fi
142+
if [[ -z "$ip" ]] && command -v ipconfig &>/dev/null; then
143+
ip=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || true)
144+
fi
145+
if [[ -z "$ip" ]]; then
146+
ip=$(hostname -I 2>/dev/null | awk '{print $1}')
147+
fi
148+
printf '%s' "${ip:-127.0.0.1}"
149+
}
150+
151+
check_deps() {
152+
section "Checking dependencies"
153+
154+
if ! command -v docker &>/dev/null; then
155+
die "Docker is not installed. Please install it first: https://docs.docker.com/get-docker/"
156+
fi
157+
success "Docker found: $(docker --version)"
158+
159+
if ! docker compose version &>/dev/null 2>&1; then
160+
die "Docker Compose plugin is not installed. Please install it: https://docs.docker.com/compose/install/"
161+
fi
162+
success "Docker Compose found: $(docker compose version --short)"
163+
164+
if ! command -v curl &>/dev/null && ! command -v wget &>/dev/null; then
165+
die "Neither curl nor wget is available. Please install one of them."
166+
fi
167+
}
168+
169+
check_volumes() {
170+
if [[ -d ".volumes" ]]; then
171+
die ".volumes directory already exists. Remove it before running setup, or this may overwrite an existing installation."
172+
fi
173+
}
174+
175+
download_compose_file() {
176+
section "Preparing compose file"
177+
178+
if [[ -f "$COMPOSE_FILE" ]]; then
179+
success "Found existing ${COMPOSE_FILE} – skipping download."
180+
return
181+
fi
182+
183+
if [[ -f "$COMPOSE_FILE_LOCAL" ]]; then
184+
cp "$COMPOSE_FILE_LOCAL" "$COMPOSE_FILE"
185+
success "Loaded compose file from local path."
186+
return
187+
fi
188+
189+
info "Downloading docker-compose.setup.yaml..."
190+
if command -v curl &>/dev/null; then
191+
curl -sSfL "$COMPOSE_FILE_URL" -o "$COMPOSE_FILE"
192+
else
193+
wget -qO "$COMPOSE_FILE" "$COMPOSE_FILE_URL"
194+
fi
195+
success "Compose file downloaded."
196+
}
197+
198+
write_env() {
199+
section "Generating configuration"
200+
201+
if [[ -f ".env" ]]; then
202+
warn ".env already exists – skipping generation. Remove it to regenerate."
203+
return
204+
fi
205+
206+
local db_password
207+
db_password=$(gen_secret | head -c 24)
208+
209+
case "$IMAGE_MODE" in
210+
dev) info "Image mode: ${C_RED}development${C_END}" ;;
211+
pre-release) info "Image mode: ${C_YELLOW}pre-release${C_END}" ;;
212+
*) info "Image mode: ${C_GREEN}latest${C_END}" ;;
213+
esac
214+
215+
cat > .env << EOF
216+
# Defguard – generated by setup.sh on $(date -u +"%Y-%m-%dT%H:%M:%SZ")
217+
218+
DEFGUARD_CORE_TAG=${DEFGUARD_CORE_TAG}
219+
DEFGUARD_PROXY_TAG=${DEFGUARD_PROXY_TAG}
220+
DEFGUARD_GATEWAY_TAG=${DEFGUARD_GATEWAY_TAG}
221+
222+
POSTGRES_DB=defguard
223+
POSTGRES_USER=defguard
224+
POSTGRES_PASSWORD=${db_password}
225+
226+
DEFGUARD_DB_NAME=defguard
227+
DEFGUARD_DB_USER=defguard
228+
DEFGUARD_DB_PASSWORD=${db_password}
229+
EOF
230+
231+
success ".env written."
232+
}
233+
234+
launch() {
235+
section "Starting Defguard"
236+
237+
mkdir -p .volumes/certs/edge
238+
mkdir -p .volumes/certs/gateway
239+
mkdir -p .volumes/db
240+
241+
info "Pulling images (this may take a moment)..."
242+
docker compose -f "$COMPOSE_FILE" pull
243+
244+
info "Starting services..."
245+
docker compose -f "$COMPOSE_FILE" up -d
246+
247+
success "All services started."
248+
}
249+
250+
show_wizard_info() {
251+
local wizard_url
252+
wizard_url="http://$(get_host_ip):8000"
253+
254+
echo
255+
echo -e " ${TXT_BEGIN} Services status:"
256+
echo
257+
docker compose -f "$COMPOSE_FILE" ps
258+
echo
259+
echo -e "${C_LGREEN}${C_BOLD} ${TXT_CHECK} All containers are up. ${C_END}"
260+
echo
261+
echo -e "${C_LGREEN}${C_BOLD} ╔══════════════════════════════════════════════════════╗"
262+
echo -e " ║ ║"
263+
echo -e " ║ Continue setup in your browser: ║"
264+
printf " ║ %-51s║\n" "${wizard_url}"
265+
echo -e " ║ ║"
266+
echo -e " ╚══════════════════════════════════════════════════════╝${C_END}"
267+
echo
268+
}
269+
270+
main() {
271+
init_term
272+
parse_args "$@"
273+
print_header
274+
check_deps
275+
check_volumes
276+
download_compose_file
277+
write_env
278+
launch
279+
show_wizard_info
280+
}
281+
282+
main "$@"

0 commit comments

Comments
 (0)