-
Notifications
You must be signed in to change notification settings - Fork 51
/
Makefile
263 lines (204 loc) · 5.91 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
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
export GO111MODULE=on
SHELL := /bin/bash
TLS_DIR = tls
TLS_NAME = osctrl-tls
TLS_CODE = ${TLS_DIR:=/*.go}
ADMIN_DIR = admin
ADMIN_NAME = osctrl-admin
ADMIN_CODE = ${ADMIN_DIR:=/*.go}
API_DIR = api
API_NAME = osctrl-api
API_CODE = ${API_DIR:=/*.go}
CLI_DIR = cli
CLI_NAME = osctrl-cli
CLI_CODE = ${CLI_DIR:=/*.go}
DEST ?= /opt/osctrl
OUTPUT = bin
STATIC_ARGS = -ldflags "-linkmode external -extldflags -static"
.PHONY: build static clean tls admin cli api
# Build code according to caller OS and architecture
build:
make tls
make admin
make api
make cli
# Build everything statically
static:
make tls-static
make admin-static
make api-static
make cli-static
# Build TLS endpoint
tls:
go build -o $(OUTPUT)/$(TLS_NAME) $(TLS_CODE)
# Build TLS endpoint statically
tls-static:
go build $(STATIC_ARGS) -o $(OUTPUT)/$(TLS_NAME) -a $(TLS_CODE)
# Build Admin UI
admin:
go build -o $(OUTPUT)/$(ADMIN_NAME) $(ADMIN_CODE)
# Build Admin UI statically
admin-static:
go build $(STATIC_ARGS) -o $(OUTPUT)/$(ADMIN_NAME) -a $(ADMIN_CODE)
# Build API
api:
go build -o $(OUTPUT)/$(API_NAME) $(API_CODE)
# Build API statically
api-static:
go build $(STATIC_ARGS) -o $(OUTPUT)/$(API_NAME) -a $(API_CODE)
# Build the CLI
cli:
go build -o $(OUTPUT)/$(CLI_NAME) $(CLI_CODE)
# Build the CLI statically
cli-static:
go build $(STATIC_ARGS) -o $(OUTPUT)/$(CLI_NAME) -a $(CLI_CODE)
# Delete all compiled binaries
clean:
rm -rf $(OUTPUT)/$(TLS_NAME)
rm -rf $(OUTPUT)/$(ADMIN_NAME)
rm -rf $(OUTPUT)/$(API_NAME)
rm -rf $(OUTPUT)/$(CLI_NAME)
# Dekete all dependencies go.sum files
clean_go:
find . -name "go.sum" -type f -exec rm -rf {} \;
# Remove all unused dependencies
tidy:
make clean
make clean_go
go mod tidy
# Install everything
# optional DEST=destination_path
install:
make clean
make build
make install_tls
make install_admin
make install_api
make install_cli
# Install TLS server and restart service
# optional DEST=destination_path
install_tls:
sudo systemctl stop $(TLS_NAME)
sudo cp $(OUTPUT)/$(TLS_NAME) $(DEST)
sudo systemctl start $(TLS_NAME)
# Install Admin server and restart service
# optional DEST=destination_path
install_admin:
sudo systemctl stop $(ADMIN_NAME)
sudo cp $(OUTPUT)/$(ADMIN_NAME) $(DEST)
sudo rsync -av admin/templates/ $(DEST)/tmpl_admin
sudo rsync -av admin/static/ $(DEST)/static
sudo systemctl start $(ADMIN_NAME)
# Install API server and restart service
# optional DEST=destination_path
install_api:
sudo systemctl stop $(API_NAME)
sudo cp $(OUTPUT)/$(API_NAME) $(DEST)
sudo systemctl start $(API_NAME)
# Install CLI
# optional DEST=destination_path
install_cli:
sudo cp $(OUTPUT)/$(CLI_NAME) $(DEST)
# Display systemd logs for TLS server
logs_tls:
sudo journalctl -f -t $(TLS_NAME)
# Display docker logs for TLS server
docker_dev_logs_tls:
docker logs -f $(TLS_NAME)-dev
# Display systemd logs for Admin server
logs_admin:
sudo journalctl -f -t $(ADMIN_NAME)
# Display docker logs for Admin server
docker_dev_logs_admin:
docker logs -f $(ADMIN_NAME)-dev
# Display systemd logs for API server
logs_api:
sudo journalctl -f -t $(API_NAME)
# Display docker logs for API server
docker_dev_logs_api:
docker logs -f $(API_NAME)-dev
# Display docker logs for nginx server
docker_dev_logs_nginx:
docker logs -f osctrl-nginx-dev
# Display docker logs for osquery client
docker_dev_logs_osquery:
docker logs -f osctrl-osquery-dev
# Display docker logs for postgresql server
docker_dev_logs_postgresql:
docker logs -f osctrl-postgres-dev
# Display docker logs for redis server
docker_dev_logs_redis:
docker logs -f osctrl-redis-dev
# Docker shell into TLS server
docker_dev_shell_tls:
docker exec -it $(TLS_NAME)-dev /bin/bash
# Docker shell into Admin server
docker_dev_shell_admin:
docker exec -it $(ADMIN_NAME)-dev /bin/bash
# Docker shell into API server
docker_dev_shell_api:
docker exec -it $(API_NAME)-dev /bin/bash
# Docker shell into osquery client
docker_dev_shell_osquery:
docker exec -it osctrl-osquery-dev /bin/bash
# Docker shell into postgresql server
docker_dev_shell_postgres:
docker exec -it osctrl-postgres-dev /bin/bash
# Docker shell into redis server
docker_dev_shell_redis:
docker exec -it osctrl-redis-dev /bin/sh
# Build dev docker containers and run them (also generates new certificates)
docker_dev_build:
ifeq (,$(wildcard ./.env))
$(error Missing .env file)
endif
ifeq (,$(wildcard ./deploy/docker/conf/tls/osctrl.crt))
$(error Missing TLS certificate file)
endif
ifeq (,$(wildcard ./deploy/docker/conf/tls/osctrl.key))
$(error Missing TLS private key file)
endif
docker-compose -f docker-compose-dev.yml build
# Build and run dev docker containers
make docker_dev:
make docker_dev_build
make docker_dev_up
# Run docker containers
docker_dev_up:
docker-compose -f docker-compose-dev.yml up
up-backend:
docker-compose -f docker-compose-dev.yml up osctrl-postgres osctrl-redis
# Takes down docker containers
docker_dev_down:
docker-compose -f docker-compose-dev.yml down
# Deletes all osctrl docker images
docker_dev_clean:
docker images | grep osctrl | awk '{print $$3}' | xargs -rI {} docker rmi -f {}
# Rebuild only the TLS server
docker_dev_rebuild_tls:
docker-compose -f docker-compose-dev.yml up --force-recreate --no-deps -d --build $(TLS_NAME)
# Rebuild only the Admin server
docker_dev_rebuild_admin:
docker-compose -f docker-compose-dev.yml up --force-recreate --no-deps -d --build $(ADMIN_NAME)
# Rebuild only the API server
docker_dev_rebuild_api:
docker-compose -f docker-compose-dev.yml up --force-recreate --no-deps -d --build $(API_NAME)
# Auto-format and simplify the code
GOFMT_ARGS = -l -w -s
gofmt-tls:
gofmt $(GOFMT_ARGS) ./$(TLS_CODE)
gofmt-admin:
gofmt $(GOFMT_ARGS) ./$(ADMIN_CODE)
gofmt-api:
gofmt $(GOFMT_ARGS) ./$(API_CODE)
gofmt-cli:
gofmt $(GOFMT_ARGS) ./$(CLI_CODE)
# Run all tests
test:
go clean -testcache ./...
go test ./utils -v
go test ./tls/handlers -v
# Check test coverage
test_cover:
cd utils && go test -cover .
cd tls/handlers && go test -cover .