-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
263 lines (237 loc) Β· 10.9 KB
/
Makefile
File metadata and controls
263 lines (237 loc) Β· 10.9 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
.PHONY: help build rebuild start stop restart clean logs shell ssh status rm install uninstall scan update
# Load environment variables from .env
ifneq (,$(wildcard ./.env))
include .env
export $(shell sed 's/=.*//' .env)
endif
# Default values if not in .env
COMPOSE_PROJECT_NAME ?= dotfiles
DEV_USER ?= dev
HOST_SSH_PORT ?= 2222
HOST_WEB_PORT ?= 8080
# Portability helper for sed -i
ifeq ($(shell uname), Darwin)
SED_I := sed -i ''
else
SED_I := sed -i
endif
# Derived variables
CONTAINER_NAME := $(COMPOSE_PROJECT_NAME)-dev-environment
IMAGE_NAME := $(COMPOSE_PROJECT_NAME)-dev-env
SSH_HOST_NAME ?= dev-environment
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[1;33m
RED := \033[0;31m
NC := \033[0m # No Color
.DEFAULT_GOAL := help
help:
@echo ""
@echo "π³ Development Environment Commands"
@echo "=================================="
@echo ""
@printf "\033[1;33mBasic Commands:\033[0m\n"
@printf " \033[0;34mbuild\033[0m Build and start the development environment\n"
@printf " \033[0;34mstart\033[0m Start existing containers\n"
@printf " \033[0;34mstop\033[0m Stop containers\n"
@printf " \033[0;34mrestart\033[0m Restart containers (stop + start)\n"
@echo ""
@printf "\033[1;33mMaintenance:\033[0m\n"
@printf " \033[0;34mclean\033[0m Clean cache and temporary data\n"
@printf " \033[0;34mrebuild\033[0m Rebuild image with fresh dependencies\n"
@printf " \033[0;34mupdate\033[0m Update pacman + AUR packages in the container\n"
@printf " \033[0;34mrm\033[0m Remove everything including volumes\n"
@echo ""
@printf "\033[1;33mAccess:\033[0m\n"
@printf " \033[0;34mshell\033[0m Open tmux session with zsh in container\n"
@printf " \033[0;34minstall\033[0m Install auto-shell to host shell config\n"
@printf " \033[0;34muninstall\033[0m Remove auto-shell from host shell config\n"
@printf " \033[0;34mssh\033[0m Connect via SSH\n"
@printf " \033[0;34mssh-setup\033[0m Set up SSH key authentication (no password)\n"
@printf " \033[0;34mlogs\033[0m Show container logs\n"
@printf " \033[0;34mstatus\033[0m Show container status\n"
@printf " \033[0;34mbuild-info\033[0m Show build cache information\n"
@printf " \033[0;34mscan\033[0m Scan container image with Trivy\n"
@echo ""
@echo ""
@printf "\033[1;33mBackup/Restore:\033[0m\n"
@printf " \033[0;34mbackup\033[0m Backup workspace and persistent volumes to ./backups\n"
@printf " \033[0;34mrestore\033[0m Restore from a backup file\n"
@echo ""
build:
build:
@echo "$(BLUE)[BUILD]$(NC) Building development environment..."
@echo "$(YELLOW)[INFO]$(NC) Using BuildKit for optimized builds..."
@export DOCKER_BUILDKIT=1 && \
export BUILDKIT_PROGRESS=plain && \
docker-compose build --parallel || { \
echo "$(RED)[ERROR]$(NC) Build failed. Check logs above."; \
exit 1; \
}
@echo "$(GREEN)[BUILD SUCCESS]$(NC) Starting services..."
@$(MAKE) start
@echo "$(GREEN)[SUCCESS]$(NC) Environment ready!"
@echo "$(YELLOW)[NOTE]$(NC) SSH access: ssh $(DEV_USER)@localhost -p $(HOST_SSH_PORT) (password: $(DEV_USER))"
@echo "$(YELLOW)[TIP]$(NC) Run 'make ssh-setup' for passwordless SSH access"
start:
@echo "$(BLUE)[START]$(NC) Starting containers..."
@docker-compose up -d
stop:
@echo "$(BLUE)[STOP]$(NC) Stopping containers..."
@docker-compose down
restart:
@echo "$(BLUE)[RESTART]$(NC) Restarting containers..."
@$(MAKE) stop
@$(MAKE) start
@echo "$(GREEN)[SUCCESS]$(NC) Containers restarted!"
clean:
@echo "$(YELLOW)[CLEAN]$(NC) Cleaning cache and temporary data..."
@docker-compose down --remove-orphans
@docker system prune -f
@docker builder prune -f
@echo "$(GREEN)[SUCCESS]$(NC) Cache cleanup complete!"
rm:
@echo "$(RED)[WARNING]$(NC) This will remove ALL data including Git credentials, GPG keys, etc."
@read -p "Are you sure? [y/N] " -n 1 -r; echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
echo "$(YELLOW)[RM]$(NC) Removing everything..."; \
docker-compose down -v --remove-orphans; \
docker system prune -af; \
docker builder prune -af; \
docker volume prune -f; \
echo "$(GREEN)[SUCCESS]$(NC) Everything removed!"; \
else \
echo "$(BLUE)[CANCELLED]$(NC) Operation cancelled."; \
fi
shell:
@echo "$(BLUE)[SHELL]$(NC) Opening tmux session with zsh in container..."
@echo "$(YELLOW)[TIP]$(NC) Use 'exit' to return to host terminal"
@docker exec -it $(CONTAINER_NAME) tmux new-session
install:
@echo "$(BLUE)[INSTALL]$(NC) Setting up auto-shell..."
@SHELL_CONFIG=""; \
if [ -f ~/.zshrc ]; then SHELL_CONFIG=~/.zshrc; \
elif [ -f ~/.bash_profile ]; then SHELL_CONFIG=~/.bash_profile; \
elif [ -f ~/.bashrc ]; then SHELL_CONFIG=~/.bashrc; \
else echo "$(RED)[ERROR]$(NC) No shell config file found" && exit 1; fi; \
\
if grep -q "DOTFILES_AUTO_SHELL_DONE" "$$SHELL_CONFIG"; then \
echo "$(YELLOW)[SKIP]$(NC) Already installed in $$SHELL_CONFIG"; \
else \
echo "" >> "$$SHELL_CONFIG"; \
echo "# Auto-enter dotfiles development container" >> "$$SHELL_CONFIG"; \
echo "if [[ -z \"\$$DOTFILES_AUTO_SHELL_DONE\" ]]; then" >> "$$SHELL_CONFIG"; \
echo " export DOTFILES_AUTO_SHELL_DONE=1" >> "$$SHELL_CONFIG"; \
echo " if docker ps | grep -q $(CONTAINER_NAME); then" >> "$$SHELL_CONFIG"; \
echo " echo \"π³ Auto-entering development container...\"" >> "$$SHELL_CONFIG"; \
echo " cd $(shell pwd) && make shell" >> "$$SHELL_CONFIG"; \
echo " fi" >> "$$SHELL_CONFIG"; \
echo "fi" >> "$$SHELL_CONFIG"; \
echo "$(GREEN)[INSTALLED]$(NC) Added to $$SHELL_CONFIG"; \
fi
@echo "$(YELLOW)[NOTE]$(NC) Open a new terminal to auto-enter the container"
uninstall:
@echo "$(BLUE)[UNINSTALL]$(NC) Removing auto-shell..."
@SHELL_CONFIG=""; \
if [ -f ~/.zshrc ]; then SHELL_CONFIG=~/.zshrc; \
elif [ -f ~/.bash_profile ]; then SHELL_CONFIG=~/.bash_profile; \
elif [ -f ~/.bashrc ]; then SHELL_CONFIG=~/.bashrc; \
else echo "$(RED)[ERROR]$(NC) No shell config file found" && exit 1; fi; \
\
if grep -q "DOTFILES_AUTO_SHELL_DONE" "$$SHELL_CONFIG"; then \
echo "$(YELLOW)[REMOVING]$(NC) Removing from $$SHELL_CONFIG"; \
$(SED_I) '/# Auto-enter dotfiles development container/,/^fi$$/d' "$$SHELL_CONFIG"; \
echo "$(GREEN)[REMOVED]$(NC) Auto-shell removed from $$SHELL_CONFIG"; \
else \
echo "$(YELLOW)[SKIP]$(NC) Auto-shell not found in $$SHELL_CONFIG"; \
fi
@echo "$(YELLOW)[NOTE]$(NC) Open a new terminal for normal behavior"
ssh:
@echo "$(BLUE)[SSH]$(NC) Connecting via SSH..."
@ssh $(SSH_HOST_NAME) || true
ssh-setup:
@echo "$(BLUE)[SSH-SETUP]$(NC) Setting up SSH key authentication..."
@echo "π§Ή Cleaning old host keys..."
@ssh-keygen -R "[localhost]:$(HOST_SSH_PORT)" 2>/dev/null || true
@if [ ! -f ~/.ssh/$(SSH_HOST_NAME) ]; then \
echo "π Generating SSH key pair..."; \
ssh-keygen -t ed25519 -f ~/.ssh/$(SSH_HOST_NAME) -N '' -C "$(SSH_HOST_NAME)-key"; \
else \
echo "β
SSH key already exists"; \
fi
@echo "βοΈ Updating SSH config..."; \
if grep -q "^Host $(SSH_HOST_NAME)" ~/.ssh/config 2>/dev/null; then \
$(SED_I) '/^Host $(SSH_HOST_NAME)$$/,/^$$/d' ~/.ssh/config; \
fi
@echo "" >> ~/.ssh/config
@echo "Host $(SSH_HOST_NAME)" >> ~/.ssh/config
@echo " HostName localhost" >> ~/.ssh/config
@echo " Port $(HOST_SSH_PORT)" >> ~/.ssh/config
@echo " User $(DEV_USER)" >> ~/.ssh/config
@echo " IdentityFile ~/.ssh/$(SSH_HOST_NAME)" >> ~/.ssh/config
@echo " StrictHostKeyChecking accept-new" >> ~/.ssh/config
@echo "π¦ Installing SSH key to running container..."
@PUB_KEY=$$(cat ~/.ssh/$(SSH_HOST_NAME).pub); \
docker exec -i $(CONTAINER_NAME) sh -c "mkdir -p /home/$(DEV_USER)/.ssh && chmod 700 /home/$(DEV_USER)/.ssh && (grep -qF \"$$PUB_KEY\" /home/$(DEV_USER)/.ssh/authorized_keys 2>/dev/null || echo \"$$PUB_KEY\" >> /home/$(DEV_USER)/.ssh/authorized_keys) && chmod 600 /home/$(DEV_USER)/.ssh/authorized_keys"
@echo "β
SSH key installed"
@echo "π§ͺ Testing SSH key authentication..."
@sleep 2 && ssh $(SSH_HOST_NAME) 'echo "β
SSH key authentication successful!"' || echo "β SSH setup failed - try 'make restart' and test again"
@echo "$(GREEN)[SUCCESS]$(NC) SSH key authentication configured!"
logs:
@docker-compose logs -f
status:
@echo "$(BLUE)[STATUS]$(NC) Container status:"
@docker-compose ps
@echo ""
@echo "$(BLUE)[VOLUMES]$(NC) Persistent volumes:"
@docker volume ls | grep dotfiles || echo "No dotfiles volumes found"
rebuild:
@echo "$(BLUE)[REBUILD]$(NC) Rebuilding environment with fresh dependencies..."
@export DOCKER_BUILDKIT=1 && \
export BUILDKIT_PROGRESS=plain && \
docker-compose build --pull --no-cache || { \
echo "$(RED)[ERROR]$(NC) Rebuild failed. Check logs above."; \
exit 1; \
}
@$(MAKE) start
@echo "$(GREEN)[SUCCESS]$(NC) Environment rebuilt with latest base packages!"
build-info:
@echo "$(BLUE)[BUILD INFO]$(NC) Docker build cache information:"
@echo ""
@echo "$(YELLOW)[CACHE USAGE]$(NC)"
@docker system df
@echo ""
@echo "$(YELLOW)[BUILD CACHE]$(NC)"
@docker buildx du 2>/dev/null || echo "BuildKit cache info not available"
@echo ""
@echo "$(YELLOW)[IMAGE INFO]$(NC)"
@docker images | grep -E "(dotfiles|dev-environment|manjarolinux)" || echo "No related images found"
scan:
@echo "$(BLUE)[SCAN]$(NC) Scanning image with Trivy..."
@docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $$HOME/.cache/trivy:/root/.cache \
aquasec/trivy:latest image $(IMAGE_NAME)
update:
@echo "$(BLUE)[UPDATE]$(NC) Ensuring development container is running..."
@docker-compose up -d dev-env >/dev/null
@echo "$(BLUE)[UPDATE]$(NC) Updating system packages via pacman..."
@docker exec -u root $(CONTAINER_NAME) bash -lc "pacman -Syyu --noconfirm"
@echo "$(BLUE)[UPDATE]$(NC) Updating AUR packages via yay..."
@docker exec $(CONTAINER_NAME) bash -lc "yay -Sua --noconfirm --needed --answerdiff None --answerclean None"
@echo "$(BLUE)[UPDATE]$(NC) Cleaning package cache..."
@docker exec -u root $(CONTAINER_NAME) bash -lc "pacman -Scc --noconfirm || true"
@echo "$(BLUE)[UPDATE]$(NC) Updating Go tools..."
@docker exec -i $(CONTAINER_NAME) bash < scripts/install-go-tools.sh
@echo "$(BLUE)[UPDATE]$(NC) Updating Zsh plugins..."
@docker exec $(CONTAINER_NAME) bash -lc 'for d in /home/$(DEV_USER)/.oh-my-zsh/custom/plugins/*; do [ -d "$$d/.git" ] && (echo "π Updating $$(basename $$d)..." && cd "$$d" && git pull --quiet); done'
@echo "$(BLUE)[UPDATE]$(NC) Updating Tmux plugins..."
@docker exec $(CONTAINER_NAME) bash -lc "/home/$(DEV_USER)/.tmux/plugins/tpm/bin/update_plugins all >/dev/null"
@echo "$(GREEN)[SUCCESS]$(NC) Container packages and tools updated. Restart with 'make restart' if needed."
backup:
@echo "$(BLUE)[BACKUP]$(NC) Backing up environment..."
@mkdir -p backups
@docker run --rm --volumes-from $(CONTAINER_NAME) -v $$(pwd)/backups:/backup alpine tar czf /backup/backup-$$(date +%Y%m%d-%H%M%S).tar.gz /home/$(DEV_USER) /workspace || echo "$(RED)[ERROR]$(NC) Backup failed. Is the container running?"
@echo "$(GREEN)[SUCCESS]$(NC) Backup created in ./backups/"
restore:
@echo "$(RED)[WARNING]$(NC) Restore functionality is a placeholder. Manually extract the tarball to restore."