forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.nu
More file actions
454 lines (407 loc) · 13.8 KB
/
docker.nu
File metadata and controls
454 lines (407 loc) · 13.8 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
export-env {
for c in [nerdctl podman docker] {
if not (which $c | is-empty) {
$env.docker-cli = $c
break
}
}
}
def sprb [flag, args] {
if $flag {
$args
} else {
[]
}
}
def spr [args] {
let lst = ($args | last)
if ($lst | is-empty) {
[]
} else {
let init = ($args | range ..-2)
if ($init | is-empty) {
[ $lst ]
} else {
$init | append $lst
}
}
}
def local_image [name] {
let s = ($name | split row '/')
if ($s | length) > 1 {
$name
} else {
['localhost', $name] | str join '/'
}
}
def "nu-complete docker ns" [] {
if $env.docker-cli == 'nerdctl' {
^$env.docker-cli namespace list
| from ssv -a
| each {|x| { value: $x.NAMES }}
} else {
[]
}
}
# list containers
export def container-process-list [-n: string@"nu-complete docker ns"] {
# ^$env.docker-cli ps --all --no-trunc --format='{{json .}}' | jq
let cli = $env.docker-cli
if $cli == 'docker' {
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}'
| lines
| each {|x|
let r = ($x | from json)
let t = ($r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z' )
$r | upsert created $t
}
} else if $cli == 'podman' {
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":"{{.Command}}", "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.Created}}"}'
| lines
| each {|x|
let r = ($x | from json)
let t = ($r.created | str substring ..32 | into datetime )
$r | upsert created $t
}
} else {
^$cli (spr [-n $n]) ps -a
| from ssv
| rename id image cmd created status port name
}
}
# list images
export def image-list [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) images
| from ssv -a
| each {|x|
let size = ($x.SIZE | into filesize)
let path = ($x.REPOSITORY | split row '/')
let image = ($path | last)
let repo = ($path | range ..(($path|length) - 2) | str join '/')
{
repo: $repo
image: $image
tag: $x.TAG
id: $x.'IMAGE ID'
created: $x.CREATED
size: $size
}
}
}
def "nu-complete docker ps" [] {
^$env.docker-cli ps
| from ssv -a
| each {|x| {description: $x.NAMES value: $x.'CONTAINER ID'}}
}
def "nu-complete docker container" [] {
^$env.docker-cli ps
| from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}}
}
def "nu-complete docker all container" [] {
^$env.docker-cli ps -a
| from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}}
}
def "nu-complete docker images" [] {
^$env.docker-cli images
| from ssv
| each {|x| $"($x.REPOSITORY):($x.TAG)"}
}
# container log
export def container-log [ctn: string@"nu-complete docker container"
-l: int = 100 # line
] {
let l = if $l == 0 { [] } else { [--tail $l] }
^$env.docker-cli logs -f $l $ctn
}
# container log with namespace
export def container-log-namespace [ctn: string@"nu-complete docker container"
-l: int = 100 # line
-n: string@"nu-complete docker ns" # namespace
] {
let l = if $l == 0 { [] } else { [--tail $l] }
^$env.docker-cli (spr [-n $n]) logs -f $l $ctn
}
# attach container
export def container-attach [
ctn: string@"nu-complete docker container"
-n: string@"nu-complete docker ns"
...args
] {
let ns = (spr [-n $n])
if ($args|is-empty) {
^$env.docker-cli $ns exec -it $ctn /bin/sh -c "[ -e /bin/zsh ] && /bin/zsh || [ -e /bin/bash ] && /bin/bash || /bin/sh"
} else {
^$env.docker-cli $ns exec -it $ctn $args
}
}
def "nu-complete docker cp" [cmd: string, offset: int] {
let argv = ($cmd | str substring ..$offset | split row ' ')
let p = if ($argv | length) > 2 { $argv | get 2 } else { $argv | get 1 }
let ctn = (
^$env.docker-cli ps
| from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $"($x.NAMES):" }}
)
let n = ($p | split row ':')
if $"($n | get 0):" in ($ctn | get value) {
^$env.docker-cli exec ($n | get 0) sh -c $"ls -dp ($n | get 1)*"
| lines
| each {|x| $"($n | get 0):($x)"}
} else {
let files = (do -i {
ls -a $"($p)*"
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
})
$files | append $ctn
}
}
# copy file
export def container-copy-file [
lhs: string@"nu-complete docker cp",
rhs: string@"nu-complete docker cp"
] {
^$env.docker-cli cp $lhs $rhs
}
# remove container
export def container-remove [ctn: string@"nu-complete docker all container" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) container rm -f $ctn
}
# inspect
export def container-inspect [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) inspect $img
}
# history
export def container-history [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) history --no-trunc $img | from ssv -a
}
# save images
export def image-save [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] {
^$env.docker-cli (spr [-n $n]) save $img
}
# load images
export def image-load [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) load
}
# system prune
export def system-prune [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) system prune -f
}
# system prune all
export def system-prune-all [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) system prune --all --force --volumes
}
# remove image
export def image-remove [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) rmi $img
}
# add new tag
export def image-tag [from: string@"nu-complete docker images" to: string -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) tag $from $to
}
# push image
export def image-push [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) push $img
}
# pull image
export def image-pull [img -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) pull $img
}
### list volume
export def volume-list [-n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) volume ls | from ssv -a
}
def "nu-complete docker volume" [] {
dvl | get name
}
# create volume
export def volume-create [name: string -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) volume create
}
# inspect volume
export def volume-inspect [name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) volume inspect $name
}
# remove volume
export def volume-remove [...name: string@"nu-complete docker volume" -n: string@"nu-complete docker ns"] {
^$env.docker-cli (spr [-n $n]) volume rm $name
}
### run
def "nu-complete docker run vol" [] {
[
$"($env.PWD):/world"
$"($env.PWD):/app"
$"($env.PWD):/srv"
$"($env.HOME)/.config/nvim:/etc/nvim"
]
}
def "nu-complete docker run sshkey" [ctx: string, pos: int] {
(do { cd ~/.ssh; ls **/*.pub } | get name)
}
def "nu-complete docker run proxy" [] {
let hostaddr = (do -i { hostname -I | split row ' ' | get 0 })
[ $"http://($hostaddr):7890" $"http://($hostaddr):" ]
}
def host-path [path] {
match ($path | str substring ..1) {
'/' => { $path }
'~' => { [ $nu.home-path ($path | str substring 2..) ] | path join }
'$' => { ($env | get ($path | str substring 1..)) }
_ => { [ $env.PWD $path ] | path join }
}
}
# run
export def container-create [
--debug(-x)
--appimage
--netadmin
--proxy: string@"nu-complete docker run proxy" # proxy
--ssh(-s): string@"nu-complete docker run sshkey" # specify ssh key
--sshuser: string=root # default root
--cache(-c): string # cache
--mnt(-m): string@"nu-complete docker run vol" # mnt
--vols(-v): any # { host: container }
--ports(-p): any # { 8080: 80 }
--envs(-e): any # { FOO: BAR }
--daemon(-d)
--attach(-a): string@"nu-complete docker container" # attach
--workdir(-w): string # workdir
--entrypoint: string # entrypoint
--dry-run
--with-x
--privileged(-P)
--namespace(-n): string@"nu-complete docker ns"
img: string@"nu-complete docker images" # image
...cmd # command args
] {
let ns = (spr [-n $namespace])
let entrypoint = (spr [--entrypoint $entrypoint])
let daemon = if $daemon { [-d] } else { [--rm -it] }
let mnt = (spr [-v $mnt])
let workdir = (spr [-w $workdir])
let vols = if ($vols|is-empty) { [] } else { $vols | transpose k v | each {|x| [-v $"(host-path $x.k):($x.v)"]} | flatten }
let envs = if ($envs|is-empty) { [] } else { $envs | transpose k v | each {|x| [-e $"($x.k)=($x.v)"]} | flatten }
let ports = if ($ports|is-empty) { [] } else { $ports | transpose k v | each {|x| [-p $"($x.k):($x.v)"] } | flatten }
let debug = (sprb $debug [--cap-add=SYS_ADMIN --cap-add=SYS_PTRACE --security-opt seccomp=unconfined])
#let appimage = (sprb $appimage [--device /dev/fuse --security-opt apparmor:unconfined])
let privileged = (sprb $privileged [--privileged])
let appimage = (sprb $appimage [--device /dev/fuse])
let netadmin = (sprb $netadmin [--cap-add=NET_ADMIN --device /dev/net/tun])
let clip = (sprb $with_x [-e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix])
let ssh = if ($ssh|is-empty) { [] } else {
let sshkey = (cat ([$env.HOME .ssh $ssh] | path join) | split row ' ' | get 1)
[-e $"ed25519_($sshuser)=($sshkey)"]
}
let proxy = if ($proxy|is-empty) { [] } else {
[-e $"http_proxy=($proxy)" -e $"https_proxy=($proxy)"]
}
let attach = if ($attach|is-empty) { [] } else {
let c = $"container:($attach)"
[--uts $c --ipc $c --pid $c --network $c]
}
let cache = (spr [-v $cache])
let args = ([
$privileged $entrypoint $attach $daemon
$ports $envs $ssh $proxy
$debug $appimage $netadmin $clip
$mnt $vols $workdir $cache
] | flatten)
let name = $"($img | split row '/' | last | str replace ':' '-')_(date now | format date %m%d%H%M)"
if $dry_run {
echo $"docker ($ns | str join ' ') run --name ($name) ($args|str join ' ') ($img) ($cmd | flatten)"
} else {
let $img = if $env.docker-cli == 'nerdctl' { local_image $img } else { $img }
^$env.docker-cli $ns run --name $name $args $img ($cmd | flatten)
}
}
def has [name] {
$name in ($in | columns) and (not ($in | get $name | is-empty))
}
def "nu-complete registry show" [cmd: string, offset: int] {
let new = ($cmd | str ends-with ' ')
let cmd = ($cmd | split row ' ')
let url = (do -i { $cmd | get 2 })
let reg = (do -i { $cmd | get 3 })
let tag = (do -i { $cmd | get 4 })
let auth = if ($env | has 'REGISTRY_TOKEN') {
[authorization $"Basic ($env.REGISTRY_TOKEN)"]
} else {
[]
}
if ($tag | is-empty) and (not $new) or ($reg | is-empty) {
http get -H $auth $"($url)/v2/_catalog"
| get repositories
} else {
http get -H $auth $"($url)/v2/($reg)/tags/list"
| get tags
}
}
### docker registry show
export def "registry show" [
url: string
reg?: string@"nu-complete registry show"
tag?: string@"nu-complete registry show"
] {
let auth = if ($env | has 'REGISTRY_TOKEN') {
[authorization $"Basic ($env.REGISTRY_TOKEN)"]
} else {
[]
}
if ($reg | is-empty) {
http get -H $auth $"($url)/v2/_catalog" | get repositories
} else if ($tag | is-empty) {
http get -H $auth $"($url)/v2/($reg)/tags/list" | get tags
} else {
http get -e -H [accept 'application/vnd.oci.image.manifest.v1+json'] -H $auth $"($url)/v2/($reg)/manifests/($tag)" | from json
}
}
### buildah
export def "bud img" [] {
buildah images
| from ssv -a
| rename repo tag id created size
| upsert size { |i| $i.size | into filesize }
}
export def "bud ls" [] {
buildah list
| from ssv -a
| rename id builder image-id image container
}
export def "bud ps" [] {
buildah ps
| from ssv -a
| rename id builder image-id image container
}
def "nu-complete bud ps" [] {
bud ps
| select 'CONTAINER ID' "CONTAINER NAME"
| rename value description
}
export def "bud rm" [
id: string@"nu-complete bud ps"
] {
buildah rm $id
}
export alias dp = container-process-list
export alias di = image-list
export alias dl = container-log
export alias dln = container-log-namespace
export alias da = container-attach
export alias dcp = container-copy-file
export alias dcr = container-remove
export alias dci = container-inspect
export alias dh = container-history
export alias dsv = image-save
export alias dld = image-load
export alias dsp = system-prune
export alias dspall = system-prune-all
export alias drmi = image-remove
export alias dt = image-tag
export alias dps = image-push
export alias dpl = image-pull
export alias dvl = volume-list
export alias dvc = volume-create
export alias dvi = volume-inspect
export alias dvr = volume-remove
export alias dr = container-create