vieter/src/docker/containers.v

124 lines
3.2 KiB
Coq
Raw Normal View History

module docker
import json
import net.urllib
import time
import net.http { Method }
struct DockerError {
message string
}
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 {
pub:
id string [json: Id]
warnings []string [json: Warnings]
}
2022-05-14 23:42:20 +02:00
// create_container creates a new container with the given config.
pub fn (mut d DockerConn) create_container(c NewContainer) ?CreatedContainer {
d.send_request_with_json(Method.post, urllib.parse('/v1.41/containers/create')?, c)?
head, res := d.read_response()?
if head.status_code != 201 {
data := json.decode(DockerError, res)?
return error(data.message)
}
data := json.decode(CreatedContainer, res)?
return data
}
2022-05-14 23:42:20 +02:00
// start_container starts the container with the given id.
pub fn (mut d DockerConn) start_container(id string) ? {
d.send_request(Method.post, urllib.parse('/v1.41/containers/$id/start')?)?
head, body := d.read_response()?
if head.status_code != 204 {
data := json.decode(DockerError, body)?
return error(data.message)
}
2022-02-20 20:26:39 +01:00
}
struct ContainerInspect {
pub mut:
state ContainerState [json: State]
}
struct ContainerState {
pub:
2022-05-08 14:53:35 +02:00
running bool [json: Running]
status string [json: Status]
exit_code int [json: ExitCode]
// These use a rather specific format so they have to be parsed later
start_time_str string [json: StartedAt]
2022-05-08 14:53:35 +02:00
end_time_str string [json: FinishedAt]
pub mut:
start_time time.Time [skip]
2022-05-08 14:53:35 +02:00
end_time time.Time [skip]
}
2022-05-14 23:42:20 +02:00
// inspect_container returns detailed information for a given container.
pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
d.send_request(Method.get, urllib.parse('/v1.41/containers/$id/json')?)?
head, body := d.read_response()?
if head.status_code != 200 {
data := json.decode(DockerError, body)?
return error(data.message)
}
mut data := json.decode(ContainerInspect, body)?
// The Docker engine API *should* always return UTC time.
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)?
if data.state.status == 'exited' {
data.state.end_time = time.parse_rfc3339(data.state.end_time_str)?
}
return data
}
2022-05-14 23:42:20 +02:00
// remove_container removes the container with the given id.
pub fn (mut d DockerConn) remove_container(id string) ? {
d.send_request(Method.delete, urllib.parse('/v1.41/containers/$id')?)?
head, body := d.read_response()?
if head.status_code != 204 {
data := json.decode(DockerError, body)?
return error(data.message)
}
}
2022-05-14 23:42:20 +02:00
// get_container_logs returns a reader object allowing access to the
// container's logs.
pub fn (mut d DockerConn) get_container_logs(id string) ?&StreamFormatReader {
d.send_request(Method.get, urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
2022-05-14 22:39:52 +02:00
head := d.read_response_head()?
if head.status_code != 200 {
content_length := head.header.get(http.CommonHeader.content_length)?.int()
body := d.read_response_body(content_length)?
data := json.decode(DockerError, body)?
return error(data.message)
}
return d.get_stream_format_reader()
}