chore: Change the factories

This commit is contained in:
Mahmoud Emad
2025-02-16 11:12:59 +00:00
parent d604d739e3
commit 61a0fd2aa6
17 changed files with 1343 additions and 641 deletions

View File

@@ -1,221 +1,208 @@
module yggdrasil
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 (
yggdrasil_global map[string]&YggdrasilInstaller
yggdrasil_default string
yggdrasil_global map[string]&YggdrasilInstaller
yggdrasil_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
pub fn get(args_ ArgsGet) !&YggdrasilInstaller {
return &YggdrasilInstaller{}
pub fn get(args_ ArgsGet) !&YggdrasilInstaller {
return &YggdrasilInstaller{}
}
@[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: 'yggdrasil.')!
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 yggdrasil.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action yggdrasil.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut yggdrasil_obj:=get(name:name)!
console.print_debug("action object:\n${yggdrasil_obj}")
if other_action.name == "start"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'yggdrasil.')!
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 yggdrasil.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action yggdrasil.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut yggdrasil_obj := get(name: name)!
console.print_debug('action object:\n${yggdrasil_obj}')
if other_action.name == 'start' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_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 YggdrasilInstaller) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('yggdrasil start')
console.print_header('yggdrasil 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 yggdrasil with ${zprocess.startuptype}...')
console.print_debug('starting yggdrasil 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('yggdrasil did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('yggdrasil did not install properly.')
}
pub fn (mut self YggdrasilInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self YggdrasilInstaller) 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 YggdrasilInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self YggdrasilInstaller) 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 YggdrasilInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self YggdrasilInstaller) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self YggdrasilInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for yggdrasil
// switch instance to be used for yggdrasil
pub fn switch(name string) {
yggdrasil_default = name
yggdrasil_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}