Project

General

Profile

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

    
4

    
5
##### Vars/functions
6

    
7
# Make
8
SHELL := /bin/bash
9

    
10
# Paths
11
bin := bin
12

    
13
##### Environment
14

    
15
export PGOPTIONS = --client-min-messages=WARNING
16

    
17
##### General targets
18

    
19
remake: _always clean
20
	$(MAKE)
21
# re-run make so that cache of existing files is reset
22

    
23
##### Installation
24

    
25
# public must be installed *after* inputs because some views depend on inputs
26
# schemas/public/install also tests that a clean public schema will be
27
# installable by full-database import
28
install: _always config core mysql inputs/download/live inputs/install \
29
schemas/public/reinstall
30
	@$(done)
31
# schemas/public/install would errexit if the public schema already exists
32

    
33
uninstall: _always rm_mysql rm_core ;
34

    
35
reinstall: _always uninstall install ;
36

    
37
##### config: live and test environments
38

    
39
config: _always
40
	mkdir -p ~/bin
41
	-ln -s $(realpath bin/make) ~/bin
42
# ignore errors if file exists
43

    
44
##### Core: VegBIEN DB and dependencies
45

    
46
core: _always $(call forOs,apache python php postgres misc) db
47
	@$(done)
48

    
49
rm_core: _always rm_db ;
50

    
51
reinstall_core: _always rm_core core ;
52

    
53
##### Apache
54

    
55
apache-Linux: _always
56
	# Apache 2.4, which comes with Ubuntu 14.04
57
	-sudo apt-get --yes install apache2
58
	#-sudo apt-get --yes install libapache2-mod-php5
59

    
60
apache-Darwin: _always ;
61

    
62
##### Python
63

    
64
python-Linux: _always
65
	-sudo apt-get --yes install python
66
	-sudo apt-get --yes install python3
67
	-sudo apt-get --yes install python-dev
68
	-sudo apt-get --yes install python-dateutil
69
	-sudo apt-get --yes install python-parallel
70
	-sudo apt-get --yes install python-profiler
71
	-sudo apt-get --yes install python-pip
72
	-sudo apt-get --yes install pymetrics
73
	-sudo pip install pp
74

    
75
python-Darwin: _always
76
	@# Python 2 comes preinstalled
77
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
78
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
79
	@echo 'Open the disk image'
80
	@echo 'Run the installer in it'
81
	@$(wait)
82
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
83
	@echo 'Download it for Python 2: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
84
	@echo 'Extract the archive'
85
	@echo 'Change to the directory of the extracted files'
86
	@echo 'Run python setup.py build'
87
	@echo 'Run sudo python setup.py install'
88
	@echo "Run python -c 'import dateutil'"
89
	@echo 'Download it for Python 3: http://labix.org/download/python-dateutil/python-dateutil-2.0.tar.gz'
90
	@echo 'Extract the archive'
91
	@echo 'Change to the directory of the extracted files'
92
	@echo 'Run python3 setup.py build'
93
	@echo 'Run sudo python3 setup.py install'
94
	@echo "Run python3 -c 'import dateutil'"
95
	@$(wait)
96
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
97
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
98
	@$(wait)
99

    
100
##### PHP
101

    
102
php-Linux: _always
103
	-sudo apt-get --yes install php-http-request
104

    
105
php-Darwin: _always
106
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
107
	@echo 'Download http://pear.php.net/go-pear.phar'
108
	@echo 'Change to the directory of the downloaded file'
109
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
110
	@echo 'Whenever prompted, press Enter to select default options'
111
	@echo 'Add $$HOME/pear/bin to your $$PATH'
112
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
113
	@echo 'If needed, set $$PHPRC to your php.ini'
114
	@echo "Run pear install HTTP_Request"
115
	@echo 'To run a php command: php -c <php.ini-path> ...'
116
	@$(wait)
