forked from vieter-v/vieter
feat(docker): partially migrate to new code
parent
dd9958ea28
commit
4c97489f8a
|
@ -17,6 +17,8 @@ const build_image_repo = 'vieter-build'
|
||||||
// makepkg with. The base image should be some Linux distribution that uses
|
// makepkg with. The base image should be some Linux distribution that uses
|
||||||
// Pacman as its package manager.
|
// Pacman as its package manager.
|
||||||
pub fn create_build_image(base_image string) ?string {
|
pub fn create_build_image(base_image string) ?string {
|
||||||
|
mut dd := docker.new_conn() ?
|
||||||
|
|
||||||
commands := [
|
commands := [
|
||||||
// Update repos & install required packages
|
// Update repos & install required packages
|
||||||
'pacman -Syu --needed --noconfirm base-devel git'
|
'pacman -Syu --needed --noconfirm base-devel git'
|
||||||
|
@ -48,12 +50,13 @@ pub fn create_build_image(base_image string) ?string {
|
||||||
// We pull the provided image
|
// We pull the provided image
|
||||||
docker.pull_image(image_name, image_tag)?
|
docker.pull_image(image_name, image_tag)?
|
||||||
|
|
||||||
id := docker.create_container(c)?
|
id := dd.create_container(c)?.id
|
||||||
docker.start_container(id)?
|
/* id := docker.create_container(c)? */
|
||||||
|
dd.start_container(id)?
|
||||||
|
|
||||||
// This loop waits until the container has stopped, so we can remove it after
|
// This loop waits until the container has stopped, so we can remove it after
|
||||||
for {
|
for {
|
||||||
data := docker.inspect_container(id)?
|
data := dd.inspect_container(id)?
|
||||||
|
|
||||||
if !data.state.running {
|
if !data.state.running {
|
||||||
break
|
break
|
||||||
|
@ -68,7 +71,7 @@ pub fn create_build_image(base_image string) ?string {
|
||||||
// conflicts.
|
// conflicts.
|
||||||
tag := time.sys_mono_now().str()
|
tag := time.sys_mono_now().str()
|
||||||
image := docker.create_image_from_container(id, 'vieter-build', tag)?
|
image := docker.create_image_from_container(id, 'vieter-build', tag)?
|
||||||
docker.remove_container(id)?
|
dd.remove_container(id)?
|
||||||
|
|
||||||
return image.id
|
return image.id
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,14 @@ struct Container {
|
||||||
|
|
||||||
pub fn (mut d DockerDaemon) containers() ?[]Container {
|
pub fn (mut d DockerDaemon) containers() ?[]Container {
|
||||||
d.send_request('GET', urllib.parse('/v1.41/containers/json')?)?
|
d.send_request('GET', urllib.parse('/v1.41/containers/json')?)?
|
||||||
res_header := d.read_response_head()?
|
_, res := d.read_response()?
|
||||||
content_length := res_header.header.get(http.CommonHeader.content_length)?.int()
|
|
||||||
res := d.read_response_body(content_length)?
|
|
||||||
|
|
||||||
data := json.decode([]Container, res)?
|
data := json.decode([]Container, res)?
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[params]
|
||||||
pub struct NewContainer {
|
pub struct NewContainer {
|
||||||
image string [json: Image]
|
image string [json: Image]
|
||||||
entrypoint []string [json: Entrypoint]
|
entrypoint []string [json: Entrypoint]
|
||||||
|
@ -31,7 +30,25 @@ pub struct NewContainer {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CreatedContainer {
|
struct CreatedContainer {
|
||||||
|
pub:
|
||||||
id string [json: Id]
|
id string [json: Id]
|
||||||
|
warnings []string [json: Warnings]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut d DockerDaemon) create_container(c NewContainer) ?CreatedContainer {
|
||||||
|
d.send_request_with_json('POST', urllib.parse('/v1.41/containers/create')?, c)?
|
||||||
|
_, res := d.read_response()?
|
||||||
|
|
||||||
|
data := json.decode(CreatedContainer, res)?
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut d DockerDaemon) start_container(id string) ?bool {
|
||||||
|
d.send_request('POST', urllib.parse('/v1.41/containers/$id/start')?)?
|
||||||
|
head := d.read_response_head() ?
|
||||||
|
|
||||||
|
return head.status_code == 204
|
||||||
}
|
}
|
||||||
|
|
||||||
// create_container creates a container defined by the given configuration. If
|
// create_container creates a container defined by the given configuration. If
|
||||||
|
@ -72,6 +89,25 @@ pub mut:
|
||||||
end_time time.Time [skip]
|
end_time time.Time [skip]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut d DockerDaemon) inspect_container(id string) ?ContainerInspect {
|
||||||
|
d.send_request('GET', urllib.parse('/v1.41/containers/$id/json')?)?
|
||||||
|
head, body := d.read_response()?
|
||||||
|
|
||||||
|
if head.status_code != 200 {
|
||||||
|
return error('Failed to inspect container.')
|
||||||
|
}
|
||||||
|
|
||||||
|
mut data := json.decode(ContainerInspect, body)?
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// inspect_container returns the result of inspecting a container with a given
|
// inspect_container returns the result of inspecting a container with a given
|
||||||
// ID.
|
// ID.
|
||||||
pub fn inspect_container(id string) ?ContainerInspect {
|
pub fn inspect_container(id string) ?ContainerInspect {
|
||||||
|
@ -92,6 +128,11 @@ pub fn inspect_container(id string) ?ContainerInspect {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut d DockerDaemon) remove_container(id string) ? {
|
||||||
|
d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
|
||||||
|
head := d.read_response_head() ?
|
||||||
|
}
|
||||||
|
|
||||||
// remove_container removes a container with a given ID.
|
// remove_container removes a container with a given ID.
|
||||||
pub fn remove_container(id string) ?bool {
|
pub fn remove_container(id string) ?bool {
|
||||||
res := request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
|
res := request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
|
||||||
|
|
|
@ -42,10 +42,10 @@ pub fn (mut d DockerDaemon) send_request_with_body(method string, url urllib.URL
|
||||||
d.socket.write_string(req)?
|
d.socket.write_string(req)?
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d DockerDaemon) request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
pub fn (mut d DockerDaemon) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
|
||||||
body := json.encode(data)
|
body := json.encode(data)
|
||||||
|
|
||||||
return request_with_body(method, url, 'application/json', body)
|
return d.send_request_with_body(method, url, '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
|
||||||
|
@ -100,3 +100,11 @@ pub fn (mut d DockerDaemon) read_response_body(length int) ?string {
|
||||||
|
|
||||||
return builder.str()
|
return builder.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut d DockerDaemon) read_response() ?(http.Response, string) {
|
||||||
|
head := d.read_response_head()?
|
||||||
|
content_length := head.header.get(http.CommonHeader.content_length)?.int()
|
||||||
|
res := d.read_response_body(content_length)?
|
||||||
|
|
||||||
|
return head, res
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import build
|
||||||
import console.git
|
import console.git
|
||||||
import console.logs
|
import console.logs
|
||||||
import cron
|
import cron
|
||||||
|
import docker
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mut app := cli.Command{
|
mut app := cli.Command{
|
||||||
|
|
Loading…
Reference in New Issue