forked from piontec/netperf-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NETPERF.rsc
384 lines (345 loc) · 14.5 KB
/
NETPERF.rsc
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
###
### Installer for "NETPERF"
###
:put "** Loading \$NETPERF"
:global NETPERF
:set NETPERF do={
:global NETPERF
:local arg1 $1
:local arg2 $2
:local action 0
:local lpath
:local lbranch
:if ([:typeof $arg1]="str") do={
:set action $arg1
}
:if ([:typeof $path]="str") do={
:set lpath $path
:put "setting path=$path"
}
:if ([:typeof $branch]="str") do={
:set lbranch $branch
}
## MAIN SETTINGS FOR CONTAINER
# name of container, used in comment to find - could be multiple so add a "containername1[2,...]" to things
:local ocipkg "netperf"
:local ocipushuser "tikoci"
:local containerregistry "ghcr.io"
:local scripthelpername "NETPERF"
:local containernum "1"
:local containeripbase "198.19.101."
:local containerprefix "24"
:local containerver "master"
:local containerbootatstart "yes"
:local containeraddresslist "LAN"
:local containerhostip "254"
:local containerbridge ""
:local containerlogging "yes"
:local containerenvs [:toarray ""]
:set ($containerenvs->"PORT") "12865"
:set ($containerenvs->"SERVER") "198.18.18.18"
:set ($containerenvs->"PORT") "12865"
:set ($containerenvs->"DURATION") "60"
:set ($containerenvs->"FORMAT") "m"
:set ($containerenvs->"INTERVAL") "10"
:local containermounts [:toarray ""]
# TODO figure out where files go...
# container specific variables
:local netserverport ($containerenvs->"PORT")
# calculate name and tag (do not change these)
:local containername "$ocipkg"
:local containertag "$containername$containernum"
:local ociprojsrcroot "https://raw.githubusercontent.com/$(ocipushuser)/$(containername)/$containerver"
# RouterOS IP config
:local containerethname "veth-$containertag"
:local containerip "$containeripbase$containernum"
:local containergw "$($containeripbase)254"
# path= option
:local rootdisk ""
:if ([:typeof $lpath]="str") do={
:set rootdisk $lpath
} else={
:set rootdisk "nfs1"
}
:local rootpath "$rootdisk/$containertag"
:put "using disk path prefix of $rootpath, use path= option to change"
# branch= option
:if ([:typeof $branch]="str") do={
:set containerver $branch
}
:if ($dump = "yes") do={
:put "NETPERF is using..."
:put " branch (ARG) = $branch "
:put " force (ARG) = $force "
:put " path (ARG) = $path "
:put " url (ARG) = $url "
:put " rootpath = $rootpath "
:put " ocipkg = $ocipkg "
:put " ocipushuser = $ocipushuser "
:put " ociprojsrcroot = $ociprojsrcroot "
:put " containerregistry = $containerregistry "
:put " scripthelpername = $scripthelpername "
:put " containernum = $containernum "
:put " containeripbase = $containeripbase "
:put " containerprefix = $containerprefix "
:put " containerver = $containerver "
:put " containerbootatstart = $containerbootatstart "
:put " containeraddresslist = $containeraddresslist "
:put " containerhostip = $containerhostip "
:put " containerbridge = $containerbridge "
:put " containerenvs = $containerenvs "
:put " containermounts = $containermounts "
:put " containerethname = $containerethname "
:put " containerip = $containerip "
:put " containergw = $containergw "
:put " rootdisk = $rootdisk "
:put " rootpath = $rootpath "
}
# "$NETPERF build" - removes any existing and install new container
:if ($action = "make") do={
## WARN BEFORE CONTINUE
:put "continuing will install $containertag and modify configuration"
:put "...starting in 5 seconds - hit ctrl-c now to STOP"
:delay 5s
# add veth
/interface/veth {
:put "check veth"
:local veth [add name="$containerethname" address="$containerip/$containerprefix" gateway=$containergw comment="#$containertag"]
:put "added VETH - $containerethname address=$(containerip)/$(containerprefix) gateway=$containergw "
}
# envs= option
/container/envs {
:put "check envs"
:foreach k,v in=$containerenvs do={
:put "setting $containertag env $k to $v"
add name="$containertag" key="$k" value=$v comment="#$containertag"
}
}
# mounts= option
/container/mounts {
:put "check mounts"
:foreach k,v in=$containermounts do={
:put "setting $containertag env $k to $v"
add name="$containertag" src="$rootpath-$[:tostr $k]" dst="$[:tostr $v]" comment="#$containertag"
}
}
# add ip address to routeros
/ip/address {
:put "check hostip"
:if ([:typeof [:tonum $containerhostip]] = "num") do={
:local ipaddr [add interface="$containerethname" address="$(containergw)/$(containerprefix)" comment="#$containertag"]
:put "added IP address=$(containergw)/$(containerprefix) interface=$containerethname"
}
}
# TODO handle bridge!=""
/interface/list/member {
:if ([:len $containeraddresslist] > 0) do={
:local iflistmem [add interface="$containerethname" list="$containeraddresslist" comment="#$containertag"]
:put "added $containerethname to $containeraddresslist interface list"
}
}
# TODO figure out if any are needed?
/ip/firewall/nat {}
/ip/firewall/filter {}
/ip/route {}
/container {
:local containerid
# tarfile= option
:if ([:typeof $tarfile]="str") do={
:put "adding new $containertag container on $containerethname using $(rootdisk)/$(containername).tar"
:set containerid [add file="$tarfile" interface="$containerethname" logging=$containerlogging root-dir="$(rootpath)-root"]
} else={
:local lastreg [$NETPERF registry github]
# TODO handle no building paths here if options messing
:local containerpulltag "$(containerregistry)/$(ocipushuser)/$(containername):$(containerver)"
:put "pulling new $containertag container on $containerethname using $containerpulltag"
:set containerid [add remote-image="$containerpulltag" interface="$containerethname" logging=$containerlogging root-dir="$(rootpath)-root"]
[$NETPERF registry url=$lastreg]
}
set $containerid comment="#$containertag"
set $containerid start-on-boot=$containerbootatstart
set $containerid mounts=[/container/mounts find comment~"#$containertag"]
:if [/container/envs find name="$containertag"] do={
set $containerid env="$containertag"
}
[$NETPERF start]
}
/ {
:put "** done"
}
:return ""
}
:if ($action = "start") do={
/container {
:local waitstart [:timestamp]
:local startrequested 0
:local containerid [find comment~"#$containertag"]
:while ([get $containerid status]!="running") do={
:put "$containertag is $[get $containerid status]";
:if ([get $containerid status] = "error") do={
:error "opps! some error importing container"
}
:delay 3s
:if ([get $containerid status] = "stopped" && $startrequested = 0) do={
:put "$containertag sending start";
:do {
start $containerid;
:set startrequested 1
} on-error={}
}
:delay 7s
:if ( [:timestamp] > ($waitstart+[:totime 90s]) ) do={
/log print proplist=
:put "opps. took too long..."
:put "dumping logs..."
/log print proplist=message where topics~"container"
:error "opps. timeout while waiting for start. check logs above for clues and retry build."
}
}
:if ([get $containerid status] = "running") do={
:put "$containertag started"
} else={
:error "$containertag failed to start"
}
}
/ {
:return ""
}
}
:if ($action = "registry") do={
/container/config {
:local curregurl [get registry-url]
:if ([:typeof $url]="str") do={
:put "registry set to provided url: $url"
set registry-url=$url
/;
:return $curregurl
}
:if ([:typeof $arg2]="str") do={
:if ($arg2~"github|ghcr") do={
set registry-url="https://ghcr.io"
:put "registry updated from $curregurl to GitHub Container Store (ghcs.io)"
/;
:return $curregurl
}
:if ($arg2~"docker") do={
set registry-url="https://registry-1.docker.io"
:put "registry updated from $curregurl to Docker Hub"
/;
:return $curregurl
} else={
:error "setting invalid or unknown registry - failing"
}
} else={
:put "current container registry is: $curregurl"
/;
:return $curregurl
}
}
:error "unhandled path in \$NETPERF registry - should return something"
}
:if ($action = "stop") do={
/container {
:local activeid [find comment~"#$containertag"]
#:local activecontainer [get $activeid ]
:foreach containerinstance in=$activeid do={
:put "$containertag found existing container to stop..."
:while (!([get $containerinstance status]~"stopped|error")) do={
:do { stop $containerinstance } on-error={}
:delay 5s
:put "$containerinstance awaiting waiting stop..."
}
}
}
:return ""
}
:if ($action = "clean") do={
/container {
:put "$containertag removing any existing container..."
:local containerexisting [find comment~"#$containertag"]
:if ([:len $containerexisting] > 0) do={
$NETPERF stop
:delay 1s
remove $containerexisting
:put "old container $containerinstance stopped and removed"
} else={
:put "no existing container found to remove"
}
}
/interface/veth {
:local rveth [remove [find comment~"#$containertag"]]
:put "$containertag removing veth $rveth"
}
/ip/address {
:local ripaddr [remove [find comment~"#$containertag"]]
:put "$containertag removing ip address from router $ripaddr"
}
/container/envs {
:local renvs [remove [find comment~"#$containertag"]]
:put "$containertag removing envs $renvs"
}
/container/mounts {
:local rmounts [remove [find comment~"#$containertag"]]
:put "$containertag removing mounts $rmounts"
}
/interface/list/member {
remove [find comment~"$containertag"]
:put "$containertag removing from interface list"
}
/ip/firewall/nat {
:local rdstnats [remove [find comment~"#$containertag"]]
:put "$containertag removing dst-nats $rdstnats"
}
/ip/firewall/filter {}
/ip/route {}
/ {
:put "$containertag $containerinstance clean done"
:return ""
}
}
:if ($action = "shell") do={
/container {
:local activeid [find comment~"#$containertag"]
:if ([:len $activeid] < 1) do={
:error "$containertag could not find the container, shell is not possible"
}
:if ([get $activeid "status"] != "running") do={
:put "container is not running. force=$force"
$NETPERF stop
:local oldcmd [get $activeid cmd]
:if ([:tostr $force] = "yes") do={
:put "saving old cmd $oldcmd"
set $activeid cmd="tail -f /dev/null"
:put "attempting start with force=$force with 'tail'"
$NETPERF start
:put "started, connected without builtin cmd"
:do { shell $activeid } on-error={}
:put "back from shell, resetting $oldcmd"
set $activeid cmd=$oldcmd
:return ""
} else={
:put "container is not running, starting for shell"
$NETPERF start
}
}
:put "starting shell"
:do { shell $activeid } on-error={}
:put "back from shell"
}
:return ""
}
:put "Usage: \$$(scripthelpername) make|clean|shell|dump|start|stop|registry "
:error "Bad Command: see docs at https://github.com/$(ocipushuser)/$(containername)"
}
# Some examples:
# To build:
# $NETPERF clean
# $NETPERF make [path=<disk>] [branch=<tagver>]
# To control/manage
# $NETPERF stop
# $NETPERF start
# $NETPERF shell force=["no"|"yes"]
# Helper to switch between DockerHub and GitHub Container Registry
# $NETPERF registry
# $NETPERF registry <github|docker> [url=<str>]
$NETPERF dump=yes