1
|
##### Vars/functions
|
2
|
|
3
|
# Make
|
4
|
SHELL := /bin/bash
|
5
|
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
|
6
|
topDir = $(word 1,$(pathParts))
|
7
|
subPath = $(word 2,$(pathParts))
|
8
|
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
|
9
|
|
10
|
# OS
|
11
|
os := $(shell uname)
|
12
|
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
|
13
|
|
14
|
# System
|
15
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
16
|
|
17
|
# Terminal
|
18
|
esc := '['
|
19
|
reset := $(esc)'0m'
|
20
|
emph := $(esc)'7m '
|
21
|
endEmph := ' '$(reset)
|
22
|
|
23
|
# User interaction
|
24
|
|
25
|
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
|
26
|
|
27
|
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
|
28
|
|
29
|
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
|
30
|
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
|
31
|
$(error Aborting))
|
32
|
|
33
|
# File editing
|
34
|
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
|
35
|
|
36
|
# DB
|
37
|
mkSchemaCmd = 'CREATE SCHEMA $(1);'
|
38
|
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
|
39
|
|
40
|
##### Environment
|
41
|
|
42
|
export PGOPTIONS = --client-min-messages=WARNING
|
43
|
|
44
|
##### General targets
|
45
|
|
46
|
all:
|
47
|
|
48
|
.SUFFIXES: # turn off built-in suffix rules
|
49
|
.SECONDARY: # don't automatically delete intermediate files
|
50
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
51
|
|
52
|
_always:
|
53
|
.PHONY: _always
|
54
|
|
55
|
remake: _always clean
|
56
|
$(MAKE)
|
57
|
# re-run make so that cache of existing files is reset
|
58
|
|
59
|
%/remake: _always
|
60
|
$(MAKE) $(@D)/clean
|
61
|
$(MAKE) $(@D)/
|
62
|
# re-run make so that cache of existing files is reset
|
63
|
|
64
|
%-remake: _always
|
65
|
rm -f $*
|
66
|
$(MAKE) $*
|
67
|
# re-run make so that cache of existing files is reset
|
68
|
|
69
|
%/reinstall: _always %/uninstall %/install ;
|
70
|
|
71
|
##### Installation
|
72
|
|
73
|
install: _always core mysql inputs/download inputs/install test
|
74
|
@$(done)
|
75
|
|
76
|
uninstall: _always inputs/uninstall rm_mysql rm_core ;
|
77
|
|
78
|
reinstall: _always uninstall install ;
|
79
|
|
80
|
##### Core: VegBIEN DB and dependencies
|
81
|
|
82
|
core: _always $(call forOs,python php postgres) db
|
83
|
@$(done)
|
84
|
|
85
|
rm_core: _always rm_db ;
|
86
|
|
87
|
reinstall_core: _always rm_core core ;
|
88
|
|
89
|
##### Python
|
90
|
|
91
|
python-Linux: _always
|
92
|
-sudo apt-get install python
|
93
|
-sudo apt-get install python-dev
|
94
|
-sudo apt-get install python-dateutil
|
95
|
-sudo apt-get install python-parallel
|
96
|
-sudo apt-get install python-profiler
|
97
|
-sudo apt-get install python-pip
|
98
|
-sudo apt-get install pymetrics
|
99
|
-sudo pip install ordereddict
|
100
|
-sudo pip install pp
|
101
|
|
102
|
python-Darwin: _always
|
103
|
@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
|
104
|
@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
|
105
|
@echo 'Extract the archive'
|
106
|
@echo 'Change to the directory of the extracted files'
|
107
|
@echo 'Run python setup.py build'
|
108
|
@echo 'Run sudo python setup.py install'
|
109
|
@echo "Run python -c 'import dateutil'"
|
110
|
@$(wait)
|
111
|
@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
|
112
|
@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
|
113
|
@$(wait)
|
114
|
|
115
|
##### PHP
|
116
|
|
117
|
php-Linux: _always
|
118
|
-sudo apt-get install php-http-request
|
119
|
|
120
|
php-Darwin: _always
|
121
|
@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
|
122
|
@echo 'Download http://pear.php.net/go-pear.phar'
|
123
|
@echo 'Change to the directory of the downloaded file'
|
124
|
@echo 'Run php -d detect_unicode=0 go-pear.phar'
|
125
|
@echo 'Whenever prompted, press Enter to select default options'
|
126
|
@echo 'Add $$HOME/pear/bin to your $$PATH'
|
127
|
@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
|
128
|
@echo 'If needed, set $$PHPRC to your php.ini'
|
129
|
@echo "Run pear install HTTP_Request"
|
130
|
@echo 'To run a php command: php -c <php.ini-path> ...'
|
131
|
@$(wait)
|
132
|
|
133
|
##### PostgreSQL
|
134
|
|
135
|
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
|
136
|
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
|
137
|
|
138
|
apacheConf := /etc/apache2/apache2.conf
|
139
|
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
|
140
|
editApacheConfForPhppgadmin =\
|
141
|
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
|
142
|
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
|
143
|
|
144
|
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
|
145
|
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
|
146
|
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
|
147
|
|
148
|
postgres-Linux: _always
|
149
|
-sudo apt-get install postgresql
|
150
|
-sudo apt-get install postgresql-plpython-"$$(env minor=1 bin/pg_version)"
|
151
|
-sudo apt-get install libpq-dev
|
152
|
-sudo apt-get install phppgadmin
|
153
|
$(editPhppgadminApacheConf)
|
154
|
$(editApacheConfForPhppgadmin)
|
155
|
$(if $(nonApacheOnPort80),$(editApachePortsConf))
|
156
|
-sudo apache2ctl restart
|
157
|
-sudo pip install psycopg2
|
158
|
# run pg_version inline because it needs postgresql to be installed first
|
159
|
# ignore errors if conf files already edited
|
160
|
|
161
|
postgres-Darwin: _always
|
162
|
@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
|
163
|
@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
|
164
|
@echo 'Open the disk image'
|
165
|
@echo 'Run the installer in it'
|
166
|
@$(wait)
|
167
|
|
168
|
postgres-: _always ; # other OSes
|
169
|
|
170
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
171
|
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
|
172
|
# -E preserves env vars so PGOPTIONS is passed to psql
|
173
|
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
|
174
|
psqlAsBien := bin/psql_vegbien $(psqlOpts)
|
175
|
bienPassword := $(shell cat config/bien_password)
|
176
|
|
177
|
##### VegBIEN DB
|
178
|
|
179
|
#### DB and bien user
|
180
|
|
181
|
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
|
182
|
DB!,This includes all archived imports and staging tables.)
|
183
|
|
184
|
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
|
185
|
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
|
186
|
|
187
|
rmPublicSchema := $(call rmSchemaCmd,public)
|
188
|
|
189
|
db: mk_db rm_initial_public schemas/install _always ;
|
190
|
|
191
|
mk_db: _always
|
192
|
-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
|
193
|
-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
|
194
|
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
|
195
|
-bin/postgres_vegbien createlang plpythonu
|
196
|
# ignore errors if user/database/etc. exists
|
197
|
|
198
|
rm_initial_public: _always
|
199
|
@$(confirmRmPublicSchema)
|
200
|
echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
|
201
|
# drop public schema *as admin* because it starts out owned by postgres
|
202
|
|
203
|
rm_db: _always
|
204
|
@$(confirmRmDb)
|
205
|
echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
|
206
|
echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
|
207
|
|
208
|
reinstall_db: _always rm_db db ;
|
209
|
|
210
|
#### Schemas
|
211
|
|
212
|
schemas := temp functions py_functions public
|
213
|
schemas/install: $(schemas:%=schemas/%/install) ;
|
214
|
|
215
|
schemasReversed := public py_functions functions temp
|
216
|
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
|
217
|
|
218
|
### public
|
219
|
|
220
|
schemas/public/install: schemas/vegbien.sql _always
|
221
|
-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
|
222
|
<$< $(psqlAsBien)
|
223
|
# ignore errors if schema exists
|
224
|
# public schema will be owned by bien
|
225
|
|
226
|
schemas/public/uninstall: _always
|
227
|
@$(confirmRmPublicSchema)
|
228
|
echo $(rmPublicSchema)|$(psqlAsBien)
|
229
|
|
230
|
schemas/rotate: _always schemas/rotate-only schemas/public/install ;
|
231
|
schemas/rotate-only: _always
|
232
|
echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
|
233
|
|
234
|
### py_functions
|
235
|
|
236
|
schemas/py_functions/install: schemas/py_functions.sql _always
|
237
|
-<$< $(psqlAsAdminVegbien)
|
238
|
# ignore errors if schema exists
|
239
|
|
240
|
### Others
|
241
|
|
242
|
schemas/%/install: schemas/%.sql _always
|
243
|
-<$< $(psqlAsBien)
|
244
|
# ignore errors if schema exists
|
245
|
|
246
|
# /%.backup must come before /% to be matched first
|
247
|
schemas/%.backup/uninstall schemas/%/uninstall: _always
|
248
|
echo $(call rmSchemaCmd,$*)|$(psqlAsBien)
|
249
|
|
250
|
# Note: This can't be used for the current (unrotated) public schema because
|
251
|
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
|
252
|
# that public already exists because it's in template1.
|
253
|
schemas/%.backup:
|
254
|
"time" env data=1 bin/pg_dump_vegbien $* >$@
|
255
|
|
256
|
restore := "time" bin/postgres_vegbien pg_restore --exit-on-error --verbose
|
257
|
restoreBien := $(restore) --dbname=vegbien
|
258
|
# pg_restore doesn't support PGDATABASE env var
|
259
|
|
260
|
schemas/%.backup/restore: schemas/%.backup _always
|
261
|
$(restoreBien) $< &>$<.log
|
262
|
|
263
|
schemas/%.sql: schemas/%.backup
|
264
|
$(restore) $< >$@ 2>$<.extract.log
|
265
|
|
266
|
schemas/%/rm_indexes: _always
|
267
|
bin/pg_dump_vegbien $*|bin/mk_rm_indexes|$(psqlAsBien) --echo-all
|
268
|
|
269
|
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
|
270
|
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
|
271
|
|
272
|
##### MySQL
|
273
|
|
274
|
mysql: _always $(call forOs,mysql) mysql_user
|
275
|
@$(done)
|
276
|
|
277
|
rm_mysql: _always rm_mysql_user ;
|
278
|
|
279
|
mysql-Linux: _always
|
280
|
@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
|
281
|
@$(wait)
|
282
|
-sudo apt-get install mysql-server mysql-client python-mysqldb
|
283
|
|
284
|
mysql-Darwin: _always
|
285
|
@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
|
286
|
@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
|
287
|
@echo 'Extract the archive'
|
288
|
@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
|
289
|
@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
|
290
|
@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
|
291
|
@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
|
292
|
@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
|
293
|
@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
|
294
|
@echo 'Change to the directory of the extracted files'
|
295
|
@echo 'Run python setup.py clean'
|
296
|
@echo 'Run python setup.py build'
|
297
|
@echo 'Run sudo python setup.py install'
|
298
|
@echo "Run python -c 'import MySQLdb'"
|
299
|
@$(wait)
|
300
|
|
301
|
mysql-: _always # other OSes
|
302
|
|
303
|
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
|
304
|
|
305
|
mysql_user: _always
|
306
|
-$($@_cmd)
|
307
|
# ignore errors if user exists
|
308
|
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
|
309
|
'$(bienPassword)';"|$(mysqlAsRoot)
|
310
|
|
311
|
rm_mysql_user: _always
|
312
|
-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
|
313
|
# ignore errors if user exists
|
314
|
|
315
|
##### Datasources
|
316
|
|
317
|
inputs: _always inputs/all ;
|
318
|
|
319
|
import: _always import-msg inputs/import ;
|
320
|
import-msg: _always
|
321
|
@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
|
322
|
@echo
|
323
|
|
324
|
verify: _always inputs/verify
|
325
|
@$(done)
|
326
|
|
327
|
test: _always inputs/test
|
328
|
@$(done)
|
329
|
|
330
|
##### Testing
|
331
|
|
332
|
test-all: _always remake test missing_mappings
|
333
|
@$(done)
|
334
|
|
335
|
##### Subdir forwarding
|
336
|
|
337
|
# Must come last so overridden targets are not forwarded
|
338
|
|
339
|
define subdirTargets
|
340
|
$(subdir): _always
|
341
|
+$$(subMake)
|
342
|
|
343
|
$(subdir)%: _always
|
344
|
+$$(subMake)
|
345
|
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
|
346
|
|
347
|
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
|
348
|
endef
|
349
|
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
|
350
|
|
351
|
Makefile: ;
|
352
|
|
353
|
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
|
354
|
|
355
|
#### Maps validation
|
356
|
|
357
|
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
|
358
|
|
359
|
missing_mappings: _always missing_join_mappings missing_input_mappings ;
|
360
|
|
361
|
missing_%_mappings: _always # stem is one of join|input
|
362
|
@echo $(emph)"Missing $* mappings:"$(endEmph)
|
363
|
@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
|
364
|
$(if $(filter join,$*), -e '^make '|sort|uniq)
|