do not rely on cache for deletes
[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 log ] || (echo Creating tmp dir log; mkdir log)
69         @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
70 .PHONY: all+init init
71
72 FORCE:
73
74 .SECONDARY:
75
76 LINKTARGETS=$(addprefix L/,$(LINKS))
77 all: $(LINKTARGETS)
78 .PHONY: all
79
80 # could also do make ++SLICE
81 showslice: ++SLICE FORCE
82
83 shownodes:
84         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
85 showips:
86         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
87 showlinks:
88         @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
89 .PHONY: shownodes showips showlinks
90
91 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
92 .PHONY: sshchecks
93
94 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
95 dbs: $(DBS)
96 .PHONY: dbs
97
98 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
99 switchs: $(SWITCHS)
100 .PHONY: switchs
101
102 start: dbs switchs
103 .PHONY: start
104
105 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
106 .PHONY: stop
107
108 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
109 .PHONY: status
110
111 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
112 bridges: $(BRIDGES)
113 .PHONY: bridges
114
115 ### node-oriented targets
116 # check ssh connectivity
117 cache/sshcheck.%: FORCE
118         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
119          else echo "ssh on" $(call display,$*) "KO !!!"; fi
120
121 # should probably replace sshcheck
122 cache/status.%: FORCE
123         @echo "=== DB and SWITCH processes on $(call display,$*)"
124         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
125
126 cache/host.%:
127         @echo "IP lookup for $(call display,$*)"
128         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
129
130 cache/db.%:
131         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
132         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
133
134 cache/switch.%: cache/db.%
135         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
136         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
137
138 cache/bridge.%: cache/switch.%
139         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
140         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
141         @echo Created bridge with tap $$(cat $@) on $(call display,$*)
142
143 # xxx this probably needs a more thorough cleanup in cache/
144 cache/stop.%: del-bridge.%
145         @echo "Stopping switch & db on $(call display,$*)"
146         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
147
148 ### link-oriented targets
149 # L/<nodeid>-<node_id>
150 L/%: cache/link.%@1 cache/link.%@2
151         @touch $@
152         @echo "Created link $*"
153
154 U/%: del-iface.%@1 del-iface.%@2
155         @rm -f L/$*
156         @echo "Deleted link $*"
157
158 del-bridge.%: cache/db.%
159         @echo "Deleting bridge on $(call display,$*)"
160         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
161         @rm -f cache/bridge.$* \
162               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
163               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
164               L/$*$(SEP)*             L/*$(SEP)$*
165
166 del-switch.%: del-bridge.%
167         @echo "Shutting down switch on $(call display,$*)"
168         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
169         @rm -f cache/switch.$* 
170
171 del-db.%:
172         @echo "Shutting down db on $(call display,$*)"
173         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
174         @rm -f cache/db.$*
175
176 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
177
178 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
179
180 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
181
182 shutdown: del-switchs del-dbs
183
184 .PHONY: del-links del-switchs del-dbs shutdown
185
186 .SECONDEXPANSION:
187
188 del-iface.%: cache/db.$$(call get,%)
189         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
190         @$(SSH) $(HOST_$(call get,$*)) \
191                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
192         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
193
194
195 ### '%' here is leftid-rightid@{1,2}
196 # we retrieve % as $(*F)
197 #linkid=$(call linkpart,%)
198 #nodeid=$(call get,%)
199 #bridgefile=cache/bridge.$(nodeid)
200 cache/iface.%: cache/bridge.$$(call get,%)
201         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
202         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
203                 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
204
205
206 # linkid=$(call linkpart,%)
207 # nodeid=$(call get,%)
208 # iface1=cache/iface.%
209 # iface2=cache/iface.$(call opp,%)
210 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
211         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
212         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
213                         $$(cat cache/host.$(call rget,$(*F))) \
214                         $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
215          && touch $@
216
217 ####################
218 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
219 clean: $(CLEANTARGETS)
220 distclean:
221         rm -rf L U cache
222 .PHONY: clean distclean
223
224 ####################
225 graph.dot:
226         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
227 graph.ps: graph.dot
228         dot -Tps < $^ > $@      
229
230 ####################
231 # 'virtual' targets in that there's no real file attached
232 define node_shortcuts
233 sshcheck.$(1): cache/sshcheck.$(1) FORCE
234 db.$(1): cache/db.$(1) FORCE
235 switch.$(1): cache/switch.$(1) FORCE
236 start.$(1): cache/start.$(1) FORCE
237 stop.$(1): cache/stop.$(1) FORCE
238 status.$(1): cache/status.$(1) FORCE
239 bridge.$(1): cache/bridge.$(1) FORCE
240 host.$(1): cache/host.$(1) FORCE
241 # switch already depends on db, but well
242 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
243 endef
244
245 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
246
247 define link_shortcuts
248 iface.%: cache/iface.%
249 link.%: cache/link.%
250 endef
251
252 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
253
254 #################### convenience, for debugging only
255 # make +foo : prints the value of $(foo)
256 # make ++foo : idem but verbose, i.e. foo=$(foo)
257 ++%: varname=$(subst +,,$@)
258 ++%:
259         @echo "$(varname)=$($(varname))"
260 +%: varname=$(subst +,,$@)
261 +%:
262         @echo "$($(varname))"