Skip to content

Commit

Permalink
docker volume dump and restore
Browse files Browse the repository at this point in the history
- docker volume dump/restore
- host-path support volume
- container-remove safely
- image-list
    - parse created
    - fix id
- image-select
  • Loading branch information
nashvi committed Mar 21, 2024
1 parent a8f8074 commit 6479c5f
Showing 1 changed file with 89 additions and 20 deletions.
109 changes: 89 additions & 20 deletions modules/docker/docker.nu
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export def container-list [
id: $r.Id
status: $r.State.Status?
image: $image
created: $r.Created
created: ($r.Created | into datetime)
ports: $p
env: $imgEnv
mounts: $m
Expand All @@ -78,26 +78,51 @@ export def container-list [
}
}

def parse-img [] {
let n = $in | split row ':'
let tag = $n.1? | default 'latest'
let repo = $n.0 | split row '/'
let image = $repo | last
let repo = $repo | range 0..-2 | str join '/'
{image: $image, tag: $tag, repo: $repo}
}

# select image
export def image-select [name] {
let n = $name | parse-img
let imgs = (image-list)
let fs = [image tag repo]
for i in 2..0 {
let r = $imgs | filter {|x|
$fs | range 0..$i | all {|y| ($n | get $y) == ($x | get $y) }
}
if ($r | is-not-empty) {
return ($r | sort-by -r created | first | get name)
}
}
$name
}

# list images
export def image-list [
-n: string@"nu-complete docker ns"
image?: string@"nu-complete docker images"
] {
if ($image | is-empty) {
^$env.docker-cli ...($n | with-flag -n) images
| from ssv -a
let fmt = '{"id":"{{.ID}}", "repo": "{{.Repository}}", "tag":"{{.Tag}}", "size":"{{.Size}}" "created":"{{.CreatedAt}}"}'
^$env.docker-cli ...($n | with-flag -n) images --format $fmt
| lines
| 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 '/'
let x = $x | from json
let img = $x.repo | parse-img
{
repo: $repo
image: $image
tag: $x.TAG
id: $x.'IMAGE ID'
created: $x.CREATED
size: $size
name: $"($x.repo):($x.tag)"
id: $x.id
created: ($x.created | into datetime)
size: ($x.size | into filesize)
repo: $img.repo
image: $img.image
tag: $x.tag
}
}
} else {
Expand All @@ -109,9 +134,14 @@ export def image-list [
let x = $i | split row '='
$a | upsert $x.0 $x.1?
}
let id = if $env.docker-cli == 'nerdctl' {
$r.RepoDigests.0? | split row ':' | get 1 | str substring 0..12
} else {
$r.Id | str substring 0..12
}
{
id: $r.Id
created: $r.Created
id: $id
created: ($r.Created | into datetime)
author: $r.Author
arch: $r.Architecture
os: $r.Os
Expand Down Expand Up @@ -236,7 +266,10 @@ export def container-copy-file [

# remove container
export def container-remove [container: string@"nu-complete docker containers" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ...($n | with-flag -n) container rm -f $container
let cs = ^$env.docker-cli ...($n | with-flag -n) ps -a | from ssv -a | get NAMES
if $container in $cs {
^$env.docker-cli ...($n | with-flag -n) container rm -f $container
}
}


Expand Down Expand Up @@ -306,12 +339,14 @@ export def volume-list [-n: string@"nu-complete docker ns"] {
}

def "nu-complete docker volume" [] {
dvl | get name
^$env.docker-cli volume ls
| from ssv -a
| get 'VOLUME NAME'
}

# create volume
export def volume-create [name: string -n: string@"nu-complete docker ns"] {
^$env.docker-cli ...($n | with-flag -n) volume create
^$env.docker-cli ...($n | with-flag -n) volume create $name
}

# inspect volume
Expand All @@ -324,6 +359,39 @@ export def volume-remove [...name: string@"nu-complete docker volume" -n: string
^$env.docker-cli ...($n | with-flag -n) volume rm ...$name
}

# dump volume
export def volume-dump [
name: string@"nu-complete docker volume"
--image(-i): string='debian'
-n: string@"nu-complete docker ns"
] {
let id = random chars -l 6
^$env.docker-cli ...($n | with-flag -n) ...[
run --rm
-v $"($name):/tmp/($id)"
$image
sh -c $'cd /tmp/($id); tar -zcf - *'
]
}

# restore volume
export def volume-restore [
name: string@"nu-complete docker volume"
--from(-f): string
--image(-i): string='debian'
-n: string@"nu-complete docker ns"
] {
let id = random chars -l 6
let src = random chars -l 6
^$env.docker-cli ...($n | with-flag -n) ...[
run --rm
-v $"($name):/tmp/($id)"
-v $"(host-path $from):/tmp/($src)"
$image
sh -c $'cd /tmp/($id); tar -zxf /tmp/($src)'
]
}

### run
def "nu-complete docker run vol" [] {
[
Expand All @@ -346,8 +414,9 @@ def "nu-complete docker run proxy" [] {
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..)) }
'=' => { $path | str substring 1.. }
'~' => { [ $env.HOME ($path | str substring 2..) ] | path join }
'$' => { $env | get ($path | str substring 1..) }
_ => { [ $env.PWD $path ] | path join }
}
}
Expand Down

0 comments on commit 6479c5f

Please sign in to comment.