refactor(docker): renamed DockerDaemon to DockerConn
parent
97cdaa18e1
commit
1d3c7a1651
|
@ -0,0 +1,5 @@
|
|||
# docker
|
||||
|
||||
This module implements part of the Docker Engine API v1.41
|
||||
([documentation](https://docs.docker.com/engine/api/v1.41/)) using socket-based
|
||||
HTTP communication.
|
|
@ -15,7 +15,7 @@ struct Container {
|
|||
}
|
||||
|
||||
// containers returns a list of all containers.
|
||||
pub fn (mut d DockerDaemon) containers() ?[]Container {
|
||||
pub fn (mut d DockerConn) containers() ?[]Container {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/json')?)?
|
||||
head, res := d.read_response()?
|
||||
|
||||
|
@ -46,7 +46,7 @@ pub:
|
|||
}
|
||||
|
||||
// create_container creates a new container with the given config.
|
||||
pub fn (mut d DockerDaemon) create_container(c NewContainer) ?CreatedContainer {
|
||||
pub fn (mut d DockerConn) create_container(c NewContainer) ?CreatedContainer {
|
||||
d.send_request_with_json('POST', urllib.parse('/v1.41/containers/create')?, c)?
|
||||
head, res := d.read_response()?
|
||||
|
||||
|
@ -62,7 +62,7 @@ pub fn (mut d DockerDaemon) create_container(c NewContainer) ?CreatedContainer {
|
|||
}
|
||||
|
||||
// start_container starts the container with the given id.
|
||||
pub fn (mut d DockerDaemon) start_container(id string) ? {
|
||||
pub fn (mut d DockerConn) start_container(id string) ? {
|
||||
d.send_request('POST', urllib.parse('/v1.41/containers/$id/start')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
@ -92,7 +92,7 @@ pub mut:
|
|||
}
|
||||
|
||||
// inspect_container returns detailed information for a given container.
|
||||
pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect {
|
||||
pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/$id/json')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
@ -114,7 +114,7 @@ pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect {
|
|||
}
|
||||
|
||||
// remove_container removes the container with the given id.
|
||||
pub fn (mut d DockerDaemon) remove_container(id string) ? {
|
||||
pub fn (mut d DockerConn) remove_container(id string) ? {
|
||||
d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
@ -127,7 +127,7 @@ pub fn (mut d DockerDaemon) remove_container(id string) ? {
|
|||
|
||||
// get_container_logs returns a reader object allowing access to the
|
||||
// container's logs.
|
||||
pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader {
|
||||
pub fn (mut d DockerConn) get_container_logs(id string) ?&StreamFormatReader {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
|
||||
head := d.read_response_head()?
|
||||
|
||||
|
|
|
@ -15,17 +15,17 @@ const (
|
|||
http_chunk_separator = [u8(`\r`), `\n`]
|
||||
)
|
||||
|
||||
pub struct DockerDaemon {
|
||||
pub struct DockerConn {
|
||||
mut:
|
||||
socket &unix.StreamConn
|
||||
reader &io.BufferedReader
|
||||
}
|
||||
|
||||
// new_conn creates a new connection to the Docker daemon.
|
||||
pub fn new_conn() ?&DockerDaemon {
|
||||
pub fn new_conn() ?&DockerConn {
|
||||
s := unix.connect_stream(docker.socket)?
|
||||
|
||||
d := &DockerDaemon{
|
||||
d := &DockerConn{
|
||||
socket: s
|
||||
reader: io.new_buffered_reader(reader: s)
|
||||
}
|
||||
|
@ -34,12 +34,12 @@ pub fn new_conn() ?&DockerDaemon {
|
|||
}
|
||||
|
||||
// close closes the underlying socket connection.
|
||||
pub fn (mut d DockerDaemon) close() ? {
|
||||
pub fn (mut d DockerConn) close() ? {
|
||||
d.socket.close()?
|
||||
}
|
||||
|
||||
// send_request sends an HTTP request without body.
|
||||
pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
||||
pub fn (mut d DockerConn) send_request(method string, url urllib.URL) ? {
|
||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
||||
|
||||
d.socket.write_string(req)?
|
||||
|
@ -49,7 +49,7 @@ pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
|||
}
|
||||
|
||||
// send_request_with_body sends an HTTP request with the given body.
|
||||
pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
|
||||
pub fn (mut d DockerConn) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
|
||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
|
||||
|
||||
d.socket.write_string(req)?
|
||||
|
@ -60,7 +60,7 @@ pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL
|
|||
|
||||
// send_request_with_json<T> is a convenience wrapper around
|
||||
// send_request_with_body that encodes the input as JSON.
|
||||
pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
||||
pub fn (mut d DockerConn) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
||||
body := json.encode(data)
|
||||
|
||||
return d.send_request_with_body(method, url, 'application/json', body)
|
||||
|
@ -70,17 +70,17 @@ pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.
|
|||
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
||||
// Importantly, this function never consumes the reader past the HTTP
|
||||
// separator, so the body can be read fully later on.
|
||||
pub fn (mut d DockerDaemon) read_response_head() ?http.Response {
|
||||
pub fn (mut d DockerConn) read_response_head() ?http.Response {
|
||||
mut res := []u8{}
|
||||
|
||||
util.read_until_separator(mut d.reader, mut res, http_separator) ?
|
||||
util.read_until_separator(mut d.reader, mut res, docker.http_separator)?
|
||||
|
||||
return http.parse_response(res.bytestr())
|
||||
}
|
||||
|
||||
// read_response_body reads `length` bytes from the stream. It can be used when
|
||||
// the response encoding isn't chunked to fully read it.
|
||||
pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
||||
pub fn (mut d DockerConn) read_response_body(length int) ?string {
|
||||
if length == 0 {
|
||||
return ''
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
|||
// read_response is a convenience function which always consumes the entire
|
||||
// response & returns it. It should only be used when we're certain that the
|
||||
// result isn't too large.
|
||||
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
||||
pub fn (mut d DockerConn) read_response() ?(http.Response, string) {
|
||||
head := d.read_response_head()?
|
||||
|
||||
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
||||
|
@ -121,7 +121,7 @@ pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
|||
|
||||
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
|
||||
// as reader.
|
||||
pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader {
|
||||
pub fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
||||
r := new_chunked_response_reader(d.reader)
|
||||
|
||||
return r
|
||||
|
@ -129,7 +129,7 @@ pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader
|
|||
|
||||
// get_stream_format_reader returns a StreamFormatReader using the socket as
|
||||
// reader.
|
||||
pub fn (mut d DockerDaemon) get_stream_format_reader() &StreamFormatReader {
|
||||
pub fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
||||
r := new_chunked_response_reader(d.reader)
|
||||
r2 := new_stream_format_reader(r)
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ pub:
|
|||
}
|
||||
|
||||
// pull_image pulls the given image:tag.
|
||||
pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? {
|
||||
pub fn (mut d DockerConn) pull_image(image string, tag string) ? {
|
||||
d.send_request('POST', urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag')?)?
|
||||
head := d.read_response_head()?
|
||||
|
||||
|
@ -33,7 +33,7 @@ pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? {
|
|||
}
|
||||
|
||||
// create_image_from_container creates a new image from a container.
|
||||
pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string, tag string) ?Image {
|
||||
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) ?Image {
|
||||
d.send_request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
@ -49,7 +49,7 @@ pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string,
|
|||
}
|
||||
|
||||
// remove_image removes the image with the given id.
|
||||
pub fn (mut d DockerDaemon) remove_image(id string) ? {
|
||||
pub fn (mut d DockerConn) remove_image(id string) ? {
|
||||
d.send_request('DELETE', urllib.parse('/v1.41/images/$id')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
|||
r.started = true
|
||||
|
||||
mut res := []u8{}
|
||||
util.read_until_separator(mut r.reader, mut res, http_chunk_separator) ?
|
||||
util.read_until_separator(mut r.reader, mut res, http_chunk_separator)?
|
||||
|
||||
// The length of the next chunk is provided as a hexadecimal
|
||||
mut num_data := hex.decode(res#[..-2].bytestr())?
|
||||
|
|
Loading…
Reference in New Issue