-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.post
147 lines (120 loc) · 7.05 KB
/
Makefile.post
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
# for later (add testbed docker instance(s))
# python3 -m pip install
# sudo apt update && sudo apt install python3-pip
# sudo pip install psycopg2-binary
new-docker-host-vm: gen-host-vm-configs
@echo "### Generating new host vm..."
if [ $(FORCE_DB_CREATION) -eq 0 ]; then \
pg_dump --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) --dbname $(DB_NAME) -f $(DB_DUMP_FILE); \
fi
@# out with the old
-multipass delete $(APP_HOST)
-multipass purge
@# WIP WIP WIP -->
@# switch multipass over to use libvirt - https://multipass.run/docs/using-libvirt
@# connect the libvirt interface/plug
@#snap connect multipass:libvirt
@# stop all instances
@#multipass stop --all
@# tell Multipass to use libvirt
@#sudo multipass set local.driver=libvirt
@# <-- WIP WIP WIP
@# and in with the new
multipass launch --verbose --cpus $(CPUS) -d $(MP_DISK_SIZE) -m 8G --name $(APP_HOST) --cloud-init $(HOST_VM_CLOUD_INIT_FILE) $(MP_IMAGE_NAME)
multipass list --verbose
@# post-install reboot
@echo "### Rebooting host vm after configuration..."
multipass restart $(APP_HOST)
@# finally, mount code repository to finish setup from the new host vm
@echo "### Mounting app directory..."
@multipass mount $(APP_HOME) $(APP_HOST):$(APP_PATH)
@# below, an alternative approach...mount, copy into place and unmount.
@#multipass mount $(APP_HOME) $(APP_HOST):/mnt
@#multipass exec $(APP_HOST) -- $(SUDO) mkdir $(APP_PATH)
@#echo "### Copying app directory..."
@# oh, transfer only works for files and not dirs - too bad...
@#multipass transfer $(APP_HOME) $(APP_HOST):$(APP_PATH)
@# ...use cp instead.
@#multipass exec $(APP_HOST) -- $(SUDO) cp -r /mnt $(APP_PATH)
@#echo "### Unmounting app directory..."
@#multipass umount $(APP_HOST):/mnt
@echo "### New host vm complete..."
# dummy target used for testing
x-new-containers:
echo executing new-containers recipe
new-containers: gen-container-configs
@echo "### Generating new containers..."
if [ $(FORCE_DB_CREATION) -eq 0 ] && [ ! -f $(DB_DUMP_FILE) ]; then \
pg_dump --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) --dbname $(DB_NAME) -f $(DB_DUMP_FILE); \
fi
@# tear it down
multipass exec $(APP_HOST) -- $(SUDO) docker-compose --project-name $(APP_NAME) --log-level warning --verbose --file $(CODE_PATH)/$(DOCKER_COMPOSE_FILE) down
$(eval HOST_VM_IP=$(shell multipass list | grep $(APP_HOST) | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b'))
@echo "HOST_VM_IP is $(HOST_VM_IP)"
@# this doesn't apply anymore
@#if [ $(FORCE_DB_CREATION) -eq 1 ]; then (cd $(TOP)/$(APP_NAME) && $(SUDO) rm -rf db); fi
@# recreate the containers
multipass exec $(APP_HOST) -- $(SUDO) docker-compose --project-name $(APP_NAME) --log-level warning --verbose --file $(CODE_PATH)/$(DOCKER_COMPOSE_FILE) up --detach --force-recreate --build
@# allow db connections from host w/o password
@#if [ $(FORCE_DB_CREATION) -eq 1 ]; then ($(SUDO) $(SED) -e 's/^host all all all md5/host all all all reject/ $(TOP)/$(APP_NAME)/db/pg_hba.conf); fi
@# set iptables forwarding on local host to expose the container host vm
@# - can this not be done via the cloud-init file?
$(SUDO) $(FORWARD_PORT_CMD) -f $(APP_HOST) $(EXTERNAL_INTERFACE) $(SERVICE_PROTO) $(WEB_EXTERNAL_PORT) $(HOST_VM_IP) $(WEB_EXTERNAL_PORT) $(WEB_ALLOWED_HOSTS)
$(SUDO) $(FORWARD_PORT_CMD) -f $(APP_HOST) $(EXTERNAL_INTERFACE) $(SERVICE_PROTO) $(DB_EXTERNAL_PORT) $(HOST_VM_IP) $(DB_EXTERNAL_PORT) $(DB_ALLOWED_HOSTS)
@# wait a bit for everything to settle
sleep 10
@echo "### New containers complete..."
gen-host-vm-configs: $(HOST_VM_CLOUD_INIT_FILE_TEMPLATE)
@echo "### Generating host vm config from templates..."
$(SED) -E \
-e 's/___WEB_EXTERNAL_PORT___/$(WEB_EXTERNAL_PORT)/g' \
-e 's/___WEB_INSTANCE_PORT___/$(WEB_INSTANCE_PORT)/g' \
$(HOST_VM_CLOUD_INIT_FILE_TEMPLATE) > $(HOST_VM_CLOUD_INIT_FILE)
host-vm-network-config-service: $(HOST_VM_NET_SVC_SRC_DIR)/$(HOST_VM_NET_SVC_DEF) $(HOST_VM_NET_SVC_SRC_DIR)/$(HOST_VM_NET_SVC_EXEC)
@# install service to expose host vm services through legacy iptables rules
@$(SUDO) $(INSTALL) --mode 0644 $(HOST_VM_NET_SVC_SRC_DIR)/$(HOST_VM_NET_SVC_DEF) $(SYSTEMD_SVC_DIR)
@$(SUDO) $(INSTALL) --mode 0755 $(HOST_VM_NET_SVC_SRC_DIR)/$(HOST_VM_NET_SVC_EXEC) $(LOCAL_BIN)
@$(SUDO) systemctl daemon-reload
@$(SUDO) systemctl enable $(HOST_VM_NET_SVC_DEF)
@$(SUDO) systemctl start $(HOST_VM_NET_SVC_DEF)
gen-container-configs: $(filter-out $(HOST_VM_CLOUD_INIT_FILE_TEMPLATE), $(wildcard *.tmpl)) $(wildcard test/*.tmpl) $(wildcard $(TEST_DIR)/*.tmpl)
@echo "### Generating container configs from templates..."
@# if these substitutions get any more complex, use m4 instead.
for infile in $^; do \
dir=$$(dirname $$infile); \
outfile=$$(basename --suffix .tmpl $$infile); \
$(SED) -E \
-e 's@___WEB_EXTERNAL_PORT___@$(WEB_EXTERNAL_PORT)@g' \
-e 's@___WEB_INSTANCE_PORT___@$(WEB_INSTANCE_PORT)@g' \
-e 's@___APP_NAME___@$(APP_NAME)@g' \
-e 's@___APP_PATH___@$(APP_PATH)@g' \
-e 's@___CODE_PATH___@$(CODE_PATH)@g' \
-e 's@___DB_PATH___@$(DB_PATH)@g' \
-e 's@___DB_NAME___@$(DB_NAME)@g' \
-e 's@___DB_NAME_TEST___@$(DB_NAME_TEST)@g' \
-e 's@___DB_USER___@$(DB_USER)@g' \
-e 's@___DB_PASSWORD___@$(DB_PASSWORD)@g' \
-e 's@___DB_HOST___@$(APP_SERVICE_IP)@g' \
-e 's@___DB_EXTERNAL_PORT___@$(DB_EXTERNAL_PORT)@g' \
-e 's@___DB_INSTANCE_PORT___@$(DB_INSTANCE_PORT)@g' \
-e 's@___TIMEZONE___@$(TIMEZONE)@g' \
$$infile > $$dir/$$outfile; \
done
new-db:
@echo "### Generating new db..."
@echo "### - create/restore the db..."
if [ $(FORCE_DB_CREATION) -eq 1 ]; then \
psql --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) --file $(TOP)/$(APP_NAME)/code/sql/init.sql; \
elif [ -f $(DB_DUMP_FILE) ]; then \
psql --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) -c 'DROP DATABASE $(DB_NAME);'; \
psql --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) -c 'CREATE DATABASE $(DB_NAME);'; \
psql --host localhost --port $(DB_EXTERNAL_PORT) --username $(DB_USER) --dbname $(DB_NAME) -f $(DB_DUMP_FILE); \
else \
echo "### FORCE_DB_CREATION is not set and yet there is no db dump file ($(DB_DUMP_FILE)) to restore, why are we even here?"; \
fi
@# adjust max connections value
@# - no, not needed because it's done via 'alter system' command in the init script
@#multipass exec $(APP_HOST) -- docker exec $(SUDO) $(SED) -i -e 's/^max_connections = .*$$/max_connections = $(DB_MAX_CONNECTIONS)/' $(DB_PATH)/postgresql.conf
@# finally, restart the db
@# - this is not needed either.
@#multipass exec $(APP_HOST) -- $(SUDO) docker-compose --file $(CODE_PATH)/$(DOCKER_COMPOSE_FILE) --project-name $(APP_NAME) restart