forked from WEBzaytsev/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-aliases.sh
More file actions
248 lines (215 loc) · 7.34 KB
/
Copy pathdocker-aliases.sh
File metadata and controls
248 lines (215 loc) · 7.34 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
#!/usr/bin/env bash
# docker-aliases.sh — system-wide docker compose aliases, reliable across bash/zsh modes
#
# Install:
# - /etc/profile.d/docker-aliases.sh (aliases definition)
# - /etc/bash.bashrc (loads aliases for interactive non-login bash)
# - /etc/zsh/zshrc (loads aliases for interactive zsh, if present)
#
# Usage:
# sudo ./docker-aliases.sh
# curl -fsSL "URL?v=$(date +%s)" | sudo bash
#
# Options:
# --uninstall remove managed blocks/lines
# --print print generated aliases block and exit
set -euo pipefail
SCRIPTS_RAW_BASE="${SCRIPTS_RAW_BASE:-https://raw.githubusercontent.com/WEBzaytsev/scripts/main}"
_bootstrap_lib() {
[[ -n "${SCRIPTS_COMMON_LOADED:-}" ]] && return 0
local lib=""
if [[ -n "${BASH_SOURCE[0]:-}" && "${BASH_SOURCE[0]}" != "/dev/fd/"* && "${BASH_SOURCE[0]}" != "/dev/stdin" ]]; then
lib="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd)/lib/common.sh"
fi
if [[ -n "$lib" && -f "$lib" ]]; then
# shellcheck source=lib/common.sh
source "$lib"
else
local tmp; tmp="$(mktemp)"
curl -fsSL "${SCRIPTS_RAW_BASE}/lib/common.sh?v=$(date +%s)" -o "$tmp" \
|| { echo "[ERROR] Failed to fetch lib/common.sh" >&2; rm -f "$tmp"; exit 1; }
source "$tmp"
rm -f "$tmp"
fi
}
_bootstrap_lib
# ---------- constants ----------
PROFILE_D_DIR="/etc/profile.d"
ALIASES_FILE="${PROFILE_D_DIR}/docker-aliases.sh"
BASH_SYSTEM_RC="/etc/bash.bashrc"
ZSH_SYSTEM_RC="/etc/zsh/zshrc"
MARKER_BEGIN="# >>> docker-aliases (managed) >>>"
MARKER_END="# <<< docker-aliases (managed) <<<"
BASH_LOADER_LINE='[[ -f /etc/profile.d/docker-aliases.sh ]] && . /etc/profile.d/docker-aliases.sh'
ZSH_LOADER_LINE='[[ -f /etc/profile.d/docker-aliases.sh ]] && source /etc/profile.d/docker-aliases.sh'
# ---------- aliases content ----------
aliases_block_content() {
cat <<'EOF'
# >>> docker-aliases (managed) >>>
# Interactive shells only (skip non-interactive)
case "$-" in
*i*) ;;
*) return 0 2>/dev/null || exit 0 ;;
esac
alias dc='docker compose'
alias dcu='docker compose up'
alias dcud='docker compose up -d'
alias dcub='docker compose up -d --build'
alias dcuw='docker compose up -w'
alias dcd='docker compose down'
alias dcs='docker compose stop'
alias dcb='docker compose build'
alias dcp='docker system prune'
alias dce='docker exec -it'
alias dcl='docker compose logs -f'
alias dclf='docker compose logs -f --tail 100'
alias dcps='docker ps --format="table {{.Names}}\t{{.Ports}}\t{{.Status}}"'
alias dcpu='docker compose pull && docker compose up -d && docker compose ps --format "table {{.Names}}\t{{.Image}}"'
alias dcr='docker compose stop && docker compose up -d --force-recreate'
alias dct='truncate -s 0 /var/lib/docker/containers/*/*-json.log'
alias dcc='docker exec -w /etc/caddy caddy caddy fmt --overwrite && docker exec -w /etc/caddy caddy caddy reload'
alias dccf='docker exec -w /etc/caddy caddy caddy fmt --overwrite && docker exec -w /etc/caddy caddy caddy reload --force'
alias docker-compose='docker compose'
# <<< docker-aliases (managed) <<<
EOF
}
# ---------- managed block helpers ----------
strip_managed_block() {
local f="$1"
[[ -f "$f" ]] || { echo ""; return 0; }
awk -v b="$MARKER_BEGIN" -v e="$MARKER_END" '
$0==b {inblk=1; next}
$0==e {inblk=0; next}
!inblk {print}
' "$f"
}
ensure_aliases_file() {
local desired final outside
desired="$(aliases_block_content)"
if [[ -f "$ALIASES_FILE" ]]; then
outside="$(strip_managed_block "$ALIASES_FILE")"
else
outside=""
fi
if [[ -n "${outside//[[:space:]]/}" ]]; then
final="${outside}"$'\n\n'"${desired}"$'\n'
else
final="${desired}"$'\n'
fi
if [[ -f "$ALIASES_FILE" ]] && cmp -s <(printf "%s" "$final") "$ALIASES_FILE"; then
ok "Already up to date: $ALIASES_FILE"
return 0
fi
mk_backup "$ALIASES_FILE"
write_atomic "$ALIASES_FILE" <<<"$final"
chmod 0644 "$ALIASES_FILE" || true
ok "Installed: $ALIASES_FILE"
}
ensure_line_once() {
local file="$1" line="$2"
[[ -f "$file" ]] || return 1
grep -Fqx "$line" "$file" 2>/dev/null && return 0
mk_backup "$file"
printf "\n# Load docker aliases (managed)\n%s\n" "$line" >>"$file"
ok "Patched: $file"
return 0
}
install_loaders() {
if [[ -f "$BASH_SYSTEM_RC" ]]; then
ensure_line_once "$BASH_SYSTEM_RC" "$BASH_LOADER_LINE" || true
else
warn "No $BASH_SYSTEM_RC found; bash non-login shells may not load aliases automatically."
fi
if [[ -f "$ZSH_SYSTEM_RC" ]]; then
ensure_line_once "$ZSH_SYSTEM_RC" "$ZSH_LOADER_LINE" || true
else
warn "No $ZSH_SYSTEM_RC found; skipping zsh global rc patch."
fi
}
uninstall() {
info "Uninstalling managed blocks/lines..."
if [[ -f "$ALIASES_FILE" ]]; then
local stripped; stripped="$(strip_managed_block "$ALIASES_FILE")"
mk_backup "$ALIASES_FILE"
write_atomic "$ALIASES_FILE" <<<"$stripped"
ok "Removed managed block from: $ALIASES_FILE"
else
ok "No aliases file: $ALIASES_FILE"
fi
if [[ -f "$BASH_SYSTEM_RC" ]]; then
mk_backup "$BASH_SYSTEM_RC"
sed -i '\|^\[\[ -f /etc/profile\.d/docker-aliases\.sh \]\] && \. /etc/profile\.d/docker-aliases\.sh$|d' "$BASH_SYSTEM_RC" || true
ok "Cleaned loader line from: $BASH_SYSTEM_RC"
fi
if [[ -f "$ZSH_SYSTEM_RC" ]]; then
mk_backup "$ZSH_SYSTEM_RC"
sed -i '\|^\[\[ -f /etc/profile\.d/docker-aliases\.sh \]\] && source /etc/profile\.d/docker-aliases\.sh$|d' "$ZSH_SYSTEM_RC" || true
ok "Cleaned loader line from: $ZSH_SYSTEM_RC"
fi
ok "Uninstall complete. Files cleaned."
warn "Aliases are still active in the CURRENT shell session."
info "To remove them now, open a new shell or run:"
echo " unalias dc dcu dcud dcub dcuw dcd dcs dcb dcp dce dcl dclf dcps dcpu dcr dct dcc dccf docker-compose 2>/dev/null || true"
}
check_docker() {
if ! command -v docker >/dev/null 2>&1; then
warn "Docker not found in PATH. Aliases will be installed but won't work until docker is installed."
return 0
fi
if ! docker compose version >/dev/null 2>&1; then
warn "Plugin 'docker compose' not found. Most aliases (dc/dcu/dcpu/...) won't work without it."
fi
}
post_check() {
info "Post-check"
grep -Fq "alias dcpu=" "$ALIASES_FILE" 2>/dev/null \
&& ok "Aliases present in: $ALIASES_FILE" \
|| warn "Aliases NOT found in: $ALIASES_FILE"
grep -Fqx "$BASH_LOADER_LINE" "$BASH_SYSTEM_RC" 2>/dev/null \
&& ok "Loader present in: $BASH_SYSTEM_RC" \
|| warn "Loader line missing in: $BASH_SYSTEM_RC"
}
# ---------- main ----------
main() {
is_root || die "Run as root (sudo)."
local do_uninstall="false"
local do_print="false"
while [[ $# -gt 0 ]]; do
case "$1" in
--uninstall) do_uninstall="true"; shift || true ;;
--print) do_print="true"; shift || true ;;
-h|--help)
cat <<EOF
Usage:
sudo $0
sudo $0 --uninstall
sudo $0 --print
Installs:
- $ALIASES_FILE
- ensures bash loads it via: $BASH_SYSTEM_RC
- ensures zsh loads it via: $ZSH_SYSTEM_RC (if present)
EOF
exit 0
;;
*) die "Unknown option: $1" ;;
esac
done
if [[ "$do_print" == "true" ]]; then
aliases_block_content
exit 0
fi
if [[ "$do_uninstall" == "true" ]]; then
uninstall
exit 0
fi
check_docker
mkdir -p "$PROFILE_D_DIR"
ensure_aliases_file
install_loaders
post_check
echo
info "Apply in current session (optional):"
echo " source /etc/profile.d/docker-aliases.sh"
ok "Done."
}
main "$@"