-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
99 lines (81 loc) · 3.31 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
SHELL=/bin/sh
ifneq (,$(wildcard ./.env))
include .env
export
endif
TIMESTAMP := $(shell TZ=UTC date +%Y%m%d-%H%M)
export TAG ?= $(TIMESTAMP)
BRANCH ?= master
## Update jenkins/intermittency-$(BRANCH) secret from .env
update_secret:
grep -v export .env | kubectl create secret generic -n jenkins intermittency-$(BRANCH) --from-env-file=/dev/stdin --dry-run=true -o yaml | kubectl apply -f -
## Update jenkins/intermittency-production secret from .env-production
update_secret_prod:
grep -v export .env-production | kubectl create secret generic -n jenkins intermittency-production --from-env-file=/dev/stdin --dry-run=true -o yaml | kubectl apply -f -
psql:
psql postgres
psql_test:
psql intermittency_test
psql_prod:
psql intermittency_prod
## Dump db to intermittency.bak
pgdump:
pg_dump --clean --no-privileges -Fc -f intermittency.bak $(DUMPARGS) postgres
TARGETDB=intermittency_prod_new
.ONESHELL:
createdb_copy:
psql <<EOF
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'postgres' AND pid <> pg_backend_pid();
CREATE DATABASE $(TARGETDB) WITH TEMPLATE postgres OWNER postgres;
EOF
## Restore db to $TARGETDB
pgrestore: pgrestore_import pgrestore_clean
pgrestore_import:
createdb $(TARGETDB)
psql $(TARGETDB) -c "ALTER EXTENSION timescaledb UPDATE;"
psql $(TARGETDB) -c "SELECT timescaledb_pre_restore();"
pg_restore --clean -Fc -d $(TARGETDB) intermittency.bak
psql $(TARGETDB) -c "SELECT timescaledb_post_restore();"
.ONESHELL:
## Clean $TARGETDB
pgrestore_clean:
psql $(TARGETDB) <<EOF
DROP SCHEMA IF EXISTS telegraf CASCADE;
DROP SCHEMA IF EXISTS ellevio CASCADE;
DELETE FROM prices WHERE area_id IN (SELECT id FROM areas WHERE source IN('nordpool','nordpool_sek');
DELETE FROM areas WHERE source='nordpool_sek';
ALTER DATABASE intermittency_prod SET search_path TO intermittency, public;
GRANT CONNECT ON DATABASE intermittency_prod TO intermittency_prod;
GRANT USAGE ON SCHEMA intermittency TO PUBLIC;
GRANT SELECT ON TABLE intermittency.areas TO intermittency_prod;
GRANT SELECT ON TABLE intermittency.generation TO intermittency_prod;
GRANT SELECT ON TABLE intermittency.load TO intermittency_prod;
GRANT SELECT ON TABLE intermittency.prices TO intermittency_prod;
GRANT SELECT ON TABLE intermittency.production_types TO intermittency_prod;
GRANT SELECT ON TABLE intermittency.transmission TO intermittency_prod;
EOF
.ONESHELL:
## Rename intermittency_prod to _prod_old and swap in $(TARGETDB)
pgrestore_swap:
psql --single-transaction <<EOF
ALTER DATABASE intermittency_prod RENAME TO intermittency_prod_old;
ALTER DATABASE $(TARGETDB) RENAME TO intermittency_prod;
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'intermittency_prod';
EOF
pgrestore_deleteold:
dropdb intermittency_prod_old
#GRANTS for intermittency_prod
#ALTER DATABASE intermittency_prod SET search_path = intermittency, public;
dropdb2:
dropdb --force intermittency
createdb2:
createdb intermittency
## Prints this help
help:
@awk -v skip=1 \
'/^##/ { sub(/^[#[:blank:]]*/, "", $$0); doc_h=$$0; doc=""; skip=0; next } \
skip { next } \
/^#/ { doc=doc "\n" substr($$0, 2); next } \
/:/ { sub(/:.*/, "", $$0); printf "\033[34m%-30s\033[0m\033[1m%s\033[0m %s\n\n", $$0, doc_h, doc; skip=1 }' \
$(MAKEFILE_LIST)