Compare commits
23 Commits
Author | SHA1 | Date |
---|---|---|
Jef Roosens | 46fcbd8be0 | |
Jef Roosens | af2512034c | |
Jef Roosens | d47a3c71a1 | |
Jef Roosens | 12d8e5c04d | |
Jef Roosens | 4db13cdd6e | |
Jef Roosens | 7913b271dd | |
Jef Roosens | 0743085bac | |
Jef Roosens | ea8e49d55f | |
Jef Roosens | d31681843c | |
Jef Roosens | 5bc54c37f3 | |
Jef Roosens | 9258bb8146 | |
Jef Roosens | 6b578a80cd | |
Jef Roosens | 2db4afc226 | |
Jef Roosens | 7149c93b6f | |
Jef Roosens | c8191a19e1 | |
Jef Roosens | 9aebb3bedc | |
Jef Roosens | 598597f726 | |
Jef Roosens | 2dcee45406 | |
Jef Roosens | 20874bfb0b | |
Jef Roosens | f881122b48 | |
Jef Roosens | db0818024e | |
Jef Roosens | 847a904e8d | |
Jef Roosens | 60fd13d25a |
|
@ -1,3 +1,4 @@
|
||||||
*.so
|
*.so
|
||||||
|
|
||||||
_docs/
|
_docs/
|
||||||
|
vls.log
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
variables:
|
||||||
|
- &vlang_image 'git.rustybever.be/chewing_bever/vlang:0.3.3-alpine3.17'
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
exclude: [ main ]
|
exclude: [ main ]
|
||||||
|
|
||||||
|
@ -5,14 +8,14 @@ platform: 'linux/amd64'
|
||||||
|
|
||||||
pipeline:
|
pipeline:
|
||||||
build:
|
build:
|
||||||
image: 'git.rustybever.be/chewing_bever/vlang:0.3.2'
|
image: *vlang_image
|
||||||
commands:
|
commands:
|
||||||
- make
|
- make
|
||||||
when:
|
when:
|
||||||
event: [ push ]
|
event: [ push ]
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
image: 'git.rustybever.be/chewing_bever/vlang:0.3.2'
|
image: *vlang_image
|
||||||
commands:
|
commands:
|
||||||
- make lint
|
- make lint
|
||||||
when:
|
when:
|
||||||
|
|
6
Makefile
6
Makefile
|
@ -1,6 +1,6 @@
|
||||||
# =====CONFIG=====
|
# =====CONFIG=====
|
||||||
V_PATH ?= v
|
V_PATH ?= v
|
||||||
V := $(V_PATH) -showcc
|
V := $(V_PATH) -showcc -d use_openssl
|
||||||
|
|
||||||
all: vdocker
|
all: vdocker
|
||||||
|
|
||||||
|
@ -10,6 +10,10 @@ all: vdocker
|
||||||
vdocker:
|
vdocker:
|
||||||
$(V) -g -shared .
|
$(V) -g -shared .
|
||||||
|
|
||||||
|
.PHONY: c
|
||||||
|
c:
|
||||||
|
$(V) -o docker.c .
|
||||||
|
|
||||||
|
|
||||||
# =====DOCS=====
|
# =====DOCS=====
|
||||||
.PHONY: api-docs
|
.PHONY: api-docs
|
||||||
|
|
|
@ -20,7 +20,7 @@ reference](https://docs.docker.com/engine/api/v1.41/).
|
||||||
- [ ] Start a container
|
- [ ] Start a container
|
||||||
- [ ] Stop a container
|
- [ ] Stop a container
|
||||||
- [ ] Restart a container
|
- [ ] Restart a container
|
||||||
- [ ] Kill a container
|
- [x] Kill a container
|
||||||
- [ ] Update a container
|
- [ ] Update a container
|
||||||
- [ ] Rename a container
|
- [ ] Rename a container
|
||||||
- [ ] Pause a container
|
- [ ] Pause a container
|
||||||
|
@ -42,7 +42,7 @@ reference](https://docs.docker.com/engine/api/v1.41/).
|
||||||
- [ ] Inspect an image
|
- [ ] Inspect an image
|
||||||
- [ ] Get the history of an image
|
- [ ] Get the history of an image
|
||||||
- [ ] Push an image
|
- [ ] Push an image
|
||||||
- [ ] Tag an image
|
- [x] Tag an image
|
||||||
- [ ] Remove an image
|
- [ ] Remove an image
|
||||||
- [ ] Search images
|
- [ ] Search images
|
||||||
- [ ] Delete unused images
|
- [ ] Delete unused images
|
||||||
|
|
154
containers.v
154
containers.v
|
@ -1,81 +1,22 @@
|
||||||
module docker
|
module docker
|
||||||
|
|
||||||
import json
|
|
||||||
import time
|
import time
|
||||||
import net.http { Method }
|
import types { ContainerListItem }
|
||||||
|
|
||||||
pub struct Port {
|
[params]
|
||||||
ip string [json: IP]
|
pub struct ContainerListConfig {
|
||||||
private_port u16 [json: PrivatePort]
|
all bool
|
||||||
public_port u16 [json: PublicPort]
|
limit int
|
||||||
type_ string [json: Type]
|
size bool
|
||||||
|
filters map[string][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HostConfig {
|
pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem {
|
||||||
network_mode string [json: NetworkMode]
|
d.request(.get, '/containers/json')
|
||||||
}
|
d.params(c)
|
||||||
|
d.send()!
|
||||||
|
|
||||||
pub struct EndpointIpamConfig {
|
return d.read_json_response[[]ContainerListItem]()
|
||||||
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]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ContainerListItem {
|
|
||||||
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]
|
|
||||||
size_root_fs i64 [json: SizeRootFs]
|
|
||||||
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]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_list() ![]ContainerListItem {
|
|
||||||
d.send_request(Method.get, '/containers/json')!
|
|
||||||
|
|
||||||
data := d.read_json_response<[]ContainerListItem>()!
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NewContainer {
|
pub struct NewContainer {
|
||||||
|
@ -94,30 +35,18 @@ pub:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
|
pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer {
|
||||||
d.send_request_with_json(Method.post, '/containers/create', c)!
|
d.request(.post, '/containers/create')
|
||||||
head, res := d.read_response()!
|
d.body_json(c)
|
||||||
|
d.send()!
|
||||||
|
|
||||||
if head.status_code != 201 {
|
return d.read_json_response[CreatedContainer]()
|
||||||
data := json.decode(DockerError, res)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := json.decode(CreatedContainer, res)!
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start_container starts the container with the given id.
|
// start_container starts the container with the given id.
|
||||||
pub fn (mut d DockerConn) container_start(id string) ! {
|
pub fn (mut d DockerConn) container_start(id string) ! {
|
||||||
d.send_request(Method.post, '/containers/$id/start')!
|
d.request(.post, '/containers/${id}/start')
|
||||||
head, body := d.read_response()!
|
d.send()!
|
||||||
|
d.read_response()!
|
||||||
if head.status_code != 204 {
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ContainerInspect {
|
struct ContainerInspect {
|
||||||
|
@ -139,16 +68,10 @@ pub mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
||||||
d.send_request(Method.get, '/containers/$id/json')!
|
d.request(.get, '/containers/${id}/json')
|
||||||
head, body := d.read_response()!
|
d.send()!
|
||||||
|
|
||||||
if head.status_code != 200 {
|
mut data := d.read_json_response[ContainerInspect]()!
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
mut data := json.decode(ContainerInspect, body)!
|
|
||||||
|
|
||||||
// The Docker engine API *should* always return UTC time.
|
// The Docker engine API *should* always return UTC time.
|
||||||
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
|
data.state.start_time = time.parse_rfc3339(data.state.start_time_str)!
|
||||||
|
@ -161,27 +84,26 @@ pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_remove(id string) ! {
|
pub fn (mut d DockerConn) container_remove(id string) ! {
|
||||||
d.send_request(Method.delete, '/containers/$id')!
|
d.request(.delete, '/containers/${id}')
|
||||||
head, body := d.read_response()!
|
d.send()!
|
||||||
|
d.read_response()!
|
||||||
|
}
|
||||||
|
|
||||||
if head.status_code != 204 {
|
pub fn (mut d DockerConn) container_kill(id string) ! {
|
||||||
data := json.decode(DockerError, body)!
|
d.request(.post, '/containers/${id}/kill')
|
||||||
|
d.send()!
|
||||||
return error(data.message)
|
d.read_response()!
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader {
|
||||||
d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')!
|
d.request(.get, '/containers/${id}/logs')
|
||||||
head := d.read_response_head()!
|
d.params({
|
||||||
|
'stdout': 'true'
|
||||||
if head.status_code != 200 {
|
'stderr': 'true'
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
})
|
||||||
body := d.read_response_body(content_length)!
|
d.send()!
|
||||||
data := json.decode(DockerError, body)!
|
d.read_response_head()!
|
||||||
|
d.check_error()!
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.get_stream_format_reader()
|
return d.get_stream_format_reader()
|
||||||
}
|
}
|
||||||
|
|
148
docker.v
148
docker.v
|
@ -4,7 +4,6 @@ import net.unix
|
||||||
import io
|
import io
|
||||||
import net.http
|
import net.http
|
||||||
import strings
|
import strings
|
||||||
import net.urllib
|
|
||||||
import json
|
import json
|
||||||
import util
|
import util
|
||||||
|
|
||||||
|
@ -17,10 +16,21 @@ const (
|
||||||
api_version = 'v1.41'
|
api_version = 'v1.41'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
[heap]
|
||||||
pub struct DockerConn {
|
pub struct DockerConn {
|
||||||
mut:
|
mut:
|
||||||
socket &unix.StreamConn
|
socket &unix.StreamConn
|
||||||
reader &io.BufferedReader
|
reader &io.BufferedReader
|
||||||
|
// Data for the request that's currently being constructed.
|
||||||
|
method http.Method
|
||||||
|
url string
|
||||||
|
params map[string]string
|
||||||
|
content_type string
|
||||||
|
// Before send: body of the request
|
||||||
|
// After send: body of response
|
||||||
|
body string
|
||||||
|
// HTTP head of the response
|
||||||
|
head http.Response
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_conn creates a new connection to the Docker daemon.
|
// new_conn creates a new connection to the Docker daemon.
|
||||||
|
@ -40,99 +50,71 @@ pub fn (mut d DockerConn) close() ! {
|
||||||
d.socket.close()!
|
d.socket.close()!
|
||||||
}
|
}
|
||||||
|
|
||||||
// send_request sends an HTTP request without body.
|
|
||||||
fn (mut d DockerConn) send_request(method http.Method, url_str string) ! {
|
|
||||||
url := urllib.parse('/$docker.api_version$url_str')!
|
|
||||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
|
||||||
|
|
||||||
d.socket.write_string(req)!
|
|
||||||
|
|
||||||
// When starting a new request, the reader needs to be reset.
|
|
||||||
d.reader = io.new_buffered_reader(reader: d.socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
// send_request_with_body sends an HTTP request with the given body.
|
|
||||||
fn (mut d DockerConn) send_request_with_body(method http.Method, url_str string, content_type string, body string) ! {
|
|
||||||
url := urllib.parse('/$docker.api_version$url_str')!
|
|
||||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
|
|
||||||
|
|
||||||
d.socket.write_string(req)!
|
|
||||||
|
|
||||||
// When starting a new request, the reader needs to be reset.
|
|
||||||
d.reader = io.new_buffered_reader(reader: d.socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
// send_request_with_json<T> is a convenience wrapper around
|
|
||||||
// send_request_with_body that encodes the input as JSON.
|
|
||||||
fn (mut d DockerConn) send_request_with_json<T>(method http.Method, url_str string, data &T) ! {
|
|
||||||
body := json.encode(data)
|
|
||||||
|
|
||||||
return d.send_request_with_body(method, url_str, 'application/json', body)
|
|
||||||
}
|
|
||||||
|
|
||||||
// read_response_head consumes the socket's contents until it encounters
|
// read_response_head consumes the socket's contents until it encounters
|
||||||
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
||||||
// Importantly, this function never consumes the reader past the HTTP
|
// Importantly, this function never consumes the reader past the HTTP
|
||||||
// separator, so the body can be read fully later on.
|
// separator, so the body can be read fully later on.
|
||||||
fn (mut d DockerConn) read_response_head() !http.Response {
|
fn (mut d DockerConn) read_response_head() ! {
|
||||||
mut res := []u8{}
|
mut res := []u8{}
|
||||||
|
|
||||||
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
|
util.read_until_separator(mut d.reader, mut res, docker.http_separator)!
|
||||||
|
|
||||||
return http.parse_response(res.bytestr())
|
d.head = http.parse_response(res.bytestr())!
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_response_body reads `length` bytes from the stream. It can be used when
|
// read_response_body consumes the rest of the HTTP response and stores it as
|
||||||
// the response encoding isn't chunked to fully read it.
|
// the response body. This function should only be called after
|
||||||
fn (mut d DockerConn) read_response_body(length int) !string {
|
// read_response_head. This function always reads the entire response into
|
||||||
if length == 0 {
|
// memory, even if it's chunked.
|
||||||
return ''
|
fn (mut d DockerConn) read_response_body() ! {
|
||||||
|
if d.head.status() == .no_content {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mut buf := []u8{len: docker.buf_len}
|
|
||||||
mut c := 0
|
|
||||||
mut builder := strings.new_builder(docker.buf_len)
|
mut builder := strings.new_builder(docker.buf_len)
|
||||||
|
|
||||||
for builder.len < length {
|
if d.head.header.get(.transfer_encoding) or { '' } == 'chunked' {
|
||||||
c = d.reader.read(mut buf) or { break }
|
mut body_stream := d.get_chunked_response_reader()
|
||||||
|
|
||||||
builder.write(buf[..c])!
|
util.reader_to_writer(mut body_stream, mut builder)!
|
||||||
|
} else {
|
||||||
|
content_length := d.head.header.get(.content_length)!.int()
|
||||||
|
|
||||||
|
if content_length == 0 {
|
||||||
|
d.body = ''
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mut buf := []u8{len: docker.buf_len}
|
||||||
|
mut c := 0
|
||||||
|
|
||||||
|
for builder.len < content_length {
|
||||||
|
c = d.reader.read(mut buf)!
|
||||||
|
|
||||||
|
builder.write(buf[..c])!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.str()
|
d.body = builder.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_response is a convenience function which always consumes the entire
|
// read_response is a convenience function that always consumes the entire
|
||||||
// response & returns it. It should only be used when we're certain that the
|
// response and loads it into memory. It should only be used when we're certain
|
||||||
// result isn't too large.
|
// that the result isn't too large, as even chunked responses will get fully
|
||||||
fn (mut d DockerConn) read_response() !(http.Response, string) {
|
// loaded into memory.
|
||||||
head := d.read_response_head()!
|
fn (mut d DockerConn) read_response() ! {
|
||||||
|
d.read_response_head()!
|
||||||
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
d.check_error()!
|
||||||
mut builder := strings.new_builder(1024)
|
d.read_response_body()!
|
||||||
mut body := d.get_chunked_response_reader()
|
|
||||||
|
|
||||||
util.reader_to_writer(mut body, mut builder)!
|
|
||||||
|
|
||||||
return head, builder.str()
|
|
||||||
}
|
|
||||||
|
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
|
||||||
res := d.read_response_body(content_length)!
|
|
||||||
|
|
||||||
return head, res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut d DockerConn) read_json_response<T>() !T {
|
// read_json_response<T> is a convenience function that runs read_response
|
||||||
head, body := d.read_response()!
|
// before parsing its contents, which is assumed to be JSON, into a struct.
|
||||||
|
fn (mut d DockerConn) read_json_response[T]() !T {
|
||||||
|
d.read_response()!
|
||||||
|
|
||||||
if head.status_code < 200 || head.status_code > 300 {
|
data := json.decode(T, d.body)!
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return docker_error(head.status_code, data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
mut data := json.decode(T, body)!
|
|
||||||
|
|
||||||
//$for field in T.fields {
|
//$for field in T.fields {
|
||||||
//$if field.typ is time.Time {
|
//$if field.typ is time.Time {
|
||||||
|
@ -144,7 +126,7 @@ fn (mut d DockerConn) read_json_response<T>() !T {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
|
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
|
||||||
// as reader.
|
// as reader. This function should only be called after check_error.
|
||||||
fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
||||||
r := new_chunked_response_reader(d.reader)
|
r := new_chunked_response_reader(d.reader)
|
||||||
|
|
||||||
|
@ -152,10 +134,28 @@ fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_stream_format_reader returns a StreamFormatReader using the socket as
|
// get_stream_format_reader returns a StreamFormatReader using the socket as
|
||||||
// reader.
|
// reader. This function should only be called after check_error.
|
||||||
fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
||||||
r := new_chunked_response_reader(d.reader)
|
r := new_chunked_response_reader(d.reader)
|
||||||
r2 := new_stream_format_reader(r)
|
r2 := new_stream_format_reader(r)
|
||||||
|
|
||||||
return r2
|
return r2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DockerError {
|
||||||
|
pub:
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
|
// check_error should be called after read_response_head. If the status code of
|
||||||
|
// the response is an error, the body is consumed and the Docker HTTP error is
|
||||||
|
// returned as a V error. If the status isn't the error, this function is a
|
||||||
|
// no-op, and the body can be read.
|
||||||
|
fn (mut d DockerConn) check_error() ! {
|
||||||
|
if d.head.status().is_error() {
|
||||||
|
d.read_response_body()!
|
||||||
|
d_err := json.decode(DockerError, d.body)!
|
||||||
|
|
||||||
|
return error_with_code('${d.head.status()}: ${d_err.message}', d.head.status_code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
21
errors.v
21
errors.v
|
@ -1,21 +0,0 @@
|
||||||
module docker
|
|
||||||
|
|
||||||
struct DockerError {
|
|
||||||
status int [skip]
|
|
||||||
message string
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (err DockerError) code() int {
|
|
||||||
return err.status
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (err DockerError) msg() string {
|
|
||||||
return err.message
|
|
||||||
}
|
|
||||||
|
|
||||||
fn docker_error(status int, message string) IError {
|
|
||||||
return IError(DockerError{
|
|
||||||
status: status
|
|
||||||
message: message
|
|
||||||
})
|
|
||||||
}
|
|
80
images.v
80
images.v
|
@ -1,25 +1,26 @@
|
||||||
module docker
|
module docker
|
||||||
|
|
||||||
import net.http { Method }
|
import types { Image }
|
||||||
import json
|
|
||||||
|
|
||||||
struct Image {
|
pub fn (mut d DockerConn) image_inspect(image string) !Image {
|
||||||
pub:
|
d.request(.get, '/images/${image}/json')
|
||||||
id string [json: Id]
|
d.send()!
|
||||||
|
|
||||||
|
data := d.read_json_response[Image]()!
|
||||||
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// pull_image pulls the given image:tag.
|
// image_pull pulls the given image:tag.
|
||||||
pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
|
pub fn (mut d DockerConn) image_pull(image string, tag string) ! {
|
||||||
d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')!
|
d.request(.post, '/images/create')
|
||||||
head := d.read_response_head()!
|
d.params({
|
||||||
|
'fromImage': image
|
||||||
if head.status_code != 200 {
|
'tag': tag
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
})
|
||||||
body := d.read_response_body(content_length)!
|
d.send()!
|
||||||
data := json.decode(DockerError, body)!
|
d.read_response_head()!
|
||||||
|
d.check_error()!
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep reading the body until the pull has completed
|
// Keep reading the body until the pull has completed
|
||||||
mut body := d.get_chunked_response_reader()
|
mut body := d.get_chunked_response_reader()
|
||||||
|
@ -32,29 +33,32 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create_image_from_container creates a new image from a container.
|
// create_image_from_container creates a new image from a container.
|
||||||
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) !Image {
|
pub fn (mut d DockerConn) image_from_container(id string, repo string, tag string) !Image {
|
||||||
d.send_request(Method.post, '/commit?container=$id&repo=$repo&tag=$tag')!
|
d.request(.post, '/commit')
|
||||||
head, body := d.read_response()!
|
d.params({
|
||||||
|
'container': id
|
||||||
|
'repo': repo
|
||||||
|
'tag': tag
|
||||||
|
})
|
||||||
|
d.body('application/json', '{}')
|
||||||
|
d.send()!
|
||||||
|
|
||||||
if head.status_code != 201 {
|
return d.read_json_response[Image]()!
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := json.decode(Image, body)!
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove_image removes the image with the given id.
|
// remove_image removes the image with the given id.
|
||||||
pub fn (mut d DockerConn) remove_image(id string) ! {
|
pub fn (mut d DockerConn) image_remove(id string) ! {
|
||||||
d.send_request(Method.delete, '/images/$id')!
|
d.request(.delete, '/images/${id}')
|
||||||
head, body := d.read_response()!
|
d.send()!
|
||||||
|
d.read_response()!
|
||||||
if head.status_code != 200 {
|
}
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! {
|
||||||
return error(data.message)
|
d.request(.post, '/images/${name}/tag')
|
||||||
}
|
d.params({
|
||||||
|
'repo': repo
|
||||||
|
'tag': tag
|
||||||
|
})
|
||||||
|
d.send()!
|
||||||
|
d.read_response()!
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
module docker
|
||||||
|
|
||||||
|
import net.http
|
||||||
|
import net.urllib
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
|
||||||
|
fn (mut d DockerConn) request(method http.Method, url string) {
|
||||||
|
d.method = method
|
||||||
|
d.url = url
|
||||||
|
d.content_type = ''
|
||||||
|
d.body = ''
|
||||||
|
|
||||||
|
d.params.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut d DockerConn) body(content_type string, body string) {
|
||||||
|
d.content_type = content_type
|
||||||
|
d.body = body
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut d DockerConn) body_json[T](data T) {
|
||||||
|
d.content_type = 'application/json'
|
||||||
|
d.body = json.encode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut d DockerConn) params[T](o T) {
|
||||||
|
$if T is map[string]string {
|
||||||
|
for key, value in o {
|
||||||
|
d.params[key] = urllib.query_escape(value.replace("'", '"'))
|
||||||
|
}
|
||||||
|
} $else {
|
||||||
|
$for field in T.fields {
|
||||||
|
v := o.$(field.name)
|
||||||
|
|
||||||
|
if !isnil(v) {
|
||||||
|
d.params[field.name] = urllib.query_escape(v.str().replace("'", '"'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut d DockerConn) send() ! {
|
||||||
|
mut full_url := '/${docker.api_version}${d.url}'
|
||||||
|
|
||||||
|
if d.params.len > 0 {
|
||||||
|
mut fields := []string{cap: d.params.len}
|
||||||
|
|
||||||
|
for key, value in d.params {
|
||||||
|
fields << '${key}=${value}'
|
||||||
|
}
|
||||||
|
params_str := fields.join('&')
|
||||||
|
// params_str := d.params.keys().map('${it}=${d.params[it]}').join('&')
|
||||||
|
full_url += '?${params_str}'
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is to make sure we actually created a valid URL
|
||||||
|
parsed_url := urllib.parse(full_url)!
|
||||||
|
final_url := parsed_url.request_uri()
|
||||||
|
|
||||||
|
req := if d.body == '' {
|
||||||
|
'${d.method} ${final_url} HTTP/1.1\nHost: localhost\n\n'
|
||||||
|
} else {
|
||||||
|
'${d.method} ${final_url} HTTP/1.1\nHost: localhost\nContent-Type: ${d.content_type}\nContent-Length: ${d.body.len}\n\n${d.body}\n\n'
|
||||||
|
}
|
||||||
|
|
||||||
|
d.socket.write_string(req)!
|
||||||
|
|
||||||
|
// When starting a new request, the reader needs to be reset.
|
||||||
|
d.reader = io.new_buffered_reader(reader: d.socket)
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
module types
|
||||||
|
|
||||||
|
pub struct Port {
|
||||||
|
pub:
|
||||||
|
ip string [json: IP]
|
||||||
|
private_port u16 [json: PrivatePort]
|
||||||
|
public_port u16 [json: PublicPort]
|
||||||
|
type_ string [json: Type]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HostConfig {
|
||||||
|
pub:
|
||||||
|
network_mode string [json: NetworkMode]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EndpointIpamConfig {
|
||||||
|
pub:
|
||||||
|
ipv4_address string [json: IPv4Address]
|
||||||
|
ipv6_address string [json: IPv6Address]
|
||||||
|
link_local_ips []string [json: LinkLocalIPs]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EndpointSettings {
|
||||||
|
pub:
|
||||||
|
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 {
|
||||||
|
pub:
|
||||||
|
networks map[string]EndpointSettings [json: Networks]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MountPoint {
|
||||||
|
pub:
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ContainerListItem {
|
||||||
|
pub:
|
||||||
|
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]
|
||||||
|
size_root_fs i64 [json: SizeRootFs]
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HealthConfig {
|
||||||
|
pub:
|
||||||
|
test []string [json: Test]
|
||||||
|
interval int [json: Interval]
|
||||||
|
timeout int [json: Timeout]
|
||||||
|
retries int [json: Retries]
|
||||||
|
start_period int [json: StartPeriod]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ContainerCreate {
|
||||||
|
pub:
|
||||||
|
hostname string [json: Hostname]
|
||||||
|
domain_name string [json: Domainname]
|
||||||
|
user string [json: User]
|
||||||
|
attach_stdin bool [json: AttachStdin]
|
||||||
|
attach_stdout bool [json: AttachStderr] = true
|
||||||
|
// ExposedPorts
|
||||||
|
tty bool [json: Tty]
|
||||||
|
open_stdin bool [json: OpenStdin]
|
||||||
|
stdin_once bool [json: StdinOnce]
|
||||||
|
env []string [json: Env]
|
||||||
|
cmd []string [json: Cmd]
|
||||||
|
healthcheck HealthConfig [json: Healthcheck]
|
||||||
|
args_escaped bool [json: ArgsEscaped]
|
||||||
|
image string [json: Image]
|
||||||
|
// Volumes
|
||||||
|
working_dir string [json: WorkingDir]
|
||||||
|
entrypoint []string [json: Entrypoint]
|
||||||
|
network_disabled bool [json: NetworkDisabled]
|
||||||
|
mac_address string [json: MacAddress]
|
||||||
|
on_build []string [json: OnBuild]
|
||||||
|
labels map[string]string [json: Labels]
|
||||||
|
stop_signal string [json: StopSignal]
|
||||||
|
stop_timeout int [json: StopTimeout]
|
||||||
|
shell []string [json: Shell]
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
module types
|
||||||
|
|
||||||
|
pub struct Image {
|
||||||
|
pub:
|
||||||
|
id string [json: Id]
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
module types
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
pub struct UsageData {
|
||||||
|
size int [json: Size]
|
||||||
|
ref_count int [json: RefCount]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Volume {
|
||||||
|
pub mut:
|
||||||
|
created_at_str string [json: CreatedAt]
|
||||||
|
created_at time.Time [skip]
|
||||||
|
name string [json: Name]
|
||||||
|
driver string [json: Driver]
|
||||||
|
mountpoint string [json: Mountpoint]
|
||||||
|
status map[string]string [json: Status]
|
||||||
|
labels map[string]string [json: Labels]
|
||||||
|
scope string [json: Scope]
|
||||||
|
options map[string]string [json: Options]
|
||||||
|
usage_data UsageData [json: UsageData]
|
||||||
|
}
|
|
@ -21,7 +21,7 @@ pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ! {
|
||||||
// match_array_in_array<T> returns how many elements of a2 overlap with a1. For
|
// match_array_in_array<T> returns how many elements of a2 overlap with a1. For
|
||||||
// example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is
|
// example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is
|
||||||
// not at the end of a1, the result is 0.
|
// not at the end of a1, the result is 0.
|
||||||
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
|
pub fn match_array_in_array[T](a1 []T, a2 []T) int {
|
||||||
mut i := 0
|
mut i := 0
|
||||||
mut match_len := 0
|
mut match_len := 0
|
||||||
|
|
||||||
|
|
26
volumes.v
26
volumes.v
|
@ -1,26 +1,7 @@
|
||||||
module docker
|
module docker
|
||||||
|
|
||||||
import net.http { Method }
|
|
||||||
import time
|
import time
|
||||||
|
import types { Volume }
|
||||||
struct UsageData {
|
|
||||||
size int [json: Size]
|
|
||||||
ref_count int [json: RefCount]
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Volume {
|
|
||||||
created_at_str string [json: CreatedAt]
|
|
||||||
pub mut:
|
|
||||||
created_at time.Time [skip]
|
|
||||||
name string [json: Name]
|
|
||||||
driver string [json: Driver]
|
|
||||||
mountpoint string [json: Mountpoint]
|
|
||||||
status map[string]string [json: Status]
|
|
||||||
labels map[string]string [json: Labels]
|
|
||||||
scope string [json: Scope]
|
|
||||||
options map[string]string [json: Options]
|
|
||||||
usage_data UsageData [json: UsageData]
|
|
||||||
}
|
|
||||||
|
|
||||||
[params]
|
[params]
|
||||||
pub struct VolumeListFilter {
|
pub struct VolumeListFilter {
|
||||||
|
@ -36,9 +17,10 @@ struct VolumeListResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerConn) volume_list() !VolumeListResponse {
|
pub fn (mut d DockerConn) volume_list() !VolumeListResponse {
|
||||||
d.send_request(Method.get, '/volumes')!
|
d.request(.get, '/volumes')
|
||||||
|
d.send()!
|
||||||
|
|
||||||
mut data := d.read_json_response<VolumeListResponse>()!
|
mut data := d.read_json_response[VolumeListResponse]()!
|
||||||
|
|
||||||
for mut vol in data.volumes {
|
for mut vol in data.volumes {
|
||||||
vol.created_at = time.parse_rfc3339(vol.created_at_str)!
|
vol.created_at = time.parse_rfc3339(vol.created_at_str)!
|
||||||
|
|
Loading…
Reference in New Issue