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 time
|
2022-06-23 20:26:45 +02:00
|
|
|
import types { ContainerListItem }
|
2022-06-18 17:59:22 +02:00
|
|
|
|
2022-06-23 20:26:45 +02:00
|
|
|
[params]
|
|
|
|
pub struct ContainerListConfig {
|
|
|
|
all bool
|
|
|
|
limit int
|
|
|
|
size bool
|
|
|
|
filters map[string][]string
|
2022-06-21 20:28:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-01 20:47:28 +01:00
|
|
|
pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.get, '/containers/json', {})
|
2022-06-23 20:26:45 +02:00
|
|
|
d.params(c)
|
2022-11-01 20:47:28 +01:00
|
|
|
d.send()!
|
2022-06-21 20:28:25 +02:00
|
|
|
|
2022-06-23 20:26:45 +02:00
|
|
|
return d.read_json_response<[]ContainerListItem>()
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NewContainer {
|
|
|
|
image string [json: Image]
|
|
|
|
entrypoint []string [json: Entrypoint]
|
|
|
|
cmd []string [json: Cmd]
|
|
|
|
env []string [json: Env]
|
|
|
|
work_dir string [json: WorkingDir]
|
|
|
|
user string [json: User]
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CreatedContainer {
|
|
|
|
pub:
|
|
|
|
id string [json: Id]
|
|
|
|
warnings []string [json: Warnings]
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.post, '/containers/create', {})
|
|
|
|
d.body_json(c)
|
|
|
|
d.send()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
2023-01-05 11:52:07 +01:00
|
|
|
return d.read_json_response<CreatedContainer>()
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// start_container starts the container with the given id.
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_start(id string) ! {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.post, '/containers/$id/start', {})
|
|
|
|
d.send()!
|
2023-01-05 11:52:07 +01:00
|
|
|
d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ContainerInspect {
|
|
|
|
pub mut:
|
|
|
|
state ContainerState [json: State]
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContainerState {
|
|
|
|
pub:
|
|
|
|
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]
|
|
|
|
end_time_str string [json: FinishedAt]
|
|
|
|
pub mut:
|
|
|
|
start_time time.Time [skip]
|
|
|
|
end_time time.Time [skip]
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.get, '/containers/$id/json', {})
|
|
|
|
d.send()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
2023-01-05 11:52:07 +01:00
|
|
|
mut data := d.read_json_response<ContainerInspect>()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
// The Docker engine API *should* always return UTC time.
|
2022-11-01 19:19:52 +01:00
|
|
|
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if data.state.status == 'exited' {
|
2022-11-01 19:19:52 +01:00
|
|
|
data.state.end_time = time.parse_rfc3339(data.state.end_time_str)!
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_remove(id string) ! {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.delete, '/containers/$id', {})
|
|
|
|
d.send()!
|
2023-01-05 11:52:07 +01:00
|
|
|
d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
2023-01-05 12:53:30 +01:00
|
|
|
d.request(.get, '/containers/$id/logs', {
|
|
|
|
'stdout': 'true'
|
|
|
|
'stderr': 'true'
|
|
|
|
})
|
|
|
|
d.send()!
|
2023-01-05 11:52:07 +01:00
|
|
|
d.read_response_head()!
|
|
|
|
d.check_error()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return d.get_stream_format_reader()
|
|
|
|
}
|