-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
262 lines (222 loc) · 12.2 KB
/
Makefile
File metadata and controls
262 lines (222 loc) · 12.2 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
.PHONY: build build-linux clean test lint run deps deb \
docker-build docker-run build-docker build-docker-dev \
docker-compose-up docker-compose-down docker-compose-logs \
integration-up integration-down integration-logs integration-test integration-clean \
deploy install-systemd uninstall-systemd help
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
# Main package
MAIN_PACKAGE=cmd/provisioner
BINARY_NAME=libvirt-volume-provisioner
BINARY_UNIX=$(BINARY_NAME)_unix
# Debian package parameters
DEB_NAME=libvirt-volume-provisioner
DEB_VERSION ?= 0.10.0
DEB_ARCH=amd64
DEB_BUILD_DIR=deb-build
# Help
.PHONY: help
help: ## Show this help message
@echo 'Usage: make [target]'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
# Build the binary
build: ## Build binary for current platform
$(GOMOD) tidy
$(GOBUILD) -o $(BINARY_NAME) -v ./$(MAIN_PACKAGE)
# Build for Linux
build-linux: ## Build binary for Linux amd64 (used by deb target)
CGO_ENABLED=1 CGO_CFLAGS="-Wno-discarded-qualifiers" GOOS=linux GOARCH=amd64 $(GOBUILD) -tags libsqlite3 -ldflags "-X main.version=$(DEB_VERSION) -X 'main.buildTime=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")'" -o $(BINARY_UNIX) -v ./$(MAIN_PACKAGE)
# Test
test: ## Run unit tests
$(GOTEST) -v ./...
# Lint
lint: ## Run golangci-lint
golangci-lint run
# Docker build
docker-build: ## Build development Docker image
docker build -t libvirt-volume-provisioner .
# Docker run
docker-run: ## Run service via Docker using .env file
docker run -p 8080:8080 --env-file .env libvirt-volume-provisioner
# Build Debian package
deb: build-linux ## Build Debian .deb package
@echo "Building Debian package..."
@rm -rf $(DEB_BUILD_DIR)
@mkdir -p $(DEB_BUILD_DIR)/DEBIAN
@mkdir -p $(DEB_BUILD_DIR)/usr/bin
@mkdir -p $(DEB_BUILD_DIR)/etc/libvirt-volume-provisioner
@mkdir -p $(DEB_BUILD_DIR)/lib/systemd/system
@mkdir -p $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)
# Copy binary
cp $(BINARY_UNIX) $(DEB_BUILD_DIR)/usr/bin/$(BINARY_NAME)
# Copy backup script
mkdir -p $(DEB_BUILD_DIR)/usr/local/bin
cp scripts/backup-db.sh $(DEB_BUILD_DIR)/usr/local/bin/$(DEB_NAME)-backup
chmod 755 $(DEB_BUILD_DIR)/usr/local/bin/$(DEB_NAME)-backup
# Create control file
@echo "Package: $(DEB_NAME)" > $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Version: $(DEB_VERSION)" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Section: admin" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Priority: optional" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Architecture: $(DEB_ARCH)" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Depends: libc6 (>= 2.4)" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Maintainer: Your Name <your.email@example.com>" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo "Description: Daemon service for provisioning LVM volumes with VM images on libvirt hypervisor hosts" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo " This service provides an HTTP API for downloading VM images from MinIO" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo " object storage, converting QCOW2 images to raw format, and populating" >> $(DEB_BUILD_DIR)/DEBIAN/control
@echo " LVM volumes with VM disk data." >> $(DEB_BUILD_DIR)/DEBIAN/control
# Copy systemd service file
cp libvirt-volume-provisioner.service $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME).service
# Create database backup service
@echo "[Unit]" > $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
@echo "Description=Libvirt Volume Provisioner Database Backup" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
@echo "" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
@echo "[Service]" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
@echo "Type=oneshot" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
@echo "ExecStart=/usr/local/bin/$(DEB_NAME)-backup" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.service
# Create database backup timer
@echo "[Unit]" > $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "Description=Daily Libvirt Volume Provisioner Database Backup" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "[Timer]" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "OnCalendar=daily" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "Persistent=true" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "[Install]" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
@echo "WantedBy=timers.target" >> $(DEB_BUILD_DIR)/lib/systemd/system/$(DEB_NAME)-backup.timer
# Install sudoers rules for unprivileged service user
# Removed sudoers for v0.9 direct exec
# Create postinst script
@echo "#!/bin/bash" > $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "set -e" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Create user if it doesn't exist" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "if ! id -u libvirt-volume-provisioner > /dev/null 2>&1; then" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo " useradd --system --shell /bin/false libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "fi" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Create database directory" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "mkdir -p /var/lib/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "chown libvirt-volume-provisioner:libvirt-volume-provisioner /var/lib/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "chmod 700 /var/lib/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Create environment file if it doesn't exist" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "if [ ! -f /etc/default/libvirt-volume-provisioner ]; then" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo " cat > /etc/default/libvirt-volume-provisioner << EOF" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "PORT=8080" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "HOST=0.0.0.0" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "MINIO_ENDPOINT=https://minio.example.com" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "MINIO_ACCESS_KEY=your-access-key" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "MINIO_SECRET_KEY=your-secret-key" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "MINIO_RETRY_ATTEMPTS=3" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "MINIO_RETRY_BACKOFF_MS=100,1000,10000" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# LVM configuration" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "LVM_VOLUME_GROUP=data" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "LVM_RETRY_ATTEMPTS=2" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "LVM_RETRY_BACKOFF_MS=100,1000" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Database configuration" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "DB_PATH=/var/lib/libvirt-volume-provisioner/jobs.db" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "EOF" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo " chmod 600 /etc/default/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo " chown root:root /etc/default/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "fi" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Set permissions" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "chown libvirt-volume-provisioner:libvirt-volume-provisioner /usr/bin/$(BINARY_NAME)" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "chmod 755 /usr/bin/$(BINARY_NAME)" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo " # No sudoers for v0.9" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "# Reload systemd and enable service" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "systemctl daemon-reload" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@echo "systemctl enable $(DEB_NAME)" >> $(DEB_BUILD_DIR)/DEBIAN/postinst
@chmod 755 $(DEB_BUILD_DIR)/DEBIAN/postinst
# Create prerm script
@echo "#!/bin/bash" > $(DEB_BUILD_DIR)/DEBIAN/prerm
@echo "set -e" >> $(DEB_BUILD_DIR)/DEBIAN/prerm
@echo "" >> $(DEB_BUILD_DIR)/DEBIAN/prerm
@echo "# Stop and disable service" >> $(DEB_BUILD_DIR)/DEBIAN/prerm
@echo "systemctl stop $(DEB_NAME) || true" >> $(DEB_BUILD_DIR)/DEBIAN/prerm
@echo "systemctl disable $(DEB_NAME) || true" >> $(DEB_BUILD_DIR)/DEBIAN/prerm
@chmod 755 $(DEB_BUILD_DIR)/DEBIAN/prerm
# Create copyright file
@echo "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/" > $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "Upstream-Name: $(DEB_NAME)" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "Source: https://github.com/rossigee/libvirt-volume-provisioner" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "Files: *" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "Copyright: $(shell date +%Y) Ross Gee" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
@echo "License: MIT" >> $(DEB_BUILD_DIR)/usr/share/doc/$(DEB_NAME)/copyright
# Build the package
dpkg-deb --build $(DEB_BUILD_DIR) $(DEB_NAME)_$(DEB_VERSION)_$(DEB_ARCH).deb
# Clean up
@rm -rf $(DEB_BUILD_DIR)
@echo "Debian package created: $(DEB_NAME)_$(DEB_VERSION)_$(DEB_ARCH).deb"
# Clean
clean: ## Remove build artefacts and packages
$(GOCLEAN)
rm -f $(BINARY_NAME)
rm -f $(BINARY_UNIX)
rm -f $(DEB_NAME)_$(DEB_VERSION)_$(DEB_ARCH).deb
rm -rf $(DEB_BUILD_DIR)
# Run
run: ## Build and run the service locally
$(GOBUILD) -o $(BINARY_NAME) -v ./$(MAIN_PACKAGE)
./$(BINARY_NAME)
# Dependencies
deps: ## Download Go module dependencies
$(GOMOD) download
$(GOGET) github.com/gin-gonic/gin
$(GOGET) github.com/google/uuid
$(GOGET) github.com/minio/minio-go/v7
# Docker targets
build-docker: ## Build production Docker image
docker build -f Dockerfile.production -t $(BINARY_NAME):latest .
build-docker-dev: ## Build development Docker image
docker build -f Dockerfile -t $(BINARY_NAME):dev .
docker-compose-up: ## Start with docker-compose
docker compose up -d
docker-compose-down: ## Stop docker-compose
docker compose down
docker-compose-logs: ## Show docker-compose logs
docker compose logs -f
# Integration testing targets
integration-up: ## Start integration test environment
cd integration && docker compose -f docker-compose.test.yml up -d
integration-down: ## Stop integration test environment
cd integration && docker compose -f docker-compose.test.yml down -v
integration-logs: ## Show integration test logs
cd integration && docker compose -f docker-compose.test.yml logs -f
integration-test: ## Run integration tests
cd integration && docker compose -f docker-compose.test.yml run --rm integration-tests
integration-clean: ## Clean up integration test environment
cd integration && docker compose -f docker-compose.test.yml down -v --rmi local
# Deploy to VM hosts
deploy: ## Deploy .deb package to configured VM hosts
@echo "Deploying $(DEB_NAME)_$(DEB_VERSION)_$(DEB_ARCH).deb to VM hosts..."
@./scripts/deploy-to-hosts.sh $(DEB_NAME)_$(DEB_VERSION)_$(DEB_ARCH).deb
# Systemd targets
install-systemd: ## Install systemd service files
sudo cp systemd/$(BINARY_NAME).service /etc/systemd/system/
sudo cp systemd/$(BINARY_NAME).socket /etc/systemd/system/
sudo cp systemd/$(BINARY_NAME).default /etc/default/$(BINARY_NAME)
sudo systemctl daemon-reload
@echo "Systemd files installed. Run 'sudo systemctl enable $(BINARY_NAME).socket && sudo systemctl start $(BINARY_NAME).socket' to start the service."
uninstall-systemd: ## Remove systemd service files
sudo systemctl stop $(BINARY_NAME).socket || true
sudo systemctl stop $(BINARY_NAME).service || true
sudo systemctl disable $(BINARY_NAME).socket || true
sudo systemctl disable $(BINARY_NAME).service || true
sudo rm -f /etc/systemd/system/$(BINARY_NAME).service
sudo rm -f /etc/systemd/system/$(BINARY_NAME).socket
sudo rm -f /etc/default/$(BINARY_NAME)
sudo systemctl daemon-reload
@echo "Systemd files removed."