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_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
64 CONF_NODE_IDS=$(subst HOST_,,$(filter HOST_%,$(.VARIABLES)))
69 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
70 @[ -d log ] || (echo Creating tmp dir log; mkdir log)
71 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
78 LINKTARGETS=$(addprefix L/,$(LINKS))
82 # could also do make ++SLICE
83 showslice: ++SLICE FORCE
86 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
88 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
90 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
91 .PHONY: shownodes showips showlinks
93 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
96 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
100 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
107 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
110 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
113 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
117 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
118 controllers: $(CONTROLLERS)
121 ### node-oriented targets
122 # check ssh connectivity
123 cache/sshcheck.%: FORCE
124 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
125 else echo "ssh on" $(call display,$*) "KO !!!"; fi
127 # should probably replace sshcheck
128 cache/status.%: FORCE
129 @echo "=== DB and SWITCH processes on $(call display,$*)"
130 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
133 @echo "IP lookup for $(call display,$*)"
134 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
137 @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
138 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
140 cache/switch.%: cache/db.%
141 @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
142 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
144 cache/bridge.%: cache/switch.%
145 @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
146 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
147 @echo Created bridge with tap $$(cat $@) on $(call display,$*)
149 cache/controller.%: cache/bridge.%
150 @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
151 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) || { rm $@; exit 1; }
153 # xxx this probably needs a more thorough cleanup in cache/
154 cache/stop.%: del-bridge.%
155 @echo "Stopping switch & db on $(call display,$*)"
156 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
158 ### link-oriented targets
159 # L/<nodeid>-<node_id>
160 L/%: cache/link.%@1 cache/link.%@2
162 @echo "Created link $*"
164 U/%: del-iface.%@1 del-iface.%@2
166 @echo "Deleted link $*"
168 del-bridge.%: cache/db.%
169 @echo "Deleting bridge on $(call display,$*)"
170 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
171 @rm -f cache/bridge.$* \
172 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
173 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
174 L/$*$(SEP)* L/*$(SEP)$*
176 del-switch.%: del-bridge.%
177 @echo "Shutting down switch on $(call display,$*)"
178 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
179 @rm -f cache/switch.$*
182 @echo "Shutting down db on $(call display,$*)"
183 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
186 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
188 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
190 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
192 shutdown: del-switchs del-dbs
194 .PHONY: del-links del-switchs del-dbs shutdown
198 remote-snapshot: remote-snapshot-ips remote-snapshot-links
201 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
204 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
205 @sort -u /dev/null $^
207 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
210 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
211 get interface $(BRIDGE) options:local_ip options:local_netmask |\
212 sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
216 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
217 sed -n 's/^L/LINKS += /p' > $@ || \
222 del-iface.%: cache/db.$$(call get,%)
223 @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
224 @$(SSH) $(HOST_$(call get,$*)) \
225 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
226 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
229 ### '%' here is leftid-rightid@{1,2}
230 # we retrieve % as $(*F)
231 #linkid=$(call linkpart,%)
232 #nodeid=$(call get,%)
233 #bridgefile=cache/bridge.$(nodeid)
234 cache/iface.%: cache/bridge.$$(call get,%)
235 @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
236 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
237 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
240 # linkid=$(call linkpart,%)
241 # nodeid=$(call get,%)
242 # iface1=cache/iface.%
243 # iface2=cache/iface.$(call opp,%)
244 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
245 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
246 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
247 $$(cat cache/host.$(call rget,$(*F))) \
248 $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
252 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
253 clean: $(CLEANTARGETS)
256 .PHONY: clean distclean
260 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
265 # 'virtual' targets in that there's no real file attached
266 define node_shortcuts
267 sshcheck.$(1): cache/sshcheck.$(1) FORCE
268 db.$(1): cache/db.$(1) FORCE
269 switch.$(1): cache/switch.$(1) FORCE
270 start.$(1): cache/start.$(1) FORCE
271 stop.$(1): cache/stop.$(1) FORCE
272 status.$(1): cache/status.$(1) FORCE
273 bridge.$(1): cache/bridge.$(1) FORCE
274 host.$(1): cache/host.$(1) FORCE
275 # switch already depends on db, but well
276 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
279 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
281 define link_shortcuts
282 iface.%: cache/iface.%
286 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
288 #################### convenience, for debugging only
289 # make +foo : prints the value of $(foo)
290 # make ++foo : idem but verbose, i.e. foo=$(foo)
291 ++%: varname=$(subst +,,$@)
293 @echo "$(varname)=$($(varname))"
294 +%: varname=$(subst +,,$@)
296 @echo "$($(varname))"