2022-02-17 22:00:46 +01:00
|
|
|
module docker
|
|
|
|
|
|
|
|
import net.unix
|
|
|
|
import net.urllib
|
|
|
|
import net.http
|
2022-02-19 22:25:52 +01:00
|
|
|
import json
|
2022-02-17 22:00:46 +01:00
|
|
|
|
|
|
|
const socket = '/var/run/docker.sock'
|
2022-02-19 21:41:26 +01:00
|
|
|
|
2022-02-17 22:00:46 +01:00
|
|
|
const buf_len = 1024
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// send writes a request to the Docker socket, waits for a response & returns
|
|
|
|
// it.
|
2022-02-19 22:25:52 +01:00
|
|
|
fn send(req &string) ?http.Response {
|
2022-02-19 21:41:26 +01:00
|
|
|
// Open a connection to the socket
|
2022-02-21 17:49:10 +01:00
|
|
|
mut s := unix.connect_stream(docker.socket) or {
|
|
|
|
return error('Failed to connect to socket ${docker.socket}.')
|
|
|
|
}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
defer {
|
|
|
|
// This or is required because otherwise, the V compiler segfaults for
|
|
|
|
// some reason
|
2022-02-19 22:25:52 +01:00
|
|
|
// https://github.com/vlang/v/issues/13534
|
2022-02-19 21:41:26 +01:00
|
|
|
s.close() or {}
|
|
|
|
}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
// Write the request to the socket
|
2022-02-21 17:18:14 +01:00
|
|
|
s.write_string(req) or { return error('Failed to write request to socket ${docker.socket}.') }
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-20 13:03:00 +01:00
|
|
|
s.wait_for_write() ?
|
|
|
|
|
|
|
|
mut c := 0
|
2022-04-30 11:43:06 +02:00
|
|
|
mut buf := []u8{len: docker.buf_len}
|
|
|
|
mut res := []u8{}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-20 13:03:00 +01:00
|
|
|
for {
|
2022-02-21 17:18:14 +01:00
|
|
|
c = s.read(mut buf) or { return error('Failed to read data from socket ${docker.socket}.') }
|
2022-02-20 13:03:00 +01:00
|
|
|
res << buf[..c]
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-20 13:03:00 +01:00
|
|
|
if c < docker.buf_len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:18:14 +01:00
|
|
|
// After reading the first part of the response, we parse it into an HTTP
|
|
|
|
// response. If it isn't chunked, we return early with the data.
|
2022-02-21 17:49:10 +01:00
|
|
|
parsed := http.parse_response(res.bytestr()) or {
|
|
|
|
return error('Failed to parse HTTP response from socket ${docker.socket}.')
|
|
|
|
}
|
2022-02-20 13:03:00 +01:00
|
|
|
|
|
|
|
if parsed.header.get(http.CommonHeader.transfer_encoding) or { '' } != 'chunked' {
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
// We loop until we've encountered the end of the chunked response
|
2022-02-21 17:18:14 +01:00
|
|
|
// A chunked HTTP response always ends with '0\r\n\r\n'.
|
2022-04-30 11:43:06 +02:00
|
|
|
for res.len < 5 || res#[-5..] != [u8(`0`), `\r`, `\n`, `\r`, `\n`] {
|
2022-02-20 12:35:10 +01:00
|
|
|
// Wait for the server to respond
|
|
|
|
s.wait_for_write() ?
|
|
|
|
|
|
|
|
for {
|
2022-02-21 17:49:10 +01:00
|
|
|
c = s.read(mut buf) or {
|
|
|
|
return error('Failed to read data from socket ${docker.socket}.')
|
|
|
|
}
|
2022-02-20 12:35:10 +01:00
|
|
|
res << buf[..c]
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-20 12:35:10 +01:00
|
|
|
if c < docker.buf_len {
|
|
|
|
break
|
|
|
|
}
|
2022-02-19 21:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
// Decode chunked response
|
|
|
|
return http.parse_response(res.bytestr())
|
2022-02-19 22:25:52 +01:00
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// request_with_body sends a request to the Docker socket with the given body.
|
2022-02-20 20:26:39 +01:00
|
|
|
fn request_with_body(method string, url urllib.URL, content_type string, body string) ?http.Response {
|
2022-02-20 22:15:10 +01: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-02-19 22:25:52 +01:00
|
|
|
|
|
|
|
return send(req)
|
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// request sends a request to the Docker socket with an empty body.
|
2022-02-19 22:25:52 +01:00
|
|
|
fn request(method string, url urllib.URL) ?http.Response {
|
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
|
|
|
|
|
|
|
return send(req)
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// request_with_json<T> sends a request to the Docker socket with a given JSON
|
|
|
|
// payload
|
2022-02-20 20:26:39 +01:00
|
|
|
pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Response {
|
2022-02-19 22:25:52 +01:00
|
|
|
body := json.encode(data)
|
|
|
|
|
2022-02-20 20:26:39 +01:00
|
|
|
return request_with_body(method, url, 'application/json', body)
|
2022-02-19 22:25:52 +01:00
|
|
|
}
|