117

    
118
##### PostgreSQL
119

    
120
define editPhppgadmin
121
-sudo patch /etc/phppgadmin/config.inc.php lib/phpPgAdmin.config.inc.php.diff
122
sudo cp -n /usr/share/phppgadmin/login.php /usr/share/phppgadmin/login.php.old
123
-sudo patch /usr/share/phppgadmin/login.php lib/phpPgAdmin.login.php.diff
124
endef
125
# ignore errors if patch has already been applied
126

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

    
130
apacheConf := /etc/apache2/apache2.conf
131
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
132
editApacheConfForPhppgadmin =\
133
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
134
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
135

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

    
140
asBien := sudo -E -u bien
141
pgVersion = "$$(env minor=1 bin/pg_version)"
142

    
143
linuxPostgresDir := /etc/postgresql/9.3/main
144
define pg_ctl-Linux
145
$(asAdmin) /etc/init.d/postgresql $(1)
146
endef
147

    
148
macPostgresDir := /Library/PostgreSQL/9.3
149
define macUsePostgresLib
150
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
151
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
152

    
153
endef
154
libDest = /usr/lib/$(1)
155

    
156
macPostgresLibs := libcrypto libcrypto.1.0.0 libssl libssl.1.0.0 libpq.5
157
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
158

    
159
define pg_ctl-Darwin
160
(cd /; $(asAdmin) pg_ctl -D $(macPostgresDir)/data -l $(macPostgresDir)/data/postgres.log $(1))
161
	# cd: due to pg_ctl bug, current directory must be accessible by it
162
sleep 2
163
endef
164

    
165
postgres_restart: postgres_restart-$(os) _always ;
166
postgres_restart-Linux: _always
167
	@# for some reason, pg_ctl does not work when run inside make
168
	@echo $(emph)'restart PostgreSQL manually:'$(endEmph)
169
	@echo $(call pg_ctl-$(os),restart)
170
	@$(wait)
171
postgres_restart-Darwin: _always
172
	$(call pg_ctl-$(os),restart)
