Project

General

Profile

1
root := .
2
include lib/common.Makefile
3

    
4

    
5
##### Vars/functions
6

    
7
# Make
8
SHELL := /bin/bash
9
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
10

    
11
# Paths
12
bin := bin
13

    
14
# Terminal
15
esc := '['
16
reset := $(esc)'0m'
17
emph := $(esc)'7m '
18
endEmph := ' '$(reset)
19

    
20
# User interaction
21

    
22
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
23

    
24
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
25

    
26
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
27
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
28
$(error Aborting))
29

    
30
# File editing
31
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
32

    
33
# DB
34
mkSchemaCmd = 'CREATE SCHEMA $(1);'
35
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
36

    
37
##### Environment
38

    
39
export PGOPTIONS = --client-min-messages=WARNING
40

    
41
##### General targets
42

    
43
remake: _always clean
44
	$(MAKE)
45
# re-run make so that cache of existing files is reset
46

    
47
%/remake: _always
48
	$(MAKE) $(@D)/clean
49
	$(MAKE) $(@D)/
50
# re-run make so that cache of existing files is reset
51

    
52
%-remake: _always
53
	rm -f $*
54
	$(MAKE) $*
55
# re-run make so that cache of existing files is reset
56

    
57
%/reinstall: _always %/uninstall %/install ;
58

    
59
##### Installation
60

    
61
# public must be installed *after* inputs because some views depend on inputs
62
install: _always core mysql inputs/download inputs/install schemas/install test
63
	@$(done)
64

    
65
uninstall: _always rm_mysql rm_core ;
66

    
67
reinstall: _always uninstall install ;
68

    
69
##### Core: VegBIEN DB and dependencies
70

    
71
core: _always $(call forOs,python php postgres) db
72
	@$(done)
73

    
74
rm_core: _always rm_db ;
75

    
76
reinstall_core: _always rm_core core ;
77

    
78
##### Python
79

    
80
python-Linux: _always
81
	-sudo apt-get --yes install python
82
	-sudo apt-get --yes install python3
83
	-sudo apt-get --yes install python-dev
84
	-sudo apt-get --yes install python-dateutil
85
	-sudo apt-get --yes install python-parallel
86
	-sudo apt-get --yes install python-profiler
87
	-sudo apt-get --yes install python-pip
88
	-sudo apt-get --yes install pymetrics
89
	-sudo pip install ordereddict
90
	-sudo pip install pp
91

    
92
python-Darwin: _always
93
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
94
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
95
	@echo 'Open the disk image'
96
	@echo 'Run the installer in it'
97
	@$(wait)
98
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
99
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
100
	@echo 'Extract the archive'
101
	@echo 'Change to the directory of the extracted files'
102
	@echo 'Run python setup.py build'
103
	@echo 'Run sudo python setup.py install'
104
	@echo "Run python -c 'import dateutil'"
105
	@$(wait)
106
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
107
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
108
	@$(wait)
109

    
110
##### PHP
111

    
112
php-Linux: _always
113
	-sudo apt-get --yes install php-http-request
114

    
115
php-Darwin: _always
116
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
117
	@echo 'Download http://pear.php.net/go-pear.phar'
118
	@echo 'Change to the directory of the downloaded file'
119
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
120
	@echo 'Whenever prompted, press Enter to select default options'
121
	@echo 'Add $$HOME/pear/bin to your $$PATH'
122
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
123
	@echo 'If needed, set $$PHPRC to your php.ini'
124
	@echo "Run pear install HTTP_Request"
125
	@echo 'To run a php command: php -c <php.ini-path> ...'
126
	@$(wait)
