2022-06-22 09:15:00 +02:00
|
|
|
module docker
|
2022-06-18 18:27:26 +02:00
|
|
|
|
2022-06-18 17:59:22 +02:00
|
|
|
import net.unix
|
|
|
|
import io
|
|
|
|
import net.http
|
|
|
|
import strings
|
|
|
|
import net.urllib
|
|
|
|
import json
|
|
|
|
import util
|
|
|
|
|
|
|
|
const (
|
|
|
|
socket = '/var/run/docker.sock'
|
|
|
|
buf_len = 10 * 1024
|
|
|
|
http_separator = [u8(`\r`), `\n`, `\r`, `\n`]
|
|
|
|
http_chunk_separator = [u8(`\r`), `\n`]
|
2022-06-21 20:28:25 +02:00
|
|
|
timestamp_attr = 'timestamp'
|
|
|
|
api_version = 'v1.41'
|
2022-06-18 17:59:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
pub struct DockerConn {
|
|
|
|
mut:
|
|
|
|
socket &unix.StreamConn
|
|
|
|
reader &io.BufferedReader
|
|
|
|
}
|
|
|
|
|
|
|
|
// new_conn creates a new connection to the Docker daemon.
|
2022-11-01 19:14:25 +01:00
|
|
|
pub fn new_conn() !&DockerConn {
|
|
|
|
s := unix.connect_stream(docker.socket)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
d := &DockerConn{
|
|
|
|
socket: s
|
|
|
|
reader: io.new_buffered_reader(reader: s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// close closes the underlying socket connection.
|
2022-11-01 19:14:25 +01:00
|
|
|
pub fn (mut d DockerConn) close() ! {
|
|
|
|
d.socket.close()!
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send_request sends an HTTP request without body.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) send_request(method http.Method, url_str string) ! {
|
|
|
|
url := urllib.parse('/$docker.api_version$url_str')!
|
2022-06-18 17:59:22 +02:00
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
d.socket.write_string(req)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
// When starting a new request, the reader needs to be reset.
|
|
|
|
d.reader = io.new_buffered_reader(reader: d.socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send_request_with_body sends an HTTP request with the given body.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) send_request_with_body(method http.Method, url_str string, content_type string, body string) ! {
|
|
|
|
url := urllib.parse('/$docker.api_version$url_str')!
|
2022-06-18 17:59:22 +02:00
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
d.socket.write_string(req)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
// When starting a new request, the reader needs to be reset.
|
|
|
|
d.reader = io.new_buffered_reader(reader: d.socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send_request_with_json<T> is a convenience wrapper around
|
|
|
|
// send_request_with_body that encodes the input as JSON.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) send_request_with_json<T>(method http.Method, url_str string, data &T) ! {
|
2022-06-18 17:59:22 +02:00
|
|
|
body := json.encode(data)
|
|
|
|
|
2022-06-21 15:47:42 +02:00
|
|
|
return d.send_request_with_body(method, url_str, 'application/json', body)
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_response_head consumes the socket's contents until it encounters
|
|
|
|
// '\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.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) read_response_head() !http.Response {
|
2022-06-18 17:59:22 +02:00
|
|
|
mut res := []u8{}
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
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.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) read_response_body(length int) !string {
|
2022-06-18 17:59:22 +02:00
|
|
|
if length == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2022-06-22 09:15:00 +02:00
|
|
|
mut buf := []u8{len: docker.buf_len}
|
2022-06-18 17:59:22 +02:00
|
|
|
mut c := 0
|
2022-06-22 09:15:00 +02:00
|
|
|
mut builder := strings.new_builder(docker.buf_len)
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
for builder.len < length {
|
|
|
|
c = d.reader.read(mut buf) or { break }
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
builder.write(buf[..c])!
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) read_response() !(http.Response, string) {
|
|
|
|
head := d.read_response_head()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
|
|
|
mut builder := strings.new_builder(1024)
|
|
|
|
mut body := d.get_chunked_response_reader()
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
util.reader_to_writer(mut body, mut builder)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return head, builder.str()
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
|
|
|
res := d.read_response_body(content_length)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return head, res
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
fn (mut d DockerConn) read_json_response<T>() !T {
|
|
|
|
head, body := d.read_response()!
|
2022-06-21 15:47:42 +02:00
|
|
|
|
2022-06-21 20:28:25 +02:00
|
|
|
if head.status_code < 200 || head.status_code > 300 {
|
2022-11-01 19:14:25 +01:00
|
|
|
data := json.decode(DockerError, body)!
|
2022-06-21 15:47:42 +02:00
|
|
|
|
2022-06-21 20:28:25 +02:00
|
|
|
return docker_error(head.status_code, data.message)
|
|
|
|
}
|
2022-06-21 15:47:42 +02:00
|
|
|
|
2022-11-01 19:14:25 +01:00
|
|
|
mut data := json.decode(T, body)!
|
2022-06-21 15:47:42 +02:00
|
|
|
|
2022-06-21 20:28:25 +02:00
|
|
|
//$for field in T.fields {
|
|
|
|
//$if field.typ is time.Time {
|
|
|
|
// data.$(field.name) = time.parse_rfc3339(data.$(field.name + '_str'))?
|
|
|
|
//}
|
|
|
|
//}
|
2022-06-21 15:47:42 +02:00
|
|
|
|
2022-06-21 20:28:25 +02:00
|
|
|
return data
|
2022-06-21 15:47:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 17:59:22 +02:00
|
|
|
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
|
|
|
|
// as reader.
|
2022-06-18 22:17:52 +02:00
|
|
|
fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
2022-06-18 17:59:22 +02:00
|
|
|
r := new_chunked_response_reader(d.reader)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_stream_format_reader returns a StreamFormatReader using the socket as
|
|
|
|
// reader.
|
2022-06-18 22:17:52 +02:00
|
|
|
fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
2022-06-18 17:59:22 +02:00
|
|
|
r := new_chunked_response_reader(d.reader)
|
|
|
|
r2 := new_stream_format_reader(r)
|
|
|
|
|
|
|
|
return r2
|
|
|
|
}
|