refactor: improve installer code

- Refactor installer code for better readability and maintainability.
- Move `dagu_server.vsh` to `examples/virt/daguserver/dagu_server.vsh`.
- Remove unnecessary `println` statements.
- Improve error handling in `zinit_installer_actions.v`.
- Update `zinit_installer_actions.v` startup type to systemd.
- Refactor several factory functions.
This commit is contained in:
Mahmoud Emad
2025-02-12 09:02:33 +00:00
parent be4d2547e4
commit ec22a8e0ec
16 changed files with 311 additions and 363 deletions

View File

@@ -1,15 +1,16 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.sysadmintools.daguserver
import freeflowuniverse.herolib.installers.infra.zinit
import freeflowuniverse.herolib.installers.infra.zinit_installer
// make sure zinit is there and running, will restart it if needed
mut z := zinit.get()!
z.destroy()!
z.start()!
// mut z := zinit_installer.get()!
// z.destroy()!
// z.install()!
// z.start()!
// mut ds := daguserver.get()!
mut ds := daguserver.get()!
// ds.destroy()!
// ds.start()!
ds.start()!
// println(ds)
println(ds)

View File

@@ -3,5 +3,6 @@
import freeflowuniverse.herolib.installers.infra.zinit_installer
mut installer := zinit_installer.get()!
// installer.install()!
installer.install()!
installer.start()!
// installer.destroy()!

View File

@@ -189,7 +189,6 @@ pub fn (mut h HTTPConnection) get(req_ Request) !string {
req.debug = true
req.method = .get
result := h.send(req)!
println(result)
return result.data
}

View File

