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 = ssh $(SSH_OPTIONS)
44 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
49 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
50 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
51 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
58 all: $(addprefix L/,$(shell cat links))
61 # could also do make ++SLICE
62 showslice: ++SLICE FORCE
65 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
68 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
71 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
74 switchs: $(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
77 ### node-oriented targets
79 @echo "IP lookup for $(call display,$*)"
80 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
83 @echo "Starting db server on $(call display,$*)"
84 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db > $@ \
87 cache/switch.%: cache/db.%
88 @echo "Starting vswitchd on $(call display,$*)"
89 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch > $@ \
92 cache/bridge.%: cache/switch.%
93 @echo "Creating bridge on $(call display,$*)"
94 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(IP_$*) > $@ \
97 cache/sshcheck.%: FORCE
98 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
99 else echo "ssh on" $(call display,$*) "KO !!!"; fi
101 ### link-oriented targets
102 L/%: cache/link.%@1 cache/link.%@2
104 @echo "Created link $*"
106 U/%: del-iface.%@1 del-iface.%@2
108 @echo "Deleted link $*"
110 del-bridge.%: cache/db.%
111 @echo "Deleting bridge on $(call display,$*)"
112 @if [ -f cache/bridge.$* ]; then \
113 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $$(cat cache/bridge.$*);\
115 @rm -f cache/bridge.$* \
116 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
117 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
118 L/$*$(SEP)* L/*$(SEP)$*
120 del-switch.%: del-bridge.%
121 @echo "Shutting down switch on $(call display,$*)"
122 @if [ -f cache/switch.$* ]; then \
123 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl exit;\
125 @rm -f cache/switch.$*
128 @echo "Shutting down db on $(call display,$*)"
129 @if [ -f cache/db.$* ]; then \
130 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
134 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
136 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
138 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
140 shutdown: del-switches del-dbs
142 .PHONY: del-links del-switchs del-dbs shutdown
146 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
151 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
155 .PHONY: clean distclean
159 del-iface.%: cache/db.$$(call get,%)
160 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
161 @$(SSH) $(HOST_$(call get,$*)) \
162 $(SUDO) sliver-ovs del-port L$(call proj1,$*)
163 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
167 cache/iface.%: cache/bridge.$$(call get,%)
168 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
169 @$(SSH) $(HOST_$(call get,$*)) \
170 $(SUDO) sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
171 || { rm $@; exit 1; }
173 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
174 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
175 @$(SSH) $(HOST_$(call get,$*)) \
176 $(SUDO) ovs-vsctl set interface L$(call proj1,$*) \
177 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
178 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
181 #################### convenience, for debugging only
182 # make +foo : prints the value of $(foo)
183 # make ++foo : idem but verbose, i.e. foo=$(foo)
184 ++%: varname=$(subst +,,$@)
186 @echo "$(varname)=$($(varname))"
187 +%: varname=$(subst +,,$@)
189 @echo "$($(varname))"