127

    
128
##### PostgreSQL
129

    
130
define editPhppgadmin
131
-sudo patch /etc/phppgadmin/config.inc.php lib/phpPgAdmin.config.inc.php.diff
132
sudo cp -n /usr/share/phppgadmin/login.php /usr/share/phppgadmin/login.php.old
133
-sudo patch /usr/share/phppgadmin/login.php lib/phpPgAdmin.login.php.diff
134
endef
135
# ignore errors if patch has already been applied
136

    
137
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
138
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
139

    
140
apacheConf := /etc/apache2/apache2.conf
141
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
142
editApacheConfForPhppgadmin =\
143
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
144
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
145

    
146
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
147
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
148
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
149

    
150
asAdmin := sudo -E -u postgres
151
asBien := sudo -E -u bien
152
pgVersion = "$$(env minor=1 bin/pg_version)"
153

    
154
linuxPostgresDir := /etc/postgresql/9.1/main
155
define pg_ctl-Linux
156
$(asAdmin) /etc/init.d/postgresql $(1)
157
sleep 2
158
endef
159

    
160
macPostgresDir := /Library/PostgreSQL/9.1
161
define macUsePostgresLib
162
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
163
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
164

    
165
endef
166
libDest = /usr/lib/$(1)
167

    
168
macPostgresLibs := libcrypto libssl
169
macPostgresLibs := $(macPostgresLibs) $(macPostgresLibs:%=%.1.0.0)
170
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
171

    
172
define pg_ctl-Darwin
173
$(asAdmin) pg_ctl -D $(macPostgresDir)/data -l $(macPostgresDir)/postgres.log $(1)
174
sleep 2
175
endef
176

    
177
confirmPostgresReload = $(call confirm,Modifying postgresql.conf and pg_hba.conf)
178

    
179
define postgresReload-Linux
180
@$(confirmPostgresReload)
181
@echo $(emph)'kernel.shmmax must be set to at least 4GB minus 1 byte:'$(endEmph)
182
@echo 'Append the following to /etc/sysctl.conf:'
183
@echo 'kernel.shmmax=4294967295'
184
@$(wait)
185
chmod a+r schemas/*.conf || $(asBien) chmod a+r schemas/*.conf
186
-$(asAdmin) mv -n $(linuxPostgresDir)/postgresql.conf $(linuxPostgresDir)/postgresql.conf.old
187
$(asAdmin) cp --preserve=timestamps schemas/postgresql.conf $(linuxPostgresDir)/postgresql.conf
188
-$(asAdmin) mv -n $(linuxPostgresDir)/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf.old
189
$(asAdmin) cp --preserve=timestamps schemas/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf
190
$(call pg_ctl-Linux,restart)
191

    
192
endef
193

    
194
define postgresReload-Darwin
195
@$(confirmPostgresReload)
196
chmod a+r schemas/*.conf
197
-$(asAdmin) mv -n $(macPostgresDir)/data/postgresql.conf $(macPostgresDir)/data/postgresql.conf.old
198
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
199
-$(asAdmin) mv -n $(macPostgresDir)/data/pg_hba.conf $(macPostgresDir)/data/pg_hba.conf.old
200
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/data/pg_hba.conf
201
$(call pg_ctl-Darwin,restart)
202

    
203
endef
204

    
205
postgres_reload: _always
206
	$($(call forOs,postgresReload))
207

    
208
postgres-Linux: _always
209
	-sudo apt-get --yes install postgresql
210
	-sudo apt-get --yes install postgresql-contrib
211
	-sudo apt-get --yes install postgresql-plpython3-$(pgVersion)
212
	-sudo apt-get --yes install libpq-dev
213
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis
214
	-sudo apt-get --yes install phppgadmin
215
	$(editPhppgadmin)
216
	$(editPhppgadminApacheConf)
217
	$(editApacheConfForPhppgadmin)
218
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
219
	-sudo apache2ctl restart
220
	-sudo pip install psycopg2
221
	-$(MAKE) $(postgresReload-Linux)
222
# run pg_version inline because it needs postgresql to be installed first
223
# ignore errors if conf files already edited
224

    
225
postgres-Darwin: _always
226
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
227
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
228
	@echo 'Open the disk image'
229
	@echo 'Run the installer in it'
230
	@$(wait)
231
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
232
	-$(MAKE) $(postgresReload-Darwin)
233

    
234
postgres-: _always ; # other OSes
235

    
236
psqlOpts := --set ON_ERROR_STOP=1 --quiet
237
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
238
    # -E preserves env vars so PGOPTIONS is passed to psql
239
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
240
psqlAsBien := bin/psql_vegbien $(psqlOpts)
241
bienPassword := $(shell cat config/bien_password)
242
bienReadPassword := $(shell cat config/bien_read_password)
243

    
244
##### VegBIEN DB
245

    
246
#### DB and users
247

    
248
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
249
DB!,This includes all archived imports and staging tables.)
250

    
251
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
252
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
253

    
254
rmPublicSchema := $(call rmSchemaCmd,public)
255

    
256
db: mk_db rm_initial_public _always ;
257

    
258
mk_db: _always
259
	-echo "CREATE USER public_;"|$(psqlAsAdmin)
260
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
261
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
262
NOINHERIT;"|$(psqlAsAdmin)
263
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
264
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
265
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
266
	-$(asAdmin) createlang plpython3u vegbien
267
# ignore errors if user/database/etc. exists
268

    
269
rm_initial_public: _always
270
	@$(confirmRmPublicSchema)
271
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
272
# drop public schema *as admin* because it starts out owned by postgres
273

    
274
rm_db: _always
275
	@$(confirmRmDb)
276
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
277
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
278
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
279
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
280

    
281
reinstall_db: _always rm_db db ;
282

    
283
#### Schemas
284

    
285
schemas := temp functions py_functions public
286
schemas/install: $(schemas:%=schemas/%/install) ;
287

    
288
schemasReversed := public py_functions functions temp
289
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
290

    
291
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
292

    
293
### public
294

    
295
schemas/public/install: schemas/vegbien.sql _always
296
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
297
	<$< $(psqlNoSearchPath)
298
# ignore errors if schema exists
299
# public schema will be owned by bien
300
# don't include public in the search_path
301

    
302
schemas/public/uninstall: _always
303
	@$(confirmRmPublicSchema)
304
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
305

    
306
schemas/rename/%: _always
307
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
308
	$(MAKE) schemas/public/install
309

    
310
schemas/rotate: _always schemas/rename/public.$(version) ;
311

    
312
### py_functions
313

    
314
schemas/py_functions/install: schemas/py_functions.sql _always
315
	-<$< env public= $(psqlAsAdminVegbien)
316
# ignore errors if schema exists
317

    
318
### Others
319

    
320
schemas/%/install: schemas/%.sql _always
321
	-<$< $(psqlNoSearchPath)
322
# ignore errors if schema exists
323

    
324
schemas/%/uninstall: _always
325
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
326

    
327
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
328
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
329

    
330
##### MySQL
331

    
332
mysql: _always $(call forOs,mysql) mysql_user
333
	@$(done)
334

    
335
rm_mysql: _always rm_mysql_user ;
336

    
337
mysql-Linux: _always
338
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
339
	@$(wait)
340
	-sudo apt-get --yes install mysql-server mysql-client python-mysqldb
341

    
342
mysql-Darwin: _always
343
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
344
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
345
	@echo 'Extract the archive'
346
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
347
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
348
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
349
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
350
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
351
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
352
	@echo 'Change to the directory of the extracted files'
353
	@echo 'Run python setup.py clean'
354
	@echo 'Run python setup.py build'
355
	@echo 'Run sudo python setup.py install'
356
	@echo "Run python -c 'import MySQLdb'"
357
	@$(wait)
358

    
359
mysql-: _always # other OSes
360

    
361
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
362

    
363
mysql_user: _always
364
	-$($@_cmd)
365
# ignore errors if user exists
366
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
367
'$(bienPassword)';"|$(mysqlAsRoot)
368

    
369
rm_mysql_user: _always
370
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
371
# ignore errors if user exists
372

    
373
##### Datasources
374

    
375
inputs: _always inputs/all ;
376

    
377
inputs/install: _always
378
	echo '. bin/reinstall_all; wait'|bash -s
379

    
380
import: _always import-msg inputs/import ;
381
import-msg: _always
382
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
383
	@echo
384

    
385
verify: _always inputs/verify
386
	@$(done)
387

    
388
test: _always inputs/test
389
	@$(done)
390

    
391
##### Testing
392

    
393
test-all: _always remake test missing_mappings
394
	@$(done)
395

    
396
##### Subdir forwarding
397

    
398
# Must come last so overridden targets are not forwarded
399

    
400
define subdirTargets
401
$(subdir): _always
402
	+$$(subMake)
403

    
404
$(subdir)%: _always
405
	+$$(subMake)
406
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
407

    
408
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
409
endef
410
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
411

    
412
Makefile: ;
413

    
414
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
415

    
416
#### Maps validation
417

    
418
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
419

    
420
include lib/mappings.Makefile
(1-1/5)