Compare commits

..

2 Commits

15 changed files with 125 additions and 121 deletions

View File

@ -7,8 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://git.rustybever.be/vieter/vieter/src/branch/dev)
## [0.3.0-alpha.2](https://git.rustybever.be/vieter/vieter/src/tag/0.3.0-alpha.2)
### Added
* Web API for adding & querying build logs

View File

@ -3,7 +3,7 @@
pkgbase='vieter'
pkgname='vieter'
pkgver='0.3.0_alpha.2'
pkgver='0.3.0_alpha.1'
pkgrel=1
depends=('glibc' 'openssl' 'libarchive' 'sqlite')
makedepends=('git' 'vieter-v')

View File

@ -7,7 +7,6 @@ import os
import db
import strings
import util
import models { GitRepo }
const (
container_build_dir = '/build'
@ -94,7 +93,7 @@ pub:
// build_repo builds, packages & publishes a given Arch package based on the
// provided GitRepo. The base image ID should be of an image previously created
// by create_build_image. It returns the logs of the container.
pub fn build_repo(address string, api_key string, base_image_id string, repo &GitRepo) ?BuildResult {
pub fn build_repo(address string, api_key string, base_image_id string, repo &db.GitRepo) ?BuildResult {
mut dd := docker.new_conn()?
defer {

View File

@ -29,10 +29,7 @@ fn (c &Client) send_request_raw(method Method, url string, params map[string]str
// Escape each query param
for k, v in params {
// An empty parameter should be the same as not providing it at all
if v != '' {
params_escaped[k] = urllib.query_escape(v)
}
params_escaped[k] = urllib.query_escape(v)
}
params_str := params_escaped.keys().map('$it=${params[it]}').join('&')

View File

@ -1,12 +1,13 @@
module client
import models { GitRepo, GitRepoFilter }
import db { GitRepo, GitRepoFilter }
import net.http { Method }
import response { Response }
import util
// get_git_repos returns the current list of repos.
pub fn (c &Client) get_git_repos(filter GitRepoFilter) ?[]GitRepo {
params := models.params_from(filter)
params := util.struct_to_map(filter)
data := c.send_request<[]GitRepo>(Method.get, '/api/repos', params)?
return data.data

View File

@ -5,7 +5,7 @@ import env
import cron.expression { parse_expression }
import client
import console
import models { GitRepoFilter }
import db { GitRepoFilter }
struct Config {
address string [required]
@ -50,21 +50,21 @@ pub fn cmd() cli.Command {
mut filter := GitRepoFilter{}
limit := cmd.flags.get_int('limit') ?
if limit != 0 {
if limit := cmd.flags.get_int('limit') {
println('limit = $limit')
filter.limit = u64(limit)
}
offset := cmd.flags.get_int('offset') ?
if offset != 0 {
filter.offset = u64(offset)
if offset := cmd.flags.get_int('offset') {
filter.limit = u64(offset)
}
repo := cmd.flags.get_string('repo') ?
if repo != '' {
if repo := cmd.flags.get_string('repo') {
filter.repo = repo
}
dump(filter)
list(conf, filter)?
}
},

View File

@ -10,7 +10,6 @@ import docker
import db
import os
import client
import models { GitRepo }
const (
// How many seconds to wait before retrying to update API if failed
@ -21,7 +20,7 @@ const (
struct ScheduledBuild {
pub:
repo GitRepo
repo db.GitRepo
timestamp time.Time
}
@ -39,7 +38,7 @@ mut:
api_update_frequency int
image_rebuild_frequency int
// Repos currently loaded from API.
repos []GitRepo
repos []db.GitRepo
// At what point to update the list of repositories.
api_update_timestamp time.Time
image_build_timestamp time.Time
@ -150,7 +149,7 @@ pub fn (mut d Daemon) run() {
}
// schedule_build adds the next occurence of the given repo build to the queue.
fn (mut d Daemon) schedule_build(repo GitRepo) {
fn (mut d Daemon) schedule_build(repo db.GitRepo) {
ce := if repo.schedule != '' {
parse_expression(repo.schedule) or {
// TODO This shouldn't return an error if the expression is empty.

View File

@ -1,7 +1,6 @@
module db
import sqlite
import models
struct VieterDb {
conn sqlite.DB
@ -12,7 +11,7 @@ pub fn init(db_path string) ?VieterDb {
conn := sqlite.connect(db_path)?
sql conn {
create table models.GitRepo
create table GitRepo
create table BuildLog
}

26
src/db/filter.v 100644
View File

@ -0,0 +1,26 @@
module db
[params]
pub struct GitRepoFilter {
pub mut:
limit u64 = 25
offset u64
repo string
}
pub fn filter_from_params<T>(params map[string]string) ?T {
mut o := GitRepoFilter{}
$for field in T.fields {
if field.name in params {
val := params[field.name]
$if field.typ is string {
o.$(field.name) = val
} $else $if field.typ is u64 {
o.$(field.name) = val.u64()
}
}
}
return o
}

View File

@ -1,9 +1,84 @@
module db
import models { GitRepo, GitRepoArch, GitRepoFilter }
pub struct GitRepoArch {
pub:
id int [primary; sql: serial]
repo_id int [nonull]
value string [nonull]
}
// str returns a string representation.
pub fn (gra &GitRepoArch) str() string {
return gra.value
}
pub struct GitRepo {
pub mut:
id int [optional; primary; sql: serial]
// URL of the Git repository
url string [nonull]
// Branch of the Git repository to use
branch string [nonull]
// Which repo the builder should publish packages to
repo string [nonull]
// Cron schedule describing how frequently to build the repo.
schedule string [optional]
// On which architectures the package is allowed to be built. In reality,
// this controls which builders will periodically build the image.
arch []GitRepoArch [fkey: 'repo_id']
}
// str returns a string representation.
pub fn (gr &GitRepo) str() string {
mut parts := [
'id: $gr.id',
'url: $gr.url',
'branch: $gr.branch',
'repo: $gr.repo',
'schedule: $gr.schedule',
'arch: ${gr.arch.map(it.value).join(', ')}',
]
str := parts.join('\n')
return str
}
// patch_from_params patches a GitRepo from a map[string]string, usually
// provided from a web.App's params
pub fn (mut r GitRepo) patch_from_params(params map[string]string) {
$for field in GitRepo.fields {
if field.name in params {
$if field.typ is string {
r.$(field.name) = params[field.name]
// This specific type check is needed for the compiler to ensure
// our types are correct
} $else $if field.typ is []GitRepoArch {
r.$(field.name) = params[field.name].split(',').map(GitRepoArch{ value: it })
}
}
}
}
// git_repo_from_params creates a GitRepo from a map[string]string, usually
// provided from a web.App's params
pub fn git_repo_from_params(params map[string]string) ?GitRepo {
mut repo := GitRepo{}
// If we're creating a new GitRepo, we want all fields to be present before
// "patching".
$for field in GitRepo.fields {
if field.name !in params && !field.attrs.contains('optional') {
return error('Missing parameter: ${field.name}.')
}
}
repo.patch_from_params(params)
return repo
}
// get_git_repos returns all GitRepo's in the database.
pub fn (db &VieterDb) get_git_repos(filter GitRepoFilter) []GitRepo {
println(filter)
// This seems to currently be blocked by a bug in the ORM, I'll have to ask
// around.
if filter.repo != '' {

View File

@ -11,7 +11,7 @@ fn main() {
mut app := cli.Command{
name: 'vieter'
description: 'Vieter is a lightweight implementation of an Arch repository server.'
version: '0.3.0-alpha.2'
version: '0.3.0-alpha.1'
flags: [
cli.Flag{
flag: cli.FlagType.string

View File

@ -1,52 +0,0 @@
module models
pub struct GitRepoArch {
pub:
id int [primary; sql: serial]
repo_id int [nonull]
value string [nonull]
}
// str returns a string representation.
pub fn (gra &GitRepoArch) str() string {
return gra.value
}
pub struct GitRepo {
pub mut:
id int [primary; sql: serial]
// URL of the Git repository
url string [nonull]
// Branch of the Git repository to use
branch string [nonull]
// Which repo the builder should publish packages to
repo string [nonull]
// Cron schedule describing how frequently to build the repo.
schedule string
// On which architectures the package is allowed to be built. In reality,
// this controls which builders will periodically build the image.
arch []GitRepoArch [fkey: 'repo_id']
}
// str returns a string representation.
pub fn (gr &GitRepo) str() string {
mut parts := [
'id: $gr.id',
'url: $gr.url',
'branch: $gr.branch',
'repo: $gr.repo',
'schedule: $gr.schedule',
'arch: ${gr.arch.map(it.value).join(', ')}',
]
str := parts.join('\n')
return str
}
[params]
pub struct GitRepoFilter {
pub mut:
limit u64 = 25
offset u64
repo string
}

View File

@ -1,37 +0,0 @@
module models
pub fn from_params<A>(params map[string]string) ?A {
mut o := A{}
patch_from_params(mut o, params) ?
return o
}
pub fn patch_from_params<B>(mut o B, params map[string]string) ? {
$for field in B.fields {
if field.name in params {
$if field.typ is string {
o.$(field.name) = params[field.name]
} $else $if field.typ is int {
o.$(field.name) = params[field.name].int()
} $else $if field.typ is u64 {
o.$(field.name) = params[field.name].u64()
} $else $if field.typ is []GitRepoArch {
o.$(field.name) = params[field.name].split(',').map(GitRepoArch{ value: it })
}
} else if field.attrs.contains('nonull') {
return error('Missing parameter: ${field.name}.')
}
}
}
pub fn params_from<D>(o &D) map[string]string {
mut out := map[string]string{}
$for field in D.fields {
out[field.name] = o.$(field.name).str()
}
return out
}

View File

@ -4,7 +4,6 @@ import web
import net.http
import response { new_data_response, new_response }
import db
import models { GitRepo, GitRepoArch, GitRepoFilter }
// get_repos returns the current list of repos.
['/api/repos'; get]
@ -13,8 +12,7 @@ fn (mut app App) get_repos() web.Result {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
filter := models.from_params<GitRepoFilter>(app.query) or {
println(err.msg())
filter := db.filter_from_params<db.GitRepoFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.'))
}
repos := app.db.get_git_repos(filter)
@ -49,7 +47,7 @@ fn (mut app App) post_repo() web.Result {
params['arch'] = app.conf.default_arch
}
new_repo := models.from_params<GitRepo>(params) or {
new_repo := db.git_repo_from_params(params) or {
return app.json(http.Status.bad_request, new_response(err.msg()))
}
@ -80,7 +78,7 @@ fn (mut app App) patch_repo(id int) web.Result {
app.db.update_git_repo(id, app.query)
if 'arch' in app.query {
arch_objs := app.query['arch'].split(',').map(GitRepoArch{ value: it })
arch_objs := app.query['arch'].split(',').map(db.GitRepoArch{ value: it })
app.db.update_git_repo_archs(id, arch_objs)
}

View File

@ -71,5 +71,6 @@ pub fn struct_to_map<T>(o T) map[string]string {
$for field in T.fields {
m[field.name] = o.$(field.name).str()
}
return m
}