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 @if [ -f cache/switch.$* ]; then \
165 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl exit;\
167 @rm -f cache/switch.$*
170 @echo "Shutting down db on $(call display,$*)"
171 @if [ -f cache/db.$* ]; then \
172 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
176 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
178 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
180 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
182 shutdown: del-switches del-dbs
184 .PHONY: del-links del-switchs del-dbs shutdown
188 del-iface.%: cache/db.$$(call get,%)
189 @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
190 @$(SSH) $(HOST_$(call get,$*)) \
191 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
192 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
195 ### '%' here is leftid-rightid@{1,2}
196 # we retrieve % as $(*F)
197 #linkid=$(call linkpart,%)
198 #nodeid=$(call get,%)
199 #bridgefile=cache/bridge.$(nodeid)
200 cache/iface.%: cache/bridge.$$(call get,%)
201 @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $@.log"
202 @$(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; }
203 echo cache/bridge.$(call get,$(*F))
204 echo cache/host.$(call get,$(*F)) cache/iface.$(*F) cache/iface.$(call opp,$(*F))
207 # linkid=$(call linkpart,%)
208 # nodeid=$(call get,%)
209 # iface1=cache/iface.%
210 # iface2=cache/iface.$(call opp,%)
211 cache/link.%: cache/host.$$(call get,%) cache/iface.% cache/iface.$$(call opp,%)
212 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $@.log"
213 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) ovs-vsctl set interface L$(call linkpart,$(*F)) \
214 options:remote_ip=$$(cat cache/host.$(call get,$(*F))) \
215 options:remote_port=$$(cat cache/iface.$(call opp,$(*F))) 2> $@.log \
219 CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
220 clean: $(CLEANTARGETS)
223 .PHONY: clean distclean
227 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
232 # 'virtual' targets in that there's no real file attached
233 define node_shortcuts
234 sshcheck.$(1): cache/sshcheck.$(1) FORCE
235 db.$(1): cache/db.$(1) FORCE
236 switch.$(1): cache/switch.$(1) FORCE
237 start.$(1): cache/start.$(1) FORCE
238 stop.$(1): cache/stop.$(1) FORCE
239 status.$(1): cache/status.$(1) FORCE
240 bridge.$(1): cache/bridge.$(1) FORCE
241 host.$(1): cache/host.$(1) FORCE
242 # switch already depends on db, but well
243 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
246 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
248 define link_shortcuts
249 iface.%: cache/iface.%
253 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
255 #################### convenience, for debugging only
256 # make +foo : prints the value of $(foo)
257 # make ++foo : idem but verbose, i.e. foo=$(foo)
258 ++%: varname=$(subst +,,$@)
260 @echo "$(varname)=$($(varname))"
261 +%: varname=$(subst +,,$@)
263 @echo "$($(varname))"