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:
@@ -1,15 +1,16 @@
|
|||||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
#!/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.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
|
// make sure zinit is there and running, will restart it if needed
|
||||||
mut z := zinit.get()!
|
// mut z := zinit_installer.get()!
|
||||||
z.destroy()!
|
// z.destroy()!
|
||||||
z.start()!
|
// z.install()!
|
||||||
|
// z.start()!
|
||||||
|
|
||||||
// mut ds := daguserver.get()!
|
mut ds := daguserver.get()!
|
||||||
// ds.destroy()!
|
// ds.destroy()!
|
||||||
// ds.start()!
|
ds.start()!
|
||||||
|
|
||||||
// println(ds)
|
println(ds)
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
import freeflowuniverse.herolib.installers.infra.zinit_installer
|
import freeflowuniverse.herolib.installers.infra.zinit_installer
|
||||||
|
|
||||||
mut installer := zinit_installer.get()!
|
mut installer := zinit_installer.get()!
|
||||||
// installer.install()!
|
installer.install()!
|
||||||
installer.start()!
|
installer.start()!
|
||||||
|
// installer.destroy()!
|
||||||
|
|||||||
@@ -189,7 +189,6 @@ pub fn (mut h HTTPConnection) get(req_ Request) !string {
|
|||||||
req.debug = true
|
req.debug = true
|
||||||
req.method = .get
|
req.method = .get
|
||||||
result := h.send(req)!
|
result := h.send(req)!
|
||||||
println(result)
|
|
||||||
return result.data
|
return result.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ fn startupcmd() ![]zinit_module.ZProcessNewArgs {
|
|||||||
res << zinit_module.ZProcessNewArgs{
|
res << zinit_module.ZProcessNewArgs{
|
||||||
name: 'zinit'
|
name: 'zinit'
|
||||||
cmd: '/usr/local/bin/zinit init'
|
cmd: '/usr/local/bin/zinit init'
|
||||||
startuptype: .zinit
|
startuptype: .systemd
|
||||||
start: true
|
start: true
|
||||||
restart: true
|
restart: true
|
||||||
}
|
}
|
||||||
@@ -126,8 +126,10 @@ fn build() ! {
|
|||||||
|
|
||||||
fn destroy() ! {
|
fn destroy() ! {
|
||||||
mut systemdfactory := systemd.new()!
|
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')!
|
osal.cmd_delete('zinit')!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ pub fn (mut self ZinitInstaller) start() ! {
|
|||||||
for zprocess in startupcmd()! {
|
for zprocess in startupcmd()! {
|
||||||
mut sm := startupmanager_get(zprocess.startuptype)!
|
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)!
|
sm.new(zprocess)!
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import freeflowuniverse.herolib.core.texttools
|
|||||||
import freeflowuniverse.herolib.installers.ulist
|
import freeflowuniverse.herolib.installers.ulist
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
//////////////////// following actions are not specific to instance of the object
|
//////////////////// following actions are not specific to instance of the object
|
||||||
|
|
||||||
// checks if a certain version or above is installed
|
// checks if a certain version or above is installed
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
|
|
||||||
@@ -4,16 +4,71 @@ import freeflowuniverse.herolib.osal
|
|||||||
import freeflowuniverse.herolib.ui.console
|
import freeflowuniverse.herolib.ui.console
|
||||||
import freeflowuniverse.herolib.core.texttools
|
import freeflowuniverse.herolib.core.texttools
|
||||||
import freeflowuniverse.herolib.core
|
import freeflowuniverse.herolib.core
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
|
||||||
import freeflowuniverse.herolib.core.httpconnection
|
import freeflowuniverse.herolib.core.httpconnection
|
||||||
|
import freeflowuniverse.herolib.installers.ulist
|
||||||
// import freeflowuniverse.herolib.develop.gittools
|
// import freeflowuniverse.herolib.develop.gittools
|
||||||
import freeflowuniverse.herolib.osal.zinit
|
import freeflowuniverse.herolib.osal.zinit
|
||||||
import freeflowuniverse.herolib.crypt.secrets
|
|
||||||
import os
|
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
|
// checks if a certain version or above is installed
|
||||||
fn installed() !bool {
|
fn installed() !bool {
|
||||||
res := os.execute('${osal.profile_path_source_and()!} dagu version')
|
res := os.execute('dagu version')
|
||||||
if res.exit_code == 0 {
|
if res.exit_code == 0 {
|
||||||
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
|
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
|
||||||
if r.len != 1 {
|
if r.len != 1 {
|
||||||
@@ -28,6 +83,15 @@ fn installed() !bool {
|
|||||||
return true
|
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() ! {
|
fn install() ! {
|
||||||
console.print_header('install daguserver')
|
console.print_header('install daguserver')
|
||||||
mut url := ''
|
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() ! {
|
fn destroy() ! {
|
||||||
cmd := '
|
cmd := '
|
||||||
systemctl disable daguserver_scheduler.service
|
systemctl disable daguserver_scheduler.service
|
||||||
@@ -138,15 +137,3 @@ fn destroy() ! {
|
|||||||
|
|
||||||
osal.execute_silent(cmd) or {}
|
osal.execute_silent(cmd) or {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_pre() ! {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start_post() ! {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stop_pre() ! {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stop_post() ! {
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ module daguserver
|
|||||||
|
|
||||||
import freeflowuniverse.herolib.core.base
|
import freeflowuniverse.herolib.core.base
|
||||||
import freeflowuniverse.herolib.core.playbook
|
import freeflowuniverse.herolib.core.playbook
|
||||||
|
import freeflowuniverse.herolib.ui.console
|
||||||
import freeflowuniverse.herolib.sysadmin.startupmanager
|
import freeflowuniverse.herolib.sysadmin.startupmanager
|
||||||
import freeflowuniverse.herolib.osal.zinit
|
import freeflowuniverse.herolib.osal.zinit
|
||||||
import freeflowuniverse.herolib.ui.console
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
__global (
|
__global (
|
||||||
@@ -22,9 +22,6 @@ pub mut:
|
|||||||
|
|
||||||
fn args_get(args_ ArgsGet) ArgsGet {
|
fn args_get(args_ ArgsGet) ArgsGet {
|
||||||
mut args := args_
|
mut args := args_
|
||||||
if args.name == '' {
|
|
||||||
args.name = daguserver_default
|
|
||||||
}
|
|
||||||
if args.name == '' {
|
if args.name == '' {
|
||||||
args.name = 'default'
|
args.name = 'default'
|
||||||
}
|
}
|
||||||
@@ -32,43 +29,50 @@ fn args_get(args_ ArgsGet) ArgsGet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(args_ ArgsGet) !&DaguInstaller {
|
pub fn get(args_ ArgsGet) !&DaguInstaller {
|
||||||
|
mut context := base.context()!
|
||||||
mut args := args_get(args_)
|
mut args := args_get(args_)
|
||||||
|
mut obj := DaguInstaller{}
|
||||||
if args.name !in daguserver_global {
|
if args.name !in daguserver_global {
|
||||||
if args.name == 'default' {
|
if !exists(args)! {
|
||||||
if !config_exists(args) {
|
set(obj)!
|
||||||
if default {
|
} else {
|
||||||
config_save(args)!
|
heroscript := context.hero_config_get('daguserver', args.name)!
|
||||||
}
|
mut obj_ := heroscript_loads(heroscript)!
|
||||||
}
|
set_in_mem(obj_)!
|
||||||
config_load(args)!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return daguserver_global[args.name] or {
|
return daguserver_global[args.name] or {
|
||||||
println(daguserver_global)
|
// bug if we get here because should be in globals
|
||||||
panic('could not get config for daguserver with name:${args.name}')
|
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 args := args_get(args_)
|
||||||
mut context := base.context() or { panic('bug') }
|
|
||||||
return context.hero_config_exists('daguserver', args.name)
|
return context.hero_config_exists('daguserver', args.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn config_load(args_ ArgsGet) ! {
|
pub fn delete(args_ ArgsGet) ! {
|
||||||
mut args := args_get(args_)
|
mut args := args_get(args_)
|
||||||
mut context := base.context()!
|
mut context := base.context()!
|
||||||
mut heroscript := context.hero_config_get('daguserver', args.name)!
|
context.hero_config_delete('daguserver', args.name)!
|
||||||
play(heroscript: heroscript)!
|
if args.name in daguserver_global {
|
||||||
|
// del daguserver_global[args.name]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn config_save(args_ ArgsGet) ! {
|
// only sets in mem, does not set as config
|
||||||
mut args := args_get(args_)
|
fn set_in_mem(o DaguInstaller) ! {
|
||||||
mut context := base.context()!
|
|
||||||
context.hero_config_set('daguserver', args.name, heroscript_default()!)!
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set(o DaguInstaller) ! {
|
|
||||||
mut o2 := obj_init(o)!
|
mut o2 := obj_init(o)!
|
||||||
daguserver_global[o.name] = &o2
|
daguserver_global[o.name] = &o2
|
||||||
daguserver_default = o.name
|
daguserver_default = o.name
|
||||||
@@ -85,18 +89,14 @@ pub mut:
|
|||||||
pub fn play(args_ PlayArgs) ! {
|
pub fn play(args_ PlayArgs) ! {
|
||||||
mut args := args_
|
mut args := args_
|
||||||
|
|
||||||
if args.heroscript == '' {
|
|
||||||
args.heroscript = heroscript_default()!
|
|
||||||
}
|
|
||||||
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
|
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
|
||||||
|
|
||||||
mut install_actions := plbook.find(filter: 'daguserver.configure')!
|
mut install_actions := plbook.find(filter: 'daguserver.configure')!
|
||||||
if install_actions.len > 0 {
|
if install_actions.len > 0 {
|
||||||
for install_action in install_actions {
|
for install_action in install_actions {
|
||||||
mut p := install_action.params
|
heroscript := install_action.heroscript()
|
||||||
mycfg := cfg_play(p)!
|
mut obj2 := heroscript_loads(heroscript)!
|
||||||
console.print_debug('install action daguserver.configure\n${mycfg}')
|
set(obj2)!
|
||||||
set(mycfg)!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,6 +198,7 @@ pub fn (mut self DaguInstaller) start() ! {
|
|||||||
|
|
||||||
for _ in 0 .. 50 {
|
for _ in 0 .. 50 {
|
||||||
if self.running()! {
|
if self.running()! {
|
||||||
|
console.print_header('daguserver started')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
time.sleep(100 * time.millisecond)
|
time.sleep(100 * time.millisecond)
|
||||||
@@ -264,3 +265,10 @@ pub fn (mut self DaguInstaller) destroy() ! {
|
|||||||
pub fn switch(name string) {
|
pub fn switch(name string) {
|
||||||
daguserver_default = name
|
daguserver_default = name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helpers
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct DefaultConfigArgs {
|
||||||
|
instance string = 'default'
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,60 +1,79 @@
|
|||||||
module daguserver
|
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
|
import os
|
||||||
|
|
||||||
pub const version = '1.14.3'
|
pub const version = '1.14.3'
|
||||||
const singleton = true
|
const singleton = true
|
||||||
const default = true
|
const default = true
|
||||||
|
const homedir = os.home_dir()
|
||||||
|
|
||||||
pub fn heroscript_default() !string {
|
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
|
||||||
heroscript := "
|
@[heap]
|
||||||
!!daguserver.configure
|
|
||||||
name:'daguserver'
|
|
||||||
title: 'My Hero DAG'
|
|
||||||
host: 'localhost'
|
|
||||||
port: 8888
|
|
||||||
"
|
|
||||||
|
|
||||||
return heroscript
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DaguInstaller {
|
pub struct DaguInstaller {
|
||||||
pub mut:
|
pub mut:
|
||||||
name string = 'default'
|
name string = 'default'
|
||||||
|
dagsdir string = '${homedir}/.dagu'
|
||||||
dagsdir string
|
configpath string = '${homedir}/.config/dagu'
|
||||||
configpath string
|
|
||||||
username string
|
username string
|
||||||
password string @[secret]
|
password string @[secret]
|
||||||
secret string @[secret]
|
secret string @[secret]
|
||||||
title string
|
title string
|
||||||
host string
|
host string = 'localhost'
|
||||||
port int
|
port int = 8014
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cfg_play(p paramsparser.Params) !DaguInstaller {
|
// your checking & initialization code if needed
|
||||||
// THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
|
fn obj_init(mycfg_ DaguInstaller) !DaguInstaller {
|
||||||
mut mycfg := DaguInstaller{
|
mut mycfg := mycfg_
|
||||||
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')
|
|
||||||
}
|
|
||||||
return mycfg
|
return mycfg
|
||||||
}
|
}
|
||||||
|
|
||||||
fn obj_init(obj_ DaguInstaller) !DaguInstaller {
|
// called before start if done
|
||||||
// never call get here, only thing we can do here is work on object itself
|
fn configure() ! {
|
||||||
mut obj := obj_
|
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
|
return obj
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)!
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@@ -1,217 +1,203 @@
|
|||||||
module docker
|
module docker
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.base
|
|
||||||
import freeflowuniverse.herolib.core.playbook
|
import freeflowuniverse.herolib.core.playbook
|
||||||
import freeflowuniverse.herolib.ui.console
|
import freeflowuniverse.herolib.ui.console
|
||||||
import freeflowuniverse.herolib.data.paramsparser
|
|
||||||
|
|
||||||
import freeflowuniverse.herolib.sysadmin.startupmanager
|
import freeflowuniverse.herolib.sysadmin.startupmanager
|
||||||
import freeflowuniverse.herolib.osal.zinit
|
import freeflowuniverse.herolib.osal.zinit
|
||||||
import time
|
import time
|
||||||
|
|
||||||
__global (
|
__global (
|
||||||
docker_global map[string]&DockerInstaller
|
docker_global map[string]&DockerInstaller
|
||||||
docker_default string
|
docker_default string
|
||||||
)
|
)
|
||||||
|
|
||||||
/////////FACTORY
|
/////////FACTORY
|
||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
pub struct ArgsGet{
|
pub struct ArgsGet {
|
||||||
pub mut:
|
pub mut:
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(args_ ArgsGet) !&DockerInstaller {
|
pub fn get(args_ ArgsGet) !&DockerInstaller {
|
||||||
return &DockerInstaller{}
|
return &DockerInstaller{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
pub struct PlayArgs {
|
pub struct PlayArgs {
|
||||||
pub mut:
|
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
|
plbook ?playbook.PlayBook
|
||||||
reset bool
|
reset bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn play(args_ PlayArgs) ! {
|
pub fn play(args_ PlayArgs) ! {
|
||||||
|
mut args := args_
|
||||||
mut args:=args_
|
|
||||||
|
|
||||||
mut plbook := args.plbook or {
|
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
|
||||||
playbook.new(text: args.heroscript)!
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mut other_actions := plbook.find(filter: 'docker.')!
|
mut other_actions := plbook.find(filter: 'docker.')!
|
||||||
for other_action in other_actions {
|
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
|
mut p := other_action.params
|
||||||
reset:=p.get_default_false("reset")
|
reset := p.get_default_false('reset')
|
||||||
if other_action.name == "destroy" || reset{
|
if other_action.name == 'destroy' || reset {
|
||||||
console.print_debug("install action docker.destroy")
|
console.print_debug('install action docker.destroy')
|
||||||
destroy()!
|
destroy()!
|
||||||
}
|
}
|
||||||
if other_action.name == "install"{
|
if other_action.name == 'install' {
|
||||||
console.print_debug("install action docker.install")
|
console.print_debug('install action docker.install')
|
||||||
install()!
|
install()!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if other_action.name in ["start","stop","restart"]{
|
if other_action.name in ['start', 'stop', 'restart'] {
|
||||||
mut p := other_action.params
|
mut p := other_action.params
|
||||||
name := p.get('name')!
|
name := p.get('name')!
|
||||||
mut docker_obj:=get(name:name)!
|
mut docker_obj := get(name: name)!
|
||||||
console.print_debug("action object:\n${docker_obj}")
|
console.print_debug('action object:\n${docker_obj}')
|
||||||
if other_action.name == "start"{
|
if other_action.name == 'start' {
|
||||||
console.print_debug("install action docker.${other_action.name}")
|
console.print_debug('install action docker.${other_action.name}')
|
||||||
docker_obj.start()!
|
docker_obj.start()!
|
||||||
}
|
}
|
||||||
|
|
||||||
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}")
|
|
||||||
docker_obj.restart()!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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}')
|
||||||
|
docker_obj.restart()!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
|
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
|
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
|
||||||
// unknown
|
// unknown
|
||||||
// screen
|
// screen
|
||||||
// zinit
|
// zinit
|
||||||
// tmux
|
// tmux
|
||||||
// systemd
|
// systemd
|
||||||
match cat{
|
match cat {
|
||||||
.zinit{
|
.zinit {
|
||||||
console.print_debug("startupmanager: zinit")
|
console.print_debug('startupmanager: zinit')
|
||||||
return startupmanager.get(cat:.zinit)!
|
return startupmanager.get(cat: .zinit)!
|
||||||
}
|
}
|
||||||
.systemd{
|
.systemd {
|
||||||
console.print_debug("startupmanager: systemd")
|
console.print_debug('startupmanager: systemd')
|
||||||
return startupmanager.get(cat:.systemd)!
|
return startupmanager.get(cat: .systemd)!
|
||||||
}else{
|
}
|
||||||
console.print_debug("startupmanager: auto")
|
else {
|
||||||
return startupmanager.get()!
|
console.print_debug('startupmanager: auto')
|
||||||
}
|
return startupmanager.get()!
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) start() ! {
|
pub fn (mut self DockerInstaller) start() ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
if self.running()!{
|
if self.running()! {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.print_header('docker start')
|
console.print_header('docker start')
|
||||||
|
|
||||||
if ! installed()!{
|
if !installed()! {
|
||||||
install()!
|
install()!
|
||||||
}
|
}
|
||||||
|
|
||||||
configure()!
|
configure()!
|
||||||
|
|
||||||
start_pre()!
|
start_pre()!
|
||||||
|
|
||||||
for zprocess in startupcmd()!{
|
for zprocess in startupcmd()! {
|
||||||
mut sm:=startupmanager_get(zprocess.startuptype)!
|
mut sm := startupmanager_get(zprocess.startuptype)!
|
||||||
|
|
||||||
console.print_debug('starting docker with ${zprocess.startuptype}...')
|
console.print_debug('starting docker with ${zprocess.startuptype}...')
|
||||||
|
|
||||||
sm.new(zprocess)!
|
sm.new(zprocess)!
|
||||||
|
|
||||||
sm.start(zprocess.name)!
|
sm.start(zprocess.name)!
|
||||||
}
|
}
|
||||||
|
|
||||||
start_post()!
|
start_post()!
|
||||||
|
|
||||||
for _ in 0 .. 50 {
|
|
||||||
if self.running()! {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
time.sleep(100 * time.millisecond)
|
|
||||||
}
|
|
||||||
return error('docker did not install properly.')
|
|
||||||
|
|
||||||
|
for _ in 0 .. 50 {
|
||||||
|
if self.running()! {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.sleep(100 * time.millisecond)
|
||||||
|
}
|
||||||
|
return error('docker did not install properly.')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! {
|
pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
self.install(args)!
|
self.install(args)!
|
||||||
self.start()!
|
self.start()!
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) stop() ! {
|
pub fn (mut self DockerInstaller) stop() ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
stop_pre()!
|
stop_pre()!
|
||||||
for zprocess in startupcmd()!{
|
for zprocess in startupcmd()! {
|
||||||
mut sm:=startupmanager_get(zprocess.startuptype)!
|
mut sm := startupmanager_get(zprocess.startuptype)!
|
||||||
sm.stop(zprocess.name)!
|
sm.stop(zprocess.name)!
|
||||||
}
|
}
|
||||||
stop_post()!
|
stop_post()!
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) restart() ! {
|
pub fn (mut self DockerInstaller) restart() ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
self.stop()!
|
self.stop()!
|
||||||
self.start()!
|
self.start()!
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) running() !bool {
|
pub fn (mut self DockerInstaller) running() !bool {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
|
|
||||||
//walk over the generic processes, if not running return
|
// walk over the generic processes, if not running return
|
||||||
for zprocess in startupcmd()!{
|
for zprocess in startupcmd()! {
|
||||||
mut sm:=startupmanager_get(zprocess.startuptype)!
|
mut sm := startupmanager_get(zprocess.startuptype)!
|
||||||
r:=sm.running(zprocess.name)!
|
r := sm.running(zprocess.name)!
|
||||||
if r==false{
|
if r == false {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return running()!
|
return running()!
|
||||||
}
|
}
|
||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
pub struct InstallArgs{
|
pub struct InstallArgs {
|
||||||
pub mut:
|
pub mut:
|
||||||
reset bool
|
reset bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) install(args InstallArgs) ! {
|
pub fn (mut self DockerInstaller) install(args InstallArgs) ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
if args.reset || (!installed()!) {
|
if args.reset || (!installed()!) {
|
||||||
install()!
|
install()!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn (mut self DockerInstaller) destroy() ! {
|
pub fn (mut self DockerInstaller) destroy() ! {
|
||||||
switch(self.name)
|
switch(self.name)
|
||||||
self.stop() or {}
|
self.stop() or {}
|
||||||
destroy()!
|
destroy()!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// switch instance to be used for docker
|
||||||
|
|
||||||
//switch instance to be used for docker
|
|
||||||
pub fn switch(name string) {
|
pub fn switch(name string) {
|
||||||
docker_default = name
|
docker_default = name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helpers
|
||||||
//helpers
|
|
||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
pub struct DefaultConfigArgs{
|
pub struct DefaultConfigArgs {
|
||||||
instance string = 'default'
|
instance string = 'default'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
module podman
|
module podman
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.base
|
|
||||||
import freeflowuniverse.herolib.core.playbook
|
import freeflowuniverse.herolib.core.playbook
|
||||||
import freeflowuniverse.herolib.ui.console
|
import freeflowuniverse.herolib.ui.console
|
||||||
import freeflowuniverse.herolib.data.paramsparser
|
|
||||||
import freeflowuniverse.herolib.sysadmin.startupmanager
|
import freeflowuniverse.herolib.sysadmin.startupmanager
|
||||||
import freeflowuniverse.herolib.osal.zinit
|
import freeflowuniverse.herolib.osal.zinit
|
||||||
import time
|
|
||||||
|
|
||||||
__global (
|
__global (
|
||||||
podman_global map[string]&PodmanInstaller
|
podman_global map[string]&PodmanInstaller
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ pub fn (mut sm StartupManager) delete(name string) ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
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 {
|
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)!
|
return systemd.journalctl(service: name)!
|
||||||
}
|
}
|
||||||
else {
|
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)
|
return zinitfactory.exists(name)
|
||||||
}
|
}
|
||||||
else {
|
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()
|
return zinitfactory.names()
|
||||||
}
|
}
|
||||||
else {
|
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}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,9 +37,6 @@ pub fn (mut self SystemdProcess) write() ! {
|
|||||||
|
|
||||||
servicecontent := $tmpl('templates/service.yaml')
|
servicecontent := $tmpl('templates/service.yaml')
|
||||||
|
|
||||||
println(self)
|
|
||||||
println(servicecontent)
|
|
||||||
|
|
||||||
p.write(servicecontent)!
|
p.write(servicecontent)!
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +51,7 @@ pub fn (mut self SystemdProcess) start() ! {
|
|||||||
|
|
||||||
_ = osal.execute_silent(cmd)!
|
_ = osal.execute_silent(cmd)!
|
||||||
self.refresh()!
|
self.refresh()!
|
||||||
|
console.print_header('started systemd process: ${self.name}')
|
||||||
}
|
}
|
||||||
|
|
||||||
// get status from system
|
// get status from system
|
||||||
|
|||||||
Reference in New Issue
Block a user