forked from alexandregv/Matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
162 lines (126 loc) · 4.96 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
# Config
STACK=matcha
# Cross-platforms
ECHO = echo
ifeq ($(shell uname),Linux)
ECHO += -e
endif
MAKEFLAGS += --no-print-directory
HOSTNAME = $(shell hostname)
# Colors
C_INFO = \033[0;36m
C_PENDING = \033[0;33m
C_SUCCESS = \033[0;32m
C_RESET = \033[0m
all: init-db load-db stack-up
# ---- MISC ----#
open:
@open http://localhost:3000/
@open http://localhost:7474/
@open http://localhost:4000/graphql
open-public:
@open http://${HOSTNAME}:3000/
@open http://${HOSTNAME}:7474/
@open http://${HOSTNAME}:4000/graphql
# -- COMPOSE -- #
pull:
@$(ECHO) "$(C_PENDING)\nPulling images...$(C_RESET)"
@docker-compose pull
@$(ECHO) "$(C_SUCCESS)Pulled images successfully.$(C_RESET)"
build:
@$(ECHO) "$(C_PENDING)\nBuilding images...$(C_RESET)"
@docker-compose build
@$(ECHO) "$(C_SUCCESS)Built images successfully.$(C_RESET)"
up:
# -@$(MAKE) pull
@$(ECHO) "$(C_PENDING)\nStarting compose project...$(C_RESET)"
@docker-compose up -d
@$(ECHO) "$(C_SUCCESS)Started compose project.$(C_RESET)"
@$(ECHO) "$(C_INFO)Run \`make logs\` to see (and follow) logs.$(C_RESET)"
-@$(MAKE) ps
down:
@$(ECHO) "$(C_PENDING)\nStoping compose project...$(C_RESET)"
@docker-compose down
@$(ECHO) "$(C_SUCCESS)Stopped compose project.$(C_RESET)"
restart:
@$(ECHO) "$(C_PENDING)\nRestarting compose project...$(C_RESET)"
@docker-compose restart
@$(ECHO) "$(C_SUCCESS)Restarted compose project.$(C_RESET)"
ps:
@docker-compose ps
@echo '------------'
@docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"
logs:
@docker-compose logs -f
# --- STACK --- #
stack-init:
@$(ECHO) "$(C_PENDING)\nCreating a new swarm...$(C_RESET)"
@docker swarm init --advertise-addr 127.0.0.1
@$(ECHO) "$(C_SUCCESS)Created swarm.$(C_RESET)"
stack-leave:
@$(ECHO) "$(C_PENDING)\nLeaving swarm...$(C_RESET)"
@docker swarm leave --force
@$(ECHO) "$(C_SUCCESS)Left swarm.$(C_RESET)"
stack-start:
@$(ECHO) "$(C_PENDING)\nDeploying stack...$(C_RESET)"
@docker stack deploy -c docker-compose.yml ${STACK}
@$(ECHO) "$(C_SUCCESS)Deployed stack.$(C_RESET)"
stack-stop:
@$(ECHO) "$(C_PENDING)\nStopping stack...$(C_RESET)"
-@docker stack rm ${STACK}
@$(ECHO) "$(C_SUCCESS)Stoped stack.$(C_RESET)"
stack-up:
-@$(MAKE) stack-init
-@$(MAKE) stack-start
@$(ECHO) ""
@docker service ls
stack-down:
-@$(MAKE) stack-stop
-@$(MAKE) stack-leave
stack-reload:
@$(ECHO) "$(C_PENDING)\nReloading stack...$(C_RESET)"
@docker stack deploy -c docker-compose.yml ${STACK}
@$(ECHO) "$(C_SUCCESS)Reloaded stack.$(C_RESET)"
stack-ls:
@docker stack services ${STACK}
stack-ps:
@docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}'
@echo '------------'
@docker service ls -q | xargs docker service ps --format 'table {{.ID}}\t{{.Image}}\t{{.Name}}\t{{.DesiredState}}\t{{.CurrentState}}\t{{.Error}}'
stack-logs:
@docker stack services ${STACK} --format '{{.Name}}' | xargs -I % sh -c ' echo "\n$(C_INFO)---------- [ % ] ----------$(C_RESET)\n"; docker service logs --raw %;'
stack-stats:
@docker stats --no-stream --format 'table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}'
stack-fix:
@$(ECHO) "$(C_PENDING)\nTrying to fix (force removing network)...$(C_RESET)"
docker network disconnect -f $(docker network inspect matcha_default -f "{{.Id}}") $(STACK)_default-endpoint
docker network prune -f
@$(ECHO) "$(C_SUCCESS)Fixed it!$(C_RESET)"
# --- DB --- #
init-db:
@$(ECHO) "$(C_PENDING)\nInitializing empty datasbase...$(C_RESET)"
ifneq ($(wildcard db/data/databases/graph.db),)
@stty -echo; read -n1 -p "Reset existing database ? [y/N]: " pwd; stty echo; echo $$pwd; if [ "$$pwd" = "y" ]; then rm -r ./db/data/databases/graph.db; echo "$(C_SUCCESS)Reseted database.$(C_RESET)"; fi
endif
@docker run -it --rm -v ${CURDIR}/db/data:/data neo4j /bin/bash -c "neo4j start; echo 'Waiting for database to be created..'; sleep 3; echo 'Database should be OK now.'; neo4j stop"
@$(ECHO) "$(C_SUCCESS)Initialized empty datasbase.$(C_RESET)"
seed-db:
-@docker-compose exec api /usr/local/bin/npm run seed
-@docker exec -it $(STACK)_api.1.$(docker service ps -f 'name=$(STACK)_api.1' stack_myservice -q --no-trunc | head -n1) /usr/local/bin/npm run seed
@$(ECHO) "$(C_SUCCESS)Seeded datasbase.$(C_RESET)"
dump-db:
@$(ECHO) "$(C_PENDING)\nDumping database to db/data/graph.db.dump$(C_RESET)"
-@docker service scale $(STACK)_db=0
-@docker-compose stop db
@docker run -it --rm -v ${CURDIR}/db/data:/data neo4j /bin/bash -c "rm /data/graph.db.dump; neo4j-admin dump --to /data"
-@docker-compose start db
-@docker service scale $(STACK)_db=1
@$(ECHO) "$(C_SUCCESS)Dumped database$(C_RESET)"
load-db:
@$(ECHO) "$(C_PENDING)\nLoading database from db/data/graph.db.dump$(C_RESET)"
-@docker service scale $(STACK)_db=0
-@docker-compose stop db
@docker run -it --rm -v ${CURDIR}/db/data:/data neo4j /bin/bash -c "neo4j-admin load --from /data/graph.db.dump --force"
-@docker-compose start db
-@docker service scale $(STACK)_db=1
@$(ECHO) "$(C_SUCCESS)Imported database.$(C_RESET)"