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 json
|
|
|
|
import time
|
|
|
|
import net.http { Method }
|
|
|
|
|
2022-06-21 20:28:25 +02:00
|
|
|
pub struct Port {
|
|
|
|
ip string [json: IP]
|
|
|
|
private_port u16 [json: PrivatePort]
|
|
|
|
public_port u16 [json: PublicPort]
|
|
|
|
type_ string [json: Type]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct HostConfig {
|
|
|
|
network_mode string [json: NetworkMode]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EndpointIpamConfig {
|
|
|
|
ipv4_address string [json: IPv4Address]
|
|
|
|
ipv6_address string [json: IPv6Address]
|
|
|
|
link_local_ips []string [json: LinkLocalIPs]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EndpointSettings {
|
|
|
|
ipam_config EndpointIpamConfig [json: IPAMConfig]
|
|
|
|
links []string [json: Links]
|
|
|
|
aliases []string [json: Aliases]
|
|
|
|
network_id string [json: NetworkID]
|
|
|
|
endpoint_id string [json: EndpointID]
|
|
|
|
gateway string [json: Gateway]
|
|
|
|
ip_address string [json: IPAddress]
|
|
|
|
ip_prefix_len int [json: IPPrefixLen]
|
|
|
|
ipv6_gateway string [json: IPv6Gateway]
|
|
|
|
global_ipv6_address string [json: GlobalIPv6Address]
|
|
|
|
global_ipv6_prefix_len i64 [json: GlobalIPv6PrefixLen]
|
|
|
|
mac_address string [json: MacAddress]
|
|
|
|
driver_opts map[string]string [json: DriverOpts]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NetworkSettings {
|
|
|
|
networks map[string]EndpointSettings [json: Networks]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MountPoint {
|
|
|
|
type_ string [json: Type]
|
|
|
|
name string [json: Name]
|
|
|
|
source string [json: Source]
|
|
|
|
destination string [json: Destination]
|
|
|
|
driver string [json: Driver]
|
|
|
|
mode string [json: Mode]
|
|
|
|
rw bool [json: RW]
|
|
|
|
propagation string [json: Propagation]
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:47:42 +02:00
|
|
|
pub struct ContainerListItem {
|
2022-06-21 20:28:25 +02:00
|
|
|
id string [json: Id]
|
|
|
|
names []string [json: Names]
|
|
|
|
image string [json: Image]
|
|
|
|
image_id string [json: ImageID]
|
|
|
|
command string [json: Command]
|
|
|
|
created i64 [json: Created]
|
|
|
|
ports []Port [json: Ports]
|
|
|
|
size_rw i64 [json: SizeRw]
|
2022-06-22 08:17:58 +02:00
|
|
|
size_root_fs i64 [json: SizeRootFs]
|
2022-06-21 20:28:25 +02:00
|
|
|
labels map[string]string [json: Labels]
|
|
|
|
state string [json: State]
|
|
|
|
status string [json: Status]
|
|
|
|
host_config HostConfig [json: HostConfig]
|
|
|
|
network_settings NetworkSettings [json: NetworkSettings]
|
|
|
|
mounts []MountPoint [json: Mounts]
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_list() ![]ContainerListItem {
|
|
|
|
d.send_request(Method.get, '/containers/json')!
|
2022-06-21 20:28:25 +02:00
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
data := d.read_json_response<[]ContainerListItem>()!
|
2022-06-21 20:28:25 +02:00
|
|
|
|
|
|
|
return data
|
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 {
|
|
|
|
d.send_request_with_json(Method.post, '/containers/create', c)!
|
|
|
|
head, res := d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.status_code != 201 {
|
2022-11-01 19:19:52 +01:00
|
|
|
data := json.decode(DockerError, res)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return error(data.message)
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
data := json.decode(CreatedContainer, res)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) ! {
|
|
|
|
d.send_request(Method.post, '/containers/$id/start')!
|
|
|
|
head, body := d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.status_code != 204 {
|
2022-11-01 19:19:52 +01:00
|
|
|
data := json.decode(DockerError, body)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return error(data.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
d.send_request(Method.get, '/containers/$id/json')!
|
|
|
|
head, body := d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.status_code != 200 {
|
2022-11-01 19:19:52 +01:00
|
|
|
data := json.decode(DockerError, body)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return error(data.message)
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
mut data := json.decode(ContainerInspect, body)!
|
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) ! {
|
|
|
|
d.send_request(Method.delete, '/containers/$id')!
|
|
|
|
head, body := d.read_response()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.status_code != 204 {
|
2022-11-01 19:19:52 +01:00
|
|
|
data := json.decode(DockerError, body)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return error(data.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:19:52 +01:00
|
|
|
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
|
|
|
d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')!
|
|
|
|
head := d.read_response_head()!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
if head.status_code != 200 {
|
2022-11-01 19:19:52 +01:00
|
|
|
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
|
|
|
body := d.read_response_body(content_length)!
|
|
|
|
data := json.decode(DockerError, body)!
|
2022-06-18 17:59:22 +02:00
|
|
|
|
|
|
|
return error(data.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.get_stream_format_reader()
|
|
|
|
}
|