~dricottone/huttese

ref: 2d2a6f66c88338f618bb6b6e5ceb6c26aec64b89 huttese/Makefile.podman -rw-r--r-- 7.8 KiB
2d2a6f66Dominic Ricottone Major updates 1 year, 7 months ago
                                                                                
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
# set podman-compliant container management CLI binary
CONMAN=podman

# set fun names for the containers
HUTTESE_POD=podracing
HUTTESE_SRHT=huttsr
HUTTESE_REDIS=huttredis
HUTTESE_POSTGRES=huttpg

# configure redis
REDIS_TARGET=docker.io/library/redis:alpine3.17
REDIS_LOCALNAME=my-redis
REDIS_DATADIR=/var/deploy/data/redis

# configure postgres
POSTGRES_TARGET=docker.io/library/postgres:14-alpine
POSTGRES_LOCALNAME=my-postgres
POSTGRES_DATADIR=/var/deploy/data/postgres
POSTGRES_CONF=/var/deploy/conf/postgres/postgresql.conf

# configure git
GIT_DATADIR=/var/deploy/data/git

# set image tag data
SRHT_LOCALNAME=srht
SRHT_LOCALVERSION=3

CERTDIR=/var/deploy/certs

keys:
	ssh-keygen -A -f etc/ssh/

pod:
	$(CONMAN) pod exists $(HUTTESE_POD) >/dev/null 2>&1 \
		|| $(CONMAN) pod create --name $(HUTTESE_POD) \
		--add-host=tatooine:127.0.0.1 \
		--add-host=dominic-ricottone.com:127.0.0.1 \
		--add-host=meta.dominic-ricottone.com:127.0.0.1 \
		--add-host=git.dominic-ricottone.com:127.0.0.1 \
		--add-host=todo.dominic-ricottone.com:127.0.0.1 \
		--publish 0.0.0.0:80:80 --publish 0.0.0.0:443:443 --publish 0.0.0.0:8822:22

databases:
	$(CONMAN) inspect -f '{{.State.Running}}' $(HUTTESE_POSTGRES) >/dev/null 2>&1 \
		|| $(CONMAN) start $(HUTTESE_POSTGRES)
	$(CONMAN) inspect -f '{{.State.Running}}' $(HUTTESE_REDIS) >/dev/null 2>&1 \
		|| $(CONMAN) start $(HUTTESE_REDIS)

cleanup:
	$(CONMAN) rm --force $(HUTTESE_REDIS) >/dev/null 2>&1 || true
	$(CONMAN) image rm --force $(REDIS_LOCALNAME):latest >/dev/null 2>&1 || true
	$(CONMAN) rm --force $(HUTTESE_POSTGRES) >/dev/null 2>&1 || true
	$(CONMAN) image rm --force $(POSTGRES_LOCALNAME):latest >/dev/null 2>&1 || true

setup: pod
	$(CONMAN) inspect $(REDIS_LOCALNAME) >/dev/null 2>&1 \
		|| $(CONMAN) pull $(REDIS_TARGET) \
		&& $(CONMAN) tag $(REDIS_TARGET) $(REDIS_LOCALNAME)
	$(CONMAN) run --detach --name $(HUTTESE_REDIS) --restart always \
		--pod $(HUTTESE_POD) \
		$(REDIS_LOCALNAME)

	$(CONMAN) inspect $(POSTGRES_LOCALNAME) >/dev/null 2>&1 \
		|| $(CONMAN) pull $(POSTGRES_TARGET) \
		&& $(CONMAN) tag $(POSTGRES_TARGET) $(POSTGRES_LOCALNAME)
	$(CONMAN) run --detach --name $(HUTTESE_POSTGRES) --restart always \
		--env POSTGRES_HOST_AUTH_METHOD=trust \
		--pod $(HUTTESE_POD) \
		--mount type=bind,src=$(POSTGRES_DATADIR),dst=/var/lib/postgresql/data,Z \
		--mount type=bind,src=$(POSTGRES_CONF),dst=/etc/postgresql/postgresql.conf,Z \
		$(POSTGRES_LOCALNAME) -c 'config_file=/etc/postgresql/postgresql.conf'

image:
	# these are only necessary if on aarch64
	cp -r /var/deploy/build/huttese-apk/pkg sr/pkg
	cp -r /var/deploy/build/huttese-apk/keys sr/etc/apk/keys

	$(CONMAN) inspect $(SRHT_LOCALNAME) >/dev/null 2>&1 \
		|| $(CONMAN) build \
		--tag $(SRHT_LOCALNAME):latest \
		--tag $(SRHT_LOCALNAME):$(SRHT_LOCALVERSION) \
		sr/

	# again, only necessary if on aarch64
	rm -rf sr/pkg sr/etc/apk/keys

dbinit:
	$(CONMAN) exec $(HUTTESE_POSTGRES) \
		createdb -U postgres meta.sr.ht
	$(CONMAN) exec $(HUTTESE_POSTGRES) \
		createdb -U postgres git.sr.ht
	$(CONMAN) exec $(HUTTESE_POSTGRES) \
		createdb -U postgres todo.sr.ht
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbinit --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) metasrht-initdb
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbinit --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) gitsrht-initdb
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbinit --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) todosrht-initdb

dbmigrate: databases
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) srht-migrate meta.sr.ht upgrade head
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) metasrht-migrate upgrade head
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) srht-migrate git.sr.ht upgrade head
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) gitsrht-migrate upgrade head
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) srht-migrate todo.sr.ht upgrade head
	$(CONMAN) run --name $(HUTTESE_SRHT)_dbmigrate --rm \
		--pod $(HUTTESE_POD) \
		$(SRHT_LOCALNAME) todosrht-migrate upgrade head

