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,6 +1,5 @@
module grafana
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
@@ -14,6 +13,65 @@ __global (
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&Grafana {
return &Grafana{}
}
@[params]
pub struct PlayArgs {
pub mut:
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 plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut other_actions := plbook.find(filter: 'grafana.')!
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 grafana.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action grafana.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut grafana_obj := get(name: name)!
console.print_debug('action object:\n${grafana_obj}')
if other_action.name == 'start' {
console.print_debug('install action grafana.${other_action.name}')
grafana_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action grafana.${other_action.name}')
grafana_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action grafana.${other_action.name}')
grafana_obj.restart()!
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -48,8 +106,8 @@ pub fn (mut self Grafana) start() ! {
console.print_header('grafana start')
if !installed_()! {
install_()!
if !installed()! {
install()!
}
configure()!
@@ -77,9 +135,9 @@ pub fn (mut self Grafana) start() ! {
return error('grafana did not install properly.')
}
pub fn (mut self Grafana) install_start(model InstallArgs) ! {
pub fn (mut self Grafana) install_start(args InstallArgs) ! {
switch(self.name)
self.install(model)!
self.install(args)!
self.start()!
}
@@ -119,19 +177,32 @@ pub mut:
reset bool
}
pub fn install(args InstallArgs) ! {
if args.reset {
destroy()!
}
if !(installed_()!) {
install_()!
pub fn (mut self Grafana) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn destroy() ! {
destroy_()!
pub fn (mut self Grafana) build() ! {
switch(self.name)
build()!
}
pub fn build() ! {
build_()!
pub fn (mut self Grafana) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for grafana
pub fn switch(name string) {
grafana_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}