feat: Improved b2 installer

This commit is contained in:
Mahmoud Emad
2025-02-16 07:48:11 +00:00
parent fc993a95d7
commit 7fb46a4c0b
6 changed files with 116 additions and 281 deletions

View File

@@ -6,6 +6,6 @@
default: true default: true
title: "" title: ""
templates: false templates: false
build: true build: false
startupmanager: true startupmanager: false

View File

@@ -2,82 +2,16 @@ module b2
import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang import freeflowuniverse.herolib.lang.python
import freeflowuniverse.herolib.installers.lang.rust
import freeflowuniverse.herolib.installers.lang.python
import os import os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut installer := get()!
mut res := []zinit.ZProcessNewArgs{}
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res << zinit.ZProcessNewArgs{
// name: 'b2'
// cmd: 'b2 server'
// env: {
// 'HOME': '/root'
// }
// }
return res
}
fn running_() !bool {
mut installer := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of b2
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
// url:='http://127.0.0.1:${cfg.port}/api/v1'
// mut conn := httpconnection.new(name: 'b2', url: url)!
// if cfg.secret.len > 0 {
// conn.default_header.add(.authorization, 'Bearer ${cfg.secret}')
// }
// conn.default_header.add(.content_type, 'application/json')
// console.print_debug("curl -X 'GET' '${url}'/tags --oauth2-bearer ${cfg.secret}")
// r := conn.get_json_dict(prefix: 'tags', debug: false) or {return false}
// println(r)
// if true{panic("ssss")}
// tags := r['Tags'] or { return false }
// console.print_debug(tags)
// console.print_debug('b2 is answering.')
return false
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}
//////////////////// following actions are not specific to instance of the object //////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed // checks if a certain version or above is installed
fn installed_() !bool { fn installed() !bool {
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED res := os.execute('b2 version')
// res := os.execute('${osal.profile_path_source_and()!} b2 version') return res.exit_code == 0
// if res.exit_code != 0 {
// return false
// }
// r := res.output.split_into_lines().filter(it.trim_space().len > 0)
// if r.len != 1 {
// return error("couldn't parse b2 version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
return false
} }
// get the Upload List of the files // get the Upload List of the files
@@ -87,101 +21,20 @@ fn ulist_get() !ulist.UList {
} }
// uploads to S3 server if configured // uploads to S3 server if configured
fn upload_() ! { fn upload() ! {}
// installers.upload(
// cmdname: 'b2'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/b2'
// )!
}
fn install_() ! { fn install() ! {
console.print_header('install b2') console.print_header('install b2')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED mut py := python.new()!
// mut url := '' py.update()!
// if core.is_linux_arm()! { py.pip('b2')!
// url = 'https://github.com/b2-dev/b2/releases/download/v${version}/b2_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel()! {
// url = 'https://github.com/b2-dev/b2/releases/download/v${version}/b2_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm()! {
// url = 'https://github.com/b2-dev/b2/releases/download/v${version}/b2_${version}_darwin_arm64.tar.gz'
// } else if core.is_osx_intel()! {
// url = 'https://github.com/b2-dev/b2/releases/download/v${version}/b2_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download( osal.done_set('install_b2', 'OK')!
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/b2'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('b2')!
// osal.cmd_add(
// cmdname: 'b2'
// source: binpath.path
// )!
} }
fn build_() ! { fn destroy() ! {
// url := 'https://github.com/threefoldtech/b2' console.print_header('uninstall b2')
// mut py := python.new()! // Should be get function, skiping for now
// make sure we install base on the node // py.update()!
// if core.platform()!= .ubuntu { // py.pip_uninstall('b2')!
// return error('only support ubuntu for now')
// }
// golang.install()!
// console.print_header('build b2')
// gitpath := gittools.get_repo(coderoot: '/tmp/builder', url: url, reset: true, pull: true)!
// cmd := '
// cd ${gitpath}
// source ~/.cargo/env
// exit 1 #todo
// '
// osal.execute_stdout(cmd)!
//
// //now copy to the default bin path
// mut binpath := dest.file_get('...')!
// adds it to path
// osal.cmd_add(
// cmdname: 'griddriver2'
// source: binpath.path
// )!
}
fn destroy_() ! {
// mut systemdfactory := systemd.new()!
// systemdfactory.destroy("zinit")!
// osal.process_kill_recursive(name:'zinit')!
// osal.cmd_delete('zinit')!
// osal.package_remove('
// podman
// conmon
// buildah
// skopeo
// runc
// ')!
// //will remove all paths where go/bin is found
// osal.profile_path_add_remove(paths2delete:"go/bin")!
// osal.rm("
// podman
// conmon
// buildah
// skopeo
// runc
// /var/lib/containers
// /var/lib/podman
// /var/lib/buildah
// /tmp/podman
// /tmp/conmon
// ")!
} }