start: pod databases image
	$(CONMAN) run --detach --name $(HUTTESE_SRHT) --restart always \
		--pod $(HUTTESE_POD) \
		--mount type=bind,src=$(GIT_DATADIR),dst=/var/lib/git,Z \
		--mount type=bind,src=$(CERTDIR),dst=/var/lets-encrypt,Z \
		$(SRHT_LOCALNAME)

USER_EMAIL?=
USER_NAME?=

adduser: databases
	$(CONMAN) exec -it $(HUTTESE_SRHT) metasrht-manageuser -e $(USER_EMAIL) -t active_free $(USER_NAME) \
		|| echo "USAGE: USER_EMAIL=me@example.com USER_NAME=me make adduser"

addadmin: databases
	$(CONMAN) exec -it $(HUTTESE_SRHT) metasrht-manageuser -e $(USER_EMAIL) -t admin $(USER_NAME) \
		|| echo "USAGE: USER_EMAIL=me@example.com USER_NAME=me make adduser"

shell:
	$(CONMAN) exec -it $(HUTTESE_SRHT) sh

psql: databases
	$(CONMAN) exec -it huttpg psql --username=postgres

stop:
	$(CONMAN) stop $(HUTTESE_SRHT)

restart: databases
	$(CONMAN) restart $(HUTTESE_SRHT)

clean:
	$(CONMAN) rm --force $(HUTTESE_SRHT) >/dev/null 2>&1 || true
	$(CONMAN) image rm --force $(SRHT_LOCALNAME):latest >/dev/null 2>&1 || true

backups/image-backup.tar:
	mkdir -p backups
	$(CONMAN) pod stop $(HUTTESE_POD)
	$(CONMAN) commit $(HUTTESE_SRHT) backup
	$(CONMAN) save --output backups/image-backup.tar backup
	#try: $(CONMAN) load --input backups/image-backup.tar

backups/db-postgres-backup.sql:
	mkdir -p backups
	$(CONMAN) exec -it $(HUTTESE_POSTGRES) bash -c 'pg_dump --clean --dbname=postgres --username=postgres > /db-postgres-backup.sql'
	$(CONMAN) cp $(HUTTESE_POSTGRES):/db-postgres-backup.sql backups/db-postgres-backup.sql
	#try: $(CONMAN) cp backups/db-postgres-backup.sql $(HUTTESE_POSTGRES):/db-postgres-backup.sql && $(CONMAN) exec -it $(HUTTESE_POSTGRES) pg_restore --clean --dbname=postgres --username=postgres /db-postgres-backup.sql

backups/db-meta.sr.ht-backup.sql:
	mkdir -p backups
	$(CONMAN) exec -it $(HUTTESE_POSTGRES) bash -c 'pg_dump --clean --dbname=meta.sr.ht --username=postgres > /db-meta.sr.ht-backup.sql'
	$(CONMAN) cp $(HUTTESE_POSTGRES):/db-meta.sr.ht-backup.sql backups/db-meta.sr.ht-backup.sql
	#try: $(CONMAN) cp backups/db-meta.sr.ht-backup.sql $(HUTTESE_POSTGRES):/db-meta.sr.ht-backup.sql && $(CONMAN) exec -it $(HUTTESE_POSTGRES) pg_restore --clean --dbname=meta.sr.ht --username=postgres /db-meta.sr.ht-backup.sql

backups/db-git.sr.ht-backup.sql:
	mkdir -p backups
	$(CONMAN) exec -it $(HUTTESE_POSTGRES) bash -c 'pg_dump --clean --dbname=git.sr.ht --username=postgres > /db-git.sr.ht-backup.sql'
	$(CONMAN) cp $(HUTTESE_POSTGRES):/db-git.sr.ht-backup.sql backups/db-git.sr.ht-backup.sql
	#try: $(CONMAN) cp backups/db-git.sr.ht-backup.sql $(HUTTESE_POSTGRES):/db-git.sr.ht-backup.sql && $(CONMAN) exec -it $(HUTTESE_POSTGRES) pg_restore --clean --dbname=git.sr.ht --username=postgres /db-git.sr.ht-backup.sql

backups/db-todo.sr.ht-backup.sql:
	mkdir -p backups
	$(CONMAN) exec -it $(HUTTESE_POSTGRES) bash -c 'pg_dump --clean --dbname=todo.sr.ht --username=postgres > /db-todo.sr.ht-backup.sql'
	$(CONMAN) cp $(HUTTESE_POSTGRES):/db-todo.sr.ht-backup.sql backups/db-todo.sr.ht-backup.sql
	#try: $(CONMAN) cp backups/db-todo.sr.ht-backup.sql $(HUTTESE_POSTGRES):/db-todo.sr.ht-backup.sql && $(CONMAN) exec -it $(HUTTESE_POSTGRES) pg_restore --clean --dbname=todo.sr.ht --username=postgres /db-todo.sr.ht-backup.sql

backups/git-backup.tar.gz:
	mkdir -p backups
	sudo tar -czvf backups/git-backup.tar.gz ../../data/git
	#try: sudo tar -xzvf backups/git-backup.tar.gz -C ../../data/git

backup: backups/image-backup.tar backups/db-postgres-backup.sql backups/db-meta.sr.ht-backup.sql backups/db-git.sr.ht-backup.sql backups/db-todo.sr.ht-backup.sql backups/git-backup.tar.gz

.PHONY: pod databases cleanup setup image dbinit dbmigrate start adduser addadmin shell psql stop restart clean backup