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
6 # run make CONF=anotherconfig.mk if you need several configs
11 # if undefined in the conf file, use single dash
14 # bridge name (XXX the same on all nodes)
20 flip=$(if $(findstring 1,$(1)),2,1)
22 cutsep=$(subst $(SEP), ,$(1))
24 leftnode=$(word 1,$(call cutsep,$(1)))
25 # rightnode (x-y) -> y
26 rightnode=$(word 2,$(call cutsep,$(1)))
28 linkpart=$(word 1,$(subst @, ,$(1)))
30 endpart=$(word 2,$(subst @, ,$(1)))
33 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
36 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
39 rget=$(call get,$(call opp,$(1)))
41 default=$(if $($(1)_$(2)),$($(1)_$(2)),$($(1)))
44 # can be redefined in conf.mk if that's not the expected behaviour
45 display?=host $(1) aka $(call solve,$(1))
48 log=$(addprefix log/,$(notdir $(1)))
50 #################### set variables after conf.mk
54 SSH_KEY_OPTION ?= -i $(SSH_KEY)
57 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
58 SSH = ssh $(SSH_OPTIONS)
62 ALL_NODE_IDS=$(sort $(foreach link,$(LINKS),$(call leftnode,$(link))) $(foreach link,$(LINKS),$(call rightnode,$(link))))
63 ALL_NODE_HOSTS=$(foreach id,$(ALL_NODE_IDS),$(call solve,$(id)))
64 ALL_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
65 CONF_NODE_IDS=$(subst HOST_,,$(filter HOST_%,$(.VARIABLES)))
70 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
71 @[ -d log ] || (echo Creating tmp dir log; mkdir log)
72 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
79 LINKTARGETS=$(addprefix L/,$(LINKS))
83 # could also do make ++SLICE
84 showslice: ++SLICE FORCE
87 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
88 showhostnames: ++ALL_NODE_HOSTS
90 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
92 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
94 @$(foreach id,$(ALL_NODE_IDS), echo $(call display,$(id)) has controller $(call default,CONTROLLER,$(id));)
95 .PHONY: shownodes showhostnames showips showlinks showcontrollers
97 sshchecks: $(foreach id,$(ALL_NODE_IDS),sshcheck-$(id))
99 ovsversions: $(foreach id,$(ALL_NODE_IDS),ovsversion-$(id))
101 # more general form; run with make sshs SSH-COMMAND="bla bla bla"
102 sshs: $(foreach id,$(ALL_NODE_IDS),ssh-$(id))
105 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
109 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
116 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
119 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
122 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
126 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
127 controllers: $(CONTROLLERS)
130 del-controllers: $(foreach id,$(ALL_NODE_IDS),del-controller-$(id))
131 .PHONY: del-controllers
133 ### node-oriented targets
134 # check ssh connectivity
136 @if $(SSH) $(HOST_$*) hostname &> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
137 else echo "ssh on" $(call display,$*) "KO !!!"; fi
140 @OVSVERSION=$$($(SSH) $(HOST_$*) rpm -q sliver-openvswitch); echo $(call display,$*) has $$OVSVERSION
143 @$(SSH) $(HOST_$*) $(SUDO) $(SSH-COMMAND)
145 # should probably replace sshcheck
146 cache/status.%: FORCE
147 @echo "=== DB and SWITCH processes on $(call display,$*)"
148 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status ||:
151 @echo "IP lookup for $(call display,$*)"
152 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
155 @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
156 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
158 cache/switch.%: | cache/db.%
159 @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
160 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
162 cache/bridge.%: | cache/db.%
163 @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
164 @$(SSH) $(HOST_$*) $(SUDO) \
165 sliver-ovs create-bridge $(BRIDGE) $(IP_$*) $(call default,BROPTIONS,$*) &> $(call log,$@) \
168 cache/controller.%: cache/bridge.%
169 @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
170 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) \
173 # xxx this probably needs a more thorough cleanup in cache/
174 cache/stop.%: del-bridge.%
175 @echo "Stopping switch & db on $(call display,$*)"
176 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.$* cache/db.$*
178 ### link-oriented targets
179 # L/<nodeid>-<node_id>
180 L/%: cache/endpoint.%@1 cache/endpoint.%@2
182 @echo "Link $* is up"
184 U/%: del-iface.%@1 del-iface.%@2
186 @echo "Deleted link $*"
188 del-bridge.%: | cache/db.%
189 @echo "Deleting bridge on $(call display,$*)"
190 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
191 @rm -f cache/bridge.$* \
192 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
193 cache/port.$*$(SEP)*@1 cache/port.*$(SEP)$*@2 \
194 cache/endpoint.$*$(SEP)*@? cache/endpoint.*$(SEP)$*@? \
195 L/$*$(SEP)* L/*$(SEP)$*
198 @echo "Shutting down switch on $(call display,$*)"
199 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
200 @rm -f cache/switch.$* \
201 cache/port.$*$(SEP)*@1 cache/port.*$(SEP)$*@2 \
202 cache/endpoint.$*$(SEP)*@2 cache/endpoint.*$(SEP)$*@1 \
203 L/$*$(SEP)* L/*$(SEP)$*
206 @echo "Shutting down db on $(call display,$*)"
207 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
210 del-controller-%: | cache/db.%
211 @echo "Removing controller for $(call display,$*)"
212 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-controller $(BRIDGE)
213 @rm -f cache/controller.$*
215 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
217 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
219 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
221 shutdown: del-switchs del-dbs
223 .PHONY: del-links del-switchs del-dbs shutdown
227 remote-snapshot: remote-snapshot-ips remote-snapshot-links
230 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
233 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
234 @sort -u /dev/null $^
236 ### update sliver-ovs
237 update: $(addprefix update-,$(CONF_NODE_IDS))
241 @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
242 @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
243 @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
244 @scp -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
245 @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
247 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
250 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
251 get interface $(BRIDGE) options:local_ip options:local_netmask |\
252 sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
256 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
257 sed -n 's/^L/LINKS += /p' > $@ || \
263 @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
264 @$(SSH) $(HOST_$(call get,$*)) \
265 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
266 @rm -f cache/iface.$* cache/port.$* cache/endpoint.$* cache/endpoint.$(call opp,$*)
269 ### '%' here is leftid-rightid@{1,2}
270 # we retrieve % as $(*F)
271 #linkid=$(call linkpart,%)
272 #nodeid=$(call get,%)
273 #bridgefile=cache/bridge.$(nodeid)
274 cache/iface.%: cache/bridge.$$(call get,%) | cache/switch.$$(call get,%)
275 @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
276 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
277 L$(call linkpart,$(*F)) &> $(call log,$@) \
280 cache/port.%: cache/iface.% cache/switch.$$(call get,%)
281 @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
282 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
283 ovs-appctl netdev-tunnel/get-port L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) \
284 || { rm $@; exit 1; }
287 # linkid=$(call linkpart,%)
288 # nodeid=$(call get,%)
289 # iface1=cache/iface.%
290 # iface2=cache/iface.$(call opp,%)
291 cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
292 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
293 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
294 $$(cat cache/host.$(call rget,$(*F))) \
295 $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@) \
299 CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
300 clean: $(CLEANTARGETS)
303 .PHONY: clean distclean
307 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
313 # 'virtual' targets in that there's no real file attached
314 define node_shortcuts
315 sshcheck.$(1): sshcheck-$(1) FORCE
316 db.$(1): cache/db.$(1) FORCE
317 switch.$(1): cache/switch.$(1) FORCE
318 start.$(1): cache/start.$(1) FORCE
319 stop.$(1): cache/stop.$(1) FORCE
320 status.$(1): cache/status.$(1) FORCE
321 bridge.$(1): cache/bridge.$(1) FORCE
322 host.$(1): cache/host.$(1) FORCE
323 # switch already depends on db, but well
324 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
327 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
329 define link_shortcuts
330 iface.%: cache/iface.%
331 endpoint.%: cache/endpoint.%
334 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
339 #################### convenience, for debugging only
340 # make +foo : prints the value of $(foo)
341 # make ++foo : idem but verbose, i.e. foo=$(foo)
342 ++%: varname=$(subst +,,$@)
344 @echo "$(varname)=$($(varname))"
345 +%: varname=$(subst +,,$@)
347 @echo "$($(varname))"