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/install
30
	@$(done)
31

    
32
uninstall: _always rm_mysql rm_core ;
33

    
34
reinstall: _always uninstall install ;
35

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

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

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

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

    
48
rm_core: _always rm_db ;
49

    
50
reinstall_core: _always rm_core core ;
51

    
52
##### Apache
53

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

    
59
apache-Darwin: _always ;
60

    
61
##### Python
62

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

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

    
99
##### PHP
100

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

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

    
117
##### PostgreSQL
118

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

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

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

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

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

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

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

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

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

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

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

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

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

    
188
endef
189

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

    
201
endef
202

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

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

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

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

    
239
postgres-: _always ; # other OSes
240

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

    
245
##### Misc.
246

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

    
250
misc-Darwin: _always ;
251

    
252
##### VegBIEN DB
253

    
254
#### DB and users
255

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

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

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

    
264
db: mk_db rm_initial_public schemas/install _always ;
265

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

    
279
rm_initial_public: _always
280
	@$(confirmRmPublicSchema)
281
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
282
# drop public schema *as admin* because it starts out owned by postgres
283

    
284
rm_db: _always
285
	@$(confirmRmDb)
286
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
287
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
288
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
289
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
290

    
291
reinstall_db: _always rm_db db ;
292

    
293
##### MySQL
294

    
295
mysql: _always $(call forOs,mysql) mysql_users
296
	@$(done)
297

    
298
rm_mysql: _always rm_mysql_users ;
299

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

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

    
326
mysql-: _always # other OSes
327

    
328
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
329

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

    
337
rm_mysql_users: _always
338
	-echo "DROP USER 'bien_read'@'localhost';"|$(mysqlAsRoot)
339
	-echo "DROP USER 'bien'@'%';"|$(mysqlAsRoot)
340
# ignore errors if user exists
341

    
342
##### Datasources
343

    
344
inputs: _always inputs/all ;
345

    
346
import: _always import-msg inputs/import_scrub ;
347
import-msg: _always
348
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
349
	@echo
350

    
351
scrub: _always inputs/.TNRS/tnrs/tnrs-remake ;
352

    
353
verify: _always inputs/verify
354
	@$(done)
355

    
356
test: _always inputs/test
357
	@$(done)
358

    
359
##### Testing
360

    
361
test-all: _always remake test missing_mappings
362
	@$(done)
363

    
364
##### Subdir forwarding
365

    
366
# Must come last so overridden targets are not forwarded
367

    
368
define subdirTargets
369
$(subdir): _always
370
	+$$(subMake)
371

    
372
$(subdir)%: _always
373
	+$$(subMake)
374
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
375

    
376
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
377
endef
378
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
379

    
380
Makefile: ;
381

    
382
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
383

    
384
#### Overrides of forwarded commands
385

    
386
# commented out to avoid a cascade of "overriding commands for target" warnings
387
#inputs/reinstall: _always
388
#	echo '. bin/reinstall_all; wait'|bash -s
389

    
390
#### Maps validation
391

    
392
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
393

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