@@ -15,7 +15,7 @@ fn startupcmd() ![]zinit_module.ZProcessNewArgs {
res << zinit_module.ZProcessNewArgs{
name: 'zinit'
cmd: '/usr/local/bin/zinit init'
startuptype: .zinit
startuptype: .systemd
start: true
restart: true
}
@@ -126,8 +126,10 @@ fn build() ! {
fn destroy() ! {
mut systemdfactory := systemd.new()!
systemdfactory.destroy('zinit')!
systemdfactory.destroy('zinit') or { return error('Could not destroy zinit due to: ${err}') }
osal.process_kill_recursive(name: 'zinit')!
osal.process_kill_recursive(name: 'zinit') or {
return error('Could not kill zinit due to: ${err}')
}
osal.cmd_delete('zinit')!
}

View File

@@ -117,7 +117,7 @@ pub fn (mut self ZinitInstaller) start() ! {
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting zinit_installer with ${zprocess.startuptype}...')
console.print_header('starting zinit_installer with ${zprocess.startuptype}...')
sm.new(zprocess)!

View File

@@ -8,7 +8,6 @@ import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed

View File

@@ -4,16 +4,71 @@ import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.installers.ulist
// import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.crypt.secrets
import os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut cfg := get()!
res << zinit.ZProcessNewArgs{
name: 'dagu'
cmd: 'dagu server'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath // config for dagu is called admin.yml and is in this dir
}
}
res << zinit.ZProcessNewArgs{
name: 'dagu_scheduler'
cmd: 'dagu scheduler'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath
}
}
return res
}
fn running() !bool {
mut cfg := get()!
url := 'http://${cfg.host}:${cfg.port}/api/v1'
mut conn := httpconnection.new(name: 'dagu', url: url)!
if cfg.secret.len > 0 {
conn.default_header.add(.authorization, 'Bearer ${cfg.secret}')
}
console.print_debug("curl -X 'GET' '${url}'/tags --oauth2-bearer ${cfg.secret}")
r := conn.get_json_dict(prefix: 'tags', debug: false) or { return false }
tags := r['Tags'] or { return false }
console.print_debug(tags)
console.print_debug('Dagu is answering.')
return true
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('${osal.profile_path_source_and()!} dagu version')
res := os.execute('dagu version')
if res.exit_code == 0 {
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
if r.len != 1 {
@@ -28,6 +83,15 @@ fn installed() !bool {
return true
}
// get the Upload List of the files
fn ulist_get() !ulist.UList {
// optionally build a UList which is all paths which are result of building, is then used e.g. in upload
return ulist.UList{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('install daguserver')
mut url := ''
@@ -56,71 +120,6 @@ fn install() ! {
)!
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut cfg := get()!
res << zinit.ZProcessNewArgs{
name: 'dagu'
cmd: 'dagu server'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath // config for dagu is called admin.yml and is in this dir
}
}
res << zinit.ZProcessNewArgs{
name: 'dagu_scheduler'
cmd: 'dagu scheduler'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath
}
}
return res
}
// user needs to us switch to make sure we get the right object
fn configure() ! {
mut cfg := get()!
if cfg.password == '' {
cfg.password = secrets.hex_secret()!
}
// TODO:use DAGU_SECRET from env variables in os if not set then empty string
if cfg.secret == '' {
cfg.secret = secrets.openssl_hex_secret(input: cfg.password)!
}
mut mycode := $tmpl('templates/dagu.yaml')
mut path := pathlib.get_file(path: '${cfg.configpath}/admin.yaml', create: true)!
path.write(mycode)!
console.print_debug(mycode)
}
fn running() !bool {
mut cfg := get()!
// this checks health of dagu
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
url := 'http://127.0.0.1:${cfg.port}/api/v1'
mut conn := httpconnection.new(name: 'dagu', url: url)!
if cfg.secret.len > 0 {
conn.default_header.add(.authorization, 'Bearer ${cfg.secret}')
}
conn.default_header.add(.content_type, 'application/json')
console.print_debug("curl -X 'GET' '${url}'/tags --oauth2-bearer ${cfg.secret}")
r := conn.get_json_dict(prefix: 'tags', debug: false) or { return false }
println(r)
// if true{panic("ssss")}
tags := r['Tags'] or { return false }
console.print_debug(tags)
console.print_debug('Dagu is answering.')
return true
}
fn destroy() ! {
cmd := '
systemctl disable daguserver_scheduler.service
@@ -138,15 +137,3 @@ fn destroy() ! {
osal.execute_silent(cmd) or {}
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}

View File

@@ -2,9 +2,9 @@ module daguserver
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.ui.console
import time
__global (
@@ -22,9 +22,6 @@ pub mut:
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = daguserver_default
}
if args.name == '' {
args.name = 'default'
}
@@ -32,43 +29,50 @@ fn args_get(args_ ArgsGet) ArgsGet {
}
pub fn get(args_ ArgsGet) !&DaguInstaller {
mut context := base.context()!
mut args := args_get(args_)
mut obj := DaguInstaller{}
if args.name !in daguserver_global {
if args.name == 'default' {
if !config_exists(args) {
if default {
config_save(args)!
}
}
config_load(args)!
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('daguserver', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return daguserver_global[args.name] or {
println(daguserver_global)
panic('could not get config for daguserver with name:${args.name}')
// bug if we get here because should be in globals
panic('could not get config for daguserver with name, is bug:${args.name}')
}
}
fn config_exists(args_ ArgsGet) bool {
// register the config for the future
pub fn set(o DaguInstaller) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('daguserver', o.name, heroscript)!
}
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
mut context := base.context() or { panic('bug') }
return context.hero_config_exists('daguserver', args.name)
}
fn config_load(args_ ArgsGet) ! {
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
mut heroscript := context.hero_config_get('daguserver', args.name)!
play(heroscript: heroscript)!
context.hero_config_delete('daguserver', args.name)!
if args.name in daguserver_global {
// del daguserver_global[args.name]
}
}
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_set('daguserver', args.name, heroscript_default()!)!
}
fn set(o DaguInstaller) ! {
// only sets in mem, does not set as config
fn set_in_mem(o DaguInstaller) ! {
mut o2 := obj_init(o)!
daguserver_global[o.name] = &o2
daguserver_default = o.name
@@ -85,18 +89,14 @@ pub mut:
pub fn play(args_ PlayArgs) ! {
mut args := args_
if args.heroscript == '' {
args.heroscript = heroscript_default()!
}
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'daguserver.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
mycfg := cfg_play(p)!
console.print_debug('install action daguserver.configure\n${mycfg}')
set(mycfg)!
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
@@ -198,6 +198,7 @@ pub fn (mut self DaguInstaller) start() ! {
for _ in 0 .. 50 {
if self.running()! {
console.print_header('daguserver started')
return
}
time.sleep(100 * time.millisecond)
@@ -264,3 +265,10 @@ pub fn (mut self DaguInstaller) destroy() ! {
pub fn switch(name string) {
daguserver_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,60 +1,79 @@
module daguserver
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import freeflowuniverse.herolib.crypt.secrets
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.pathlib
import os
pub const version = '1.14.3'
const singleton = true
const default = true
const homedir = os.home_dir()
pub fn heroscript_default() !string {
heroscript := "
!!daguserver.configure
name:'daguserver'
title: 'My Hero DAG'
host: 'localhost'
port: 8888
"
return heroscript
}
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct DaguInstaller {
pub mut:
name string = 'default'
dagsdir string
configpath string
dagsdir string = '${homedir}/.dagu'
configpath string = '${homedir}/.config/dagu'
username string
password string @[secret]
secret string @[secret]
title string
host string
port int
host string = 'localhost'
port int = 8014
}
fn cfg_play(p paramsparser.Params) !DaguInstaller {
// THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
mut mycfg := DaguInstaller{
name: p.get_default('name', 'default')!
dagsdir: p.get_default('homedir', '${os.home_dir()}/hero/var/daguserver')!
configpath: p.get_default('configpath', '${os.home_dir()}/hero/cfg/dagu')!
username: p.get_default('username', 'admin')!
password: p.get_default('password', 'secretpassword')!
secret: p.get_default('secret', '')!
title: p.get_default('title', 'HERO DAG')!
host: p.get_default('host', 'localhost')!
port: p.get_int_default('port', 8888)!
}
if mycfg.password == '' && mycfg.secret == '' {
return error('password or secret needs to be filled in for daguserver')
}
// your checking & initialization code if needed
fn obj_init(mycfg_ DaguInstaller) !DaguInstaller {
mut mycfg := mycfg_
return mycfg
}
fn obj_init(obj_ DaguInstaller) !DaguInstaller {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
// called before start if done
fn configure() ! {
mut cfg := get()!
if cfg.password == '' {
cfg.password = secrets.hex_secret()!
}
// TODO:use DAGU_SECRET from env variables in os if not set then empty string
if cfg.secret == '' {
cfg.secret = secrets.openssl_hex_secret(input: cfg.password)!
}
if cfg.dagsdir == '' {
cfg.dagsdir = '${homedir}/.dagu'
}
if cfg.configpath == '' {
cfg.configpath = '${homedir}/.config/dagu'
}
if cfg.host == '' {
cfg.host = 'localhost'
}
if cfg.port == 0 {
cfg.port = 8014
}
mut mycode := $tmpl('templates/dagu.yaml')
mut path := pathlib.get_file(path: '${cfg.configpath}/admin.yaml', create: true)!
path.write(mycode)!
console.print_debug(mycode)
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj DaguInstaller) !string {
return encoderhero.encode[DaguInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !DaguInstaller {
mut obj := encoderhero.decode[DaguInstaller](heroscript)!
return obj
}

View File

@@ -1,48 +0,0 @@
module daguserver
import os
@[params]
pub struct DaguCommunicationConfig {
pub:
log_dir string // directory path to save logs from standard output
history_retention_days int // history retention days (default: 30)
mail_on MailOn // Email notification settings
smtp SMTP // SMTP server settings
error_mail Mail // Error mail configuration
info_mail Mail // Info mail configuration
}
pub struct SMTP {
pub:
host string
port string
username string
password string
error_mail Mail
}
pub struct Mail {
pub:
from string
to string
prefix string
}
pub struct MailOn {
pub:
failure bool
success bool
}
pub fn (mut self DaguInstaller) comms_configure(config DaguCommunicationConfig) ! {
// mut homedir := self.config()!.homedir
// config_yaml := $tmpl('./templates/communication.yaml')
// os.write_file('${homedir}/communication.yaml', config_yaml)!
// dags_dir := '${homedir}/dags'
// if !os.exists(dags_dir) {
// os.mkdir(dags_dir)!
// }
}

View File

@@ -1,10 +1,7 @@
module docker
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
@@ -17,7 +14,7 @@ __global (
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
}
@@ -29,58 +26,52 @@ pub fn get(args_ ArgsGet) !&DockerInstaller {
@[params]
pub struct PlayArgs {
pub mut:
heroscript string //if filled in then plbook will be made out of it
heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook
reset bool
}
pub fn play(args_ PlayArgs) ! {
mut args := args_
mut args:=args_
mut plbook := args.plbook or {
playbook.new(text: args.heroscript)!
}
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut other_actions := plbook.find(filter: 'docker.')!
for other_action in other_actions {
if other_action.name in ["destroy","install","build"]{
if other_action.name in ['destroy', 'install', 'build'] {
mut p := other_action.params
reset:=p.get_default_false("reset")
if other_action.name == "destroy" || reset{
console.print_debug("install action docker.destroy")
reset := p.get_default_false('reset')
if other_action.name == 'destroy' || reset {
console.print_debug('install action docker.destroy')
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action docker.install")
if other_action.name == 'install' {
console.print_debug('install action docker.install')
install()!
}
}
if other_action.name in ["start","stop","restart"]{
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut docker_obj:=get(name:name)!
console.print_debug("action object:\n${docker_obj}")
if other_action.name == "start"{
console.print_debug("install action docker.${other_action.name}")
mut docker_obj := get(name: name)!
console.print_debug('action object:\n${docker_obj}')
if other_action.name == 'start' {
console.print_debug('install action docker.${other_action.name}')
docker_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action docker.${other_action.name}")
if other_action.name == 'stop' {
console.print_debug('install action docker.${other_action.name}')
docker_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action docker.${other_action.name}")
if other_action.name == 'restart' {
console.print_debug('install action docker.${other_action.name}')
docker_obj.restart()!
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -91,31 +82,31 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
// zinit
// tmux
// systemd
match cat{
.zinit{
console.print_debug("startupmanager: zinit")
return startupmanager.get(cat:.zinit)!
match cat {
.zinit {
console.print_debug('startupmanager: zinit')
return startupmanager.get(cat: .zinit)!
}
.systemd{
console.print_debug("startupmanager: systemd")
return startupmanager.get(cat:.systemd)!
}else{
console.print_debug("startupmanager: auto")
.systemd {
console.print_debug('startupmanager: systemd')
return startupmanager.get(cat: .systemd)!
}
else {
console.print_debug('startupmanager: auto')
return startupmanager.get()!
}
}
}
pub fn (mut self DockerInstaller) start() ! {
switch(self.name)
if self.running()!{
if self.running()! {
return
}
console.print_header('docker start')
if ! installed()!{
if !installed()! {
install()!
}
@@ -123,8 +114,8 @@ pub fn (mut self DockerInstaller) start() ! {
start_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting docker with ${zprocess.startuptype}...')
@@ -142,7 +133,6 @@ pub fn (mut self DockerInstaller) start() ! {
time.sleep(100 * time.millisecond)
}
return error('docker did not install properly.')
}
pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! {
@@ -154,8 +144,8 @@ pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! {
pub fn (mut self DockerInstaller) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)!
}
stop_post()!
@@ -170,11 +160,11 @@ pub fn (mut self DockerInstaller) restart() ! {
pub fn (mut self DockerInstaller) running() !bool {
switch(self.name)
//walk over the generic processes, if not running return
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
r:=sm.running(zprocess.name)!
if r==false{
// walk over the generic processes, if not running return
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
r := sm.running(zprocess.name)!
if r == false {
return false
}
}
@@ -182,7 +172,7 @@ pub fn (mut self DockerInstaller) running() !bool {
}
@[params]
pub struct InstallArgs{
pub struct InstallArgs {
pub mut:
reset bool
}
@@ -194,24 +184,20 @@ pub fn (mut self DockerInstaller) install(args InstallArgs) ! {
}
}
pub fn (mut self DockerInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for docker
// switch instance to be used for docker
pub fn switch(name string) {
docker_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,12 +1,9 @@
module podman
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
podman_global map[string]&PodmanInstaller

View File

@@ -222,7 +222,7 @@ pub fn (mut sm StartupManager) delete(name string) ! {
}
}
else {
panic('to implement, startup manager only support screen & systemd for now ${mycat}')
panic('to implement, startup manager only support screen & systemd for now ${sm.cat}')
}
}
}
@@ -280,7 +280,7 @@ pub fn (mut sm StartupManager) status(name string) !ProcessStatus {
}
}
else {
panic('to implement, startup manager only support screen & systemd for now ${mycat}')
panic('to implement, startup manager only support screen & systemd for now ${sm.cat}')
}
}
}
@@ -303,7 +303,7 @@ pub fn (mut sm StartupManager) output(name string) !string {
return systemd.journalctl(service: name)!
}
else {
panic('to implement, startup manager only support screen & systemd for now ${mycat}')
panic('to implement, startup manager only support screen & systemd for now ${sm.cat}')
}
}
}
@@ -326,7 +326,7 @@ pub fn (mut sm StartupManager) exists(name string) !bool {
return zinitfactory.exists(name)
}
else {
panic('to implement. startup manager only support screen & systemd for now ${mycat}')
panic('to implement. startup manager only support screen & systemd for now ${sm.cat}')
}
}
}
@@ -347,7 +347,7 @@ pub fn (mut sm StartupManager) list() ![]string {
return zinitfactory.names()
}
else {
panic('to implement. startup manager only support screen & systemd for now: ${mycat}')
panic('to implement. startup manager only support screen & systemd for now: ${sm.cat}')
}
}
}

View File

@@ -37,9 +37,6 @@ pub fn (mut self SystemdProcess) write() ! {
servicecontent := $tmpl('templates/service.yaml')
println(self)
println(servicecontent)
p.write(servicecontent)!
}
@@ -54,6 +51,7 @@ pub fn (mut self SystemdProcess) start() ! {
_ = osal.execute_silent(cmd)!
self.refresh()!
console.print_header('started systemd process: ${self.name}')
}
// get status from system