From 6f86033cd9aa5cfe875e91a98809a5ac54c96c44 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 17 Feb 2022 22:00:46 +0100 Subject: [PATCH] Currently broken start of docker wrapper [CI SKIP] --- Makefile | 2 +- src/build.v | 7 ++++++ src/docker/containers.v | 15 +++++++++++ src/docker/docker.v | 48 +++++++++++++++++++++++++++++++++++ src/main.v | 54 ++++++---------------------------------- src/server.v | 55 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 47 deletions(-) create mode 100644 src/build.v create mode 100644 src/docker/containers.v create mode 100644 src/docker/docker.v create mode 100644 src/server.v diff --git a/Makefile b/Makefile index 7166c665..63d772ff 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ c: # Run the server in the default 'data' directory .PHONY: run run: vieter - API_KEY=test DOWNLOAD_DIR=data/downloads REPO_DIR=data/repo PKG_DIR=data/pkgs LOG_LEVEL=DEBUG ./vieter + API_KEY=test DOWNLOAD_DIR=data/downloads REPO_DIR=data/repo PKG_DIR=data/pkgs LOG_LEVEL=DEBUG ./vieter server .PHONY: run-prod run-prod: prod diff --git a/src/build.v b/src/build.v new file mode 100644 index 00000000..e635cc08 --- /dev/null +++ b/src/build.v @@ -0,0 +1,7 @@ +module main + +import docker + +fn build() { + println(docker.containers() or { panic("yeet") }) +} diff --git a/src/docker/containers.v b/src/docker/containers.v new file mode 100644 index 00000000..5bdd39ed --- /dev/null +++ b/src/docker/containers.v @@ -0,0 +1,15 @@ +module docker + +import json +import net.urllib + +struct Container { + id string + names []string +} + +pub fn containers() ?[]Container { + res := docker.get(urllib.parse('/containers/json') ?) ? + + return json.decode([]Container, res.text) +} diff --git a/src/docker/docker.v b/src/docker/docker.v new file mode 100644 index 00000000..a090f025 --- /dev/null +++ b/src/docker/docker.v @@ -0,0 +1,48 @@ +module docker + +import net.unix +import net.urllib +import net.http + +const socket = '/var/run/docker.sock' +const buf_len = 1024 + +fn request(method string, url urllib.URL) ?http.Response { + req := "$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n" + + // Open a connection to the socket + mut s := unix.connect_stream(socket) ? + + defer { + s.close() ? + } + + // Write the request to the socket + s.write_string(req) ? + + // Wait for the server to respond + s.wait_for_write() ? + + mut buf := []byte{len: buf_len} + mut res := []byte{} + + mut c := 0 + + for { + c = s.read(mut buf) or { + return error('Failed to read data from socket.') + } + res << buf[..c] + + if c < buf_len { + break + } + } + + // Decode chunked response + return http.parse_response(res.bytestr()) +} + +fn get(url urllib.URL) ?http.Response { + return request('GET', url) +} diff --git a/src/main.v b/src/main.v index cddd9aec..7b19d4b6 100644 --- a/src/main.v +++ b/src/main.v @@ -2,7 +2,6 @@ module main import web import os -import log import io import repo @@ -54,50 +53,13 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } fn main() { - // Configure logger - log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } - log_level := log.level_from_tag(log_level_str) or { - exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') - } - log_file := os.getenv_opt('LOG_FILE') or { 'vieter.log' } + if os.args.len == 1 { + exit_with_message(1, 'No action provided.') + } - mut logger := log.Log{ - level: log_level - } - - logger.set_full_logpath(log_file) - logger.log_to_console_too() - - defer { - logger.info('Flushing log file') - logger.flush() - logger.close() - } - - // Configure web server - key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') } - repo_dir := os.getenv_opt('REPO_DIR') or { - exit_with_message(1, 'No repo directory was configured.') - } - pkg_dir := os.getenv_opt('PKG_DIR') or { - exit_with_message(1, 'No package directory was configured.') - } - dl_dir := os.getenv_opt('DOWNLOAD_DIR') or { - exit_with_message(1, 'No download directory was configured.') - } - - // This also creates the directories if needed - repo := repo.new(repo_dir, pkg_dir) or { - logger.error(err.msg) - exit(1) - } - - os.mkdir_all(dl_dir) or { exit_with_message(1, 'Failed to create download directory.') } - - web.run(&App{ - logger: logger - api_key: key - dl_dir: dl_dir - repo: repo - }, port) + match os.args[1] { + 'server' { server() } + 'build' { build() } + else { exit_with_message(1, 'Unknown action: ${os.args[1]}') } + } } diff --git a/src/server.v b/src/server.v new file mode 100644 index 00000000..0e6ba45a --- /dev/null +++ b/src/server.v @@ -0,0 +1,55 @@ +module main + +import web +import os +import log +import repo + +fn server() { + // Configure logger + log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } + log_level := log.level_from_tag(log_level_str) or { + exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') + } + log_file := os.getenv_opt('LOG_FILE') or { 'vieter.log' } + + mut logger := log.Log{ + level: log_level + } + + logger.set_full_logpath(log_file) + logger.log_to_console_too() + + defer { + logger.info('Flushing log file') + logger.flush() + logger.close() + } + + // Configure web server + key := os.getenv_opt('API_KEY') or { exit_with_message(1, 'No API key was provided.') } + repo_dir := os.getenv_opt('REPO_DIR') or { + exit_with_message(1, 'No repo directory was configured.') + } + pkg_dir := os.getenv_opt('PKG_DIR') or { + exit_with_message(1, 'No package directory was configured.') + } + dl_dir := os.getenv_opt('DOWNLOAD_DIR') or { + exit_with_message(1, 'No download directory was configured.') + } + + // This also creates the directories if needed + repo := repo.new(repo_dir, pkg_dir) or { + logger.error(err.msg) + exit(1) + } + + os.mkdir_all(dl_dir) or { exit_with_message(1, 'Failed to create download directory.') } + + web.run(&App{ + logger: logger + api_key: key + dl_dir: dl_dir + repo: repo + }, port) +}