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 bin/install config/download core mysql inputs/download/live \
29
inputs/install 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
##### bin/install: live and test environments
38

    
39
bin/install: _always
40
	(mkdir -p ~/software/uutils/; cd ~/software/uutils/; \
41
svn co http://uutils.googlecode.com/svn/trunk/ .; make install)
42
	mkdir -p ~/bin
43
	-ln -s $(realpath bin/make) ~/bin
44
# ignore errors if file exists
45

    
46
##### Core: VegBIEN DB and dependencies
47

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

    
51
rm_core: _always rm_db ;
52

    
53
reinstall_core: _always rm_core core ;
54

    
55
##### Apache
56

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

    
62
apache-Darwin: _always ;
63

    
64
##### Python
65

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

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

    
102
##### PHP
103

    
104
php-Linux: _always
105
	-sudo apt-get --yes install php-http-request
106

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

    
120
##### PostgreSQL
121

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

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

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

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

    
141
linuxPostgresDir := /etc/postgresql/9.3/main
142
define pg_ctl-Linux
143
$(asAdmin) /etc/init.d/postgresql $(1)\
144
# if doesn'\''t work, run `rm $(linuxPostgresDir)/postmaster.pid` and retry
145
endef
146

    
147
macPostgresDir := /usr/local/var/postgres
148
define pg_ctl-Darwin
149
brew services $(1) postgresql\
150
# if doesn'\''t work, run `rm $(macPostgresDir)/postmaster.pid` and retry
151
endef
152
#pg_ctl -D $(macPostgresDir) $(1)
153

    
154
postgres_restart: postgres_restart-$(os) _always ;
155
postgres_restart-Linux: _always
156
	@# for some reason, pg_ctl does not work when run inside make
157
	@echo $(emph)'restart PostgreSQL manually:'$(endEmph)
158
	@echo '$(call pg_ctl-$(os),restart)'
159
	@$(wait)
160
postgres_restart-Darwin: _always
161
	@# for some reason, pg_ctl does not work when run inside make
162
	@echo $(emph)'restart PostgreSQL manually:'$(endEmph)
163
	@echo '$(call pg_ctl-$(os),restart)'
164
	@$(wait)
165

    
166
confirmPostgresReload = $(call confirm,Modifying postgresql.conf and pg_hba.conf)
167

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

    
181
endef
182

    
183
define postgresReload-Darwin
184
@$(confirmPostgresReload)
185
chmod a+r schemas/*.conf
186
-$(asAdmin) mv -n $(macPostgresDir)/postgresql.conf $(macPostgresDir)/postgresql.conf.old
187
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/postgresql.conf
188
-$(asAdmin) mv -n $(macPostgresDir)/pg_hba.conf $(macPostgresDir)/pg_hba.conf.old
189
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/pg_hba.conf
190
-$(asAdmin) mv -n $(macPostgresDir)/pg_ident.conf $(macPostgresDir)/pg_ident.conf.old
191
$(asAdmin) cp schemas/pg_ident.Mac.conf $(macPostgresDir)/pg_ident.conf
192
$(MAKE) postgres_restart-$(os)
193

    
194
endef
195

    
196
postgres_reload: _always
197
	$($(call forOs,postgresReload))
198

    
199
phppgadmin-Linux: _always
200
	-sudo apt-get --yes install phppgadmin
201
	$(editPhppgadmin)
202
	$(editPhppgadminApacheConf)
203
	-sudo ln -s ../conf.d/phppgadmin /etc/apache2/conf-available/phppgadmin.conf
204
	sudo a2enconf phppgadmin
205
	$(editApacheConfForPhppgadmin)
206
	-sudo apache2ctl restart
207

    
208
postgres-Linux: _always
209
	-sudo apt-get --yes install postgresql-9.3
210
	-sudo apt-get --yes install postgresql-contrib-$(pgVersion)
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-2.1-scripts
214
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis-2.1
215
	-$(MAKE) phppgadmin-Linux
216
	-sudo pip install psycopg2
217
	-$(MAKE) $(postgresReload-Linux)
218
# run pg_version inline because it needs postgresql to be installed first
219
# ignore errors if conf files already edited
220

    
221
postgres-Darwin: _always
222
	ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
223
	brew doctor
224
	brew install postgis # also installs PostgreSQL
225
	-$(postgresReload-Darwin)
226
	-sudo easy_install psycopg2
227
	-$(postgresReload-Darwin)
228

    
229
postgres-: _always ; # other OSes
230

    
231
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
232
bienPassword := $(shell cat config/bien_password)
233
bienReadPassword := $(shell cat config/bien_read_password)
234

    
235
##### Misc.
236

    
237
misc-Linux: _always
238
	cp -p "$(shell which dotlockfile)" bin/
239

    
240
misc-Darwin: _always ;
241

    
242
##### VegBIEN DB
243

    
244
#### DB and users
245

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

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

    
252
rmPublicSchema := $(call rmSchemaCmd,public)
253

    
254
db: mk_db rm_initial_public schemas/reinstall _always ;
255

    
256
mk_db: _always
257
	-echo "CREATE USER public_ PASSWORD 'password';"|$(psqlAsAdmin)
258
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
259
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
260
NOINHERIT;"|$(psqlAsAdmin)
261
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
262
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
263
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
264
	-echo "CREATE EXTENSION plpython3u;"|$(psqlAsAdminVegbien)
265
	-echo 'CREATE SCHEMA postgis;'|$(psqlAsAdminVegbien)
266
	-echo 'GRANT USAGE ON SCHEMA postgis TO public;'|$(psqlAsAdminVegbien)
267
	-echo 'CREATE EXTENSION postgis SCHEMA postgis;'\
268
|$(psqlAsAdminVegbien)
269
	# unset ON_ERROR_STOP so user-exists errors don't prevent further user-adds
270
	-cat config/users.sql|$(psqlAsAdmin) --set ON_ERROR_STOP
271
# ignore errors if user/database/etc. exists
272

    
273
rm_initial_public: _always
274
	@$(confirmRmPublicSchema)
275
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
276
# drop public schema *as admin* because it starts out owned by postgres
277

    
278
rm_db: _always
279
	@$(confirmRmDb)
280
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
281
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
282
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
283
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
284

    
285
reinstall_db: _always rm_db db ;
286

    
287
##### MySQL
288

    
289
mysql: _always $(call forOs,mysql) mysql_users
290
	@$(done)
291

    
292
rm_mysql: _always rm_mysql_users ;
293

    
294
mysql-Linux: _always
295
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
296
	@$(wait)
297
	-sudo apt-get --yes install mysql-server
298
	-sudo apt-get --yes install mysql-client
299
	-sudo apt-get --yes install mysql-workbench
300
	-sudo apt-get --yes install python-mysqldb
301
	-sudo apt-get --yes install phpmyadmin
302

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

    
320
mysql-: _always # other OSes
321

    
322
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
323

    
324
mysql_users: _always
325
	-echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY '$(bienPassword)';"\
326
|$(mysqlAsRoot)
327
	-echo "CREATE USER 'bien_read'@'%' IDENTIFIED BY '$(bienReadPassword)';"\
328
|$(mysqlAsRoot)
329
# ignore errors if user exists
330

    
331
rm_mysql_users: _always
332
	-echo "DROP USER 'bien_read'@'localhost';"|$(mysqlAsRoot)
333
	-echo "DROP USER 'bien'@'%';"|$(mysqlAsRoot)
334
# ignore errors if user exists
335

    
336
##### Datasources
337

    
338
inputs: _always inputs/all ;
339

    
340
import: _always import-msg inputs/import_scrub ;
341
import-msg: _always
342
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
343
	@echo
344

    
345
scrub: _always inputs/.TNRS/tnrs/tnrs-remake ;
346

    
347
verify: _always inputs/verify
348
	@$(done)
349

    
350
test: _always inputs/test
351
	@$(done)
352

    
353
##### Testing
354

    
355
test-all: _always remake test missing_mappings
356
	@$(done)
357

    
358
##### Subdir forwarding
359

    
360
# Must come last so overridden targets are not forwarded
361

    
362
define subdirTargets
363
$(subdir): _always
364
	+$$(subMake)
365

    
366
$(subdir)%: _always
367
	+$$(subMake)
368
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
369

    
370
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
371
endef
372
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
373

    
374
Makefile: ;
375

    
376
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
377

    
378
#### Overrides of forwarded commands
379

    
380
# commented out to avoid a cascade of "overriding commands for target" warnings
381
#inputs/reinstall: _always
382
#	echo '. bin/reinstall_all; wait'|bash -s
383

    
384
#### Maps validation
385

    
386
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
387

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