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-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
|
|
|
|
mut s := unix.connect_stream(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
|
|
|
|
s.write_string(req) ?
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
// Wait for the server to respond
|
|
|
|
s.wait_for_write() ?
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
mut buf := []byte{len: docker.buf_len}
|
|
|
|
mut res := []byte{}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
mut c := 0
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
for {
|
|
|
|
c = s.read(mut buf) or { return error('Failed to read data from socket.') }
|
|
|
|
res << buf[..c]
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
if c < docker.buf_len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_with_body(method string, url urllib.URL, body &string) ?http.Response {
|
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Length: ${body.len}\n$body\n'
|
|
|
|
|
|
|
|
return send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request(method string, url urllib.URL) ?http.Response {
|
|
|
|
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
|
|
|
|
|
|
|
return send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn request_with_json<T>(method string, url urllib.URL, data T) ?http.Response {
|
|
|
|
body := json.encode(data)
|
|
|
|
println(body)
|
|
|
|
|
|
|
|
return request_with_body(method, url, body)
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get(url urllib.URL) ?http.Response {
|
2022-02-19 21:41:26 +01:00
|
|
|
return request('GET', url)
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
2022-02-19 22:25:52 +01:00
|
|
|
|
|
|
|
struct ImagePull {
|
|
|
|
from_image string [json: fromImage]
|
|
|
|
tag string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pull(image string, tag string) ?http.Response {
|
|
|
|
// data := ImagePull{
|
|
|
|
// from_image: image
|
|
|
|
// tag: tag
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return request_with_json("POST", urllib.parse("/images/create") ?, data)
|
|
|
|
return request("POST", urllib.parse("/images/create?fromImage=$image&tag=$tag") ?)
|
|
|
|
}
|