-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev
More file actions
executable file
·276 lines (218 loc) · 6.51 KB
/
Copy pathdev
File metadata and controls
executable file
·276 lines (218 loc) · 6.51 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
set -e
# dev-command v2.0.1
# One-command containerized development environments, Podman-native.
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"
CONTAINERFILE="$CONTAINER_DIR/Containerfile"
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 container (one-off if not running)"
echo -e "${DEV_TAG_BLUE} dev init Initialize container environment"
echo -e "${DEV_TAG_BLUE} dev clean Remove container image and environment"
echo
}
require_podman() {
if ! command -v podman >/dev/null 2>&1; then
echo -e "${DEV_TAG_RED} Error: podman is not installed."
echo -e "${DEV_TAG_RED} dev v2 is Podman-native: install podman and podman-compose, then run again."
exit 1
fi
if ! command -v podman-compose >/dev/null 2>&1; then
echo -e "${DEV_TAG_RED} Error: podman-compose is not installed."
echo -e "${DEV_TAG_RED} Install podman-compose (e.g. pacman -S podman podman-compose), then run again."
exit 1
fi
}
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)
}
compose_project_name() {
grep -m1 '^name:' "$COMPOSE_FILE" | awk '{print $2}'
}
compose_image_exists() {
local name
name=$(compose_project_name)
# podman-compose tags images "<project>_app" (underscore) and short names
# are normalized with a localhost/ prefix. Check both forms; falls back
# to rebuilding if the naming ever differs.
for img in "${name}-app" "localhost/${name}-app" "${name}_app" "localhost/${name}_app"; do
if podman image exists "$img" 2>/dev/null; then
return 0
fi
done
return 1
}
dev_dev() {
require_podman
ensure_project_root
ensure_initialized
export_host_ids
if ! compose_image_exists; then
echo -e "${DEV_TAG_BLUE} No image found. Building…"
podman-compose -f "$COMPOSE_FILE" build
fi
echo -e "${DEV_TAG_BLUE} Starting…"
podman-compose -f "$COMPOSE_FILE" run --rm --service-ports app
}
dev_build() {
require_podman
ensure_project_root
ensure_initialized
export_host_ids
podman-compose -f "$COMPOSE_FILE" build
}
dev_rebuild() {
require_podman
ensure_project_root
ensure_initialized
export_host_ids
podman-compose -f "$COMPOSE_FILE" build --no-cache
}
dev_run() {
require_podman
ensure_project_root
ensure_initialized
export_host_ids
podman-compose -f "$COMPOSE_FILE" run --rm --service-ports app
}
dev_cli() {
require_podman
ensure_project_root
ensure_initialized
export_host_ids
if cid=$(podman ps \
--filter "label=com.docker.compose.project=$(compose_project_name)" \
--filter "label=com.docker.compose.service=app" -q 2>/dev/null); then
podman exec -it "$cid" sh
else
echo -e "${DEV_TAG_BLUE} App not running. Opening a shell in a one-off container…"
echo -e "${DEV_TAG_BLUE} Tip: run 'npm install' to set up dependencies"
podman-compose -f "$COMPOSE_FILE" run --rm --service-ports app sh
fi
}
dev_init() {
require_podman
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 Containerfile
sed -e "s|{{IMAGE}}|$IMAGE|g" "$TEMPLATES_DIR/Containerfile" > "$CONTAINERFILE"
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" "$CONTAINERFILE"
sed -i "/{{NPM_HARDENING}}/d" "$CONTAINERFILE"
rm "$tmpf"
else
sed -i "/{{NPM_HARDENING}}/d" "$CONTAINERFILE"
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() {
require_podman
ensure_project_root
if [ ! -d "$CONTAINER_DIR" ]; then
dev_help
exit 1
fi
echo -e "${DEV_TAG_BLUE} Cleaning…"
podman-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