add global start stop status targets for handling db+switch
[sliver-openvswitch.git] / planetlab / exp-tool / Makefile
1 include conf.mk
2
3
4 # proj1(x@y) = x
5 proj1=$(word 1,$(subst @, ,$(1)))
6
7 # proj2(x@y) = y
8 proj2=$(word 2,$(subst @, ,$(1)))
9
10 SEP?=-
11 # get(x-y@1) = x
12 # get(x-y@2) = y
13 get=$(word $(call proj2,$(1)),$(subst $(SEP), ,$(call proj1,$(1))))
14
15 # flip(1) = 2
16 # flip(2) = 1
17 flip=$(if $(findstring 1,$(1)),2,1)
18
19 # opp(x-y@1) = x-y@2
20 # opp(x-y@2) = x-y@1
21 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
22
23 # rget(x-y@1) = y
24 # rget(x-y@2) = x
25 rget=$(call get,$(call opp,$(1)))
26
27 solve=$(HOST_$(1))
28 solve_ip=$(IP_$(1))
29 # can be redefined in conf.mk if that's not the expected behaviour
30 display?=host $(1) aka $(call solve,$(1))
31
32 #################### set variables after conf.mk
33 ifeq "$(SSH_KEY)" ""
34 SSH_KEY_OPTION ?=
35 else
36 SSH_KEY_OPTION ?= -i $(SSH_KEY)
37 endif
38
39 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
40 SSH = ssh $(SSH_OPTIONS)
41
42 SUDO ?= sudo -S
43
44 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
45
46 ####################
47 all+init: init all
48 init:
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)
52 .PHONY: all+init init
53
54 FORCE:
55
56 .SECONDARY:
57
58 all: $(addprefix L/,$(shell cat links))
59 .PHONY: all
60
61 # could also do make ++SLICE
62 showslice: ++SLICE FORCE
63
64 shownodes:
65         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
66 .PHONY: shownodes
67
68 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
69 .PHONY: sshchecks
70
71 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
72 .PHONY: dbs
73
74 switchs: $(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
75 .PHONY: switchs
76
77 start: dbs switchs
78 .PHONY: start
79
80 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
81 .PHONY: stop
82
83 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
84 .PHONY: status
85
86 ### node-oriented targets
87 # check ssh connectivity
88 cache/sshcheck.%: FORCE
89         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
90          else echo "ssh on" $(call display,$*) "KO !!!"; fi
91
92 # should probably replace sshcheck
93 cache/status.%: FORCE
94         @echo "=== DB and SWITCH processes on $(call display,$*)"
95         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
96
97 cache/host.%:
98         @echo "IP lookup for $(call display,$*)"
99         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
100
101 cache/db.%:
102         @echo "Starting db server on $(call display,$*) - logs in $@.log"
103         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $@.log && touch $@
104
105 cache/switch.%: cache/db.%
106         @echo "Starting vswitchd on $(call display,$*) - logs in $@.log"
107         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $@.log && touch $@
108
109 cache/bridge.%: cache/switch.%
110         @echo "Creating bridge on $(call display,$*) - logs in $@.log"
111         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(IP_$*) > $@.log && touch $@
112
113 # xxx this probably needs a more thorough cleanup in cache/
114 cache/stop.%: del-bridge.%
115         @echo "Stopping switch & db on $(call display,$*)"
116         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
117
118 ### link-oriented targets
119 L/%: cache/link.%@1 cache/link.%@2
120         @touch $@
121         @echo "Created link $*"
122
123 U/%: del-iface.%@1 del-iface.%@2
124         @rm -f L/$*
125         @echo "Deleted link $*"
126
127 del-bridge.%: cache/db.%
128         @echo "Deleting bridge on $(call display,$*)"
129         @if [ -f cache/bridge.$* ]; then \
130                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $$(cat cache/bridge.$*);\
131          fi
132         @rm -f cache/bridge.$* \
133               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
134               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
135               L/$*$(SEP)*             L/*$(SEP)$*
136
137 del-switch.%: del-bridge.%
138         @echo "Shutting down switch on $(call display,$*)"
139         @if [ -f cache/switch.$* ]; then \
140                 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl exit;\
141          fi
142         @rm -f cache/switch.$*
143
144 del-db.%:
145         @echo "Shutting down db on $(call display,$*)"
146         @if [ -f cache/db.$* ]; then \
147                 $(SSH) $(HOST_$*) $(SUDO) ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
148          fi
149         @rm -f cache/db.$*
150
151 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
152
153 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
154
155 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
156
157 shutdown: del-switches del-dbs
158
159 .PHONY: del-links del-switchs del-dbs shutdown
160
161 ####################
162 graph.dot:
163         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
164 graph.ps: graph.dot
165         dot -Tps < $^ > $@      
166
167 ####################
168 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
169 distclean:
170         rm -rf L U cache
171
172 .PHONY: clean distclean
173
174 .SECONDEXPANSION:
175
176 del-iface.%: cache/db.$$(call get,%)
177         @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
178         @$(SSH) $(HOST_$(call get,$*)) \
179                 $(SUDO) sliver-ovs del-port L$(call proj1,$*)
180         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
181
182
183
184 cache/iface.%: cache/bridge.$$(call get,%)
185         @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
186         @$(SSH) $(HOST_$(call get,$*)) \
187                 $(SUDO) sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
188          || { rm $@; exit 1; }
189
190 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
191         @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
192         @$(SSH) $(HOST_$(call get,$*)) \
193                 $(SUDO) ovs-vsctl set interface L$(call proj1,$*) \
194                         options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
195                         options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
196          && touch $@
197
198 #################### convenience, for debugging only
199 # make +foo : prints the value of $(foo)
200 # make ++foo : idem but verbose, i.e. foo=$(foo)
201 ++%: varname=$(subst +,,$@)
202 ++%:
203         @echo "$(varname)=$($(varname))"
204 +%: varname=$(subst +,,$@)
205 +%:
206         @echo "$($(varname))"