From 05d79e8ba6bc2e594a556571f3c3ef20a5bf4adf Mon Sep 17 00:00:00 2001 From: Helder Correia <174525+helderco@users.noreply.github.com> Date: Fri, 25 Mar 2022 15:17:11 -0100 Subject: [PATCH] Add docker cli package Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com> --- docs/drafts/1216-docker-cli-load.md | 20 ++++ docs/drafts/1216-engine-load.md | 20 ---- docs/drafts/1217-docker-cli-run.md | 23 +++++ .../plans/{ => docker-cli-load}/local.cue | 3 +- .../plans/{ => docker-cli-load}/ssh.cue | 3 +- docs/drafts/plans/docker-cli-run/local.cue | 15 +++ docs/drafts/plans/docker-cli-run/ssh.cue | 22 +++++ docs/drafts/plans/docker-cli-run/tcp.cue | 20 ++++ .../docker/{load.cue => cli/client.cue} | 94 +++++++------------ pkg/universe.dagger.io/docker/cli/load.cue | 38 ++++++++ .../docker/{ => cli}/test/load.cue | 10 +- .../docker/cli/test/run.cue | 39 ++++++++ .../docker/cli/test/test.bats | 10 ++ pkg/universe.dagger.io/docker/test/test.bats | 1 - 14 files changed, 228 insertions(+), 90 deletions(-) create mode 100644 docs/drafts/1216-docker-cli-load.md delete mode 100644 docs/drafts/1216-engine-load.md create mode 100644 docs/drafts/1217-docker-cli-run.md rename docs/drafts/plans/{ => docker-cli-load}/local.cue (86%) rename docs/drafts/plans/{ => docker-cli-load}/ssh.cue (90%) create mode 100644 docs/drafts/plans/docker-cli-run/local.cue create mode 100644 docs/drafts/plans/docker-cli-run/ssh.cue create mode 100644 docs/drafts/plans/docker-cli-run/tcp.cue rename pkg/universe.dagger.io/docker/{load.cue => cli/client.cue} (56%) create mode 100644 pkg/universe.dagger.io/docker/cli/load.cue rename pkg/universe.dagger.io/docker/{ => cli}/test/load.cue (86%) create mode 100644 pkg/universe.dagger.io/docker/cli/test/run.cue create mode 100644 pkg/universe.dagger.io/docker/cli/test/test.bats diff --git a/docs/drafts/1216-docker-cli-load.md b/docs/drafts/1216-docker-cli-load.md new file mode 100644 index 00000000..63375c70 --- /dev/null +++ b/docs/drafts/1216-docker-cli-load.md @@ -0,0 +1,20 @@ +--- +slug: /1216/docker-cli-load +displayed_sidebar: europa +--- + +# Loading a dagger image into a docker daemon + +Using `cli.#Load`, you can save a dagger image (`docker.#Image`) into a local or remote engine. + +It can be useful to debug or test a build locally before pushing. + +## Local daemon + +```cue file=./plans/docker-cli-load/local.cue +``` + +## Remote daemon, via SSH + +```cue file=./plans/docker-cli-load/ssh.cue +``` diff --git a/docs/drafts/1216-engine-load.md b/docs/drafts/1216-engine-load.md deleted file mode 100644 index acfd41a1..00000000 --- a/docs/drafts/1216-engine-load.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -slug: /1216/engine-load -displayed_sidebar: europa ---- - -# Loading a dagger image into a docker daemon - -Using `docker.#Load`, you can save a dagger image (`docker.#Image`) into a local or remote engine. - -It can be useful to debug or test a build locally before pushing. - -## Local daemon - -```cue file=./plans/local.cue -``` - -## Remote daemon, via SSH - -```cue file=./plans/ssh.cue -``` diff --git a/docs/drafts/1217-docker-cli-run.md b/docs/drafts/1217-docker-cli-run.md new file mode 100644 index 00000000..0def1c47 --- /dev/null +++ b/docs/drafts/1217-docker-cli-run.md @@ -0,0 +1,23 @@ +--- +slug: /1217/docker-cli-run +displayed_sidebar: europa +--- + +# Running commands with the docker binary (CLI) + +There's a `universe.dagger.io/docker/cli` package that allows you to run docker commands against a local or remote docker engine. Here's a few examples. + +## Local daemon + +```cue file=./plans/docker-cli-run/local.cue +``` + +## Remote daemon, via SSH + +```cue file=./plans/docker-cli-run/ssh.cue +``` + +## Remote daemon, via HTTPS + +```cue file=./plans/docker-cli-run/tcp.cue +``` diff --git a/docs/drafts/plans/local.cue b/docs/drafts/plans/docker-cli-load/local.cue similarity index 86% rename from docs/drafts/plans/local.cue rename to docs/drafts/plans/docker-cli-load/local.cue index add0b372..a7c1c854 100644 --- a/docs/drafts/plans/local.cue +++ b/docs/drafts/plans/docker-cli-load/local.cue @@ -3,6 +3,7 @@ package main import ( "dagger.io/dagger" "universe.dagger.io/docker" + "universe.dagger.io/docker/cli" ) dagger.#Plan & { @@ -13,7 +14,7 @@ dagger.#Plan & { ... } - load: docker.#Load & { + load: cli.#Load & { image: build.output host: client.filesystem."/var/run/docker.sock".read.contents tag: "myimage" diff --git a/docs/drafts/plans/ssh.cue b/docs/drafts/plans/docker-cli-load/ssh.cue similarity index 90% rename from docs/drafts/plans/ssh.cue rename to docs/drafts/plans/docker-cli-load/ssh.cue index bebeec9c..e1423e1b 100644 --- a/docs/drafts/plans/ssh.cue +++ b/docs/drafts/plans/docker-cli-load/ssh.cue @@ -3,6 +3,7 @@ package main import ( "dagger.io/dagger" "universe.dagger.io/docker" + "universe.dagger.io/docker/cli" ) dagger.#Plan & { @@ -16,7 +17,7 @@ dagger.#Plan & { ... } - load: docker.#Load & { + load: cli.#Load & { image: build.output tag: "myimage:v2" host: "ssh://root@93.184.216.34" diff --git a/docs/drafts/plans/docker-cli-run/local.cue b/docs/drafts/plans/docker-cli-run/local.cue new file mode 100644 index 00000000..2df6e6ba --- /dev/null +++ b/docs/drafts/plans/docker-cli-run/local.cue @@ -0,0 +1,15 @@ +package main + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker/cli" +) + +dagger.#Plan & { + client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service + + actions: run: cli.#Run & { + host: client.filesystem."/var/run/docker.sock".read.contents + command: name: "info" + } +} diff --git a/docs/drafts/plans/docker-cli-run/ssh.cue b/docs/drafts/plans/docker-cli-run/ssh.cue new file mode 100644 index 00000000..58e9861f --- /dev/null +++ b/docs/drafts/plans/docker-cli-run/ssh.cue @@ -0,0 +1,22 @@ +package main + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker/cli" +) + +dagger.#Plan & { + client: filesystem: { + "/home/user/.ssh/id_rsa": read: contents: dagger.#Secret + "/home/user/.ssh/known_hosts": read: contents: dagger.#Secret + } + + actions: run: cli.#Run & { + host: "ssh://root@93.184.216.34" + ssh: { + key: client.filesystem."/home/user/.ssh/id_rsa".read.contents + knownHosts: client.filesystem."/home/user/.ssh/known_hosts".read.contents + } + command: name: "info" + } +} diff --git a/docs/drafts/plans/docker-cli-run/tcp.cue b/docs/drafts/plans/docker-cli-run/tcp.cue new file mode 100644 index 00000000..8fe79769 --- /dev/null +++ b/docs/drafts/plans/docker-cli-run/tcp.cue @@ -0,0 +1,20 @@ +package main + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker/cli" +) + +dagger.#Plan & { + // Directory with certificates. Needs the following files: + // - ca.pem --> (Certificate authority that signed the registry certificate) + // - cert.pem --> (Client certificate) + // - key.pem --> (Client private key) + client: filesystem: "./certs": read: contents: dagger.#FS + + actions: run: cli.#Run & { + host: "tcp://93.184.216.34:2376" + certs: client.filesystem."./certs".read.contents + command: name: "info" + } +} diff --git a/pkg/universe.dagger.io/docker/load.cue b/pkg/universe.dagger.io/docker/cli/client.cue similarity index 56% rename from pkg/universe.dagger.io/docker/load.cue rename to pkg/universe.dagger.io/docker/cli/client.cue index bb3df59e..3cb7aba6 100644 --- a/pkg/universe.dagger.io/docker/load.cue +++ b/pkg/universe.dagger.io/docker/cli/client.cue @@ -1,47 +1,17 @@ -package docker +package cli import ( "dagger.io/dagger" + "universe.dagger.io/docker" ) -// Load an image into a docker daemon -#Load: { - // Image to load - image: #Image +// See https://github.com/dagger/dagger/issues/1856 - // Name and optionally a tag in the 'name:tag' format - tag: #Ref +// Run a docker CLI command +#Run: { + #RunSocket | #RunSSH | #RunTCP - // Exported image ID - imageID: _export.imageID - - // Root filesystem with exported file - result: _export.output - - _export: dagger.#Export & { - "tag": tag - input: image.rootfs - config: image.config - } - - #_cli & { - mounts: src: { - dest: "/src" - contents: _export.output - } - command: { - name: "load" - flags: "-i": "/src/image.tar" - } - } -} - -// FIXME: Move this into docker/client or -// create a better abstraction to reuse here. -#_cli: { - #_socketConn | #_sshConn | #_tcpConn - - _image: #Pull & { + _image: docker.#Pull & { source: "docker:20.10.13-alpine3.15" } @@ -49,10 +19,10 @@ import ( } // Connect via local docker socket -#_socketConn: { +#RunSocket: { host: dagger.#Service - #Run & { + docker.#Run & { mounts: docker: { dest: "/var/run/docker.sock" contents: host @@ -60,28 +30,8 @@ import ( } } -// Connect via HTTP/HTTPS -#_tcpConn: { - host: =~"^tcp://.+" - - #Run & { - env: DOCKER_HOST: host - - // Directory with certificates to verify ({ca,cert,key}.pem files). - // This enables HTTPS. - certs?: dagger.#FS - - if certs != _|_ { - mounts: "certs": { - dest: "/certs/client" - contents: certs - } - } - } -} - // Connect via SSH -#_sshConn: { +#RunSSH: { host: =~"^ssh://.+" ssh: { @@ -90,9 +40,11 @@ import ( // Known hosts file contents knownHosts?: dagger.#Secret + + // FIXME: implement keyPassphrase } - #Run & { + docker.#Run & { env: DOCKER_HOST: host if ssh.key != _|_ { @@ -110,3 +62,23 @@ import ( } } } + +// Connect via HTTP/HTTPS +#RunTCP: { + host: =~"^tcp://.+" + + docker.#Run & { + env: DOCKER_HOST: host + + // Directory with certificates to verify ({ca,cert,key}.pem files). + // This enables HTTPS. + certs?: dagger.#FS + + if certs != _|_ { + mounts: "certs": { + dest: "/certs/client" + contents: certs + } + } + } +} diff --git a/pkg/universe.dagger.io/docker/cli/load.cue b/pkg/universe.dagger.io/docker/cli/load.cue new file mode 100644 index 00000000..27af165a --- /dev/null +++ b/pkg/universe.dagger.io/docker/cli/load.cue @@ -0,0 +1,38 @@ +package cli + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker" +) + +// Load an image into a docker daemon +#Load: { + // Image to load + image: docker.#Image + + // Name and optionally a tag in the 'name:tag' format + tag: docker.#Ref + + // Exported image ID + imageID: _export.imageID + + // Root filesystem with exported file + result: _export.output + + _export: dagger.#Export & { + "tag": tag + input: image.rootfs + config: image.config + } + + #Run & { + mounts: src: { + dest: "/src" + contents: _export.output + } + command: { + name: "load" + flags: "-i": "/src/image.tar" + } + } +} diff --git a/pkg/universe.dagger.io/docker/test/load.cue b/pkg/universe.dagger.io/docker/cli/test/load.cue similarity index 86% rename from pkg/universe.dagger.io/docker/test/load.cue rename to pkg/universe.dagger.io/docker/cli/test/load.cue index 84b0ec16..46e3629b 100644 --- a/pkg/universe.dagger.io/docker/test/load.cue +++ b/pkg/universe.dagger.io/docker/cli/test/load.cue @@ -1,4 +1,4 @@ -package docker +package test import ( "dagger.io/dagger" @@ -6,12 +6,13 @@ import ( "universe.dagger.io/alpine" "universe.dagger.io/bash" "universe.dagger.io/docker" + "universe.dagger.io/docker/cli" ) dagger.#Plan & { client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service - actions: test: load: { + actions: test: { _cli: alpine.#Build & { packages: { bash: {} @@ -27,7 +28,7 @@ dagger.#Plan & { } } - load: docker.#Load & { + load: cli.#Load & { image: _image.output host: client.filesystem."/var/run/docker.sock".read.contents tag: "dagger:load" @@ -51,7 +52,4 @@ dagger.#Plan & { """# } } - - // FIXME: test remote connections with `docker:dind` - // image when we have long running tasks } diff --git a/pkg/universe.dagger.io/docker/cli/test/run.cue b/pkg/universe.dagger.io/docker/cli/test/run.cue new file mode 100644 index 00000000..e29a043a --- /dev/null +++ b/pkg/universe.dagger.io/docker/cli/test/run.cue @@ -0,0 +1,39 @@ +package test + +import ( + "dagger.io/dagger" + + "universe.dagger.io/alpine" + "universe.dagger.io/docker/cli" +) + +dagger.#Plan & { + client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service + + actions: test: { + run: cli.#Run & { + host: client.filesystem."/var/run/docker.sock".read.contents + command: name: "info" + } + + differentImage: { + _cli: alpine.#Build & { + packages: { + bash: {} + "docker-cli": {} + } + } + run: cli.#RunSocket & { + input: _cli.output + host: client.filesystem."/var/run/docker.sock".read.contents + command: { + name: "docker" + args: ["info"] + } + } + } + + // FIXME: test remote connections with `docker:dind` image + // when we have long running tasks + } +} diff --git a/pkg/universe.dagger.io/docker/cli/test/test.bats b/pkg/universe.dagger.io/docker/cli/test/test.bats new file mode 100644 index 00000000..6a9874ac --- /dev/null +++ b/pkg/universe.dagger.io/docker/cli/test/test.bats @@ -0,0 +1,10 @@ +setup() { + load '../../../bats_helpers' + + common_setup +} + +@test "docker/cli" { + dagger "do" -p ./run.cue test + dagger "do" -p ./load.cue test +} diff --git a/pkg/universe.dagger.io/docker/test/test.bats b/pkg/universe.dagger.io/docker/test/test.bats index b126fcf6..bebf70cb 100644 --- a/pkg/universe.dagger.io/docker/test/test.bats +++ b/pkg/universe.dagger.io/docker/test/test.bats @@ -9,5 +9,4 @@ setup() { dagger "do" -p ./dockerfile.cue test dagger "do" -p ./run.cue test dagger "do" -p ./image.cue test - dagger "do" -p ./load.cue test }