-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·119 lines (101 loc) · 4.43 KB
/
init.sh
File metadata and controls
executable file
·119 lines (101 loc) · 4.43 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
#!/usr/bin/env bash
set -euo pipefail
# Load configuration (USER_NAME, IMAGE_NAME, CONTAINER_NAME)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1091
source "${SCRIPT_DIR}/config.env"
# Fixed paths inside the container
HOST_ZSHRC="${SCRIPT_DIR}/zshrc"
IN_CONTAINER_HOME="/home/${USER_NAME}"
IN_CONTAINER_ZSHRC="${IN_CONTAINER_HOME}/.zshrc"
IN_CONTAINER_ZSHENV="${IN_CONTAINER_HOME}/.zshenv"
echo "[*] init: container=${CONTAINER_NAME} user=${USER_NAME} image=${IMAGE_NAME}"
# -----------------------------------------------------------------------------
# 0) Ensure the container exists and is running
# -----------------------------------------------------------------------------
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>&1; then
docker start "${CONTAINER_NAME}" >/dev/null || true
else
echo "[!] ERROR: container '${CONTAINER_NAME}' not found. Run ./setup.sh first." >&2
exit 1
fi
# -----------------------------------------------------------------------------
# 1) Set password for USER_NAME (executed as root)
# Prefer DOCKER_ENV_PASSWORD if provided by setup.sh
# -----------------------------------------------------------------------------
P="${DOCKER_ENV_PASSWORD:-}"
if [ -z "${P}" ]; then
read -r -s -p "Password (for ${USER_NAME}): " P
echo
fi
if [ -z "${P}" ]; then
echo "[!] ERROR: empty password" >&2
exit 1
fi
printf "%s:%s\n" "${USER_NAME}" "${P}" | docker exec -i -u root "${CONTAINER_NAME}" chpasswd
unset P
echo "[*] init: password set"
# -----------------------------------------------------------------------------
# 2) HOST_LABEL -> write it to ~/.zshenv (prompt uses: ${USER_NAME}@${HOST_LABEL})
# Prefer DOCKER_ENV_HOST_LABEL if provided by setup.sh
# -----------------------------------------------------------------------------
HOST_LABEL="${DOCKER_ENV_HOST_LABEL:-}"
if [ -z "${HOST_LABEL}" ]; then
read -r -p "HOST_LABEL (if set to 'dev', prompt becomes '${USER_NAME}@dev'): " HOST_LABEL
fi
if [ -z "${HOST_LABEL}" ]; then
echo "[!] ERROR: empty HOST_LABEL" >&2
exit 1
fi
# Minimal escaping for backslashes and double quotes
HOST_LABEL_ESC="${HOST_LABEL//\\/\\\\}"
HOST_LABEL_ESC="${HOST_LABEL_ESC//\"/\\\"}"
docker exec -i -u root "${CONTAINER_NAME}" sh -lc "cat > '${IN_CONTAINER_ZSHENV}' <<EOF
export HOST_LABEL=\"${HOST_LABEL_ESC}\"
EOF
chown 1100:0 '${IN_CONTAINER_ZSHENV}'
chmod 600 '${IN_CONTAINER_ZSHENV}'"
echo "[*] init: wrote ${IN_CONTAINER_ZSHENV} (prompt example: ${USER_NAME}@${HOST_LABEL})"
# -----------------------------------------------------------------------------
# 3) Copy .zshrc template into the container
# -----------------------------------------------------------------------------
if [ ! -f "${HOST_ZSHRC}" ]; then
echo "[!] ERROR: ${HOST_ZSHRC} not found" >&2
exit 1
fi
docker cp "${HOST_ZSHRC}" "${CONTAINER_NAME}:${IN_CONTAINER_ZSHRC}"
docker exec -u root "${CONTAINER_NAME}" sh -lc "
chown 1100:0 '${IN_CONTAINER_ZSHRC}' &&
chmod 644 '${IN_CONTAINER_ZSHRC}'
"
echo "[*] init: installed ${IN_CONTAINER_ZSHRC}"
# -----------------------------------------------------------------------------
# 4) Install oh-my-zsh if missing (executed as USER_NAME)
# -----------------------------------------------------------------------------
docker exec -u "${USER_NAME}" "${CONTAINER_NAME}" sh -lc '
if [ ! -f "$HOME/.oh-my-zsh/oh-my-zsh.sh" ]; then
echo "[*] init: installing oh-my-zsh..."
RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
echo "[*] init: oh-my-zsh already installed"
fi
'
# -----------------------------------------------------------------------------
# 5) Install zsh plugins if missing (executed as USER_NAME)
# -----------------------------------------------------------------------------
docker exec -u "${USER_NAME}" "${CONTAINER_NAME}" sh -lc '
ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
mkdir -p "$ZSH_CUSTOM/plugins"
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
echo "[*] init: installing plugin: zsh-syntax-highlighting"
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
"$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
fi
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
echo "[*] init: installing plugin: zsh-autosuggestions"
git clone https://github.com/zsh-users/zsh-autosuggestions \
"$ZSH_CUSTOM/plugins/zsh-autosuggestions"
fi
'
echo "[*] init: done"