forked from vieter-v/vieter
Compare commits
2 Commits
97cdaa18e1
...
055b168ff1
| Author | SHA1 | Date |
|---|---|---|
|
|
055b168ff1 | |
|
|
1d3c7a1651 |
|
|
@ -9,9 +9,10 @@ import client
|
|||
import strings
|
||||
import util
|
||||
|
||||
const container_build_dir = '/build'
|
||||
|
||||
const build_image_repo = 'vieter-build'
|
||||
const (
|
||||
container_build_dir = '/build'
|
||||
build_image_repo = 'vieter-build'
|
||||
)
|
||||
|
||||
// create_build_image creates a builder image given some base image which can
|
||||
// then be used to build & package Arch images. It mostly just updates the
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
This module implements part of the Docker Engine API v1.41
|
||||
([documentation](https://docs.docker.com/engine/api/v1.41/)) using socket-based
|
||||
HTTP communication.
|
||||
|
|
@ -15,7 +15,7 @@ struct Container {
|
|||
}
|
||||
|
||||
// containers returns a list of all containers.
|
||||
pub fn (mut d DockerDaemon) containers() ?[]Container {
|
||||
pub fn (mut d DockerConn) containers() ?[]Container {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/json')?)?
|
||||
head, res := d.read_response()?
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ pub:
|
|||
}
|
||||
|
||||
// create_container creates a new container with the given config.
|
||||
pub fn (mut d DockerDaemon) create_container(c NewContainer) ?CreatedContainer {
|
||||
pub fn (mut d DockerConn) create_container(c NewContainer) ?CreatedContainer {
|
||||
d.send_request_with_json('POST', urllib.parse('/v1.41/containers/create')?, c)?
|
||||
head, res := d.read_response()?
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ pub fn (mut d DockerDaemon) create_container(c NewContainer) ?CreatedContainer {
|
|||
}
|
||||
|
||||
// start_container starts the container with the given id.
|
||||
pub fn (mut d DockerDaemon) start_container(id string) ? {
|
||||
pub fn (mut d DockerConn) start_container(id string) ? {
|
||||
d.send_request('POST', urllib.parse('/v1.41/containers/$id/start')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ pub mut:
|
|||
}
|
||||
|
||||
// inspect_container returns detailed information for a given container.
|
||||
pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect {
|
||||
pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/$id/json')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect {
|
|||
}
|
||||
|
||||
// remove_container removes the container with the given id.
|
||||
pub fn (mut d DockerDaemon) remove_container(id string) ? {
|
||||
pub fn (mut d DockerConn) remove_container(id string) ? {
|
||||
d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
@ -127,7 +127,7 @@ pub fn (mut d DockerDaemon) remove_container(id string) ? {
|
|||
|
||||
// get_container_logs returns a reader object allowing access to the
|
||||
// container's logs.
|
||||
pub fn (mut d DockerDaemon) get_container_logs(id string) ?&StreamFormatReader {
|
||||
pub fn (mut d DockerConn) get_container_logs(id string) ?&StreamFormatReader {
|
||||
d.send_request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
|
||||
head := d.read_response_head()?
|
||||
|
||||
|
|
|
|||
|
|
@ -15,17 +15,17 @@ const (
|
|||
http_chunk_separator = [u8(`\r`), `\n`]
|
||||
)
|
||||
|
||||
pub struct DockerDaemon {
|
||||
pub struct DockerConn {
|
||||
mut:
|
||||
socket &unix.StreamConn
|
||||
reader &io.BufferedReader
|
||||
}
|
||||
|
||||
// new_conn creates a new connection to the Docker daemon.
|
||||
pub fn new_conn() ?&DockerDaemon {
|
||||
pub fn new_conn() ?&DockerConn {
|
||||
s := unix.connect_stream(docker.socket)?
|
||||
|
||||
d := &DockerDaemon{
|
||||
d := &DockerConn{
|
||||
socket: s
|
||||
reader: io.new_buffered_reader(reader: s)
|
||||
}
|
||||
|
|
@ -34,12 +34,12 @@ pub fn new_conn() ?&DockerDaemon {
|
|||
}
|
||||
|
||||
// close closes the underlying socket connection.
|
||||
pub fn (mut d DockerDaemon) close() ? {
|
||||
pub fn (mut d DockerConn) close() ? {
|
||||
d.socket.close()?
|
||||
}
|
||||
|
||||
// send_request sends an HTTP request without body.
|
||||
pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
||||
pub fn (mut d DockerConn) send_request(method string, url urllib.URL) ? {
|
||||
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
|
||||
|
||||
d.socket.write_string(req)?
|
||||
|
|
@ -49,7 +49,7 @@ pub fn (mut d DockerDaemon) send_request(method string, url urllib.URL) ? {
|
|||
}
|
||||
|
||||
// send_request_with_body sends an HTTP request with the given body.
|
||||
pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
|
||||
pub fn (mut d DockerConn) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
|
||||
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)?
|
||||
|
|
@ -60,7 +60,7 @@ pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL
|
|||
|
||||
// send_request_with_json<T> is a convenience wrapper around
|
||||
// send_request_with_body that encodes the input as JSON.
|
||||
pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
||||
pub fn (mut d DockerConn) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
||||
body := json.encode(data)
|
||||
|
||||
return d.send_request_with_body(method, url, 'application/json', body)
|
||||
|
|
@ -70,17 +70,17 @@ pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.
|
|||
// '\r\n\r\n', after which it parses the response as an HTTP response.
|
||||
// Importantly, this function never consumes the reader past the HTTP
|
||||
// separator, so the body can be read fully later on.
|
||||
pub fn (mut d DockerDaemon) read_response_head() ?http.Response {
|
||||
pub fn (mut d DockerConn) read_response_head() ?http.Response {
|
||||
mut res := []u8{}
|
||||
|
||||
util.read_until_separator(mut d.reader, mut res, http_separator) ?
|
||||
util.read_until_separator(mut d.reader, mut res, docker.http_separator)?
|
||||
|
||||
return http.parse_response(res.bytestr())
|
||||
}
|
||||
|
||||
// read_response_body reads `length` bytes from the stream. It can be used when
|
||||
// the response encoding isn't chunked to fully read it.
|
||||
pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
||||
pub fn (mut d DockerConn) read_response_body(length int) ?string {
|
||||
if length == 0 {
|
||||
return ''
|
||||
}
|
||||
|
|
@ -101,7 +101,7 @@ pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
|||
// read_response is a convenience function which always consumes the entire
|
||||
// response & returns it. It should only be used when we're certain that the
|
||||
// result isn't too large.
|
||||
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
||||
pub fn (mut d DockerConn) read_response() ?(http.Response, string) {
|
||||
head := d.read_response_head()?
|
||||
|
||||
if head.header.get(http.CommonHeader.transfer_encoding) or { '' } == 'chunked' {
|
||||
|
|
@ -121,7 +121,7 @@ pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
|||
|
||||
// get_chunked_response_reader returns a ChunkedResponseReader using the socket
|
||||
// as reader.
|
||||
pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader {
|
||||
pub fn (mut d DockerConn) get_chunked_response_reader() &ChunkedResponseReader {
|
||||
r := new_chunked_response_reader(d.reader)
|
||||
|
||||
return r
|
||||
|
|
@ -129,7 +129,7 @@ pub fn (mut d DockerDaemon) get_chunked_response_reader() &ChunkedResponseReader
|
|||
|
||||
// get_stream_format_reader returns a StreamFormatReader using the socket as
|
||||
// reader.
|
||||
pub fn (mut d DockerDaemon) get_stream_format_reader() &StreamFormatReader {
|
||||
pub fn (mut d DockerConn) get_stream_format_reader() &StreamFormatReader {
|
||||
r := new_chunked_response_reader(d.reader)
|
||||
r2 := new_stream_format_reader(r)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ pub:
|
|||
}
|
||||
|
||||
// pull_image pulls the given image:tag.
|
||||
pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? {
|
||||
pub fn (mut d DockerConn) pull_image(image string, tag string) ? {
|
||||
d.send_request('POST', urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag')?)?
|
||||
head := d.read_response_head()?
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ pub fn (mut d DockerDaemon) pull_image(image string, tag string) ? {
|
|||
}
|
||||
|
||||
// create_image_from_container creates a new image from a container.
|
||||
pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string, tag string) ?Image {
|
||||
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) ?Image {
|
||||
d.send_request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
@ -49,7 +49,7 @@ pub fn (mut d DockerDaemon) create_image_from_container(id string, repo string,
|
|||
}
|
||||
|
||||
// remove_image removes the image with the given id.
|
||||
pub fn (mut d DockerDaemon) remove_image(id string) ? {
|
||||
pub fn (mut d DockerConn) remove_image(id string) ? {
|
||||
d.send_request('DELETE', urllib.parse('/v1.41/images/$id')?)?
|
||||
head, body := d.read_response()?
|
||||
|
||||
|
|
|
|||
|
|
@ -58,9 +58,9 @@ pub fn (mut r ChunkedResponseReader) read(mut buf []u8) ?int {
|
|||
// This function should only be called if the previous chunk has been
|
||||
// completely consumed.
|
||||
fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
||||
mut buf := []u8{len: 2}
|
||||
|
||||
if r.started {
|
||||
mut buf := []u8{len: 2}
|
||||
|
||||
// Each chunk ends with a `\r\n` which we want to skip first
|
||||
r.reader.read(mut buf)?
|
||||
}
|
||||
|
|
@ -68,7 +68,7 @@ fn (mut r ChunkedResponseReader) read_chunk_size() ?u64 {
|
|||
r.started = true
|
||||
|
||||
mut res := []u8{}
|
||||
util.read_until_separator(mut r.reader, mut res, http_chunk_separator) ?
|
||||
util.read_until_separator(mut r.reader, mut res, http_chunk_separator)?
|
||||
|
||||
// The length of the next chunk is provided as a hexadecimal
|
||||
mut num_data := hex.decode(res#[..-2].bytestr())?
|
||||
|
|
|
|||
|
|
@ -3,12 +3,13 @@ module env
|
|||
import os
|
||||
import toml
|
||||
|
||||
// The prefix that every environment variable should have
|
||||
const prefix = 'VIETER_'
|
||||
|
||||
// The suffix an environment variable in order for it to be loaded from a file
|
||||
// instead
|
||||
const file_suffix = '_FILE'
|
||||
const (
|
||||
// The prefix that every environment variable should have
|
||||
prefix = 'VIETER_'
|
||||
// The suffix an environment variable in order for it to be loaded from a file
|
||||
// instead
|
||||
file_suffix = '_FILE'
|
||||
)
|
||||
|
||||
fn get_env_var(field_name string) ?string {
|
||||
env_var_name := '$env.prefix$field_name.to_upper()'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
This module defines a few useful functions used throughout the codebase that
|
||||
don't specifically fit inside a module.
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
// Functions for interacting with `io.Reader` & `io.Writer` objects.
|
||||
module util
|
||||
|
||||
import io
|
||||
import os
|
||||
|
||||
// reader_to_writer tries to consume the entire reader & write it to the writer.
|
||||
pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
|
||||
mut buf := []u8{len: 10 * 1024}
|
||||
|
||||
for {
|
||||
bytes_read := reader.read(mut buf) or { break }
|
||||
mut bytes_written := 0
|
||||
|
||||
for bytes_written < bytes_read {
|
||||
c := writer.write(buf[bytes_written..bytes_read]) or { break }
|
||||
|
||||
bytes_written += c
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reader_to_file writes the contents of a BufferedReader to a file
|
||||
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||
mut file := os.create(path)?
|
||||
defer {
|
||||
file.close()
|
||||
}
|
||||
|
||||
mut buf := []u8{len: reader_buf_size}
|
||||
mut bytes_left := length
|
||||
|
||||
// Repeat as long as the stream still has data
|
||||
for bytes_left > 0 {
|
||||
// TODO check if just breaking here is safe
|
||||
bytes_read := reader.read(mut buf) or { break }
|
||||
bytes_left -= bytes_read
|
||||
|
||||
mut to_write := bytes_read
|
||||
|
||||
for to_write > 0 {
|
||||
// TODO don't just loop infinitely here
|
||||
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
||||
// file.flush()
|
||||
|
||||
to_write = to_write - bytes_written
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// not at the end of a1, the result is 0.
|
||||
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
|
||||
mut i := 0
|
||||
mut match_len := 0
|
||||
|
||||
for i + match_len < a1.len {
|
||||
if a1[i + match_len] == a2[match_len] {
|
||||
match_len += 1
|
||||
} else {
|
||||
i += match_len + 1
|
||||
match_len = 0
|
||||
}
|
||||
}
|
||||
|
||||
return match_len
|
||||
}
|
||||
|
||||
// read_until_separator consumes an io.Reader until it encounters some
|
||||
// separator array. The data read is stored inside the provided res array.
|
||||
pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? {
|
||||
mut buf := []u8{len: sep.len}
|
||||
|
||||
for {
|
||||
c := reader.read(mut buf)?
|
||||
res << buf[..c]
|
||||
|
||||
match_len := match_array_in_array(buf[..c], sep)
|
||||
|
||||
if match_len == sep.len {
|
||||
break
|
||||
}
|
||||
|
||||
if match_len > 0 {
|
||||
match_left := sep.len - match_len
|
||||
c2 := reader.read(mut buf[..match_left])?
|
||||
res << buf[..c2]
|
||||
|
||||
if buf[..c2] == sep[match_len..] {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
module util
|
||||
|
||||
import os
|
||||
import io
|
||||
import crypto.md5
|
||||
import crypto.sha256
|
||||
|
||||
const reader_buf_size = 1_000_000
|
||||
|
||||
const prefixes = ['B', 'KB', 'MB', 'GB']
|
||||
const (
|
||||
reader_buf_size = 1_000_000
|
||||
prefixes = ['B', 'KB', 'MB', 'GB']
|
||||
)
|
||||
|
||||
// Dummy struct to work around the fact that you can only share structs, maps &
|
||||
// arrays
|
||||
|
|
@ -23,50 +23,6 @@ pub fn exit_with_message(code int, msg string) {
|
|||
exit(code)
|
||||
}
|
||||
|
||||
// reader_to_writer tries to consume the entire reader & write it to the writer.
|
||||
pub fn reader_to_writer(mut reader io.Reader, mut writer io.Writer) ? {
|
||||
mut buf := []u8{len: 10 * 1024}
|
||||
|
||||
for {
|
||||
bytes_read := reader.read(mut buf) or { break }
|
||||
mut bytes_written := 0
|
||||
|
||||
for bytes_written < bytes_read {
|
||||
c := writer.write(buf[bytes_written..bytes_read]) or { break }
|
||||
|
||||
bytes_written += c
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reader_to_file writes the contents of a BufferedReader to a file
|
||||
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||
mut file := os.create(path)?
|
||||
defer {
|
||||
file.close()
|
||||
}
|
||||
|
||||
mut buf := []u8{len: util.reader_buf_size}
|
||||
mut bytes_left := length
|
||||
|
||||
// Repeat as long as the stream still has data
|
||||
for bytes_left > 0 {
|
||||
// TODO check if just breaking here is safe
|
||||
bytes_read := reader.read(mut buf) or { break }
|
||||
bytes_left -= bytes_read
|
||||
|
||||
mut to_write := bytes_read
|
||||
|
||||
for to_write > 0 {
|
||||
// TODO don't just loop infinitely here
|
||||
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
||||
// file.flush()
|
||||
|
||||
to_write = to_write - bytes_written
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hash_file returns the md5 & sha256 hash of a given file
|
||||
// TODO actually implement sha256
|
||||
pub fn hash_file(path &string) ?(string, string) {
|
||||
|
|
@ -108,49 +64,3 @@ pub fn pretty_bytes(bytes int) string {
|
|||
|
||||
return '${n:.2}${util.prefixes[i]}'
|
||||
}
|
||||
|
||||
// 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
|
||||
// not at the end of a1, the result is 0.
|
||||
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
|
||||
mut i := 0
|
||||
mut match_len := 0
|
||||
|
||||
for i + match_len < a1.len {
|
||||
if a1[i + match_len] == a2[match_len] {
|
||||
match_len += 1
|
||||
} else {
|
||||
i += match_len + 1
|
||||
match_len = 0
|
||||
}
|
||||
}
|
||||
|
||||
return match_len
|
||||
}
|
||||
|
||||
// read_until_separator consumes an io.Reader until it encounters some
|
||||
// separator array. The data read is stored inside the provided res array.
|
||||
pub fn read_until_separator(mut reader io.Reader, mut res []u8, sep []u8) ? {
|
||||
mut buf := []u8{len: sep.len}
|
||||
|
||||
for {
|
||||
c := reader.read(mut buf)?
|
||||
res << buf[..c]
|
||||
|
||||
match_len := match_array_in_array(buf[..c], sep)
|
||||
|
||||
if match_len == sep.len {
|
||||
break
|
||||
}
|
||||
|
||||
if match_len > 0 {
|
||||
match_left := sep.len - match_len
|
||||
c2 := reader.read(mut buf[..match_left])?
|
||||
res << buf[..c2]
|
||||
|
||||
if buf[..c2] == sep[match_len..] {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue