move logs to a separate directory
[sliver-openvswitch.git] / planetlab / exp-tool / Makefile
1 # see README
2 # conf.mk is expected to define
3 # HOST_<id> and IP_<id> for all nodes involved, as well as 
4 # LINKS as a list of <node_id>-<node_id> elements
5
6 # run make CONF=anotherconfig.mk if you need several configs
7
8 CONF ?= conf.mk
9 include $(CONF)
10
11 # if undefined in the conf file, use single dash
12 SEP?=-
13
14 # bridge name (XXX the same on all nodes)
15 BRIDGE?=$(SLICE)
16
17 ### helper functions
18 # flip(1) = 2
19 # flip(2) = 1
20 flip=$(if $(findstring 1,$(1)),2,1)
21 # cutsep (x-y)-> x y
22 cutsep=$(subst $(SEP), ,$(1))
23 # leftnode (x-y) -> x
24 leftnode=$(word 1,$(call cutsep,$(1)))
25 # rightnode (x-y) -> y
26 rightnode=$(word 2,$(call cutsep,$(1)))
27 # linkpart(x@y) = x
28 linkpart=$(word 1,$(subst @, ,$(1)))
29 # endpart(x@y) = y
30 endpart=$(word 2,$(subst @, ,$(1)))
31 # get(x-y@1) = x
32 # get(x-y@2) = y
33 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
34 # opp(x-y@1) = x-y@2
35 # opp(x-y@2) = x-y@1
36 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
37 # rget(x-y@1) = y
38 # rget(x-y@2) = x
39 rget=$(call get,$(call opp,$(1)))
40 ###
41 solve=$(HOST_$(1))
42 solve_ip=$(IP_$(1))
43 # can be redefined in conf.mk if that's not the expected behaviour
44 display?=host $(1) aka $(call solve,$(1))
45
46 # log file name
47 log=$(addprefix log/,$(notdir $(1)))
48
49 #################### set variables after conf.mk
50 ifeq "$(SSH_KEY)" ""
51 SSH_KEY_OPTION ?=
52 else
53 SSH_KEY_OPTION ?= -i $(SSH_KEY)
54 endif
55
56 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
57 SSH = ssh $(SSH_OPTIONS)
58
59 SUDO ?= sudo -S
60
61 ALL_NODE_IDS=$(sort $(foreach link,$(LINKS),$(call leftnode,$(link))) $(foreach link,$(LINKS),$(call rightnode,$(link))))
62 ALL_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
63
64 ####################
65 all+init: init all
66 init:
67         @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
68         @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
69         @[ -d log ] || (echo Creating tmp dir log; mkdir log)
70         @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
71 .PHONY: all+init init
72
73 FORCE:
74
75 .SECONDARY:
76
77 LINKTARGETS=$(addprefix L/,$(LINKS))
78 all: $(LINKTARGETS)
79 .PHONY: all
80
81 # could also do make ++SLICE
82 showslice: ++SLICE FORCE
83
84 shownodes:
85         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
86 showips:
87         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
88 showlinks:
89         @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
90 .PHONY: shownodes showips showlinks
91
92 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
93 .PHONY: sshchecks
94
95 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
96 dbs: $(DBS)
97 .PHONY: dbs
98
99 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
100 switchs: $(SWITCHS)
101 .PHONY: switchs
102
103 start: dbs switchs
104 .PHONY: start
105
106 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
107 .PHONY: stop
108
109 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
110 .PHONY: status
111
112 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
113 bridges: $(BRIDGES)
114 .PHONY: bridges
115
116 ### node-oriented targets
117 # check ssh connectivity
118 cache/sshcheck.%: FORCE
119         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
120          else echo "ssh on" $(call display,$*) "KO !!!"; fi
121
122 # should probably replace sshcheck
123 cache/status.%: FORCE
124         @echo "=== DB and SWITCH processes on $(call display,$*)"
125         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
126
127 cache/host.%:
128         @echo "IP lookup for $(call display,$*)"
129         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
130
131 cache/db.%:
132         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
133         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
134
135 cache/switch.%: cache/db.%
136         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
137         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
138
139 cache/bridge.%: cache/switch.%
140         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
141         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
142         @echo Created bridge with tap $$(cat $@) on $(call display,$*)
143
144 # xxx this probably needs a more thorough cleanup in cache/
145 cache/stop.%: del-bridge.%
146         @echo "Stopping switch & db on $(call display,$*)"
147         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
148
149 ### link-oriented targets
150 # L/<nodeid>-<node_id>
151 L/%: cache/link.%@1 cache/link.%@2
152         @touch $@
153         @echo "Created link $*"
154
155 U/%: del-iface.%@1 del-iface.%@2
156         @rm -f L/$*
157         @echo "Deleted link $*"
158
159 del-bridge.%: cache/db.%
160         @echo "Deleting bridge on $(call display,$*)"
161         @if [ -f cache/bridge.$* ]; then \
162                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);\
163          fi
164         @rm -f cache/bridge.$* \
165               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
166               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
167               L/$*$(SEP)*             L/*$(SEP)$*
168
169 del-switch.%: del-bridge.%
170         @echo "Shutting down switch on $(call display,$*)"
171         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
172         @rm -f cache/switch.$* 
173
174 del-db.%:
175         @echo "Shutting down db on $(call display,$*)"
176         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
177         @rm -f cache/db.$*
178
179 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
180
181 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
182
183 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
184
185 shutdown: del-switchs del-dbs
186
187 .PHONY: del-links del-switchs del-dbs shutdown
188
189 .SECONDEXPANSION:
190
191 del-iface.%: cache/db.$$(call get,%)
192         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
193         @$(SSH) $(HOST_$(call get,$*)) \
194                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
195         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
196
197
198 ### '%' here is leftid-rightid@{1,2}
199 # we retrieve % as $(*F)
200 #linkid=$(call linkpart,%)
201 #nodeid=$(call get,%)
202 #bridgefile=cache/bridge.$(nodeid)
203 cache/iface.%: cache/bridge.$$(call get,%)
204         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
205         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
206                 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
207
208
209 # linkid=$(call linkpart,%)
210 # nodeid=$(call get,%)
211 # iface1=cache/iface.%
212 # iface2=cache/iface.$(call opp,%)
213 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
214         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
215         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
216                         $$(cat cache/host.$(call rget,$(*F))) \
217                         $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
218          && touch $@
219
220 ####################
221 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
222 clean: $(CLEANTARGETS)
223 distclean:
224         rm -rf L U cache
225 .PHONY: clean distclean
226
227 ####################
228 graph.dot:
229         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
230 graph.ps: graph.dot
231         dot -Tps < $^ > $@      
232
233 ####################
234 # 'virtual' targets in that there's no real file attached
235 define node_shortcuts
236 sshcheck.$(1): cache/sshcheck.$(1) FORCE
237 db.$(1): cache/db.$(1) FORCE
238 switch.$(1): cache/switch.$(1) FORCE
239 start.$(1): cache/start.$(1) FORCE
240 stop.$(1): cache/stop.$(1) FORCE
241 status.$(1): cache/status.$(1) FORCE
242 bridge.$(1): cache/bridge.$(1) FORCE
243 host.$(1): cache/host.$(1) FORCE
244 # switch already depends on db, but well
245 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
246 endef
247
248 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
249
250 define link_shortcuts
251 iface.%: cache/iface.%
252 link.%: cache/link.%
253 endef
254
255 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
256
257 #################### convenience, for debugging only
258 # make +foo : prints the value of $(foo)
259 # make ++foo : idem but verbose, i.e. foo=$(foo)
260 ++%: varname=$(subst +,,$@)
261 ++%:
262         @echo "$(varname)=$($(varname))"
263 +%: varname=$(subst +,,$@)
264 +%:
265         @echo "$($(varname))"