-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
143 lines (125 loc) · 4.26 KB
/
Makefile
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
GO := go
GO_BUILD = CGO_ENABLED=1 $(GO) build
GO_GENERATE = $(GO) generate
GO_TAGS ?=
TARGET_GOARCH ?= amd64,arm64
GOARCH ?= amd64
GOOS ?= linux
VERSION=$(shell git describe --tags --always)
# For compiling libpcap and CGO
CC ?= gcc
# 变量定义
SSH_PASS := sshpass -p root
SSH_OPTS := -o 'StrictHostKeyChecking no' -P 10022
REMOTE_HOST := root@127.0.0.1
MAX_RETRIES := 3
QEMU_LOG := qemu.log
# 定义超时时间(5分钟 = 300秒)
TIMEOUT := 300
LOG_FILE := /root/log/info.log
# .PHONY 声明
.PHONY: deploy check-connection copy-files clean retry all
# 默认目标
all: deploy
# 主要部署流程
deploy: check-connection copy-files
@echo "Deployment complete"
# 连接检查
check-connection:
@echo "Checking remote host connection..."
while ! nc -z 127.0.0.1 10022 ; do echo "waiting for ssh"; sleep 1; done
# 文件复制
copy-files:
@echo "Copying files to remote host..."
@for i in $$(seq 1 $(MAX_RETRIES)); do \
echo "Attempt $$i of $(MAX_RETRIES)"; \
if $(SSH_PASS) scp $(SSH_OPTS) ./cmd/shepherd $(REMOTE_HOST):/root/shepherd && \
$(SSH_PASS) scp $(SSH_OPTS) ./cmd/config.yaml $(REMOTE_HOST):/root/config.yaml; then \
echo "Files copied successfully"; \
exit 0; \
fi; \
echo "Copy failed, retrying..."; \
sleep 2; \
done; \
echo "All retry attempts failed"; \
exit 1
build: elf
cd ./cmd;CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_LDFLAGS='-g -lcapstone -static' go build -tags=netgo,osusergo -gcflags "all=-N -l" -v -o shepherd
dlv: build
dlv --headless --listen=:2345 --api-version=2 exec ./cmd/shepherd -- --config-path=./cmd/config.yaml
run: build
./cmd/shepherd --config-path=./cmd/config.yaml
elf:
TARGET_GOARCH=$(TARGET_GOARCH) $(GO_GENERATE)
CC=$(CC) GOARCH=$(TARGET_GOARCH) $(GO_BUILD) $(if $(GO_TAGS),-tags $(GO_TAGS)) \
-ldflags "-w -s "
image:
docker buildx create --use
docker buildx build --platform linux/amd64 -t ghostbaby/shepherd:v0.0.1-amd64 --push .
docker buildx build --platform linux/arm64 -t ghostbaby/shepherd:v0.0.1-arm64 --push .
.ONESHELL:
prepare_e2e_filesystem:
cd ./tests/e2e/vm/filesystem
# build filesystem image and store as tar archive
DOCKER_BUILDKIT=1 docker build --output "type=tar,dest=filesystem.tar" .
# convert tar to qcow2 image
sudo virt-make-fs --format=qcow2 --size=+100M filesystem.tar filesystem-large.qcow2
# reduce size of image
qemu-img convert filesystem-large.qcow2 -O qcow2 filesystem.qcow2
# reduce size by packing
zip filesystem.zip filesystem.qcow2
# remove unnecessary files
rm -f filesystem-large.qcow2 filesystem.qcow2 filesystem.tar
.ONESHELL:
start_qemu:
cd ./tests/e2e/vm/filesystem
rm -f filesystem.qcow2 filesystem-diff.qcow2
unzip ./filesystem.zip
sudo qemu-img create -f qcow2 -b filesystem.qcow2 -F qcow2 filesystem-diff.qcow2
PWD=$(pwd)
sudo qemu-system-x86_64 \
-cpu host \
-m 4G \
-smp 4 \
-kernel ${PWD}/tests/e2e/vm/kernels/${KERNEL}/bzImage \
-append "console=ttyS0 root=/dev/sda rw" \
-drive file="${PWD}/tests/e2e/vm/filesystem/filesystem-diff.qcow2,format=qcow2" \
-net nic -net user,hostfwd=tcp::10022-:22,hostfwd=tcp::16676-:6676,hostfwd=tcp::10443-:443 \
-enable-kvm \
-pidfile qemu.pid \
-nographic > $(QEMU_LOG) 2>&1 &
.ONESHELL:
prepare_e2e: start_qemu deploy
sshpass -p root ssh -p 10022 root@127.0.0.1 'chmod 0655 /root/shepherd && systemctl start shepherd.service'
while ! sshpass -p root ssh -p 10022 root@127.0.0.1 'systemctl is-active shepherd.service' ; do echo "waiting for shepherd service"; sleep 1; done
.ONESHELL:
e2e: prepare_e2e check-log
ifconfig
RC=$$?
sshpass -p root ssh -p 10022 root@127.0.0.1 'systemctl status shepherd.service'
sudo cat ./tests/e2e/vm/filesystem/qemu.pid | sudo xargs kill
exit $$RC
check-log:
@echo "Checking log file..."
@start_time=$$(date +%s); \
while true; do \
current_time=$$(date +%s); \
elapsed_time=$$((current_time - start_time)); \
if [ $$elapsed_time -gt $(TIMEOUT) ]; then \
echo "Timeout after $(TIMEOUT) seconds"; \
exit 1; \
fi; \
if sshpass -p root ssh -p 10022 root@127.0.0.1 "\
if [ -f $(LOG_FILE) ] && [ -s $(LOG_FILE) ]; then \
cat $(LOG_FILE); \
exit 0; \
else \
exit 1; \
fi"; then \
echo "Log file found and not empty"; \
break; \
else \
echo "Waiting for log file... ($$elapsed_time seconds elapsed)"; \
sleep 5; \
fi; \
done