From ec22a8e0ecdd6060e9c082d4b66460bd77ae6f38 Mon Sep 17 00:00:00 2001 From: Mahmoud Emad Date: Wed, 12 Feb 2025 09:02:33 +0000 Subject: [PATCH] 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. --- examples/installers/dagu.vsh | 15 +- examples/installers/zinit_installer.vsh | 3 +- .../daguserver}/dagu_server.vsh | 0 lib/core/httpconnection/connection_methods.v | 1 - .../zinit_installer/zinit_installer_actions.v | 8 +- .../zinit_installer_factory_.v | 2 +- lib/installers/lang/python/python_actions.v | 1 - .../sysadmintools/daguserver/dagu_test.v | 1 - .../daguserver/daguserver_actions.v | 147 +++++----- .../daguserver/daguserver_factory_.v | 72 ++--- .../daguserver/daguserver_model.v | 97 ++++--- .../sysadmintools/daguserver/model_comms.v | 48 ---- lib/installers/virt/docker/docker_factory_.v | 262 +++++++++--------- lib/installers/virt/podman/podman_factory_.v | 3 - lib/osal/startupmanager/startupmanager.v | 10 +- lib/osal/systemd/systemd_process.v | 4 +- 16 files changed, 311 insertions(+), 363 deletions(-) rename examples/{installers => virt/daguserver}/dagu_server.vsh (100%) delete mode 100644 lib/installers/sysadmintools/daguserver/dagu_test.v delete mode 100644 lib/installers/sysadmintools/daguserver/model_comms.v diff --git a/examples/installers/dagu.vsh b/examples/installers/dagu.vsh index 39704612..5d2787f0 100755 --- a/examples/installers/dagu.vsh +++ b/examples/installers/dagu.vsh @@ -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) diff --git a/examples/installers/zinit_installer.vsh b/examples/installers/zinit_installer.vsh index 418a176d..ac3bce5f 100755 --- a/examples/installers/zinit_installer.vsh +++ b/examples/installers/zinit_installer.vsh @@ -3,5 +3,6 @@ import freeflowuniverse.herolib.installers.infra.zinit_installer mut installer := zinit_installer.get()! -// installer.install()! +installer.install()! installer.start()! +// installer.destroy()! diff --git a/examples/installers/dagu_server.vsh b/examples/virt/daguserver/dagu_server.vsh similarity index 100% rename from examples/installers/dagu_server.vsh rename to examples/virt/daguserver/dagu_server.vsh diff --git a/lib/core/httpconnection/connection_methods.v b/lib/core/httpconnection/connection_methods.v index ee3283d2..5c06fcaa 100644 --- a/lib/core/httpconnection/connection_methods.v +++ b/lib/core/httpconnection/connection_methods.v @@ -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 } diff --git a/lib/installers/infra/zinit_installer/zinit_installer_actions.v b/lib/installers/infra/zinit_installer/zinit_installer_actions.v index 383870e0..da73fcfd 100644 --- a/lib/installers/infra/zinit_installer/zinit_installer_actions.v +++ b/lib/installers/infra/zinit_installer/zinit_installer_actions.v @@ -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')! } diff --git a/lib/installers/infra/zinit_installer/zinit_installer_factory_.v b/lib/installers/infra/zinit_installer/zinit_installer_factory_.v index d3e6b21b..c951b932 100644 --- a/lib/installers/infra/zinit_installer/zinit_installer_factory_.v +++ b/lib/installers/infra/zinit_installer/zinit_installer_factory_.v @@ -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)! diff --git a/lib/installers/lang/python/python_actions.v b/lib/installers/lang/python/python_actions.v index 9d65f6ec..6414118b 100644 --- a/lib/installers/lang/python/python_actions.v +++ b/lib/installers/lang/python/python_actions.v @@ -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 diff --git a/lib/installers/sysadmintools/daguserver/dagu_test.v b/lib/installers/sysadmintools/daguserver/dagu_test.v deleted file mode 100644 index 8b137891..00000000 --- a/lib/installers/sysadmintools/daguserver/dagu_test.v +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lib/installers/sysadmintools/daguserver/daguserver_actions.v b/lib/installers/sysadmintools/daguserver/daguserver_actions.v index d98e2230..caef4804 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_actions.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_actions.v @@ -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() ! { -} diff --git a/lib/installers/sysadmintools/daguserver/daguserver_factory_.v b/lib/installers/sysadmintools/daguserver/daguserver_factory_.v index 5f9f4e13..a034c961 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_factory_.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_factory_.v @@ -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' +} diff --git a/lib/installers/sysadmintools/daguserver/daguserver_model.v b/lib/installers/sysadmintools/daguserver/daguserver_model.v index e9bece7f..b33233dd 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_model.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_model.v @@ -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 + name string = 'default' + 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 } diff --git a/lib/installers/sysadmintools/daguserver/model_comms.v b/lib/installers/sysadmintools/daguserver/model_comms.v deleted file mode 100644 index 6a5db486..00000000 --- a/lib/installers/sysadmintools/daguserver/model_comms.v +++ /dev/null @@ -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)! - // } -} diff --git a/lib/installers/virt/docker/docker_factory_.v b/lib/installers/virt/docker/docker_factory_.v index ed3cc833..e9cc9147 100644 --- a/lib/installers/virt/docker/docker_factory_.v +++ b/lib/installers/virt/docker/docker_factory_.v @@ -1,217 +1,203 @@ 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 __global ( - docker_global map[string]&DockerInstaller - docker_default string + docker_global map[string]&DockerInstaller + docker_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&DockerInstaller { - return &DockerInstaller{} +pub fn get(args_ ArgsGet) !&DockerInstaller { + return &DockerInstaller{} } @[params] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + 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"]{ - mut p := other_action.params - 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") - install()! - } - } - 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}") - 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()! - } - } - } + mut other_actions := plbook.find(filter: 'docker.')! + for other_action in other_actions { + 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') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action docker.install') + install()! + } + } + 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}') + 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()! + } + } + } } - //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { - // unknown - // screen - // zinit - // tmux - // systemd - 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") - return startupmanager.get()! - } - } + // unknown + // screen + // zinit + // tmux + // systemd + 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') + return startupmanager.get()! + } + } } - pub fn (mut self DockerInstaller) start() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('docker start') + console.print_header('docker start') - if ! installed()!{ - install()! - } + if !installed()! { + install()! + } - configure()! + configure()! - start_pre()! + 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}...') + console.print_debug('starting docker with ${zprocess.startuptype}...') - sm.new(zprocess)! + sm.new(zprocess)! - sm.start(zprocess.name)! - } + sm.start(zprocess.name)! + } - start_post()! - - for _ in 0 .. 50 { - if self.running()! { - return - } - time.sleep(100 * time.millisecond) - } - return error('docker did not install properly.') + start_post()! + 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) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self DockerInstaller) stop() ! { - switch(self.name) - stop_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - sm.stop(zprocess.name)! - } - stop_post()! + switch(self.name) + stop_pre()! + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! + sm.stop(zprocess.name)! + } + stop_post()! } pub fn (mut self DockerInstaller) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self DockerInstaller) running() !bool { - switch(self.name) + 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{ - return false - } - } - return running()! + // 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 + } + } + return running()! } @[params] -pub struct InstallArgs{ +pub struct InstallArgs { pub mut: - reset bool + reset bool } pub fn (mut self DockerInstaller) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } - pub fn (mut self DockerInstaller) destroy() ! { - switch(self.name) - self.stop() or {} - 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 + docker_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/virt/podman/podman_factory_.v b/lib/installers/virt/podman/podman_factory_.v index e2c1396f..319145a3 100644 --- a/lib/installers/virt/podman/podman_factory_.v +++ b/lib/installers/virt/podman/podman_factory_.v @@ -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 diff --git a/lib/osal/startupmanager/startupmanager.v b/lib/osal/startupmanager/startupmanager.v index c3730f1c..95b33e1f 100644 --- a/lib/osal/startupmanager/startupmanager.v +++ b/lib/osal/startupmanager/startupmanager.v @@ -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}') } } } diff --git a/lib/osal/systemd/systemd_process.v b/lib/osal/systemd/systemd_process.v index 78f40ae0..efd1e9e6 100644 --- a/lib/osal/systemd/systemd_process.v +++ b/lib/osal/systemd/systemd_process.v @@ -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