2022-02-21 20:51:41 +01:00
|
|
|
module server
|
2022-02-17 22:00:46 +01:00
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import log
|
|
|
|
import repo
|
2022-02-21 20:51:41 +01:00
|
|
|
import util
|
2022-05-01 22:47:00 +02:00
|
|
|
import db
|
2022-12-06 14:11:17 +01:00
|
|
|
import build { BuildJobQueue }
|
|
|
|
import cron.expression
|
2022-12-28 16:09:00 +01:00
|
|
|
import metrics
|
2022-02-21 20:40:13 +01:00
|
|
|
|
2022-05-03 16:54:12 +02:00
|
|
|
const (
|
|
|
|
log_file_name = 'vieter.log'
|
|
|
|
repo_dir_name = 'repos'
|
|
|
|
db_file_name = 'vieter.sqlite'
|
2022-05-07 15:10:07 +02:00
|
|
|
logs_dir_name = 'logs'
|
2022-05-03 16:54:12 +02:00
|
|
|
)
|
2022-02-21 20:40:13 +01:00
|
|
|
|
|
|
|
struct App {
|
|
|
|
web.Context
|
|
|
|
pub:
|
2022-04-06 18:20:14 +02:00
|
|
|
conf Config [required; web_global]
|
2022-02-21 20:40:13 +01:00
|
|
|
pub mut:
|
2022-03-27 16:33:06 +02:00
|
|
|
repo repo.RepoGroupManager [required; web_global]
|
2022-12-06 14:11:17 +01:00
|
|
|
// Keys are the various architectures for packages
|
|
|
|
job_queue BuildJobQueue [required; web_global]
|
2022-12-12 20:33:51 +01:00
|
|
|
db db.VieterDb
|
2022-02-21 20:40:13 +01:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:51:42 +01:00
|
|
|
// init_job_queue populates a fresh job queue with all the targets currently
|
|
|
|
// stored in the database.
|
2022-12-12 20:33:51 +01:00
|
|
|
fn (mut app App) init_job_queue() ! {
|
|
|
|
// Initialize build queues
|
|
|
|
mut targets := app.db.get_targets(limit: 25)
|
|
|
|
mut i := u64(0)
|
2022-12-06 14:11:17 +01:00
|
|
|
|
2022-12-12 20:33:51 +01:00
|
|
|
for targets.len > 0 {
|
|
|
|
for target in targets {
|
2022-12-14 17:23:51 +01:00
|
|
|
app.job_queue.insert_all(target)!
|
2022-12-12 20:33:51 +01:00
|
|
|
}
|
2022-12-06 14:11:17 +01:00
|
|
|
|
2022-12-12 20:33:51 +01:00
|
|
|
i += 25
|
|
|
|
targets = app.db.get_targets(limit: 25, offset: i)
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 14:11:17 +01:00
|
|
|
|
2022-02-22 08:14:20 +01:00
|
|
|
// server starts the web server & starts listening for requests
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn server(conf Config) ! {
|
2022-04-07 13:47:06 +02:00
|
|
|
// Prevent using 'any' as the default arch
|
|
|
|
if conf.default_arch == 'any' {
|
|
|
|
util.exit_with_message(1, "'any' is not allowed as the value for default_arch.")
|
|
|
|
}
|
|
|
|
|
2022-12-06 14:11:17 +01:00
|
|
|
global_ce := expression.parse_expression(conf.global_schedule) or {
|
|
|
|
util.exit_with_message(1, 'Invalid global cron expression: $err.msg()')
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:47:53 +01:00
|
|
|
log_removal_ce := expression.parse_expression(conf.log_removal_schedule) or {
|
|
|
|
util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()')
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:00:46 +01:00
|
|
|
// Configure logger
|
2022-02-21 20:40:13 +01:00
|
|
|
log_level := log.level_from_tag(conf.log_level) or {
|
2022-02-21 20:51:41 +01:00
|
|
|
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
|
|
|
|
2022-05-03 16:54:12 +02:00
|
|
|
os.mkdir_all(conf.data_dir) or { util.exit_with_message(1, 'Failed to create data directory.') }
|
|
|
|
|
2022-05-07 15:10:07 +02:00
|
|
|
logs_dir := os.join_path_single(conf.data_dir, server.logs_dir_name)
|
|
|
|
|
|
|
|
if !os.exists(logs_dir) {
|
|
|
|
os.mkdir(os.join_path_single(conf.data_dir, server.logs_dir_name)) or {
|
|
|
|
util.exit_with_message(1, 'Failed to create logs directory.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:00:46 +01:00
|
|
|
mut logger := log.Log{
|
|
|
|
level: log_level
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:54:12 +02:00
|
|
|
log_file := os.join_path_single(conf.data_dir, server.log_file_name)
|
|
|
|
logger.set_full_logpath(log_file)
|
2022-02-17 22:00:46 +01:00
|
|
|
logger.log_to_console_too()
|
|
|
|
|
|
|
|
defer {
|
|
|
|
logger.info('Flushing log file')
|
|
|
|
logger.flush()
|
|
|
|
logger.close()
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:54:12 +02:00
|
|
|
repo_dir := os.join_path_single(conf.data_dir, server.repo_dir_name)
|
2022-02-17 22:00:46 +01:00
|
|
|
// This also creates the directories if needed
|
2022-05-03 16:54:12 +02:00
|
|
|
repo := repo.new(repo_dir, conf.pkg_dir, conf.default_arch) or {
|
2022-04-13 22:20:05 +02:00
|
|
|
logger.error(err.msg())
|
2022-02-17 22:00:46 +01:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:54:12 +02:00
|
|
|
db_file := os.join_path_single(conf.data_dir, server.db_file_name)
|
2022-05-24 22:48:17 +02:00
|
|
|
db := db.init(db_file) or {
|
|
|
|
util.exit_with_message(1, 'Failed to initialize database: $err.msg()')
|
|
|
|
}
|
2022-05-01 22:47:00 +02:00
|
|
|
|
2022-12-12 20:33:51 +01:00
|
|
|
mut app := &App{
|
2022-02-17 22:00:46 +01:00
|
|
|
logger: logger
|
2022-09-04 19:32:22 +02:00
|
|
|
api_key: conf.api_key
|
2022-02-21 20:40:13 +01:00
|
|
|
conf: conf
|
2022-02-17 22:00:46 +01:00
|
|
|
repo: repo
|
2022-05-01 22:47:00 +02:00
|
|
|
db: db
|
2022-12-12 20:33:51 +01:00
|
|
|
job_queue: build.new_job_queue(global_ce, conf.base_image)
|
2022-12-28 16:09:00 +01:00
|
|
|
collector: metrics.new_default_collector()
|
2022-12-12 20:33:51 +01:00
|
|
|
}
|
|
|
|
app.init_job_queue() or {
|
|
|
|
util.exit_with_message(1, 'Failed to inialize job queue: $err.msg()')
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:11:19 +01:00
|
|
|
if conf.max_log_age > 0 {
|
2022-12-19 09:47:53 +01:00
|
|
|
go app.log_removal_daemon(log_removal_ce)
|
2022-12-17 17:11:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 20:33:51 +01:00
|
|
|
web.run(app, conf.port)
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|