-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
151 lines (129 loc) · 5.57 KB
/
Copy pathsetup.sh
File metadata and controls
151 lines (129 loc) · 5.57 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
#!/usr/bin/env bash
set -e
echo "=== Devbox Setup ==="
# Detect target user and home directory
TARGET_USER="${SUDO_USER:-$USER}"
TARGET_HOME=$(getent passwd "$TARGET_USER" | cut -d: -f6)
[ -z "$TARGET_HOME" ] && TARGET_HOME="$HOME"
# Hostname configuration
CURRENT_HOST=$(hostname)
echo "Current hostname: $CURRENT_HOST"
read -rp "Keep current hostname or enter a new one [press enter to keep '$CURRENT_HOST']: " INPUT_HOST
DOMAIN_NAME="${INPUT_HOST:-$CURRENT_HOST}"
DOMAIN_NAME="$(echo "$DOMAIN_NAME" | sed 's/\.local$//' | tr -d ' ' | tr '[:upper:]' '[:lower:]')"
[ -z "$DOMAIN_NAME" ] && DOMAIN_NAME="$CURRENT_HOST"
if [ "$(hostname)" != "$DOMAIN_NAME" ]; then
echo "Setting hostname to: $DOMAIN_NAME"
sudo hostnamectl set-hostname "$DOMAIN_NAME" || {
echo "$DOMAIN_NAME" | sudo tee /etc/hostname > /dev/null
sudo hostname "$DOMAIN_NAME"
}
fi
# Update /etc/hosts
if ! grep -qE "127\.0\.1\.1[[:space:]]+.*${DOMAIN_NAME}" /etc/hosts; then
sudo sed -i '/^127\.0\.1\.1/d' /etc/hosts
echo -e "127.0.1.1\t${DOMAIN_NAME}\t${DOMAIN_NAME}.local" | sudo tee -a /etc/hosts > /dev/null
fi
# Base packages, Avahi (mDNS), OpenSSH, Python 3.11
echo "=== Installing Base Packages, Avahi, OpenSSH, Python 3.11 ==="
sudo apt-get update -y
sudo apt-get install -y \
curl \
ca-certificates \
gnupg \
git \
htop \
openssh-server \
avahi-daemon \
avahi-utils \
libnss-mdns
if ! command -v python3.11 &>/dev/null; then
sudo apt-get install -y python3.11 python3.11-venv python3-pip || sudo apt-get install -y python3 python3-venv python3-pip
fi
# Enable and start SSH server (for VS Code Remote SSH)
sudo systemctl enable --now ssh || sudo systemctl enable --now sshd
# Configure and start Avahi daemon
if [ -f /etc/avahi/avahi-daemon.conf ]; then
sudo sed -i "s/^#\?host-name=.*/host-name=${DOMAIN_NAME}/" /etc/avahi/avahi-daemon.conf
fi
sudo systemctl enable --now avahi-daemon
sudo systemctl restart avahi-daemon
# Node Version Manager (nvm) & Node.js
echo "=== Installing NVM & Latest Node.js ==="
sudo -u "$TARGET_USER" -H bash -c '
export NVM_DIR="$HOME/.nvm"
if [ ! -d "$NVM_DIR" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.7/install.sh | bash
fi
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install node
nvm alias default node
nvm use default
'
# Install code server
read -rp "Install code-server (browser-based VS Code)? [y/N]: " INSTALL_VSCODE
if [[ "$INSTALL_VSCODE" =~ ^[Yy]$ ]]; then
echo "=== Installing code-server ==="
curl -fsSL https://code-server.dev/install.sh | sh
sudo systemctl enable --now "code-server@$TARGET_USER"
fi
# Kernel inotify boost (essential for microservices / hot-reloading)
echo "=== Configuring inotify watchers ==="
echo "fs.inotify.max_user_watches=524288" | sudo tee /etc/sysctl.d/99-devbox.conf > /dev/null
echo "fs.inotify.max_user_instances=512" | sudo tee -a /etc/sysctl.d/99-devbox.conf > /dev/null
sudo sysctl -p /etc/sysctl.d/99-devbox.conf
# Native Docker CE & Compose plugin
echo "=== Installing Docker CE & Compose ==="
if ! command -v docker &>/dev/null; then
sudo install -m 0755 -d /etc/apt/keyrings
DOCKER_OS="debian"
if [ -f /etc/os-release ]; then
. /etc/os-release
if [[ "${ID:-}" == "ubuntu" || "${ID_LIKE:-}" == *"ubuntu"* ]]; then
DOCKER_OS="ubuntu"
fi
fi
if [ ! -f /etc/apt/keyrings/docker.gpg ]; then
curl -fsSL "https://download.docker.com/linux/${DOCKER_OS}/gpg" | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg --yes
sudo chmod a+r /etc/apt/keyrings/docker.gpg
fi
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${DOCKER_OS} \
$(. /etc/os-release && echo "${VERSION_CODENAME:-bookworm}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
fi
if ! id -nG "$TARGET_USER" | grep -qw "docker"; then
sudo usermod -aG docker "$TARGET_USER"
echo "Added $TARGET_USER to docker group."
fi
# K3s Kubernetes Setup & Traefik Ingress
echo "=== Installing K3s Kubernetes (with Traefik Ingress) ==="
if ! command -v k3s &>/dev/null; then
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
fi
# Configure kubeconfig for target user
mkdir -p "$TARGET_HOME/.kube"
if [ -f /etc/rancher/k3s/k3s.yaml ]; then
sudo cp /etc/rancher/k3s/k3s.yaml "$TARGET_HOME/.kube/config"
sudo chmod 600 "$TARGET_HOME/.kube/config"
sudo chown -R "$TARGET_USER:$TARGET_USER" "$TARGET_HOME/.kube"
fi
export KUBECONFIG="$TARGET_HOME/.kube/config"
kubectl create namespace devbox --dry-run=client -o yaml | kubectl apply -f -
PRIMARY_IP=$(hostname -I | tr ' ' '\n' | grep '^192\.168\.' | head -n1)
[ -z "$PRIMARY_IP" ] && PRIMARY_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "=== Setup Complete! ==="
echo "Host broadcast: http://${DOMAIN_NAME}.local"
echo "Primary IP: $PRIMARY_IP"
echo "SSH access: ssh ${TARGET_USER}@${DOMAIN_NAME}.local (or ssh ${TARGET_USER}@${PRIMARY_IP})"
echo "Docker: $(docker --version)"
echo "Python: $(python3.11 --version 2>/dev/null || python3 --version)"
echo "K3s node: $(kubectl get nodes -o wide --no-headers | awk '{print $1, $2}')"
if systemctl is-active --quiet "code-server@$TARGET_USER" 2>/dev/null; then
echo "code-server: http://${DOMAIN_NAME}.local:8080 (or http://${PRIMARY_IP}:8080)"
fi
echo ""
echo "Note: If you were just added to the docker group, log out and back in."