define PLC_MAIL_FROM_ADDRESS
[tests.git] / federation / Makefile
1 ### -*-Makefile-*-
2 # $Id$
3 PLC1=plc1.inria.fr
4 PLC2=plc2.inria.fr
5
6 CHROOTJAIL=/plc/root
7 CHROOT=chroot $(CHROOTJAIL)
8 PORT=5432
9 SITEDIR=/etc/planetlab/configs
10 SITE=site.xml
11 APIDIR=/usr/share/plc_api
12 ROOTAPIDIR=/plc/root$(APIDIR)
13 WORKDIR=new_plc_api/tests
14
15 PLC1SSH=root@$(PLC1)
16 PLC2SSH=root@$(PLC2)
17
18 PY=python -u
19
20 PLCS=-1 -2
21 SIZES=-m -n -b -h
22
23 all:help
24
25 # if make is invoked with -n, run rsync, but with --dry-run
26 RSYNC_EXCLUDES          := --exclude .svn --exclude '*~' --exclude '*.pyc'
27 RSYNC_CMD               := rsync
28 RSYNC_COND_DRY_RUN      := $(if $(findstring n,$(MAKEFLAGS)),--dry-run,)
29 RSYNC_ARGS              += $(RSYNC_COND_DRY_RUN) $(RSYNC_EXCLUDES)
30 RSYNC                   := $(RSYNC_CMD) $(RSYNC_ARGS)
31
32 #################### remote invokation and chroot hops
33 # rule to invoke a target within the chroot context
34 %.chroot: target=$(subst .chroot,,$@)
35 %.chroot:
36         $(CHROOT) $(MAKE) -C $(APIDIR)/tests INCHROOT=true $(target)
37         -cp $(ROOTAPIDIR)/tests/$(target) .
38
39 # generic rules to ssh and make
40 # need to explicitly export MAKEFLAGS that cannot be exported by make through ssh
41 %.1: prefix=$(subst .1,,$@)
42 %.1: output=$(subst .chroot,,$(prefix))
43 %.1:
44         @echo ">>>>>>>> entering ssh $(PLC1SSH)"
45         ssh $(PLC1SSH) $(MAKE) MAKEFLAGS=$(MAKEFLAGS) -C $(WORKDIR) PLCEND=1 $(prefix) 
46         @echo "<<<<<<<< exiting  ssh $(PLC1SSH)"
47         -scp $(PLC1SSH):$(WORKDIR)/$(output) . 2> /dev/null
48 %.2: prefix=$(subst .2,,$@)
49 %.2: output=$(subst .chroot,,$(prefix))
50 %.2:
51         @echo ">>>>>>>> entering ssh $(PLC2SSH)"
52         ssh $(PLC2SSH) $(MAKE) MAKEFLAGS=$(MAKEFLAGS) -C $(WORKDIR) PLCEND=2 $(prefix) 
53         @echo "<<<<<<<< exiting  ssh $(PLC2SSH)"
54         -scp $(PLC2SSH):$(WORKDIR)/$(output) . 2> /dev/null
55
56 # cannot just depend on .1 and .2 because they are phony
57 %.3: prefix=$(subst .3,,$@)
58 %.3: 
59         $(MAKE) $(prefix).1 $(prefix).2
60
61 %.both: prefix=$(subst .both,,$@)
62 %.both:
63         $(MAKE) $(prefix).chroot.1 $(prefix).chroot.2
64
65 ####################
66 # phony targets MUST NOT try to get their result through scp
67 # usage: ssh_phony_target <target>
68 define ssh_phony_target
69 $(1).chroot:
70         $(CHROOT) $(MAKE) -C $(APIDIR)/tests INCHROOT=true $(1)
71 $(1).1:
72         @echo ">>>>>>>> entering ssh $(PLC1SSH)"
73         ssh $(PLC1SSH) $(MAKE) MAKEFLAGS=$(MAKEFLAGS) -C $(WORKDIR) PLCEND=1 $(1)
74         @echo "<<<<<<<< exiting  ssh $(PLC1SSH)"
75 $(1).2:
76         @echo ">>>>>>>> entering ssh $(PLC2SSH)"
77         ssh $(PLC2SSH) $(MAKE) MAKEFLAGS=$(MAKEFLAGS) -C $(WORKDIR) PLCEND=2 $(1)
78         @echo "<<<<<<<< exiting  ssh $(PLC2SSH)"
79 endef
80
81 ####################
82 define here_and_proceed_target
83 PHONIES += $(1)
84 ifdef INCHROOT
85 $(1): 
86         $(2)
87 else
88 ifdef PLCEND
89 $(1): $(1).chroot
90         $(2)
91 else
92 $(1): $(1).3
93         $(2)
94 endif
95 endif
96 endef
97
98 ####################
99 ####################
100 ####################
101 PLC-PUSHS=  ../PLC ../planetlab4.sql ../migrations ../plcsh ../tests
102
103 PUSH += pclean
104 pclean:
105         -find .. '(' -name '*.pyc' -o -name '*~' ')' | xargs rm
106 PUSH += proot1
107 proot1:
108         +$(RSYNC) -a -v -C ../ root@$(PLC1):new_plc_api/
109 PUSH += pchroot1
110 pchroot1:
111         +$(RSYNC) -a -v -C $(PLC-PUSHS) root@$(PLC1):$(CHROOTJAIL)$(APIDIR)/
112 PUSH += proot2
113 proot2:
114         +$(RSYNC) -a -v -C ../ root@$(PLC2):new_plc_api/
115 PUSH += pchroot2
116 pchroot2:
117         +$(RSYNC) -a -v -C $(PLC-PUSHS) root@$(PLC2):$(CHROOTJAIL)$(APIDIR)/
118
119 push: $(PUSH)
120 push.1: proot1 pchroot1
121 push.2: proot2 pchroot2
122 # DONT! mention this in PHONIES
123 .PHONY: push push.1 push.2
124
125 PHONIES += $(PUSH)
126
127 ####################
128 PEERS += peer-gpg-1
129 peer-gpg-1:
130         ssh $(PLC1SSH) "gpg --homedir=/etc/planetlab --export --armor > /etc/planetlab/gpg_plc1.pub"
131 PEERS += peer-gpg-2
132 peer-gpg-2:
133         ssh $(PLC2SSH) "gpg --homedir=/etc/planetlab --export --armor > /etc/planetlab/gpg_plc2.pub"
134 # directly scp'ing from one url to the other does not work, looks like
135 # first host tries to connect the second one
136 PEERS += peer-push-gpg-1
137 peer-push-gpg-1:
138         scp $(PLC1SSH):/etc/planetlab/gpg_plc1.pub ./
139         scp ./gpg_plc1.pub $(PLC2SSH):/etc/planetlab/
140 PEERS += peer-push-gpg-2
141 peer-push-gpg-2:
142         scp $(PLC2SSH):/etc/planetlab/gpg_plc2.pub ./
143         scp ./gpg_plc2.pub $(PLC1SSH):/etc/planetlab/
144 PEERS += peer-push-cacert-1
145 peer-push-cacert-1:
146         scp $(PLC1SSH):/etc/planetlab/api_ca_ssl.crt ./api_plc1.crt
147         scp ./api_plc1.crt $(PLC2SSH):/etc/planetlab/
148         scp ./api_plc1.crt $(PLC1SSH):/etc/planetlab/
149 PEERS += peer-push-cacert-2
150 peer-push-cacert-2:
151         scp $(PLC2SSH):/etc/planetlab/api_ca_ssl.crt ./api_plc2.crt
152         scp ./api_plc2.crt $(PLC1SSH):/etc/planetlab/
153         scp ./api_plc2.crt $(PLC2SSH):/etc/planetlab/
154
155 PHONIES += $(PEERS)
156
157 PHONIES += peers
158 peers: $(PEERS)
159
160 PHONIES += peers-clean
161 peers-clean:
162         rm -f /etc/planetlab/*plc[12]*
163
164 HELP += peers-clean.3
165 ####################
166 # upgrade : install the most recent myplc rpm found in /root/$(WORKDIR)
167 RPM=$(shell ls -rt /root/$(WORKDIR)/myplc*rpm | tail -1)
168 HELP += rpm
169 PHONIES += rpm
170 rpm:
171         @echo upgrade would install latest rpm : $(RPM)
172 HELP += version
173 PHONIES += version
174 version:
175         rpm -q myplc
176
177 #### how to upgrade
178 UPGRADE += plc-stop
179 plc-stop:
180         -$(CHROOT) service plc safestop
181 UPGRADE += plc-preserve
182 plc-preserve:
183         cp $(SITEDIR)/$(SITE) .
184 UPGRADE += plc-uninstall
185 plc-uninstall:
186         rpm -e myplc
187 UPGRADE += plc-clean
188 plc-clean:
189         -rm -rf /plc/root /plc/data
190 UPGRADE += plc-install
191 plc-install:
192         rpm -i $(RPM)
193 UPGRADE += plc-reconfig
194 plc-reconfig:
195         service plc mount
196         mkdir -p $(SITEDIR)
197         cp $(SITE) $(SITEDIR)
198         (echo w; echo q) | $(CHROOT) plc-config-tty
199 UPGRADE += plc-start
200 plc-start:
201         $(CHROOT) service plc start
202
203 PHONIES += $(UPGRADE)
204
205 PHONIES += upgrade
206 upgrade: $(UPGRADE)
207
208 ####################
209 # cleaning the database
210
211 DB += db-drop
212 db-drop:
213         $(CHROOT) psql -U postgres --port $(PORT) template1 -c 'drop database planetlab4'
214 DB += db-restart
215 db-restart:
216         service plc stop db postgresql httpd
217         service plc start httpd postgresql db 
218 DB += db-clean-save
219 db-clean-save:
220         $(CHROOT) pg_dump -c -U pgsqluser planetlab4 > test-clean.sql
221 DB += db-safeclean
222 db-safeclean: db-drop db-restart db-clean-save
223
224 DB += db-clean
225 db-clean:
226         $(CHROOT) psql -U pgsqluser --port $(PORT) -d planetlab4 < test-clean.sql &> test-clean.restore 
227
228 PHONIES += $(DB)
229
230 ####################
231 # TestPeers options :
232 # -m -b -h : allow to select test size (mini, normal, big, huge)
233 # -l 1|2        required for running -p locally from a given end for performance
234 # -1 | -2       shorthands for -l 1 and -l 2 - for integration with this Makefile
235 ### the idea is:
236 # first clean off dbs
237 # run populate separately on both ends
238 # optionally save database at this point
239 # then run end of script locally
240 ####################
241
242 ### for a given 'targetname' like, e.g. 'e-n', we define the following targets
243 # testpeers.<targetname>.out -- for running the test
244 # testpeers.<targetname>.diff -- to compute the difference with expected result
245 # testpeers.<targetname>.adopt -- phony target for adopting new result - no commit done
246
247 ###
248 # generic rule for running TestPeers
249 # make testpeers.-option.some.stuff.out
250 # => python -u ./TestPeers.py -option some stuff > testpeers.-option.some.stuff.out 2>&1
251 testpeers.%.out: prefix=$(subst .out,,$@)
252 testpeers.%.out: dotargs=$(subst testpeers.,,$(prefix))
253 testpeers.%.out: args=$(subst ., ,$(dotargs))
254 testpeers.%.out:
255         $(PY) ./TestPeers.py $(args) > $@ 2>&1 ; if [ "$$?" != 0 ] ; then mv $@ $@.fail ; exit 1; fi
256
257 ### generic rule for computing differences -- 
258 # xxx this requires changes in TestPeers due to the new return code of RefreshPeers
259 # remove time/delay dependent output
260 normalize       = egrep -v "'expires':|^+++.*ellapsed"
261 %.nout: %.out
262         $(normalize) $*.out > $@
263 %.nref: %.ref
264         $(normalize) $*.ref > $@
265
266 testpeers.%.diff: out=$(subst .diff,.out,$@)
267 testpeers.%.diff: nout=$(subst .diff,.nout,$@)
268 testpeers.%.diff: ref=$(subst .diff,.ref,$@)
269 testpeers.%.diff: nref=$(subst .diff,.nref,$@)
270 testpeers.%.diff: 
271         @if [ ! -f $(out) ] ; then echo "Could not locate $(out)" ; exit 1 ; fi
272         @if [ ! -f $(ref) ] ; then echo "Could not locate $(ref)" ; exit 1 ; fi
273         $(MAKE) $(nref) $(nout)
274         diff $(nref) $(nout) > $@
275
276 ### generic rule for adopting current result 
277 testpeers.%.ckp: prefix=$(subst .ckp,,$@)
278 testpeers.%.ckp: out=$(subst .ckp,.out,$@)
279 testpeers.%.ckp: ref=$(subst .ckp,.ref,$@)
280 testpeers.%.ckp:
281         @if [ ! -f $(out) ] ; then echo "Could not locate $(out)" ; exit 1 ; fi
282         cp $(out) $(ref)
283         rm -f $(prefix).{diff,nout,nref}
284
285 testpeers.%.clean: prefix=$(subst .clean,,$@)
286 testpeers.%.clean: 
287         rm -f $(prefix).{out,nout,nref,diff}
288
289
290 HELP += testpeers-clean
291 $(eval $(call here_and_proceed_target,testpeers-clean,rm -f testpeers*.nref testpeers*.nout))
292
293 HELP += testpeers-distclean
294 $(eval $(call here_and_proceed_target,testpeers-distclean,rm -f testpeers*.out testpeers*.sql testpeers*.nref testpeers*.nout))
295
296 HELP += clean 
297 clean: testpeers-clean
298 HELP += sqlclean
299 $(eval $(call here_and_proceed_target,sqlclean,rm test*.sql))
300 HELP += distclean
301 distclean: testpeers-distclean
302 PHONIES += clean distclean
303
304 HELP += sql-clean
305 $(eval $(call here_and_proceed_target,sql-clean,rm testpeers.*.sql))
306
307 ##############################
308 # creating a snapshot of the databases
309 # for efficiency this is done from a Direct API method, thus under a chroot
310
311 define snapshot_and_restore_size_plc
312 testpeers.$(1).$(2).sql: testpeers.$(1).$(2).out
313         pg_dump -c -U pgsqluser planetlab4 > testpeers.$(1).$(2).sql
314
315 testpeers.$(1).$(2).restore: testpeers.$(1).$(2).sql
316         echo Restoring testpeers.$(1).$(2).sql
317         psql -U pgsqluser --port $(PORT) -d planetlab4 -f testpeers.$(1).$(2).sql &> testpeers.$(1).$(2).restore
318 DB-SQL += testpeers.$(1).$(2).sql 
319 DB-RESTORE += testpeers.$(1).$(2).restore
320 endef
321
322 define snapshot_and_restore_size
323 $(foreach plc,$(PLCS),$(eval $(call snapshot_and_restore_size_plc,$(1),$(plc).populate)))
324 endef
325
326 $(foreach size,$(SIZES),$(eval $(call snapshot_and_restore_size,$(size))))
327
328 define populate_shortcut
329 ifndef PLCEND
330 populate$(1): populate$(1).init populate$(1).run
331 populate$(1).all: db-clean.3 populate$(1).clean populate$(1).init populate$(1).run
332 populate$(1).init: testpeers.$(1).-1.populate.out.chroot.1 testpeers.$(1).-1.populate.sql.chroot.1 
333 populate$(1).init: testpeers.$(1).-2.populate.out.chroot.2 testpeers.$(1).-2.populate.sql.chroot.2 
334 populate$(1).restore: testpeers.$(1).-1.populate.restore.chroot.1 testpeers.$(1).-2.populate.restore.chroot.2
335 populate$(1).run: testpeers.$(1).populate_end.out
336 populate$(1).diff: testpeers.$(1).-1.populate.diff testpeers.$(1).-2.populate.diff testpeers.$(1).populate_end.diff 
337 populate$(1).ckp: testpeers.$(1).-1.populate.ckp testpeers.$(1).-2.populate.ckp testpeers.$(1).populate_end.ckp 
338 $(eval $(call here_and_proceed_target,populate$(1).clean,rm -f testpeers.$(1)*populate*.out testpeers.$(1)*populate*.restore))
339 $(eval $(call here_and_proceed_target,populate$(1).sqlclean,rm -f testpeers.$(1)*populate*.sql))
340 endif
341 endef
342
343 $(foreach size,$(SIZES),$(eval $(call populate_shortcut,$(size))))
344
345 populate.help:
346         @echo "===================="
347         @echo "populate test targets"   
348         @echo -e "populate-n\t\truns .init and .run"
349         @echo -e "populate-b.init\t\texpects a clean db, populates only and dumps"
350         @echo -e "populate-b.all\t\tcleans the db and former test results, runs the full test"
351         @echo -e "populate-m.run\t\truns the actual test after both plcs were inited"
352         @echo -e "populate-h.restore\t\trestores both plcs as if just inited"
353         @echo -e "populate-n.ckp\t\tcheckpoints out files as references"
354         @echo -e "populate-n.diff\t\tcompares out files against reference"
355
356 ####################
357 define testpeers_shortcut
358 ifndef PLCEND
359 testpeers$(1).run: testpeers.$(1).test_all.out
360 testpeers$(1).all: db-clean.3 testpeers.$(1).test_all.out
361 testpeers$(1).diff: testpeers.$(1).test_all.diff
362 testpeers$(1).ckp: testpeers.$(1).test_all.ckp
363 endif
364 endef
365
366 $(foreach size,$(SIZES),$(eval $(call testpeers_shortcut,$(size))))
367
368 testpeers.help:
369         @echo "Available sizes" $(SIZES)
370         @echo "testpeers targets"       
371         @echo -e "testpeers-n.run\t\truns the test"
372         @echo -e "testpeers-b.all\t\tcleans the db and former test results, runs the full test"
373         @echo -e "testpeers-m.run\t\truns the actual test after both plcs were inited"
374         @echo -e "testpeers-h.restore\t\trestores both plcs as if just inited"
375         @echo -e "testpeers-n.ckp\t\tcheckpoints out files as references"
376         @echo -e "testpeers-n.diff\t\tcompares out files against reference"
377
378
379 ####################
380 RUN += api
381 api:
382         $(CHROOT) /usr/bin/plcsh
383 RUN += sql
384 sql:
385         $(CHROOT) psql -U pgsqluser planetlab4
386 RUN += log
387 log:
388         emacs /plc/data/var/log/httpd/error_log /plc/data/var/log/boot.log
389
390 PHONIES += $(RUN)
391
392
393 $(foreach phony,$(PHONIES),$(eval $(call ssh_phony_target,$(phony))))
394
395 #######
396 help: testpeers.help populate.help 
397         @echo "===================="
398         @echo "other known targets:"
399         @echo "db family:" $(DB) 
400         @echo -e "e.g.\tmake db-clean.3"
401         @echo "push:" $(PUSH) 
402         @echo "peers:" $(PEERS)
403         @echo "===================="
404         @echo "For running on plc1, on plc2, or both:"
405         @echo -e "\t make <target>.1 <target>.2 <target.3>"
406         @echo "For running in a chroot jail"
407         @echo -e "\t make <target>.chroot"
408         @echo "So for running on both nodes chroots"
409         @echo -e "\t make <target>.chroot.3"
410         @echo "for running TestPeers : testpeers=<args separated by +>.out"
411         @echo -e "\t e.g. make testpeers.-m.test_all"
412         @echo -e "\t e.g. make testpeers.-h.-1.populate"
413         @echo "comparing run with reference : testpeers.<args separated by .>.diff"
414         @echo "adopt run as reference : testpeers.<args separated by .>.ckp"
415         @echo -e "\t e.g. make testpeers.-m.test_all"
416         @echo -e "\t e.g. make testpeers.-h.-1.populate"
417         @echo "===================="
418         @echo "Supported size variants:" $(SIZES)
419         @echo "Supported plc ids:" $(PLCS)
420         @echo "snapshotting the DB : " $(DB-SQL) $(DB-RESTORE)
421         @echo "===================="
422         @echo "upgrade:" $(UPGRADE)
423         @echo "run family (convenience):" $(RUN)
424         @echo "OTHERS:" $(HELP)
425
426
427 #################### convenience, for debugging only
428 # make +foo : prints the value of $(foo)
429 # make ++foo : idem but verbose, i.e. foo=$(foo)
430 ++%: varname=$(subst +,,$@)
431 ++%:
432         @echo "$(varname)=$($(varname))"
433 +%: varname=$(subst +,,$@)
434 +%:
435         @echo "$($(varname))"
436