harbor icon indicating copy to clipboard operation
harbor copied to clipboard

Docker API coverage

Open hrbrmstr opened this issue 8 years ago • 7 comments

Moving the discussion started in #10 to here. Some examples of using the Unix socket API using curl:

library(curl)
library(jsonlite)
library(openssl)

docker_auth <- function(username=Sys.getenv("DOCKER_USERNAME"),
                        password=Sys.getenv("DOCKER_PASSWORD"),
                        email=Sys.getenv("DOCKER_EMAIL"),
                        server_address=Sys.getenv("DOCKER_REGISTRY"),
                        encode=TRUE) {

  toJSON(
    list(
      username=unbox(username),
      password=unbox(password),
      email=unbox(email),
      auth=unbox(""),
      serveraddress=unbox(server_address)
    )
  ) -> auth_json

  if (encode) auth_json <- openssl::base64_encode(auth_json)

  auth_json

}

docker_handle <- function(verbose=FALSE) {
  h <- curl::new_handle()
  h <- curl::handle_reset(h)
  h <- curl::handle_setopt(h, UNIX_SOCKET_PATH = "/var/run/docker.sock")
  h <- curl::handle_setopt(h, VERBOSE = verbose)
  h <- curl::handle_setheaders(h,
                               `Content-Type` = "application/json",
                               `X-Registry-Auth` = docker_auth())
  h
}

# Auth check ------------------------------------------------------------------------
h <- docker_handle(TRUE)
handle_setopt(h, copypostfields = docker_auth(encode=FALSE))
res <- curl_fetch_memory(url = "http://v1.26/auth", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker info -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/info", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker version --------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/version", handle = h)
str(fromJSON(rawToChar(res$content)))

# Docker ping -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/_ping", handle = h)
cat(rawToChar(res$content))

# Data usage ------------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/system/df", handle = h)
str(fromJSON(rawToChar(res$content)))

# List images -----------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/images/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# Search images ---------------------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/images/search?term=alpine&limit=10", handle = h)
str(fromJSON(rawToChar(res$content)))

# Pull an image ---------------------------------------------------
h <- docker_handle(verbose = TRUE)
handle_setopt(h, copypostfields = '')
res <- curl_fetch_memory(url = "http://v1.26/images/create?fromImage=alpine&tag=latest", handle = h)
cat(rawToChar(res$content))

# List running containers -----------------------------------------------------------
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/containers/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# all: true/false
# limit: int
# size: true/false
# filters: json
h <- docker_handle()
res <- curl_fetch_memory(url = "http://v1.26/containers/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# Inspect a container ---------------------------------------------------------------

# size: true/false
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/json", handle = h)
str(fromJSON(rawToChar(res$content)))

# List container processes ----------------------------------------------------------

# ps_args: string
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/top", handle = h)
str(fromJSON(rawToChar(res$content)))

# Get container logs ----------------------------------------------------------------

# follow: true/false
# stdout: t/f
# stderr: r/f
# since: int
# timestamps: t/f
# tail: string
h <- docker_handle(verbose = TRUE)
res <- curl_fetch_memory(url = "http://v1.26/containers/b834259b9e9ed70cd00a43c9a539ab0601dab68a5966e531fea76ec91bc76940/logs?stdout=true&stderr=true", handle = h)
cat(rawToChar(res$content[res$content > 0x09]))


# Run a container -------------------------------------------------------------------
h <- docker_handle()
handle_setopt(h, copypostfields = '{"Image": "alpine", "Cmd": ["echo", "hello world"]}')
res <- curl_fetch_memory(url = "http:/v1.26/containers/create", handle = h)
str(fromJSON(rawToChar(res$content)))

hrbrmstr avatar Feb 17 '17 20:02 hrbrmstr

I am not initially inclined to support everything in the API and I still think we kinda need a way to use the CLI vs API since I'm also not thinking I want to deal with Windows named pipes in R code, esp since libcurl does not support it.

So, what's the minimum viable product for the API & CLI?

hrbrmstr avatar Feb 17 '17 20:02 hrbrmstr

From my point of view a minimum is:

Critical:

  • run
  • pull
  • exec

For building docker images:

  • commit
  • tag
  • build
  • push

Then useful to have:

  • stop
  • ps
  • rm
  • logs
  • images

MarkEdmondson1234 avatar Feb 17 '17 20:02 MarkEdmondson1234

it might be better to use CLI to be cross-platform, since the commands are the same for windows and linux. There will be some changes to implement in the functions using Sys.info()['sysname]

Or, another solution, would be to use the linux layer in the new windows, using bash -c docker ...

I have dualboot with winstuff on my machine, I can boot on the dark side and install docker to make some test.

EDIT: I was thinking also "images" to list docker on the computer, and get their name

The-Dub avatar Feb 17 '17 20:02 The-Dub

aye. the Windows named pipe wld require a diff interface but supporting CLI and API wldn't be too hard.

On Fri, Feb 17, 2017 at 3:50 PM, Ricky Dubos [email protected] wrote:

it might be better to use CLI to be cross-platform, since the commands are the same for windows and linux. There will be some changes to implement in the functions using Sys.info()['sysname]

Or, another solution, would be to use the linux layer in the new windows, but I'm not quite sure how to pipe command to it...

I have dualboot with winstuff on my machine, I can boot on the dark side and install docker to make some test.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/wch/harbor/issues/11#issuecomment-280762804, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfHtmI2M87hilv7quGEFBnP6xcUitqbks5rdggYgaJpZM4MEstm .

hrbrmstr avatar Feb 17 '17 21:02 hrbrmstr

So, I'm tabling the idea of direct API for now and may hold off a non-S3 implementation for now as well unless there's alot of feedback otherwise. I just setup Travis on my fork of harbor and base pkg passes CRAN checks (all of which are PR'd back here).

I agree with @MarkEdmondson1234 on the short list of "have's" above and that wld prbly be enough of a core for a release milestone.

I need to add non-Travis ssh tests and then work out docs for the cloud-y platforms to dovetail into the harbor pkg API (if they/y'all want to).

My biggest problem now is that I have no way to test on Windows. I grabbed the 90-day trial edition of Windows 10, enabled all the things in VMware Fusion for hypervisor stuff to work, installed Docker for Windows and it just keeps crashing or stalling. Need to figure out a way to get Windows tests working (Windows is a large chunk of the R user base but I'm not sure how many have a system config that can run Docker for Windows which is Win10 Anniv Ed+ & CPUs that can handle the base Docker VM).

hrbrmstr avatar Feb 22 '17 18:02 hrbrmstr

I can test it on Windows if you'd like. I have a dual boot, so it'll be easier & more accurate

The-Dub avatar Feb 22 '17 20:02 The-Dub

pls! :-)

hrbrmstr avatar Feb 22 '17 20:02 hrbrmstr