4 proj1=$(word 1,$(subst @, ,$(1)))
7 proj2=$(word 2,$(subst @, ,$(1)))
11 get=$(word $(call proj2,$(1)),$(subst -, ,$(call proj1,$(1))))
15 flip=$(if $(findstring 1,$(1)),2,1)
19 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
23 rget=$(call get,$(call opp,$(1)))
26 # can be redefined in conf.mk if that's not the expected behaviour
27 display?=host $(1) aka $(call solve,$(1))
29 #################### set variables after conf.mk
33 SSH_KEY_OPTION ?= -i $(SSH_KEY)
36 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
37 SSH = ssh $(SSH_OPTIONS)
39 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
44 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
45 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
46 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
53 all: $(addprefix L/,$(shell cat links))
57 @echo $(foreach id,$(ALL_NODE_IDS),$(call display,$(id)))
60 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
63 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
66 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
69 ### node-oriented targets
71 @echo "IP lookup for $(call display,$*)"
72 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
75 @echo "Starting db server on $(call display,$*)"
76 @$(SSH) $(HOST_$*) sudo start_ovsdb-server > $@ \
79 cache/switchd.%: cache/db.%
80 @echo "Starting vswitchd on $(call display,$*)"
81 @$(SSH) $(HOST_$*) sudo start_vswitchd > $@ \
84 cache/bridge.%: cache/switchd.%
85 @echo "Creating bridge on $(call display,$*)"
86 @$(SSH) $(HOST_$*) sudo create_bridge $(IP_$*) > $@ \
89 cache/sshcheck.%: FORCE
90 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
91 else echo "ssh on" $(call display,$*) "KO !!!"; fi
93 ### link-oriented targets
94 L/%: cache/link.%@1 cache/link.%@2
96 @echo "Created link $*"
98 U/%: del-iface.%@1 del-iface.%@2
100 @echo "Deleted link $*"
102 del-bridge.%: cache/db.%
103 @echo "Deleting bridge on $(call display,$*)"
104 @if [ -f cache/bridge.$* ]; then \
105 $(SSH) $(HOST_$*) sudo del_bridge $$(cat cache/bridge.$*);\
107 @rm -f cache/bridge.$* \
108 cache/iface.$*-*@1 cache/iface.*-$*@2 \
109 cache/link.$*-*@? cache/link.*-$*@? \
112 del-switchd.%: del-bridge.%
113 @echo "Shutting down switchd on $(call display,$*)"
114 @if [ -f cache/switchd.$* ]; then \
115 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
117 @rm -f cache/switchd.$*
120 @echo "Shutting down db on $(call display,$*)"
121 @if [ -f cache/db.$* ]; then \
122 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
126 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
128 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
130 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
132 shutdown: del-switches del-dbs
134 .PHONY: del-links del-switchds del-dbs shutdown
138 ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
143 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
147 .PHONY: clean distclean
151 del-iface.%: cache/db.$$(call get,%)
152 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
153 @$(SSH) $(HOST_$(call get,$*)) \
154 sudo del_port L$(call proj1,$*)
155 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
159 cache/iface.%: cache/bridge.$$(call get,%)
160 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
161 @$(SSH) $(HOST_$(call get,$*)) \
162 sudo create_port $$(cat $^) L$(call proj1,$*) > $@ \
163 || { rm $@; exit 1; }
165 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
166 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
167 @$(SSH) $(HOST_$(call get,$*)) \
168 sudo ovs-vsctl set interface L$(call proj1,$*) \
169 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
170 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
173 #################### convenience, for debugging only
174 # make +foo : prints the value of $(foo)
175 # make ++foo : idem but verbose, i.e. foo=$(foo)
176 ++%: varname=$(subst +,,$@)
178 @echo "$(varname)=$($(varname))"
179 +%: varname=$(subst +,,$@)
181 @echo "$($(varname))"