173

    
174
confirmPostgresReload = $(call confirm,Modifying postgresql.conf and pg_hba.conf)
175

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

    
189
endef
190

    
191
define postgresReload-Darwin
192
@$(confirmPostgresReload)
193
chmod a+r schemas/*.conf
194
-$(asAdmin) mv -n $(macPostgresDir)/data/postgresql.conf $(macPostgresDir)/data/postgresql.conf.old
195
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
196
-$(asAdmin) mv -n $(macPostgresDir)/data/pg_hba.conf $(macPostgresDir)/data/pg_hba.conf.old
197
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/data/pg_hba.conf
198
-$(asAdmin) mv -n $(macPostgresDir)/data/pg_ident.conf $(macPostgresDir)/data/pg_ident.conf.old
199
$(asAdmin) cp schemas/pg_ident.Mac.conf $(macPostgresDir)/data/pg_ident.conf
200
$(MAKE) postgres_restart-$(os)
201

    
202
endef
203

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

    
207
phppgadmin-Linux: _always
208
	-sudo apt-get --yes install phppgadmin
209
	$(editPhppgadmin)
210
	$(editPhppgadminApacheConf)
211
	-sudo ln -s ../conf.d/phppgadmin /etc/apache2/conf-available/phppgadmin.conf
212
	sudo a2enconf phppgadmin
213
	$(editApacheConfForPhppgadmin)
214
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
215
	-sudo apache2ctl restart
216

    
217
postgres-Linux: _always
218
	-sudo apt-get --yes install postgresql-9.3
219
	-sudo apt-get --yes install postgresql-contrib-$(pgVersion)
220
	-sudo apt-get --yes install postgresql-plpython3-$(pgVersion)
221
	-sudo apt-get --yes install libpq-dev
222
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis-2.1-scripts
223
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis-2.1
224
	-$(MAKE) phppgadmin-Linux
225
	-sudo pip install psycopg2
226
	-$(MAKE) $(postgresReload-Linux)
227
# run pg_version inline because it needs postgresql to be installed first
228
# ignore errors if conf files already edited
229

    
230
postgres-Darwin: _always
231
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
232
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
233
	@echo 'Open the disk image'
234
	@echo 'Run the installer in it'
235
	@$(wait)
236
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
237
	-sudo easy_install psycopg2
238
	-$(postgresReload-Darwin)
239

    
240
postgres-: _always ; # other OSes
241

    
242
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
243
bienPassword := $(shell cat config/bien_password)
244
bienReadPassword := $(shell cat config/bien_read_password)
245

    
246
##### Misc.
247

    
248
misc-Linux: _always
249
	cp -p "$(shell which dotlockfile)" bin/
250

    
251
misc-Darwin: _always ;
252

    
253
##### VegBIEN DB
254

    
255
#### DB and users
256

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

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

    
263
rmPublicSchema := $(call rmSchemaCmd,public)
264

    
265
db: mk_db rm_initial_public schemas/install _always ;
266

    
267
mk_db: _always
268
	-echo "CREATE USER public_;"|$(psqlAsAdmin)
269
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
270
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
271
NOINHERIT;"|$(psqlAsAdmin)
272
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
273
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
274
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
275
	-$(asAdmin) createlang -U postgres plpython3u vegbien
276
	-echo 'CREATE SCHEMA postgis;'|$(psqlAsAdminVegbien)
277
	-echo 'CREATE EXTENSION postgis SCHEMA postgis;'\
278
|$(psqlAsAdminVegbien)
279
	# unset ON_ERROR_STOP so user-exists errors don't prevent further user-adds
280
	-cat config/users.sql|$(psqlAsAdmin) --set ON_ERROR_STOP
281
# ignore errors if user/database/etc. exists
282

    
283
rm_initial_public: _always
284
	@$(confirmRmPublicSchema)
285
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
286
# drop public schema *as admin* because it starts out owned by postgres
287

    
288
rm_db: _always
289
	@$(confirmRmDb)
290
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
291
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
292
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
293
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
294

    
295
reinstall_db: _always rm_db db ;
296

    
297
##### MySQL
298

    
299
mysql: _always $(call forOs,mysql) mysql_users
300
	@$(done)
301

    
302
rm_mysql: _always rm_mysql_users ;
303

    
304
mysql-Linux: _always
305
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
306
	@$(wait)
307
	-sudo apt-get --yes install mysql-server
308
	-sudo apt-get --yes install mysql-client
309
	-sudo apt-get --yes install mysql-workbench
310
	-sudo apt-get --yes install python-mysqldb
311
	-sudo apt-get --yes install phpmyadmin
312

    
313
mysql-Darwin: _always
314
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
315
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
316
	@echo 'Extract the archive'
317
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
318
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
319
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
320
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
321
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
322
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
323
	@echo 'Change to the directory of the extracted files'
324
	@echo 'Run python setup.py clean'
325
	@echo 'Run python setup.py build'
326
	@echo 'Run sudo python setup.py install'
327
	@echo "Run python -c 'import MySQLdb'"
328
	@$(wait)
329

    
330
mysql-: _always # other OSes
331

    
332
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
333

    
334
mysql_users: _always
335
	-echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY '$(bienPassword)';"\
336
|$(mysqlAsRoot)
337
	-echo "CREATE USER 'bien_read'@'%' IDENTIFIED BY '$(bienReadPassword)';"\
338
|$(mysqlAsRoot)
339
# ignore errors if user exists
340

    
341
rm_mysql_users: _always
342
	-echo "DROP USER 'bien_read'@'localhost';"|$(mysqlAsRoot)
343
	-echo "DROP USER 'bien'@'%';"|$(mysqlAsRoot)
344
# ignore errors if user exists
345

    
346
##### Datasources
347

    
348
inputs: _always inputs/all ;
349

    
350
import: _always import-msg inputs/import_scrub ;
351
import-msg: _always
352
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
353
	@echo
354

    
355
scrub: _always inputs/.TNRS/tnrs/tnrs-remake ;
356

    
357
verify: _always inputs/verify
358
	@$(done)
359

    
360
test: _always inputs/test
361
	@$(done)
362

    
363
##### Testing
364

    
365
test-all: _always remake test missing_mappings
366
	@$(done)
367

    
368
##### Subdir forwarding
369

    
370
# Must come last so overridden targets are not forwarded
371

    
372
define subdirTargets
373
$(subdir): _always
374
	+$$(subMake)
375

    
376
$(subdir)%: _always
377
	+$$(subMake)
378
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
379

    
380
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
381
endef
382
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
383

    
384
Makefile: ;
385

    
386
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
387

    
388
#### Overrides of forwarded commands
389

    
390
# commented out to avoid a cascade of "overriding commands for target" warnings
391
#inputs/reinstall: _always
392
#	echo '. bin/reinstall_all; wait'|bash -s
393

    
394
#### Maps validation
395

    
396
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
397

    
398
include lib/mappings.Makefile
(5-5/11)