-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev
More file actions
executable file
·226 lines (173 loc) · 4.96 KB
/
Copy pathdev
File metadata and controls
executable file
·226 lines (173 loc) · 4.96 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env bash
set -e
DEV_TAG_GREEN="\033[48;5;2m\033[38;5;16m dev \033[0m"
DEV_TAG_RED="\033[48;5;1m\033[38;5;16m dev \033[0m"
DEV_TAG_BLUE="\033[48;5;4m\033[38;5;16m dev \033[0m"
PROJECT_ROOT="$PWD"
PROJECT_NAME=$(basename "$PROJECT_ROOT")
REPO_DIR="$PROJECT_ROOT/repo"
CONTAINER_DIR="$PROJECT_ROOT/container"
COMPOSE_FILE="$CONTAINER_DIR/compose.yml"
DOCKERFILE="$CONTAINER_DIR/Dockerfile"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEMPLATES_DIR="$SCRIPT_DIR/templates"
dev_help() {
echo
echo -e "${DEV_TAG_BLUE} Available commands:"
echo -e "${DEV_TAG_BLUE} dev Start container (builds if no image)"
echo -e "${DEV_TAG_BLUE} dev build Build container image"
echo -e "${DEV_TAG_BLUE} dev rebuild Rebuild container image (--no-cache)"
echo -e "${DEV_TAG_BLUE} dev run Run container (no build)"
echo -e "${DEV_TAG_BLUE} dev cli Open shell in running container"
echo -e "${DEV_TAG_BLUE} dev init Initialize container environment"
echo -e "${DEV_TAG_BLUE} dev clean Remove container image and environment"
echo
}
ensure_project_root() {
if [ ! -d "$REPO_DIR" ]; then
echo -e "${DEV_TAG_RED} Error: repo/ folder not found."
echo -e "${DEV_TAG_RED} Run this command from the project root."
exit 1
fi
}
ensure_initialized() {
if [ ! -d "$CONTAINER_DIR" ]; then
echo -e "${DEV_TAG_RED} Error: container/ not found. Run 'dev init' first."
exit 1
fi
}
export_host_ids() {
export HOST_UID=$(id -u)
export HOST_GID=$(id -g)
}
dev_dev() {
ensure_project_root
ensure_initialized
export_host_ids
if ! docker images "${PROJECT_NAME}-dev-app" -q 2>/dev/null | grep -q .; then
echo -e "${DEV_TAG_BLUE} No image found. Building…"
docker compose --progress plain -f "$COMPOSE_FILE" build
fi
echo -e "${DEV_TAG_BLUE} Starting…"
docker compose -f "$COMPOSE_FILE" run --rm --service-ports app
}
dev_build() {
ensure_project_root
ensure_initialized
export_host_ids
docker compose --progress plain -f "$COMPOSE_FILE" build
}
dev_rebuild() {
ensure_project_root
ensure_initialized
export_host_ids
docker compose --progress plain -f "$COMPOSE_FILE" build --no-cache
}
dev_run() {
ensure_project_root
ensure_initialized
export_host_ids
docker compose -f "$COMPOSE_FILE" run --rm --service-ports app
}
dev_cli() {
ensure_project_root
ensure_initialized
export_host_ids
docker compose -f "$COMPOSE_FILE" exec app sh
}
dev_init() {
ensure_project_root
if [ -d "$CONTAINER_DIR" ]; then
echo -e "${DEV_TAG_RED} Environment already exists."
echo -e "${DEV_TAG_RED} Run: dev"
exit 1
fi
mkdir -p "$CONTAINER_DIR"
RUNTIME=$(printf "%s\n" node ubuntu | fzf --prompt="Select runtime > ")
if [ -z "$RUNTIME" ]; then
echo -e "${DEV_TAG_RED} No runtime selected."
exit 1
fi
IMAGE_TAG=$(curl -s "https://registry.hub.docker.com/v2/repositories/library/${RUNTIME}/tags?page_size=100" |
jq -r '.results[].name' |
grep -E '^[0-9]+(\.[0-9]+)?' |
sort -Vr |
fzf --prompt="Select ${RUNTIME} version > ")
if [ -z "$IMAGE_TAG" ]; then
echo -e "${DEV_TAG_RED} No version selected."
exit 1
fi
IMAGE="${RUNTIME}:${IMAGE_TAG}"
# Render Dockerfile
sed -e "s|{{IMAGE}}|$IMAGE|g" "$TEMPLATES_DIR/Dockerfile" > "$DOCKERFILE"
if [ "$RUNTIME" = "node" ]; then
local tmpf=$(mktemp)
cat > "$tmpf" <<'HARDENING'
# Harden npm config - blocks postinstall hooks and fast-burst worms
RUN echo "ignore-scripts=true" >> /root/.npmrc && \
echo "min-release-age=7" >> /root/.npmrc && \
echo "ignore-scripts=true" >> /etc/npmrc && \
echo "min-release-age=7" >> /etc/npmrc
HARDENING
sed -i "/{{NPM_HARDENING}}/r $tmpf" "$DOCKERFILE"
sed -i "/{{NPM_HARDENING}}/d" "$DOCKERFILE"
rm "$tmpf"
else
sed -i "/{{NPM_HARDENING}}/d" "$DOCKERFILE"
fi
# Render compose.yml
sed -e "s|{{PROJECT_NAME}}|$PROJECT_NAME|g" "$TEMPLATES_DIR/compose.yml" > "$COMPOSE_FILE"
# Inject sinkhosts
local shf=$(mktemp)
while IFS= read -r host; do
[[ -z "$host" || "$host" == \#* ]] && continue
echo " - \"$host:0.0.0.0\""
done < "$TEMPLATES_DIR/sinkhosts" > "$shf"
sed -i "/{{SINKHOSTS}}/r $shf" "$COMPOSE_FILE"
sed -i "/{{SINKHOSTS}}/d" "$COMPOSE_FILE"
rm "$shf"
# Render .env
cp "$TEMPLATES_DIR/.env" "$CONTAINER_DIR/.env"
echo
echo -e "${DEV_TAG_GREEN} Environment initialized."
echo -e "${DEV_TAG_GREEN} Run: dev"
}
dev_clean() {
ensure_project_root
if [ ! -d "$CONTAINER_DIR" ]; then
dev_help
exit 1
fi
echo -e "${DEV_TAG_BLUE} Cleaning…"
docker compose -f "$COMPOSE_FILE" down --rmi all --volumes --remove-orphans || true
rm -rf "$CONTAINER_DIR" || true
echo
echo -e "${DEV_TAG_GREEN} Environment cleaned."
}
CMD="$1"
case "$CMD" in
init)
dev_init
;;
clean)
dev_clean
;;
build)
dev_build
;;
rebuild)
dev_rebuild
;;
run)
dev_run
;;
cli)
dev_cli
;;
"")
dev_dev
;;
*)
dev_help
;;
esac