-
Notifications
You must be signed in to change notification settings - Fork 14
/
scripts.go
193 lines (157 loc) · 4.77 KB
/
scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2021 The Everoute Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package netns
import (
"bytes"
"fmt"
"strconv"
"strings"
"golang.org/x/crypto/ssh"
)
const (
startNewEndpoint = `
set -o errexit
set -o pipefail
set -o nounset
set -o xtrace
netns=${1}
bridgeName=${2}
ipAddr=${3}
tcpPorts=${4:-}
udpPorts=${5:-}
vlanTag=${6:-[]}
proto=${7:-}
vethName="veth-${netns}"
portName=${vethName}
vethPeerName="vethpeer-${netns}"
portExternalIDName="iface-id"
portExternalIDValue="uuid-${netns}"
ip netns add ${netns}
ip link add ${vethName} type veth peer name ${vethPeerName}
ip link set ${vethName} up
ip link set ${vethPeerName} netns ${netns}
ip netns exec ${netns} ip link set lo up
ip netns exec ${netns} ip link set ${vethPeerName} up
ip netns exec ${netns} ip a add ${ipAddr} dev ${vethPeerName}
attached_mac=$(ip netns exec ${netns} cat /sys/class/net/${vethPeerName}/address)
ovs-vsctl add-port ${bridgeName} ${portName} tag=${vlanTag} \
-- set interface ${portName} external_ids=${portExternalIDName}=${portExternalIDValue} \
-- set interface ${portName} external_ids:attached-mac="${attached_mac}"
execCommand="ip netns exec ${netns} net-utils server -d -s"
if [[ ${tcpPorts} != 0 ]]; then
execCommand="${execCommand} --tcp-ports ${tcpPorts}"
fi
if [[ ${udpPorts} != 0 ]]; then
execCommand="${execCommand} --udp-ports ${udpPorts}"
fi
if [[ ${proto} = "FTP" ]]; then
ip=${ipAddr%/*}
execCommand="${execCommand} --ftp-server ${ip}"
fi
eval ${execCommand}
`
updateEndpointIP = `
set -o errexit
set -o pipefail
set -o nounset
set -o xtrace
netns=${1}
ipAddr=${2}
vethPeerName="vethpeer-${netns}"
ip netns exec ${netns} ip a flush dev ${vethPeerName}
ip netns exec ${netns} ip a add ${ipAddr} dev ${vethPeerName}
`
updateEndpointPort = `
set -o errexit
set -o pipefail
set -o nounset
set -o xtrace
netns=${1}
tcpPorts=${2:-}
udpPorts=${3:-}
## clean old net-utils process
comm -12 <(pidof net-utils | tr " " "\n" | sort) <(ip netns pid ${netns} | sort) | xargs -rl kill -9
execCommand="ip netns exec ${netns} net-utils server -d -s"
if [[ ${tcpPorts} != 0 ]]; then
execCommand="${execCommand} --tcp-ports ${tcpPorts}"
fi
if [[ ${udpPorts} != 0 ]]; then
execCommand="${execCommand} --udp-ports ${udpPorts}"
fi
eval ${execCommand}
`
destroyEndpoint = `
set -o nounset
set -o xtrace
netns=${1}
vethName="veth-${netns}"
portName=${vethName}
kill -9 "$(ip netns pids ${netns})"
ovs-vsctl del-port ${portName}
ip netns del ${netns}
ip link del ${vethName} || true
`
)
func runStartNewEndpoint(client *ssh.Client, netns, bridgeName string, ipAddr string, tcpPort, udpPort int, vlanTag int, proto string) error {
rc, out, err := runScriptRemote(client, startNewEndpoint, netns, bridgeName, ipAddr, strconv.Itoa(tcpPort), strconv.Itoa(udpPort), strconv.Itoa(vlanTag), proto)
if err != nil {
return err
}
if rc != 0 {
return fmt.Errorf("exit code: %d, output: %s", rc, out)
}
return nil
}
func runUpdateEndpointIP(client *ssh.Client, netns string, ipaddr string) error {
rc, out, err := runScriptRemote(client, updateEndpointIP, netns, ipaddr)
if err != nil {
return err
}
if rc != 0 {
return fmt.Errorf("exit code: %d, output: %s", rc, out)
}
return nil
}
func runUpdateEndpointPort(client *ssh.Client, netns string, tcpPort, udpPort int) error {
rc, out, err := runScriptRemote(client, updateEndpointPort, netns, strconv.Itoa(tcpPort), strconv.Itoa(udpPort))
if err != nil {
return err
}
if rc != 0 {
return fmt.Errorf("exit code: %d, output: %s", rc, out)
}
return nil
}
func runDestroyEndpoint(client *ssh.Client, netns string) error {
rc, out, err := runScriptRemote(client, destroyEndpoint, netns)
if err != nil {
return err
}
if rc != 0 {
return fmt.Errorf("exit code: %d, output: %s", rc, out)
}
return nil
}
func runScriptRemote(client *ssh.Client, script string, arg ...string) (int, []byte, error) {
session, err := client.NewSession()
if err != nil {
return 0, nil, err
}
defer session.Close()
session.Stdin = bytes.NewBufferString(script)
out, err := session.CombinedOutput("bash -s " + strings.Join(arg, " "))
if _, ok := err.(*ssh.ExitError); ok {
return err.(*ssh.ExitError).ExitStatus(), out, nil
}
return 0, out, err
}