2022-02-17 22:00:46 +01:00
|
|
|
module docker
|
|
|
|
|
|
|
|
import json
|
|
|
|
import net.urllib
|
|
|
|
|
|
|
|
struct Container {
|
2022-02-20 13:10:48 +01:00
|
|
|
id string [json: Id]
|
|
|
|
names []string [json: Names]
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// containers returns a list of all currently running containers
|
2022-02-17 22:00:46 +01:00
|
|
|
pub fn containers() ?[]Container {
|
2022-02-21 17:49:10 +01:00
|
|
|
res := request('GET', urllib.parse('/v1.41/containers/json') ?) ?
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
return json.decode([]Container, res.text) or {}
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
pub struct NewContainer {
|
2022-02-20 22:15:10 +01:00
|
|
|
image string [json: Image]
|
2022-02-20 20:26:39 +01:00
|
|
|
entrypoint []string [json: Entrypoint]
|
2022-02-20 22:15:10 +01:00
|
|
|
cmd []string [json: Cmd]
|
|
|
|
env []string [json: Env]
|
2022-02-25 21:54:16 +01:00
|
|
|
work_dir string [json: WorkingDir]
|
|
|
|
user string [json: User]
|
2022-02-20 20:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CreatedContainer {
|
|
|
|
id string [json: Id]
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// create_container creates a container defined by the given configuration. If
|
|
|
|
// successful, it returns the ID of the newly created container.
|
2022-02-20 20:26:39 +01:00
|
|
|
pub fn create_container(c &NewContainer) ?string {
|
2022-02-21 17:49:10 +01:00
|
|
|
res := request_with_json('POST', urllib.parse('/v1.41/containers/create') ?, c) ?
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
if res.status_code != 201 {
|
|
|
|
return error('Failed to create container.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.decode(CreatedContainer, res.text) ?.id
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// start_container starts a container with a given ID. It returns whether the
|
|
|
|
// container was started or not.
|
2022-02-20 20:26:39 +01:00
|
|
|
pub fn start_container(id string) ?bool {
|
2022-02-21 17:49:10 +01:00
|
|
|
res := request('POST', urllib.parse('/v1.41/containers/$id/start') ?) ?
|
2022-02-20 21:09:06 +01:00
|
|
|
|
|
|
|
return res.status_code == 204
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContainerInspect {
|
|
|
|
pub:
|
|
|
|
state ContainerState [json: State]
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContainerState {
|
|
|
|
pub:
|
|
|
|
running bool [json: Running]
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// inspect_container returns the result of inspecting a container with a given
|
|
|
|
// ID.
|
2022-02-20 21:09:06 +01:00
|
|
|
pub fn inspect_container(id string) ?ContainerInspect {
|
2022-02-21 17:49:10 +01:00
|
|
|
res := request('GET', urllib.parse('/v1.41/containers/$id/json') ?) ?
|
2022-02-20 21:09:06 +01:00
|
|
|
|
|
|
|
if res.status_code != 200 {
|
2022-02-20 22:15:10 +01:00
|
|
|
return error('Failed to inspect container.')
|
2022-02-20 21:09:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
return json.decode(ContainerInspect, res.text) or {}
|
2022-02-20 21:09:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 22:15:10 +01:00
|
|
|
// remove_container removes a container with a given ID.
|
2022-02-20 21:09:06 +01:00
|
|
|
pub fn remove_container(id string) ?bool {
|
2022-02-21 17:49:10 +01:00
|
|
|
res := request('DELETE', urllib.parse('/v1.41/containers/$id') ?) ?
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
return res.status_code == 204
|
|
|
|
}
|
2022-05-08 10:29:06 +02:00
|
|
|
|
|
|
|
pub fn get_container_logs(id string) ?string {
|
|
|
|
res := request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true') ?) ?
|
|
|
|
mut res_bytes := res.text.bytes()
|
|
|
|
|
|
|
|
// Docker uses a special "stream" format for their logs, so we have to
|
|
|
|
// clean up the data.
|
|
|
|
mut index := 0
|
|
|
|
|
|
|
|
for index < res_bytes.len {
|
|
|
|
// The reverse is required because V reads in the bytes differently
|
|
|
|
t := res_bytes[index + 4..index + 8].reverse()
|
|
|
|
len_length := unsafe { *(&u32(&t[0])) }
|
|
|
|
|
|
|
|
res_bytes.delete_many(index, 8)
|
|
|
|
index += int(len_length)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res_bytes.bytestr()
|
|
|
|
}
|