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
%/remake: _always
24
	$(MAKE) $(@D)/clean
25
	$(MAKE) $(@D)/
26
# re-run make so that cache of existing files is reset
27

    
28
%-remake: _always
29
	rm -f $*
30
	$(MAKE) $*
31
# re-run make so that cache of existing files is reset
32

    
33
%/reinstall: _always %/uninstall %/install ;
34

    
35
##### Installation
36

    
37
# public must be installed *after* inputs because some views depend on inputs
38
install: _always core mysql inputs/download inputs/install schemas/install test
39
	@$(done)
40

    
41
uninstall: _always rm_mysql rm_core ;
42

    
43
reinstall: _always uninstall install ;
44

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

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

    
50
rm_core: _always rm_db ;
51

    
52
reinstall_core: _always rm_core core ;
53

    
54
##### Python
55

    
56
python-Linux: _always
57
	-sudo apt-get --yes install python
58
	-sudo apt-get --yes install python3
59
	-sudo apt-get --yes install python-dev
60
	-sudo apt-get --yes install python-dateutil
61
	-sudo apt-get --yes install python-parallel
62
	-sudo apt-get --yes install python-profiler
63
	-sudo apt-get --yes install python-pip
64
	-sudo apt-get --yes install pymetrics
65
	-sudo pip install ordereddict
66
	-sudo pip install pp
67

    
68
python-Darwin: _always
69
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
70
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
71
	@echo 'Open the disk image'
72
	@echo 'Run the installer in it'
73
	@$(wait)
74
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
75
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
76
	@echo 'Extract the archive'
77
	@echo 'Change to the directory of the extracted files'
78
	@echo 'Run python setup.py build'
79
	@echo 'Run sudo python setup.py install'
80
	@echo "Run python -c 'import dateutil'"
81
	@$(wait)
82
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
83
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
84
	@$(wait)
85

    
86
##### PHP
87

    
88
php-Linux: _always
89
	-sudo apt-get --yes install php-http-request
90

    
91
php-Darwin: _always
92
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
93
	@echo 'Download http://pear.php.net/go-pear.phar'
94
	@echo 'Change to the directory of the downloaded file'
95
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
96
	@echo 'Whenever prompted, press Enter to select default options'
97
	@echo 'Add $$HOME/pear/bin to your $$PATH'
98
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
99
	@echo 'If needed, set $$PHPRC to your php.ini'
100
	@echo "Run pear install HTTP_Request"
101
	@echo 'To run a php command: php -c <php.ini-path> ...'
102
	@$(wait)