View File

@@ -1,11 +1,9 @@
module b2 module b2
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.sysadmin.startupmanager import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit import freeflowuniverse.herolib.osal.zinit
import time
__global ( __global (
b2_global map[string]&BackBase b2_global map[string]&BackBase
@@ -14,6 +12,46 @@ __global (
/////////FACTORY /////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&BackBase {
return &BackBase{}
}
@[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: 'b2.')!
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 b2.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action b2.install')
install()!
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -40,98 +78,32 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
} }
} }
pub fn (mut self BackBase) start() ! {
switch(self.name)
if self.running()! {
return
}
console.print_header('b2 start')
if !installed_()! {
install_()!
}
configure()!
start_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting b2 with ${zprocess.startuptype}...')
sm.new(zprocess)!
sm.start(zprocess.name)!
}
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('b2 did not install properly.')
}
pub fn (mut self BackBase) install_start(model InstallArgs) ! {
switch(self.name)
self.install(model)!
self.start()!
}
pub fn (mut self BackBase) stop() ! {
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 BackBase) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self BackBase) running() !bool {
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()!
}
@[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 BackBase) 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 BackBase) destroy() ! {
destroy_()! switch(self.name)
destroy()!
} }
pub fn build() ! { // switch instance to be used for b2
build_()! pub fn switch(name string) {
b2_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
} }

View File

@@ -1,24 +0,0 @@
module b2
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.lang.python
// import os
pub fn install(args_ InstallArgs) ! {
mut args := args_
if args.reset == false && osal.done_exists('install_b2') {
return
}
console.print_header('install b2')
mut py := python.new(name: 'default')! // a python env with name test
py.update()!
py.pip('b2')!
osal.done_set('install_b2', 'OK')!
return
}

View File

@@ -1,9 +1,8 @@
module b2 module b2
import freeflowuniverse.herolib.data.paramsparser import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '0.0.0' pub const version = '4.3.0'
const singleton = true const singleton = true
const default = true const default = true
@@ -14,14 +13,24 @@ pub mut:
name string = 'default' name string = 'default'
} }
fn obj_init(obj_ BackBase) !BackBase { // 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_ BackBase) !BackBase {
mut obj := obj_ mut mycfg := mycfg_
panic('implement') return mycfg
return obj
} }
// called before start if done // called before start if done
fn configure() ! { fn configure() ! {
// mut installer := get()! // mut installer := get()!
} }
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj BackBase) !string {
return encoderhero.encode[BackBase](obj)!
}
pub fn heroscript_loads(heroscript string) !BackBase {
mut obj := encoderhero.decode[BackBase](heroscript)!
return obj
}

View File

@@ -94,6 +94,31 @@ pub fn (py PythonEnv) update() ! {
console.print_debug('Pip update complete') console.print_debug('Pip update complete')
} }
// comma separated list of packages to uninstall
pub fn (mut py PythonEnv) pip_uninstall(packages string) ! {
mut to_uninstall := []string{}
for i in packages.split(',') {
pip := i.trim_space()
if !py.pips_done_check(pip)! {
to_uninstall << pip
console.print_debug('Package to uninstall: ${pip}')
}
}
if to_uninstall.len == 0 {
return
}
console.print_debug('uninstalling Python packages: ${packages}')
packages2 := to_uninstall.join(' ')
cmd := '
cd ${py.path.path}
source bin/activate
pip3 uninstall ${packages2} -q
'
osal.exec(cmd: cmd)!
}
// comma separated list of packages to install // comma separated list of packages to install
pub fn (mut py PythonEnv) pip(packages string) ! { pub fn (mut py PythonEnv) pip(packages string) ! {
mut to_install := []string{} mut to_install := []string{}