refactor: update installer generator templates to instance-based API

Update generator templates to produce installers following the new pattern:

Actions template (objname_actions.vtemplate):
- Convert all functions to methods on the config struct
- startupcmd() -> (self &Struct) startupcmd()
- running() -> (self &Struct) running_check()
- start_pre/post, stop_pre/post -> methods on struct
- installed(), install(), build(), destroy() -> methods on struct
- Add InstallArgs struct with reset parameter
- Remove get()! calls, use self instead

Factory template (objname_factory_.vtemplate):
- Update play() to get name parameter for all actions
- Call instance methods instead of module-level functions
- Add support for start_pre, start_post, stop_pre, stop_post actions
- Update start(), stop(), running() to use self.method() calls
- Remove duplicate InstallArgs and wrapper methods
- Use self.running_check() instead of running()

All newly generated installers will now follow the consistent
instance-based pattern with proper lifecycle hook support.
This commit is contained in:
Timur Gordon
2025-11-19 15:09:24 +01:00
parent fcb178156b
commit a40e172457
2 changed files with 56 additions and 56 deletions

View File

@@ -20,8 +20,7 @@ import incubaid.herolib.installers.lang.python
import os
@if args.startupmanager
fn startupcmd () ![]startupmanager.ZProcessNewArgs{
mut installer := get()!
fn (self &${args.classname}) startupcmd() ![]startupmanager.ZProcessNewArgs {
mut res := []startupmanager.ZProcessNewArgs{}
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res << startupmanager.ZProcessNewArgs{
@@ -36,8 +35,7 @@ fn startupcmd () ![]startupmanager.ZProcessNewArgs{
}
fn running() !bool {
mut installer := get()!
fn (self &${args.classname}) running_check() !bool {
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of ${args.name}
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
@@ -58,19 +56,19 @@ fn running() !bool {
return false
}
fn start_pre()!{
fn (self &${args.classname}) start_pre() ! {
}
fn start_post()!{
fn (self &${args.classname}) start_post() ! {
}
fn stop_pre()!{
fn (self &${args.classname}) stop_pre() ! {
}
fn stop_post()!{
fn (self &${args.classname}) stop_post() ! {
}
@@ -80,7 +78,7 @@ fn stop_post()!{
@if args.cat == .installer
// checks if a certain version or above is installed
fn installed() !bool {
fn (self &${args.classname}) installed() !bool {
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res := os.execute('??{osal.profile_path_source_and()!} ${args.name} version')
// if res.exit_code != 0 {
@@ -111,7 +109,14 @@ fn upload() ! {
}
fn install() ! {
@[params]
pub struct InstallArgs {
pub mut:
reset bool
}
fn (mut self ${args.classname}) install(args InstallArgs) ! {
console.print_header('install ${args.name}')
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
@@ -143,7 +148,7 @@ fn install() ! {
}
@if args.build
fn build() ! {
fn (mut self ${args.classname}) build() ! {
//url := 'https://github.com/threefoldtech/${args.name}'
// make sure we install base on the node
@@ -174,7 +179,8 @@ fn build() ! {
}
@end
fn destroy() ! {
fn (mut self ${args.classname}) destroy() ! {
self.stop()!
// mut systemdfactory := systemd.new()!
// systemdfactory.destroy("zinit")!

View File

@@ -171,18 +171,26 @@ pub fn play(mut plbook PlayBook) ! {
for mut other_action in other_actions {
if other_action.name in ["destroy","install","build"]{
mut p := other_action.params
name := p.get_default('name', 'default')!
reset:=p.get_default_false("reset")
mut ${args.name}_obj:=get(name:name)!
console.print_debug("action object:\n??{${args.name}_obj}")
if other_action.name == "destroy" || reset{
console.print_debug("install action ${args.name}.destroy")
destroy()!
${args.name}_obj.destroy()!
}
if other_action.name == "install"{
console.print_debug("install action ${args.name}.install")
install()!
}
${args.name}_obj.install(reset: reset)!
}
if other_action.name == "build"{
console.print_debug("install action ${args.name}.build")
${args.name}_obj.build()!
}
}
@if args.startupmanager
if other_action.name in ["start","stop","restart"]{
if other_action.name in ["start","stop","restart","start_pre","start_post","stop_pre","stop_post"]{
mut p := other_action.params
name := p.get('name')!
mut ${args.name}_obj:=get(name:name)!
@@ -191,7 +199,6 @@ pub fn play(mut plbook PlayBook) ! {
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.stop()!
@@ -200,6 +207,22 @@ pub fn play(mut plbook PlayBook) ! {
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.restart()!
}
if other_action.name == "start_pre"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.start_pre()!
}
if other_action.name == "start_post"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.start_post()!
}
if other_action.name == "stop_pre"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.stop_pre()!
}
if other_action.name == "stop_post"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.stop_post()!
}
}
@end
other_action.done = true
@@ -262,15 +285,13 @@ pub fn (mut self ${args.classname}) start() ! {
console.print_header('installer: ${args.name} start')
if ! installed()!{
install()!
if ! self.installed()!{
self.install()!
}
configure()!
self.start_pre()!
start_pre()!
for zprocess in startupcmd()!{
for zprocess in self.startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
console.print_debug('installer: ${args.name} starting with ??{zprocess.startuptype}...')
@@ -280,7 +301,7 @@ pub fn (mut self ${args.classname}) start() ! {
sm.start(zprocess.name)!
}
start_post()!
self.start_post()!
for _ in 0 .. 50 {
if self.running()! {
@@ -300,12 +321,12 @@ pub fn (mut self ${args.classname}) install_start(args InstallArgs) ! {
pub fn (mut self ${args.classname}) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()!{
self.stop_pre()!
for zprocess in self.startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)!
}
stop_post()!
self.stop_post()!
}
pub fn (mut self ${args.classname}) restart() ! {
@@ -318,7 +339,7 @@ pub fn (mut self ${args.classname}) running() !bool {
switch(self.name)
//walk over the generic processes, if not running return
for zprocess in startupcmd()!{
for zprocess in self.startupcmd()!{
if zprocess.startuptype != .screen{
mut sm:=startupmanager_get(zprocess.startuptype)!
r:=sm.running(zprocess.name)!
@@ -327,37 +348,10 @@ pub fn (mut self ${args.classname}) running() !bool {
}
}
}
return running()!
return self.running_check()!
}
@end
@@[params]
pub struct InstallArgs{
pub mut:
reset bool
}
pub fn (mut self ${args.classname}) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
@if args.build
pub fn (mut self ${args.classname}) build() ! {
switch(self.name)
build()!
}
@end
pub fn (mut self ${args.classname}) destroy() ! {
switch(self.name)
@if args.startupmanager
self.stop() or {}
@end
destroy()!
}
@end