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
17 flip=$(if $(findstring 1,$(1)),2,1)
19 cutsep=$(subst $(SEP), ,$(1))
21 leftnode=$(word 1,$(call cutsep,$(1)))
22 # rightnode (x-y) -> y
23 rightnode=$(word 2,$(call cutsep,$(1)))
25 linkpart=$(word 1,$(subst @, ,$(1)))
27 endpart=$(word 2,$(subst @, ,$(1)))
30 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
33 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
36 rget=$(call get,$(call opp,$(1)))
40 # can be redefined in conf.mk if that's not the expected behaviour
41 display?=host $(1) aka $(call solve,$(1))
43 #################### set variables after conf.mk
47 SSH_KEY_OPTION ?= -i $(SSH_KEY)
50 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
51 SSH = ssh $(SSH_OPTIONS)
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))
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)
70 LINKTARGETS=$(addprefix L/,$(LINKS))
74 # could also do make ++SLICE
75 showslice: ++SLICE FORCE
78 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
80 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
82 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
83 .PHONY: shownodes showips showlinks
85 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
88 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
92 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
99 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
102 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
105 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
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
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
121 @echo "IP lookup for $(call display,$*)"
122 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
125 @echo "Starting db server on $(call display,$*) - logs in $@.log"
126 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $@.log && touch $@
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 $@
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_$*)
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.%
142 ### link-oriented targets
143 # L/<nodeid>-<node_id>
144 L/%: cache/link.%@1 cache/link.%@2
146 @echo "Created link $*"
148 U/%: del-iface.%@1 del-iface.%@2
150 @echo "Deleted link $*"
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.$*);\
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)$*
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.$*
168 @echo "Shutting down db on $(call display,$*)"
169 @[ -f cache/db.$* ] && $(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
172 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
174 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
176 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
178 shutdown: del-switchs del-dbs
180 .PHONY: del-links del-switchs del-dbs shutdown
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,$*)
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))
203 # linkid=$(call linkpart,%)
204 # nodeid=$(call get,%)
205 # iface1=cache/iface.%
206 # iface2=cache/iface.$(call opp,%)
207 cache/link.%: cache/host.$$(call get,%) 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) ovs-vsctl set interface L$(call linkpart,$(*F)) \
210 options:remote_ip=$$(cat cache/host.$(call get,$(*F))) \
211 options:remote_port=$$(cat cache/iface.$(call opp,$(*F))) 2> $@.log \
215 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
216 clean: $(CLEANTARGETS)
219 .PHONY: clean distclean
223 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
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
242 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
244 define link_shortcuts
245 iface.%: cache/iface.%
249 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
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 +,,$@)
256 @echo "$(varname)=$($(varname))"
257 +%: varname=$(subst +,,$@)
259 @echo "$($(varname))"