-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
158 lines (109 loc) · 4.45 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
SHELL := /bin/bash
ifneq ($(shell groups $(shell whoami) | grep -c "\bdocker\b"), 1)
$(error User $(shell whoami) should belong to the docker group for running recipes)
endif
PROD ?= false
ifeq ($(PROD),true)
DOCKER_COMPOSE_UP_FLAGS := -f docker-compose.yml -f docker-compose.prod.yml
endif
MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
DONE_DIR := $(abspath $(MAKEFILE_DIR)/.done)
APT_PKG_DIR := $(DONE_DIR)/apt
MLSPLOIT_SETUP_DIR := $(DONE_DIR)/mlsploit
APT_PACKAGES :=\
$(APT_PKG_DIR)/curl \
$(APT_PKG_DIR)/git
MLSPLOIT_MODULE_NAMES :=\
mlsploit-rest-api \
mlsploit-execution-backend \
mlsploit-web-ui
GIT_SUBMODULES := $(addsuffix /.git,$(MLSPLOIT_MODULE_NAMES))
MLSPLOIT_MODULES := $(addprefix $(MLSPLOIT_SETUP_DIR)/,$(MLSPLOIT_MODULE_NAMES))
MODULE_PREREQUISITES := /usr/bin/docker-compose $(MLSPLOIT_SETUP_DIR)
DOCKER_COMPOSE_RELEASE := https://github.com/docker/compose/releases/download
DOCKER_COMPOSE_VERSION := 1.25.5/docker-compose-$(shell uname -s)-$(shell uname -m)
DOCKER_COMPOSE_URL := $(DOCKER_COMPOSE_RELEASE)/$(DOCKER_COMPOSE_VERSION)
# ~~~
.PHONY: all
all: ubuntu_deps docker_compose_build
.PHONY: clean
clean:
sudo rm -rf $(dir $(GIT_SUBMODULES)) && git submodule deinit -f .
rm -rf $(MLSPLOIT_SETUP_DIR)
.PHONY: ubuntu_deps
ubuntu_deps: $(APT_PACKAGES) /usr/bin/docker-compose
$(APT_PKG_DIR) $(MLSPLOIT_SETUP_DIR):
mkdir -p $@
# ~~~
.DELETE_ON_ERROR: $(APT_PACKAGES)
$(APT_PACKAGES): | $(APT_PKG_DIR)
sudo apt-get update -y
sudo apt-get install -y $(@F) | tee $@; test $${PIPESTATUS[0]} -eq 0
# ~~~
/usr/bin/docker: | /snap/bin/docker
/usr/bin/docker-compose: | /usr/local/bin/docker-compose
/usr/bin/docker /usr/bin/docker-compose:
sudo ln -s $| $@
/snap/bin/docker:
sudo snap install docker
@echo "Waiting 30s for docker to start..." && sleep 30
/usr/local/bin/docker-compose: | /usr/bin/docker
sudo curl -L $(DOCKER_COMPOSE_URL) -o $@
sudo chmod +x $@
# ~~~
$(GIT_SUBMODULES): | $(APT_PKG_DIR)/git
git submodule update --init `dirname $@`
.PHONY: git_submodules
git_submodules: $(GIT_SUBMODULES)
# ~~~
DOCKER_API_SETUP_ARGS ?= -apt
mlsploit-rest-api/modules.csv: | mlsploit-rest-api/.git
cd $(@D) && ln -s $(abspath modules.csv.example) $(@F)
mlsploit-rest-api/.env: | mlsploit-rest-api/.git
cp $(@D)/.env.example $@
.DELETE_ON_ERROR: $(MLSPLOIT_SETUP_DIR)/mlsploit-rest-api
$(MLSPLOIT_SETUP_DIR)/mlsploit-rest-api: mlsploit-rest-api/modules.csv | $(MODULE_PREREQUISITES)
cd $(@F) && ./docker-setup-api.sh $(DOCKER_API_SETUP_ARGS) | tee $@; test $${PIPESTATUS[0]} -eq 0
mlsploit-rest-api/.admintoken: $(MLSPLOIT_SETUP_DIR)/mlsploit-rest-api
cd $(@D) && ./docker-manage-api.sh drf_create_token admin | cut -d " " -f 3 > $(@F)
# ~~~
BACKEND_PREREQUISITES := mlsploit-execution-backend/.env mlsploit-execution-backend/modules.csv
mlsploit-execution-backend/.env: mlsploit-rest-api/.admintoken | mlsploit-execution-backend/.git
API_ADMIN_TOKEN=$$(cat $<) && cd $(@D) && ./env-set-token.sh "$${API_ADMIN_TOKEN}"
mlsploit-execution-backend/modules.csv: | mlsploit-execution-backend/.git
cd $(@D) && ln -s $(abspath modules.csv.example) $(@F)
.DELETE_ON_ERROR: $(MLSPLOIT_SETUP_DIR)/mlsploit-execution-backend
$(MLSPLOIT_SETUP_DIR)/mlsploit-execution-backend: $(BACKEND_PREREQUISITES) | $(MODULE_PREREQUISITES)
cd $(@F) && ./docker-setup-execution.sh | tee $@; test $${PIPESTATUS[0]} -eq 0
# ~~~
.DELETE_ON_ERROR: $(MLSPLOIT_SETUP_DIR)/mlsploit-web-ui
$(MLSPLOIT_SETUP_DIR)/mlsploit-web-ui: | mlsploit-web-ui/.git $(MODULE_PREREQUISITES)
cd $(@F) && ./docker-setup-ui.sh | tee $@; test $${PIPESTATUS[0]} -eq 0
# ~~~
.PHONY: git_pull_recursive
git_pull_recursive: $(MLSPLOIT_MODULES)
git pull --recurse-submodules
# ~~~
.PHONY: docker_compose_build
docker_compose_build: $(MLSPLOIT_MODULES) | /usr/bin/docker-compose
docker-compose build
.PHONY: docker_compose_up
docker_compose_up: docker_compose_build
docker-compose $(DOCKER_COMPOSE_UP_FLAGS) up -d
.PHONY: docker_compose_up_prod
docker_compose_up_prod:
$(MAKE) docker_compose_up PROD=true
.PHONY: docker_compose_logs
docker_compose_logs:
docker-compose logs -f
.PHONY: docker_compose_down
docker_compose_down:
docker-compose down
# ~~~
.PHONY: docker_hosting_certbot
docker_hosting_certbot:
docker-compose exec hosting certbot --apache $(ARGS)
.PHONY: docker_api_enable_ssl
docker_api_enable_ssl: mlsploit-rest-api/.env
LINENUM=$$(grep -nm 1 "^MLSPLOIT_HOSTING_SSL_ENABLED=" $< | cut -f1 -d:) \
&& (sed "$${LINENUM}s/.*/MLSPLOIT_HOSTING_SSL_ENABLED=true/" $< > $<.tmp) && mv $<.tmp $<