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
##### Environment
15

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

    
18
##### General targets
19

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

    
24
%/remake: _always
25
	$(MAKE) $(@D)/clean
26
	$(MAKE) $(@D)/
27
# re-run make so that cache of existing files is reset
28

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

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

    
36
##### Installation
37

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

    
42
uninstall: _always rm_mysql rm_core ;
43

    
44
reinstall: _always uninstall install ;
45

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

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

    
51
rm_core: _always rm_db ;
52

    
53
reinstall_core: _always rm_core core ;
54

    
55
##### Python
56

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

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

    
87
##### PHP
88

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

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

    
105
##### PostgreSQL
106

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

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

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

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

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

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

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

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

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

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

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

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

    
169
endef
170

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

    
180
endef
181

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

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

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

    
211
postgres-: _always ; # other OSes
212

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

    
220
##### VegBIEN DB
221

    
222
#### DB and users
223

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

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

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

    
232
db: mk_db rm_initial_public _always ;
233

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

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

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

    
257
reinstall_db: _always rm_db db ;
258

    
259
##### MySQL
260

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

    
264
rm_mysql: _always rm_mysql_user ;
265

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

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

    
288
mysql-: _always # other OSes
289

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

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

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

    
302
##### Datasources
303

    
304
inputs: _always inputs/all ;
305

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

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

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

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

    
320
##### Testing
321

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

    
325
##### Subdir forwarding
326

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

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

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

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

    
341
Makefile: ;
342

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

    
345
#### Maps validation
346

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

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