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