feat: Improved the griddriver installer, added an example

This commit is contained in:
Mahmoud-Emad
2025-02-10 11:51:39 +00:00
parent 1b192328b2
commit e86504ecd5
4 changed files with 154 additions and 61 deletions

View File

@@ -4,3 +4,4 @@ import freeflowuniverse.herolib.installers.threefold.griddriver
mut griddriver_installer := griddriver.get()! mut griddriver_installer := griddriver.get()!
griddriver_installer.install()! griddriver_installer.install()!
// griddriver_installer.destroy()!

View File

@@ -1,15 +1,14 @@
module griddriver module griddriver
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.develop.gittools import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.installers.ulist import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang import freeflowuniverse.herolib.installers.lang.golang
import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.develop.gittools
import os import os
// 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('/bin/bash -c "griddriver --version"') res := os.execute('/bin/bash -c "griddriver --version"')
if res.exit_code != 0 { if res.exit_code != 0 {
return false return false
@@ -27,12 +26,22 @@ fn installed_() !bool {
return true return true
} }
fn install_() ! { // get the Upload List of the files
// console.print_header('install griddriver') fn ulist_get() !ulist.UList {
build()! // optionally build a UList which is all paths which are result of building, is then used e.g. in upload
return ulist.UList{}
} }
fn build_() ! { // uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('install griddriver')
build()!
console.print_header('install griddriver OK')
}
fn build() ! {
console.print_header('build griddriver') console.print_header('build griddriver')
mut installer := golang.get()! mut installer := golang.get()!
installer.install()! installer.install()!
@@ -58,37 +67,17 @@ fn build_() ! {
console.print_header('build griddriver OK') console.print_header('build griddriver OK')
} }
// get the Upload List of the files fn destroy() ! {
fn ulist_get() !ulist.UList { console.print_header('uninstall griddriver')
// mut installer := get()! mut res := os.execute('sudo rm -rf /usr/local/bin/griddriver')
// optionally build a UList which is all paths which are result of building, is then used e.g. in upload if res.exit_code != 0 {
return ulist.UList{} return error('failed to uninstall griddriver: ${res.output}')
} }
// uploads to S3 server if configured res = os.execute('sudo rm -rf ~/code/github/threefoldtech/web3gw')
fn upload_() ! { if res.exit_code != 0 {
// mut installer := get()! return error('failed to uninstall griddriver: ${res.output}')
// installers.upload( }
// cmdname: 'griddriver'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/griddriver' console.print_header('uninstall griddriver OK')
// )!
}
fn destroy_() ! {
// mut installer := get()!
// cmd:="
// systemctl disable griddriver_scheduler.service
// systemctl disable griddriver.service
// systemctl stop griddriver_scheduler.service
// systemctl stop griddriver.service
// systemctl list-unit-files | grep griddriver
// pkill -9 -f griddriver
// ps aux | grep griddriver
// "
// osal.exec(cmd: cmd, stdout:true, debug: false)!
} }

View File

@@ -3,40 +3,126 @@ module griddriver
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.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 (
griddriver_global map[string]&GridDriverInstaller griddriver_global map[string]&GridDriverInstaller
griddriver_default string griddriver_default string
) )
/////////FACTORY /////////FACTORY
@[params]
pub struct ArgsGet{
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&GridDriverInstaller {
return &GridDriverInstaller{}
}
@[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: 'griddriver.')!
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 griddriver.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action griddriver.install")
install()!
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////# 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()!
}
}
}
@[params] @[params]
pub struct InstallArgs { pub struct InstallArgs{
pub mut: pub mut:
reset bool reset bool
} }
pub fn install(args InstallArgs) ! { pub fn (mut self GridDriverInstaller) install(args InstallArgs) ! {
if args.reset { switch(self.name)
destroy()! if args.reset || (!installed()!) {
} install()!
if !(installed_()!) { }
install_()!
}
} }
pub fn destroy() ! { pub fn (mut self GridDriverInstaller) build() ! {
destroy_()! switch(self.name)
build()!
} }
pub fn build() ! { pub fn (mut self GridDriverInstaller) destroy() ! {
build_()! switch(self.name)
destroy()!
}
//switch instance to be used for griddriver
pub fn switch(name string) {
griddriver_default = name
}
//helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
} }

View File

@@ -1,19 +1,36 @@
module griddriver module griddriver
pub const version = 'v0.1.0' import freeflowuniverse.herolib.data.encoderhero
pub const version = '0.1.1'
const singleton = true const singleton = true
const default = true const default = true
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct GridDriverInstaller { pub struct GridDriverInstaller {
pub mut: pub mut:
name string = 'default' name string = 'default'
} }
fn obj_init(obj_ GridDriverInstaller) !GridDriverInstaller { // your checking & initialization code if needed
// never call get here, only thing we can do here is work on object itself fn obj_init(mycfg_ GridDriverInstaller) !GridDriverInstaller {
mut obj := obj_ mut mycfg := mycfg_
return obj return mycfg
} }
// called before start if done
fn configure() ! { fn configure() ! {
// mut installer := get()!
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj GridDriverInstaller) !string {
return encoderhero.encode[GridDriverInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !GridDriverInstaller {
mut obj := encoderhero.decode[GridDriverInstaller](heroscript)!
return obj
} }