Added some documentation; ran format

cron
Jef Roosens 2022-02-20 22:15:10 +01:00
parent 36693900a3
commit 9cc88e629f
Signed by untrusted user: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 24 additions and 11 deletions

View File

@ -11,7 +11,9 @@ import git
const container_build_dir = '/build' const container_build_dir = '/build'
fn build(key string, repo_dir string) ? { fn build(key string, repo_dir string) ? {
server_url := os.getenv_opt('VIETER_ADDRESS') or { exit_with_message(1, 'No Vieter server address was provided.') } server_url := os.getenv_opt('VIETER_ADDRESS') or {
exit_with_message(1, 'No Vieter server address was provided.')
}
// Read in the repos from a json file // Read in the repos from a json file
filename := os.join_path_single(repo_dir, 'repos.json') filename := os.join_path_single(repo_dir, 'repos.json')
@ -22,14 +24,14 @@ fn build(key string, repo_dir string) ? {
// Update repos & install required packages // Update repos & install required packages
'pacman -Syu --needed --noconfirm base-devel git' 'pacman -Syu --needed --noconfirm base-devel git'
// Add a non-root user to run makepkg // Add a non-root user to run makepkg
'groupadd -g 1000 builder' 'groupadd -g 1000 builder',
'useradd -mg builder builder' 'useradd -mg builder builder'
// Make sure they can use sudo without a password // Make sure they can use sudo without a password
"echo 'builder ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers" "echo 'builder ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
// Create the directory for the builds & make it writeable for the // Create the directory for the builds & make it writeable for the
// build user // build user
'mkdir /build' 'mkdir /build',
'chown -R builder:builder /build' 'chown -R builder:builder /build',
] ]
// Each repo gets a unique UUID to avoid naming conflicts when cloning // Each repo gets a unique UUID to avoid naming conflicts when cloning

View File

@ -8,6 +8,7 @@ struct Container {
names []string [json: Names] names []string [json: Names]
} }
// containers returns a list of all currently running containers
pub fn containers() ?[]Container { pub fn containers() ?[]Container {
res := request('GET', urllib.parse('/containers/json') ?) ? res := request('GET', urllib.parse('/containers/json') ?) ?
@ -15,16 +16,18 @@ pub fn containers() ?[]Container {
} }
pub struct NewContainer { pub struct NewContainer {
image string [json: Image] image string [json: Image]
entrypoint []string [json: Entrypoint] entrypoint []string [json: Entrypoint]
cmd []string [json: Cmd] cmd []string [json: Cmd]
env []string [json: Env] env []string [json: Env]
} }
struct CreatedContainer { struct CreatedContainer {
id string [json: Id] id string [json: Id]
} }
// create_container creates a container defined by the given configuration. If
// successful, it returns the ID of the newly created container.
pub fn create_container(c &NewContainer) ?string { pub fn create_container(c &NewContainer) ?string {
res := request_with_json('POST', urllib.parse('/containers/create') ?, c) ? res := request_with_json('POST', urllib.parse('/containers/create') ?, c) ?
@ -35,6 +38,8 @@ pub fn create_container(c &NewContainer) ?string {
return json.decode(CreatedContainer, res.text) ?.id return json.decode(CreatedContainer, res.text) ?.id
} }
// start_container starts a container with a given ID. It returns whether the
// container was started or not.
pub fn start_container(id string) ?bool { pub fn start_container(id string) ?bool {
res := request('POST', urllib.parse('/containers/$id/start') ?) ? res := request('POST', urllib.parse('/containers/$id/start') ?) ?
@ -51,16 +56,19 @@ pub:
running bool [json: Running] running bool [json: Running]
} }
// inspect_container returns the result of inspecting a container with a given
// ID.
pub fn inspect_container(id string) ?ContainerInspect { pub fn inspect_container(id string) ?ContainerInspect {
res := request('GET', urllib.parse('/containers/$id/json') ?) ? res := request('GET', urllib.parse('/containers/$id/json') ?) ?
if res.status_code != 200 { if res.status_code != 200 {
return error("Failed to inspect container.") return error('Failed to inspect container.')
} }
return json.decode(ContainerInspect, res.text) return json.decode(ContainerInspect, res.text) or {}
} }
// 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('/containers/$id') ?) ? res := request('DELETE', urllib.parse('/containers/$id') ?) ?

View File

@ -65,7 +65,7 @@ fn send(req &string) ?http.Response {
} }
fn request_with_body(method string, url urllib.URL, content_type string, body string) ?http.Response { fn request_with_body(method string, url urllib.URL, content_type string, body string) ?http.Response {
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: ${content_type}\nContent-Length: $body.len\n\n$body\n\n' req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
return send(req) return send(req)
} }
@ -76,12 +76,15 @@ fn request(method string, url urllib.URL) ?http.Response {
return send(req) return send(req)
} }
// request_with_json<T> sends a request to the Docker socket with a given JSON
// payload
pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Response { pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Response {
body := json.encode(data) body := json.encode(data)
return request_with_body(method, url, 'application/json', body) return request_with_body(method, url, 'application/json', body)
} }
// pull_image pulls tries to pull the image for the given image & tag
pub fn pull_image(image string, tag string) ?http.Response { pub fn pull_image(image string, tag string) ?http.Response {
return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?) return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?)
} }

View File

@ -2,6 +2,6 @@ module git
pub struct GitRepo { pub struct GitRepo {
pub: pub:
url string [required] url string [required]
branch string [required] branch string [required]
} }