5 proj1=$(word 1,$(subst @, ,$(1)))
8 proj2=$(word 2,$(subst @, ,$(1)))
13 get=$(word $(call proj2,$(1)),$(subst $(SEP), ,$(call proj1,$(1))))
17 flip=$(if $(findstring 1,$(1)),2,1)
21 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
25 rget=$(call get,$(call opp,$(1)))
29 # can be redefined in conf.mk if that's not the expected behaviour
30 display?=host $(1) aka $(call solve,$(1))
32 #################### set variables after conf.mk
36 SSH_KEY_OPTION ?= -i $(SSH_KEY)
39 #SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
40 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
41 SSH = ssh $(SSH_OPTIONS)
45 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
50 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
51 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
52 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
59 all: $(addprefix L/,$(shell cat links))
62 # could also do make ++SLICE
63 showslice: ++SLICE FORCE
66 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
68 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
69 .PHONY: shownodes showips
71 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
74 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
77 switchs: $(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
83 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
86 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
89 ### node-oriented targets
90 # check ssh connectivity
91 cache/sshcheck.%: FORCE
92 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
93 else echo "ssh on" $(call display,$*) "KO !!!"; fi
95 # should probably replace sshcheck
97 @echo "=== DB and SWITCH processes on $(call display,$*)"
98 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
101 @echo "IP lookup for $(call display,$*)"
102 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
105 @echo "Starting db server on $(call display,$*) - logs in $@.log"
106 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $@.log && touch $@
108 cache/switch.%: cache/db.%
109 @echo "Starting vswitchd on $(call display,$*) - logs in $@.log"
110 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $@.log && touch $@
112 cache/bridge.%: cache/switch.%
113 @echo "Creating bridge on $(call display,$*) - logs in $@.log"
114 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(IP_$*) > $@ 2> $@.log || { rm $@; exit 1; }
116 # xxx this probably needs a more thorough cleanup in cache/
117 cache/stop.%: del-bridge.%
118 @echo "Stopping switch & db on $(call display,$*)"
119 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
121 ### link-oriented targets
122 L/%: cache/link.%@1 cache/link.%@2
124 @echo "Created link $*"
126 U/%: del-iface.%@1 del-iface.%@2
128 @echo "Deleted link $*"
130 del-bridge.%: cache/db.%
131 @echo "Deleting bridge on $(call display,$*)"
132 @if [ -f cache/bridge.$* ]; then \
133 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $$(cat cache/bridge.$*);\
135 @rm -f cache/bridge.$* \
136 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
137 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
138 L/$*$(SEP)* L/*$(SEP)$*
140 del-switch.%: del-bridge.%
141 @echo "Shutting down switch on $(call display,$*)"
142 @if [ -f cache/switch.$* ]; then \
143 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl exit;\
145 @rm -f cache/switch.$*
148 @echo "Shutting down db on $(call display,$*)"
149 @if [ -f cache/db.$* ]; then \
150 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
154 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
156 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
158 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
160 shutdown: del-switches del-dbs
162 .PHONY: del-links del-switchs del-dbs shutdown
166 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
171 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
175 .PHONY: clean distclean
179 del-iface.%: cache/db.$$(call get,%)
180 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
181 @$(SSH) $(HOST_$(call get,$*)) \
182 $(SUDO) sliver-ovs del-port L$(call proj1,$*)
183 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
187 cache/iface.%: cache/bridge.$$(call get,%)
188 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
189 @$(SSH) $(HOST_$(call get,$*)) \
190 $(SUDO) sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
191 || { rm $@; exit 1; }
193 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
194 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
195 @$(SSH) $(HOST_$(call get,$*)) \
196 $(SUDO) ovs-vsctl set interface L$(call proj1,$*) \
197 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
198 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
202 # 'virtual' targets in that there's no real file attached
203 define node_shortcuts
204 sshcheck.$(1): cache/sshcheck.$(1) FORCE
205 db.$(1): cache/db.$(1) FORCE
206 switch.$(1): cache/switch.$(1) FORCE
207 start.$(1): cache/start.$(1) FORCE
208 stop.$(1): cache/stop.$(1) FORCE
209 status.$(1): cache/status.$(1) FORCE
210 bridge.$(1): cache/bridge.$(1) FORCE
211 # switch already depends on db, but well
212 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
215 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
217 #define link_shortcuts
221 #################### convenience, for debugging only
222 # make +foo : prints the value of $(foo)
223 # make ++foo : idem but verbose, i.e. foo=$(foo)
224 ++%: varname=$(subst +,,$@)
226 @echo "$(varname)=$($(varname))"
227 +%: varname=$(subst +,,$@)
229 @echo "$($(varname))"