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)))
43 # can be redefined in conf.mk if that's not the expected behaviour
44 display?=host $(1) aka $(call solve,$(1))
47 log=$(addprefix log/,$(notdir $(1)))
49 #################### set variables after conf.mk
53 SSH_KEY_OPTION ?= -i $(SSH_KEY)
56 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
57 SSH = ssh $(SSH_OPTIONS)
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 CONF_NODE_IDS=$(subst HOST_,,$(filter HOST_%,$(.VARIABLES)))
68 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
69 @[ -d log ] || (echo Creating tmp dir log; mkdir log)
70 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
77 LINKTARGETS=$(addprefix L/,$(LINKS))
81 # could also do make ++SLICE
82 showslice: ++SLICE FORCE
85 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
87 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
89 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
90 .PHONY: shownodes showips showlinks
92 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
95 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
99 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
106 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
109 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
112 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
116 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
117 controllers: $(CONTROLLERS)
120 ### node-oriented targets
121 # check ssh connectivity
122 cache/sshcheck.%: FORCE
123 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
124 else echo "ssh on" $(call display,$*) "KO !!!"; fi
126 # should probably replace sshcheck
127 cache/status.%: FORCE
128 @echo "=== DB and SWITCH processes on $(call display,$*)"
129 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
132 @echo "IP lookup for $(call display,$*)"
133 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
136 @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
137 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
139 cache/switch.%: cache/db.%
140 @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
141 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
143 cache/bridge.%: cache/switch.%
144 @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
145 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
146 @echo Created bridge with tap $$(cat $@) on $(call display,$*)
148 cache/controller.%: cache/bridge.%
149 @echo "Setting controller on $(call display,$*) - logs in $(call log,$@)"
150 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs set-controller $(BRIDGE) $(CONTROLLER_$*) &> $(call log,$@) || { rm $@; exit 1; }
152 # xxx this probably needs a more thorough cleanup in cache/
153 cache/stop.%: del-bridge.%
154 @echo "Stopping switch & db on $(call display,$*)"
155 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
157 ### link-oriented targets
158 # L/<nodeid>-<node_id>
159 L/%: cache/link.%@1 cache/link.%@2
161 @echo "Created link $*"
163 U/%: del-iface.%@1 del-iface.%@2
165 @echo "Deleted link $*"
167 del-bridge.%: cache/db.%
168 @echo "Deleting bridge on $(call display,$*)"
169 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
170 @rm -f cache/bridge.$* \
171 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
172 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
173 L/$*$(SEP)* L/*$(SEP)$*
175 del-switch.%: del-bridge.%
176 @echo "Shutting down switch on $(call display,$*)"
177 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
178 @rm -f cache/switch.$*
181 @echo "Shutting down db on $(call display,$*)"
182 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
185 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
187 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
189 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
191 shutdown: del-switchs del-dbs
193 .PHONY: del-links del-switchs del-dbs shutdown
197 remote-snapshot: remote-snapshot-ips remote-snapshot-links
200 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
203 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
204 @sort -u /dev/null $^
206 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
209 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
210 get interface $(BRIDGE) options:local_ip options:local_netmask |\
211 sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
215 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
216 sed -n 's/^L/LINKS += /p' > $@ || \
221 del-iface.%: cache/db.$$(call get,%)
222 @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
223 @$(SSH) $(HOST_$(call get,$*)) \
224 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
225 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
228 ### '%' here is leftid-rightid@{1,2}
229 # we retrieve % as $(*F)
230 #linkid=$(call linkpart,%)
231 #nodeid=$(call get,%)
232 #bridgefile=cache/bridge.$(nodeid)
233 cache/iface.%: cache/bridge.$$(call get,%)
234 @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
235 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
236 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
239 # linkid=$(call linkpart,%)
240 # nodeid=$(call get,%)
241 # iface1=cache/iface.%
242 # iface2=cache/iface.$(call opp,%)
243 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
244 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
245 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
246 $$(cat cache/host.$(call rget,$(*F))) \
247 $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
251 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
252 clean: $(CLEANTARGETS)
255 .PHONY: clean distclean
259 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
264 # 'virtual' targets in that there's no real file attached
265 define node_shortcuts
266 sshcheck.$(1): cache/sshcheck.$(1) FORCE
267 db.$(1): cache/db.$(1) FORCE
268 switch.$(1): cache/switch.$(1) FORCE
269 start.$(1): cache/start.$(1) FORCE
270 stop.$(1): cache/stop.$(1) FORCE
271 status.$(1): cache/status.$(1) FORCE
272 bridge.$(1): cache/bridge.$(1) FORCE
273 host.$(1): cache/host.$(1) FORCE
274 # switch already depends on db, but well
275 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
278 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
280 define link_shortcuts
281 iface.%: cache/iface.%
285 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
287 #################### convenience, for debugging only
288 # make +foo : prints the value of $(foo)
289 # make ++foo : idem but verbose, i.e. foo=$(foo)
290 ++%: varname=$(subst +,,$@)
292 @echo "$(varname)=$($(varname))"
293 +%: varname=$(subst +,,$@)
295 @echo "$($(varname))"