103

    
104
##### PostgreSQL
105

    
106
define editPhppgadmin
107
-sudo patch /etc/phppgadmin/config.inc.php lib/phpPgAdmin.config.inc.php.diff
108
sudo cp -n /usr/share/phppgadmin/login.php /usr/share/phppgadmin/login.php.old
109
-sudo patch /usr/share/phppgadmin/login.php lib/phpPgAdmin.login.php.diff
110
endef
111
# ignore errors if patch has already been applied
112

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

    
116
apacheConf := /etc/apache2/apache2.conf
117
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
118
editApacheConfForPhppgadmin =\
119
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
120
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
121

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

    
126
asAdmin := sudo -E -u postgres
127
asBien := sudo -E -u bien
128
pgVersion = "$$(env minor=1 bin/pg_version)"
129

    
130
linuxPostgresDir := /etc/postgresql/9.1/main
131
define pg_ctl-Linux
132
$(asAdmin) /etc/init.d/postgresql $(1)
133
sleep 2
134
endef
135

    
136
macPostgresDir := /Library/PostgreSQL/9.1
137
define macUsePostgresLib
138
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
139
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
140

    
141
endef
142
libDest = /usr/lib/$(1)
143

    
144
macPostgresLibs := libcrypto libssl
145
macPostgresLibs := $(macPostgresLibs) $(macPostgresLibs:%=%.1.0.0)
146
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
147

    
148
define pg_ctl-Darwin
149
$(asAdmin) pg_ctl -D $(macPostgresDir)/data -l $(macPostgresDir)/postgres.log $(1)
150
sleep 2
151
endef
152

    
153
confirmPostgresReload = $(call confirm,Modifying postgresql.conf and pg_hba.conf)
154

    
155
define postgresReload-Linux
156
@$(confirmPostgresReload)
157
@echo $(emph)'kernel.shmmax must be set to at least 4GB minus 1 byte:'$(endEmph)
158
@echo 'Append the following to /etc/sysctl.conf:'
159
@echo 'kernel.shmmax=4294967295'
160
@$(wait)
161
chmod a+r schemas/*.conf || $(asBien) chmod a+r schemas/*.conf
162
-$(asAdmin) mv -n $(linuxPostgresDir)/postgresql.conf $(linuxPostgresDir)/postgresql.conf.old
163
$(asAdmin) cp --preserve=timestamps schemas/postgresql.conf $(linuxPostgresDir)/postgresql.conf
164
-$(asAdmin) mv -n $(linuxPostgresDir)/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf.old
165
$(asAdmin) cp --preserve=timestamps schemas/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf
166
$(call pg_ctl-Linux,restart)
167

    
168
endef
169

    
170
define postgresReload-Darwin
171
@$(confirmPostgresReload)
172
chmod a+r schemas/*.conf
173
-$(asAdmin) mv -n $(macPostgresDir)/data/postgresql.conf $(macPostgresDir)/data/postgresql.conf.old
174
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
175
-$(asAdmin) mv -n $(macPostgresDir)/data/pg_hba.conf $(macPostgresDir)/data/pg_hba.conf.old
176
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/data/pg_hba.conf
177
$(call pg_ctl-Darwin,restart)
178

    
179
endef
180

    
181
postgres_reload: _always
182
	$($(call forOs,postgresReload))
183

    
184
postgres-Linux: _always
185
	-sudo apt-get --yes install postgresql
186
	-sudo apt-get --yes install postgresql-contrib
187
	-sudo apt-get --yes install postgresql-plpython3-$(pgVersion)
188
	-sudo apt-get --yes install libpq-dev
189
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis
190
	-sudo apt-get --yes install phppgadmin
191
	$(editPhppgadmin)
192
	$(editPhppgadminApacheConf)
193
	$(editApacheConfForPhppgadmin)
194
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
195
	-sudo apache2ctl restart
196
	-sudo pip install psycopg2
197
	-$(MAKE) $(postgresReload-Linux)
198
# run pg_version inline because it needs postgresql to be installed first
199
# ignore errors if conf files already edited
200

    
201
postgres-Darwin: _always
202
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
203
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
204
	@echo 'Open the disk image'
205
	@echo 'Run the installer in it'
206
	@$(wait)
207
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
208
	-$(MAKE) $(postgresReload-Darwin)
209

    
210
postgres-: _always ; # other OSes
211

    
212
psqlOpts := --set ON_ERROR_STOP=1 --quiet
213
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
214
    # -E preserves env vars so PGOPTIONS is passed to psql
215
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
216
bienPassword := $(shell cat config/bien_password)
217
bienReadPassword := $(shell cat config/bien_read_password)
218

    
219
##### VegBIEN DB
220

    
221
#### DB and users
222

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

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

    
229
rmPublicSchema := $(call rmSchemaCmd,public)
230

    
231
db: mk_db rm_initial_public _always ;
232

    
233
mk_db: _always
234
	-echo "CREATE USER public_;"|$(psqlAsAdmin)
235
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
236
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
237
NOINHERIT;"|$(psqlAsAdmin)
238
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
239
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
240
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
241
	-$(asAdmin) createlang plpython3u vegbien
242
# ignore errors if user/database/etc. exists
243

    
244
rm_initial_public: _always
245
	@$(confirmRmPublicSchema)
246
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
247
# drop public schema *as admin* because it starts out owned by postgres
248

    
249
rm_db: _always
250
	@$(confirmRmDb)
251
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
252
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
253
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
254
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
255

    
256
reinstall_db: _always rm_db db ;
257

    
258
##### MySQL
259

    
260
mysql: _always $(call forOs,mysql) mysql_user
261
	@$(done)
262

    
263
rm_mysql: _always rm_mysql_user ;
264

    
265
mysql-Linux: _always
266
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
267
	@$(wait)
268
	-sudo apt-get --yes install mysql-server mysql-client python-mysqldb
269

    
270
mysql-Darwin: _always
271
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
272
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
273
	@echo 'Extract the archive'
274
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
275
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
276
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
277
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
278
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
279
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
280
	@echo 'Change to the directory of the extracted files'
281
	@echo 'Run python setup.py clean'
282
	@echo 'Run python setup.py build'
283
	@echo 'Run sudo python setup.py install'
284
	@echo "Run python -c 'import MySQLdb'"
285
	@$(wait)
286

    
287
mysql-: _always # other OSes
288

    
289
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
290

    
291
mysql_user: _always
292
	-$($@_cmd)
293
# ignore errors if user exists
294
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
295
'$(bienPassword)';"|$(mysqlAsRoot)
296

    
297
rm_mysql_user: _always
298
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
299
# ignore errors if user exists
300

    
301
##### Datasources
302

    
303
inputs: _always inputs/all ;
304

    
305
inputs/install: _always
306
	echo '. bin/reinstall_all; wait'|bash -s
307

    
308
import: _always import-msg inputs/import ;
309
import-msg: _always
310
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
311
	@echo
312

    
313
verify: _always inputs/verify
314
	@$(done)
315

    
316
test: _always inputs/test
317
	@$(done)
318

    
319
##### Testing
320

    
321
test-all: _always remake test missing_mappings
322
	@$(done)
323

    
324
##### Subdir forwarding
325

    
326
# Must come last so overridden targets are not forwarded
327

    
328
define subdirTargets
329
$(subdir): _always
330
	+$$(subMake)
331

    
332
$(subdir)%: _always
333
	+$$(subMake)
334
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
335

    
336
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
337
endef
338
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
339

    
340
Makefile: ;
341

    
342
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
343

    
344
#### Maps validation
345

    
346
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
347

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