-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
207 lines (167 loc) · 8.8 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
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
197
198
199
200
201
202
203
204
205
206
207
help:
@IFS=$$'\n' ; \
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//'`); \
for help_line in $${help_lines[@]}; do \
IFS=$$'#' ; \
help_split=($$help_line) ; \
help_command=`echo $${help_split[0]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
help_info=`echo $${help_split[2]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
printf "%-30s %s\n" $$help_command $$help_info ; \
done
postgres_user := $(shell id -un)
define _restore_db
$(call _reset_db,$1,$3)
sudo -u $3 psql $1 -f $2
endef
define _reset_db
sudo -u $2 psql postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$1' AND pid <> pg_backend_pid()"
-sudo -u $2 psql postgres -c "create user nhsrc with password 'password'";
-sudo -u $2 psql postgres -c 'drop database $1'
sudo -u $2 psql postgres -c 'create database $1 with owner nhsrc'
sudo -u $2 psql $1 -c 'create extension if not exists "uuid-ossp"'
endef
define _download_db_backup
-mkdir backups
-mkdir backups/$3
-mkdir backups/$3/$4
scp $1:/home/app/$2/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql backups/$3/$4
endef
define _apply_latest_db_local
$(call _download_db_backup,$1,$3,$4,$5)
$(call _restore_latest_db_local,$2,$3,$4)
endef
define _restore_latest_db_local
$(call _restore_db,$1,backups/$3/$4/facilities_assessment_$(shell date +%a).sql,$(postgres_user))
endef
define _alert_success
$(call _alert_message,Script Completed)
endef
define _alert_message
osascript -e 'tell application (path to frontmost application as text) to display dialog "$1" buttons {"OK"} with icon stop'
endef
define _fix_sql_file
find deployment-migrations/$1/local -type f -exec sed -i '' 's/"insert/insert/g' {} \;
find deployment-migrations/$1/local -type f -exec sed -i '' 's/"update/update/g' {} \;
find deployment-migrations/$1/local -type f -exec sed -i '' 's/"delete/delete/g' {} \;
find deployment-migrations/$1/local -type f -exec sed -i '' 's/;"/;/g' {} \;
endef
init-db:
-psql postgres -c "create user nhsrc with password 'password'";
apply-latest-db-from-jss-to-local: ## Downloads and applies the database dump
$(call _apply_latest_db_local,igunatmac,facilities_assessment_cg,jss,prod)
$(call _alert_success)
apply-latest-db-from-nhsrc-prod-to-local:
$(call _apply_latest_db_local,gunak-main,facilities_assessment_nhsrc,nhsrc,prod)
$(call _alert_success)
apply-latest-db-from-nhsrc-qa-to-local:
$(call _apply_latest_db_local,gunak-other,facilities_assessment_nhsrc,qa-server,nhsrc,qa)
$(call _alert_success)
download-latest-db-from-nhsrc-prod-to-local:
$(call _download_db_backup,gunak-main,,nhsrc,prod)
$(call _alert_success)
copy-latest-db-from-nhsrc-prod-to-nhsrc-qa:
ssh gunak-other "scp gunak-main:/home/app/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql /home/app/qa-server/facilities-assessment-host/backup"
download-latest-db-from-nhsrc-qa-to-local:
$(call _download_db_backup,gunak-other,qa-server,nhsrc,qa)
$(call _alert_success)
download-latest-db-from-jss-prod-to-local:
$(call _download_db_backup,igunatmac,,jss,prod)
$(call _alert_success)
apply-latest-db-from-nhsrc-prod-to-nhsrc-qa:
ssh gunak-other "scp gunak-main:/home/app/facilities-assessment-host/backup/facilities_assessment_$(date +%a).sql /home/app/qa-server/facilities-assessment-host/backup/"
ssh gunak-other "sudo -u postgres psql postgres -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'facilities_assessment_qa' AND pid <> pg_backend_pid()\""
-ssh gunak-other "sudo -u postgres psql postgres -c 'drop database facilities_assessment_qa'"
ssh gunak-other "sudo -u postgres psql postgres -c 'create database facilities_assessment_qa with owner nhsrc'"
ssh gunak-other "sudo -u postgres psql facilities_assessment_qa -c 'create extension if not exists \"uuid-ossp\"'"
ssh gunak-other "sudo -u postgres psql facilities_assessment_qa -f /home/app/qa-server/facilities-assessment-host/backup/facilities_assessment_$(date +%a).sql > /dev/null"
$(call _alert_success)
apply-latest-db-from-nhsrc-qa-to-nhsrc-prod:
ssh gunak-main "scp gunak-other:/home/app/qa-server/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql /home/app/facilities-assessment-host/backup/qa-backup.sql"
ssh gunak-main "sudo -u postgres psql postgres -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'facilities_assessment' AND pid <> pg_backend_pid()\""
-ssh gunak-main "sudo -u postgres psql postgres -c 'drop database facilities_assessment'"
ssh gunak-main "sudo -u postgres psql postgres -c 'create database facilities_assessment with owner nhsrc'"
ssh gunak-main "sudo -u postgres psql facilities_assessment -c 'create extension if not exists \"uuid-ossp\"'"
ssh gunak-main "sudo -u postgres psql facilities_assessment -f /home/app/facilities-assessment-host/backup/qa-backup.sql > /dev/null"
$(call _alert_success)
restore-nhsrc-local-db-from-file:
ifndef file
@echo "VARIABLE MISSING: file"
ls -lt backups/nhsrc/local
exit 1
else
$(call _restore_db,facilities_assessment_nhsrc,backups/nhsrc/local/${file},$(postgres_user))
endif
restore-jss-local-db-from-latest-file:
$(call _restore_db,facilities_assessment_cg,backups/jss/facilities_assessment_latest.sql,$(postgres_user))
restore-nhsrc-local-db-from-latest-file:
$(call _restore_db,facilities_assessment_nhsrc,backups/nhsrc/local/facilities_assessment_latest.sql,$(postgres_user))
restore-nhsrc-qa-db-from-todays-backup:
$(call _restore_db,facilities_assessment_qa,/home/app/qa-server/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql,postgres)
restore-nhsrc-prod-db-from-backup-file:
$(call _restore_db,facilities_assessment,/home/app/facilities-assessment-host/backup/backup.sql,postgres)
restore-nhsrc-qa-db-from-backup-file:
$(call _restore_db,facilities_assessment_qa,/home/app/qa-server/facilities-assessment-host/backup/backup.sql,postgres)
restore-jss-local-db-from-prod-local-backup:
ifndef day
@echo "VARIABLE MISSING: day"
ls -lt backups/jss/prod
else
$(call _restore_db,facilities_assessment_cg,backups/jss/prod/facilities_assessment_$(day).sql,$(postgres_user))
endif
restore-nhsrc-local-db-from-qa-local-backup:
ifndef day
@echo "VARIABLE MISSING: day"
ls -lt backups/nhsrc/qa
else
$(call _restore_db,facilities_assessment_nhsrc,backups/nhsrc/qa/facilities_assessment_$(day).sql,$(postgres_user))
endif
restore-nhsrc-local-db-from-prod-local-backup:
ifndef day
@echo "VARIABLE MISSING: day"
ls -lt backups/nhsrc/prod
exit 1
else
$(call _restore_db,facilities_assessment_nhsrc,backups/nhsrc/prod/facilities_assessment_$(day).sql,$(postgres_user))
endif
#############################
define _deploy_migrations
-ssh $4 "rm /home/app/$1/facilities-assessment-host/db/*.sql"
scp deployment-migrations/$5/$2/*.sql $4:/home/app/$1/facilities-assessment-host/db/
ssh $4 "cat /home/app/$1/facilities-assessment-host/db/*.sql | psql -h localhost -d $3 nhsrc -1"
endef
define _deploy_migrations_local
cd deployment-migrations/$2/$3 && cat master.sql | psql -h localhost -d $1 nhsrc -1
endef
migrations-to-nhsrc-local:
$(call _fix_sql_file,nhsrc)
$(call _deploy_migrations_local,facilities_assessment_nhsrc,nhsrc,local)
$(call _alert_success)
# Cannot do it remotely because it would require checkout locally anyway and it is faster to do locally
migrations-to-nhsrc-qa-locally:
$(call _deploy_migrations_local,facilities_assessment_xxx,nhsrc,qa)
$(call _alert_success)
migrations-to-nhsrc-prod-locally:
$(call _deploy_migrations_local,facilities_assessment_qa,nhsrc,prod)
migrations-to-jss-local:
$(call _fix_sql_file,jss)
$(call _deploy_migrations_local,facilities_assessment_cg,jss,local)
$(call _alert_success)
migrations-to-jss-prod:
$(call _deploy_migrations,,prod,facilities_assessment,igunatmac,jss)
$(call _alert_success)
#############################
backup-db-nhsrc-to-latest-file:
pg_dump -Unhsrc -hlocalhost -d facilities_assessment_nhsrc > backups/nhsrc/local/facilities_assessment_latest.sql
$(call _alert_success)
backup-db-jss-to-latest-file:
pg_dump -Unhsrc -hlocalhost -d facilities_assessment_cg > backups/jss/local/facilities_assessment_latest.sql
$(call _alert_success)
backup-db-jss-prod:
ssh igunatmac "pg_dump -Unhsrc -hlocalhost -d facilities_assessment > /home/app/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql"
$(call _alert_success)
backup-db-nhsrc-qa:
ssh gunak-other "pg_dump -Unhsrc -hlocalhost -d facilities_assessment_qa > /home/app/qa-server/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql"
$(call _alert_success)
backup-db-nhsrc-prod:
ssh gunak-main "pg_dump -Unhsrc -hlocalhost facilities_assessment > /home/app/facilities-assessment-host/backup/facilities_assessment_$(shell date +%a).sql"
$(call _alert_success)