Currently broken start of docker wrapper [CI SKIP]

cron
Jef Roosens 2022-02-17 22:00:46 +01:00
parent 022f8c4fbe
commit 6f86033cd9
Signed by untrusted user: Jef Roosens
GPG Key ID: B580B976584B5F30
6 changed files with 134 additions and 47 deletions

View File

@ -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

7
src/build.v 100644
View File

@ -0,0 +1,7 @@
module main
import docker
fn build() {
println(docker.containers() or { panic("yeet") })
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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' }
mut logger := log.Log{
level: log_level
if os.args.len == 1 {
exit_with_message(1, 'No action provided.')
}
logger.set_full_logpath(log_file)
logger.log_to_console_too()
defer {
logger.info('Flushing log file')
logger.flush()
logger.close()
match os.args[1] {
'server' { server() }
'build' { build() }
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
}
// 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)
}

55
src/server.v 100644
View File

@ -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)
}