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

@@ -1,15 +1,14 @@
module griddriver
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.develop.gittools
import os
// checks if a certain version or above is installed
fn installed_() !bool {
fn installed() !bool {
res := os.execute('/bin/bash -c "griddriver --version"')
if res.exit_code != 0 {
return false
@@ -27,12 +26,22 @@ fn installed_() !bool {
return true
}
fn install_() ! {
// console.print_header('install griddriver')
build()!
// 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{}
}
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')
mut installer := golang.get()!
installer.install()!
@@ -58,37 +67,17 @@ fn build_() ! {
console.print_header('build griddriver OK')
}
// get the Upload List of the files
fn ulist_get() !ulist.UList {
// mut installer := get()!
// 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_() ! {
// mut installer := get()!
// installers.upload(
// cmdname: 'griddriver'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/griddriver'
// )!
}
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)!
fn destroy() ! {
console.print_header('uninstall griddriver')
mut res := os.execute('sudo rm -rf /usr/local/bin/griddriver')
if res.exit_code != 0 {
return error('failed to uninstall griddriver: ${res.output}')
}
res = os.execute('sudo rm -rf ~/code/github/threefoldtech/web3gw')
if res.exit_code != 0 {
return error('failed to uninstall griddriver: ${res.output}')
}
console.print_header('uninstall griddriver OK')
}

View File

@@ -3,40 +3,126 @@ module griddriver
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 (
griddriver_global map[string]&GridDriverInstaller
griddriver_default string
griddriver_global map[string]&GridDriverInstaller
griddriver_default string
)
/////////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 ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
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]
pub struct InstallArgs {
pub struct InstallArgs{
pub mut:
reset bool
reset bool
}
pub fn install(args InstallArgs) ! {
if args.reset {
destroy()!
}
if !(installed_()!) {
install_()!
}
pub fn (mut self GridDriverInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn destroy() ! {
destroy_()!
pub fn (mut self GridDriverInstaller) build() ! {
switch(self.name)
build()!
}
pub fn build() ! {
build_()!
pub fn (mut self GridDriverInstaller) destroy() ! {
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
pub const version = 'v0.1.0'
import freeflowuniverse.herolib.data.encoderhero
pub const version = '0.1.1'
const singleton = 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 mut:
name string = 'default'
}
fn obj_init(obj_ GridDriverInstaller) !GridDriverInstaller {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
// your checking & initialization code if needed
fn obj_init(mycfg_ GridDriverInstaller) !GridDriverInstaller {
mut mycfg := mycfg_
return mycfg
}
// called before start if done
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
}