feat: Add ZeroDB installer

- Add a new ZeroDB installer to the installers.
This commit is contained in:
Mahmoud Emad
2025-02-12 13:44:52 +00:00
parent f6e7644284
commit 147c889b53
10 changed files with 481 additions and 627 deletions

9
examples/installers/zerodb.vsh Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.db.zerodb as zerodb_installer
mut db := zerodb_installer.get()!
db.install()!
db.start()!
db.destroy()!

View File

@@ -1,4 +1,4 @@
module zdb module zerodb_client
import freeflowuniverse.herolib.core.redisclient import freeflowuniverse.herolib.core.redisclient
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
@@ -14,11 +14,12 @@ pub mut:
// /tmp/redis-default.sock // /tmp/redis-default.sock
pub fn get(addr string, auth string, namespace string) !ZDB { pub fn get(addr string, auth string, namespace string) !ZDB {
console.print_header(' ZDB get: addr:${addr} namespace:${namespace}') console.print_header(' ZDB get: addr:${addr} namespace:${namespace}')
mut redis := redisclient.get(addr)! mut redis := redisclient.new(addr)!
mut zdb := ZDB{ mut zdb := ZDB{
redis: redis redis: redis
} }
println('Here..')
if auth != '' { if auth != '' {
zdb.redis.send_expect_ok(['AUTH', auth])! zdb.redis.send_expect_ok(['AUTH', auth])!
} }

View File

@@ -1,7 +1,7 @@
!!hero_code.generate_installer !!hero_code.generate_installer
name: "zerodb" name: "zerodb"
classname: "ZeroDB" classname: "ZeroDB"
hasconfig: false hasconfig: true
singleton: true singleton: true
default: true default: true
title: "" title: ""
@@ -9,4 +9,3 @@
build: true build: true
startupmanager: true startupmanager: true
supported_platforms: "" supported_platforms: ""

View File

@@ -1,29 +0,0 @@
module zdb
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.ui.console
// install zdb will return true if it was already installed
pub fn build_() ! {
base.install()!
console.print_header('package_install install zdb')
if !osal.done_exists('install_zdb') && !osal.cmd_exists('zdb') {
mut gs := gittools.new()!
mut repo := gs.get_repo(
url: 'git@github.com:threefoldtech/0-db.git'
reset: false
pull: true
)!
path := repo.path()
cmd := '
set -ex
cd ${path}
make
sudo rsync -rav ${path}/bin/zdb* /usr/local/bin/
'
osal.execute_silent(cmd) or { return error('Cannot install zdb.\n${err}') }
osal.done_set('install_zdb', 'OK')!
}
}

View File

@@ -1,156 +0,0 @@
module zdb
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.crypt.secrets
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.clients.zdb
import os
import time
@[params]
pub struct InstallArgs {
pub mut:
reset bool
secret string
start bool = true
restart bool
sequential bool // if sequential then we autoincrement the keys
datadir string = '${os.home_dir()}/var/zdb/data'
indexdir string = '${os.home_dir()}/var/zdb/index'
rotateperiod int = 1200 // 20 min
}
pub fn install_(args_ InstallArgs) ! {
mut args := args_
version := '2.0.7'
res := os.execute('${osal.profile_path_source_and()!} zdb --version')
if res.exit_code == 0 {
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
if r.len != 3 {
return error("couldn't parse zdb version.\n${res.output}")
}
myversion := r[1].all_after_first('server, v').all_before_last('(').trim_space()
if texttools.version(version) > texttools.version(myversion) {
args.reset = true
}
} else {
args.reset = true
}
if args.reset {
console.print_header('install zdb')
mut url := ''
if core.is_linux_intel()! {
url = 'https://github.com/threefoldtech/0-db/releases/download/v${version}/zdb-${version}-linux-amd64-static'
} else {
return error('unsported platform, only linux 64 for zdb for now')
}
mut dest := osal.download(
url: url
minsize_kb: 1000
)!
osal.cmd_add(
cmdname: 'zdb'
source: dest.path
)!
}
if args.restart {
restart(args)!
return
}
if args.start {
start(args)!
}
}
pub fn restart(args_ InstallArgs) ! {
stop(args_)!
start(args_)!
}
pub fn stop(args_ InstallArgs) ! {
console.print_header('zdb stop')
mut sm := startupmanager.get()!
sm.stop('zdb')!
}
pub fn start(args_ InstallArgs) ! {
mut args := args_
console.print_header('zdb start')
mut box := secrets.get()!
secret := box.secret(key: 'ZDB.SECRET', default: args.secret)!
mut sm := startupmanager.get()!
mut cmd := 'zdb --socket ${os.home_dir()}/hero/var/zdb.sock --port 3355 --admin ${secret} --data ${args.datadir} --index ${args.indexdir} --dualnet --protect --rotate ${args.rotateperiod}'
if args.sequential {
cmd += ' --mode seq'
}
pathlib.get_dir(path: '${os.home_dir()}/hero/var', create: true)!
sm.start(
name: 'zdb'
cmd: cmd
)!
console.print_debug(cmd)
for _ in 0 .. 50 {
if check()! {
return
}
time.sleep(10 * time.millisecond)
}
return error('zdb not installed properly, check failed.')
}
pub fn check() !bool {
cmd := 'redis-cli -s /root/hero/var/zdb.sock PING'
result := os.execute(cmd)
if result.exit_code > 0 {
return error('${cmd} failed with exit code: ${result.exit_code} and error: ${result.output}')
}
if result.output.trim_space() == 'PONG' {
console.print_debug('zdb is answering.')
// return true
}
// TODO: need to work on socket version
// mut db := zdb.get('${os.home_dir()}/hero/var/zdb.sock', secret()!, 'test')!
mut db := client()!
// check info returns info about zdb
info := db.info()!
// console.print_debug(info)
assert info.contains('server_name: 0-db')
console.print_debug('zdb is answering.')
return true
}
pub fn secret() !string {
mut box := secrets.get()!
secret := box.get('ZDB.SECRET')!
return secret
}
pub fn client() !ZDB {
mut db := zdb.get('localhost:3355', secret()!, 'test')!
return db
}

View File

@@ -1,22 +0,0 @@
module zdb
import freeflowuniverse.herolib.clients.zdb
fn test_get() {
// must set unix domain with --socket argument when running zdb
// run zdb as following:
// mkdir -p ~/.zdb/ && zdb --socket ~/.zdb/socket --admin 1234
install(secret: 'hamada', start: true) or { panic(err) }
mut client := zdb.get('/root/hero/var/zdb.sock', 'hamada', 'test') or { panic(err) }
// check info returns info about zdb
info := client.info()!
assert info.contains('server_name: 0-db')
nslist := client.nslist()!
assert nslist == ['default', 'test']
nsinfo := client.nsinfo('default')!
assert nsinfo['name'] == 'default'
}

View File

@@ -2,51 +2,58 @@ module zerodb
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
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.osal.zinit import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.installers.lang.rust import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.installers.lang.python import freeflowuniverse.herolib.crypt.secrets
import freeflowuniverse.herolib.clients.zerodb_client
import crypto.md5
import rand
import os import os
import time
fn startupcmd() ![]zinit.ZProcessNewArgs { fn startupcmd() ![]zinit.ZProcessNewArgs {
mut installer := get()! mut cfg := get()!
mut res := []zinit.ZProcessNewArgs{} mut cmd := 'zdb --socket ${os.home_dir()}/var/zdb.sock --port ${cfg.port} --admin ${cfg.secret} --data ${cfg.datadir} --index ${cfg.indexdir} --dualnet --protect --rotate ${cfg.rotateperiod}'
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED if cfg.sequential {
// res << zinit.ZProcessNewArgs{ cmd += ' --mode seq'
// name: 'zerodb' }
// cmd: 'zerodb server'
// env: {
// 'HOME': '/root'
// }
// }
mut res := []zinit.ZProcessNewArgs{}
res << zinit.ZProcessNewArgs{
name: 'zdb'
cmd: cmd
startuptype: .zinit
}
return res return res
} }
fn running_() !bool { fn running() !bool {
mut installer := get()! time.sleep(time.second * 2)
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED cfg := get()!
// this checks health of zerodb cmd := 'redis-cli -s ${os.home_dir()}/var/zdb.sock PING'
// 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: 'zerodb', url: url)!
// if cfg.secret.len > 0 { result := os.execute(cmd)
// conn.default_header.add(.authorization, 'Bearer ${cfg.secret}') if result.exit_code > 0 {
// } return error('${cmd} failed with exit code: ${result.exit_code} and error: ${result.output}')
// 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} if result.output.trim_space() == 'PONG' {
// println(r) console.print_debug('zdb is answering.')
// if true{panic("ssss")} return true
// tags := r['Tags'] or { return false } }
// console.print_debug(tags)
// console.print_debug('zerodb is answering.') mut db := zerodb_client.get('localhost:${cfg.port}', cfg.secret, 'test')!
return false
// check info returns info about zdb
info := db.info()!
assert info.contains('server_name: 0-db')
console.print_debug('zdb is answering.')
return true
} }
fn start_pre() ! { fn start_pre() ! {
@@ -64,20 +71,9 @@ 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('zdb --version')
// res := os.execute('${osal.profile_path_source_and()!} zerodb 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 zerodb 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 +83,68 @@ fn ulist_get() !ulist.UList {
} }
// uploads to S3 server if configured // uploads to S3 server if configured
fn upload_() ! { fn upload() ! {
// installers.upload( // installers.upload(
// cmdname: 'zerodb' // cmdname: 'zerodb'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/zerodb' // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/zerodb'
// )! // )!
} }
fn install_() ! { fn install() ! {
console.print_header('install zerodb') console.print_header('install zdb')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm()! {
// url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel()! {
// url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm()! {
// url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_darwin_arm64.tar.gz'
// } else if core.is_osx_intel()! {
// url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download( mut url := ''
// url: url if core.is_linux_intel()! {
// minsize_kb: 9000 url = 'https://github.com/threefoldtech/0-db/releases/download/v${version}/zdb-${version}-linux-amd64-static'
// expand_dir: '/tmp/zerodb' } else {
// )! return error('unsported platform, only linux 64 for zdb for now')
}
// //dest.moveup_single_subdir()! mut dest := osal.download(
url: url
minsize_kb: 1000
)!
// mut binpath := dest.file_get('zerodb')! osal.cmd_add(
// osal.cmd_add( cmdname: 'zdb'
// cmdname: 'zerodb' source: dest.path
// source: binpath.path )!
// )!
} }
fn build_() ! { fn build() ! {
// url := 'https://github.com/threefoldtech/zerodb' base.install()!
console.print_header('package_install install zdb')
// make sure we install base on the node if !osal.done_exists('install_zdb') && !osal.cmd_exists('zdb') {
// if core.platform()!= .ubuntu { mut gs := gittools.new()!
// return error('only support ubuntu for now') mut repo := gs.get_repo(
// } url: 'git@github.com:threefoldtech/0-db.git'
// golang.install()! reset: false
pull: true
// console.print_header('build zerodb') )!
path := repo.path()
// gitpath := gittools.get_repo(coderoot: '/tmp/builder', url: url, reset: true, pull: true)! cmd := '
set -ex
// cmd := ' cd ${path}
// cd ${gitpath} make
// source ~/.cargo/env sudo rsync -rav ${path}/bin/zdb* /usr/local/bin/
// exit 1 #todo '
// ' osal.execute_silent(cmd) or { return error('Cannot install zdb.\n${err}') }
// osal.execute_stdout(cmd)! osal.done_set('install_zdb', 'OK')!
// }
// //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_() ! { fn destroy() ! {
// mut systemdfactory := systemd.new()! res := os.execute('sudo rm -rf /usr/local/bin/zdb')
// systemdfactory.destroy("zinit")! if res.exit_code != 0 {
return error('Could not remove zdb binary due to: ${res.output}')
}
// osal.process_kill_recursive(name:'zinit')! mut zinit_factory := zinit.new()!
// osal.cmd_delete('zinit')! if zinit_factory.exists('zdb') {
zinit_factory.stop('zdb') or { return error('Could not stop zdb service due to: ${err}') }
// osal.package_remove(' zinit_factory.delete('zdb') or {
// podman return error('Could not delete zdb service due to: ${err}')
// conmon }
// buildah }
// skopeo console.print_header('zdb removed')
// 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

@@ -3,219 +3,277 @@ module zerodb
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 (
zerodb_global map[string]&ZeroDB zerodb_global map[string]&ZeroDB
zerodb_default string zerodb_default string
) )
/////////FACTORY /////////FACTORY
@[params] @[params]
pub struct ArgsGet{ pub struct ArgsGet {
pub mut: pub mut:
name string name string
} }
pub fn get(args_ ArgsGet) !&ZeroDB { fn args_get(args_ ArgsGet) ArgsGet {
return &ZeroDB{} mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&ZeroDB {
mut context := base.context()!
mut args := args_get(args_)
mut obj := ZeroDB{}
if args.name !in zerodb_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('zerodb', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return zerodb_global[args.name] or {
println(zerodb_global)
// bug if we get here because should be in globals
panic('could not get config for zerodb with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o ZeroDB) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('zerodb', o.name, heroscript)!
}
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('zerodb', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('zerodb', args.name)!
if args.name in zerodb_global {
// del zerodb_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o ZeroDB) ! {
mut o2 := obj_init(o)!
zerodb_global[o.name] = &o2
zerodb_default = o.name
} }
@[params] @[params]
pub struct PlayArgs { pub struct PlayArgs {
pub mut: pub mut:
heroscript string //if filled in then plbook will be made out of it heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook plbook ?playbook.PlayBook
reset bool reset bool
} }
pub fn play(args_ PlayArgs) ! { pub fn play(args_ PlayArgs) ! {
mut args := args_
mut args:=args_
mut plbook := args.plbook or { mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
playbook.new(text: args.heroscript)!
}
mut other_actions := plbook.find(filter: 'zerodb.')! mut install_actions := plbook.find(filter: 'zerodb.configure')!
for other_action in other_actions { if install_actions.len > 0 {
if other_action.name in ["destroy","install","build"]{ for install_action in install_actions {
mut p := other_action.params heroscript := install_action.heroscript()
reset:=p.get_default_false("reset") mut obj2 := heroscript_loads(heroscript)!
if other_action.name == "destroy" || reset{ set(obj2)!
console.print_debug("install action zerodb.destroy") }
destroy()! }
}
if other_action.name == "install"{
console.print_debug("install action zerodb.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut zerodb_obj:=get(name:name)!
console.print_debug("action object:\n${zerodb_obj}")
if other_action.name == "start"{
console.print_debug("install action zerodb.${other_action.name}")
zerodb_obj.start()!
}
if other_action.name == "stop"{ mut other_actions := plbook.find(filter: 'zerodb.')!
console.print_debug("install action zerodb.${other_action.name}") for other_action in other_actions {
zerodb_obj.stop()! if other_action.name in ['destroy', 'install', 'build'] {
} mut p := other_action.params
if other_action.name == "restart"{ reset := p.get_default_false('reset')
console.print_debug("install action zerodb.${other_action.name}") if other_action.name == 'destroy' || reset {
zerodb_obj.restart()! console.print_debug('install action zerodb.destroy')
} destroy()!
} }
} if other_action.name == 'install' {
console.print_debug('install action zerodb.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut zerodb_obj := get(name: name)!
console.print_debug('action object:\n${zerodb_obj}')
if other_action.name == 'start' {
console.print_debug('install action zerodb.${other_action.name}')
zerodb_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action zerodb.${other_action.name}')
zerodb_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action zerodb.${other_action.name}')
zerodb_obj.restart()!
}
}
}
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown // unknown
// screen // screen
// zinit // zinit
// tmux // tmux
// systemd // systemd
match cat{ match cat {
.zinit{ .zinit {
console.print_debug("startupmanager: zinit") console.print_debug('startupmanager: zinit')
return startupmanager.get(cat:.zinit)! return startupmanager.get(cat: .zinit)!
} }
.systemd{ .systemd {
console.print_debug("startupmanager: systemd") console.print_debug('startupmanager: systemd')
return startupmanager.get(cat:.systemd)! return startupmanager.get(cat: .systemd)!
}else{ }
console.print_debug("startupmanager: auto") else {
return startupmanager.get()! console.print_debug('startupmanager: auto')
} return startupmanager.get()!
} }
}
} }
// load from disk and make sure is properly intialized
pub fn (mut self ZeroDB) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self ZeroDB) start() ! { pub fn (mut self ZeroDB) start() ! {
switch(self.name) switch(self.name)
if self.running()!{ if self.running()! {
return return
} }
console.print_header('zerodb start') console.print_header('zerodb start')
if ! installed()!{ if !installed()! {
install()! install()!
} }
configure()! configure()!
start_pre()! start_pre()!
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting zerodb with ${zprocess.startuptype}...') console.print_debug('starting zerodb with ${zprocess.startuptype}...')
sm.new(zprocess)! sm.new(zprocess)!
sm.start(zprocess.name)! sm.start(zprocess.name)!
} }
start_post()! start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('zerodb did not install properly.')
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('zerodb did not install properly.')
} }
pub fn (mut self ZeroDB) install_start(args InstallArgs) ! { pub fn (mut self ZeroDB) install_start(args InstallArgs) ! {
switch(self.name) switch(self.name)
self.install(args)! self.install(args)!
self.start()! self.start()!
} }
pub fn (mut self ZeroDB) stop() ! { pub fn (mut self ZeroDB) stop() ! {
switch(self.name) switch(self.name)
stop_pre()! stop_pre()!
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)! sm.stop(zprocess.name)!
} }
stop_post()! stop_post()!
} }
pub fn (mut self ZeroDB) restart() ! { pub fn (mut self ZeroDB) restart() ! {
switch(self.name) switch(self.name)
self.stop()! self.stop()!
self.start()! self.start()!
} }
pub fn (mut self ZeroDB) running() !bool { pub fn (mut self ZeroDB) running() !bool {
switch(self.name) switch(self.name)
//walk over the generic processes, if not running return // walk over the generic processes, if not running return
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
r:=sm.running(zprocess.name)! r := sm.running(zprocess.name)!
if r==false{ if r == false {
return false return false
} }
} }
return running()! return running()!
} }
@[params] @[params]
pub struct InstallArgs{ pub struct InstallArgs {
pub mut: pub mut:
reset bool reset bool
} }
pub fn (mut self ZeroDB) install(args InstallArgs) ! { pub fn (mut self ZeroDB) install(args InstallArgs) ! {
switch(self.name) switch(self.name)
if args.reset || (!installed()!) { if args.reset || (!installed()!) {
install()! install()!
} }
} }
pub fn (mut self ZeroDB) build() ! { pub fn (mut self ZeroDB) build() ! {
switch(self.name) switch(self.name)
build()! build()!
} }
pub fn (mut self ZeroDB) destroy() ! { pub fn (mut self ZeroDB) destroy() ! {
switch(self.name) switch(self.name)
self.stop() or {} self.stop() or {}
destroy()! destroy()!
} }
// switch instance to be used for zerodb
//switch instance to be used for zerodb
pub fn switch(name string) { pub fn switch(name string) {
zerodb_default = name zerodb_default = name
} }
// helpers
//helpers
@[params] @[params]
pub struct DefaultConfigArgs{ pub struct DefaultConfigArgs {
instance string = 'default' instance string = 'default'
} }

View File

@@ -1,27 +1,71 @@
module zerodb module zerodb
import freeflowuniverse.herolib.data.paramsparser import freeflowuniverse.herolib.data.encoderhero
import os import os
import rand
import crypto.md5
import freeflowuniverse.herolib.crypt.secrets
pub const version = '0.0.0' pub const version = '2.0.7'
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] @[heap]
pub struct ZeroDB { pub struct ZeroDB {
pub mut: pub mut:
name string = 'default' name string = 'default'
secret string @[secret]
sequential bool // if sequential then we autoincrement the keys
datadir string = '${os.home_dir()}/var/zdb/data'
indexdir string = '${os.home_dir()}/var/zdb/index'
rotateperiod int = 1200 // 20 min
port int = 3355
} }
fn obj_init(obj_ ZeroDB) !ZeroDB { // 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_ ZeroDB) !ZeroDB {
mut obj := obj_ mut mycfg := mycfg_
panic('implement') if mycfg.name == '' {
return obj mycfg.name = 'default'
}
if mycfg.secret == '' {
secret := md5.hexhash(rand.string(16))
mut box := secrets.get(secret: secret)!
mycfg.secret = box.encrypt(secret)!
}
if mycfg.datadir == '' {
mycfg.datadir = '${os.home_dir()}/var/zdb/data'
}
if mycfg.indexdir == '' {
mycfg.indexdir = '${os.home_dir()}/var/zdb/index'
}
if mycfg.rotateperiod == 0 {
mycfg.rotateperiod = 1200
}
if mycfg.port == 0 {
mycfg.port = 3355
}
return mycfg
} }
// 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 ZeroDB) !string {
return encoderhero.encode[ZeroDB](obj)!
}
pub fn heroscript_loads(heroscript string) !ZeroDB {
mut obj := encoderhero.decode[ZeroDB](heroscript)!
return obj
}

View File

@@ -1,221 +1,208 @@
module zerofs module zerofs
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 (
zerofs_global map[string]&ZeroFS zerofs_global map[string]&ZeroFS
zerofs_default string zerofs_default string
) )
/////////FACTORY /////////FACTORY
@[params] @[params]
pub struct ArgsGet{ pub struct ArgsGet {
pub mut: pub mut:
name string name string
} }
pub fn get(args_ ArgsGet) !&ZeroFS { pub fn get(args_ ArgsGet) !&ZeroFS {
return &ZeroFS{} return &ZeroFS{}
} }
@[params] @[params]
pub struct PlayArgs { pub struct PlayArgs {
pub mut: pub mut:
heroscript string //if filled in then plbook will be made out of it heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook plbook ?playbook.PlayBook
reset bool reset bool
} }
pub fn play(args_ PlayArgs) ! { pub fn play(args_ PlayArgs) ! {
mut args := args_
mut args:=args_
mut plbook := args.plbook or { mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
playbook.new(text: args.heroscript)!
}
mut other_actions := plbook.find(filter: 'zerofs.')! mut other_actions := plbook.find(filter: 'zerofs.')!
for other_action in other_actions { for other_action in other_actions {
if other_action.name in ["destroy","install","build"]{ if other_action.name in ['destroy', 'install', 'build'] {
mut p := other_action.params mut p := other_action.params
reset:=p.get_default_false("reset") reset := p.get_default_false('reset')
if other_action.name == "destroy" || reset{ if other_action.name == 'destroy' || reset {
console.print_debug("install action zerofs.destroy") console.print_debug('install action zerofs.destroy')
destroy()! destroy()!
} }
if other_action.name == "install"{ if other_action.name == 'install' {
console.print_debug("install action zerofs.install") console.print_debug('install action zerofs.install')
install()! install()!
} }
} }
if other_action.name in ["start","stop","restart"]{ if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params mut p := other_action.params
name := p.get('name')! name := p.get('name')!
mut zerofs_obj:=get(name:name)! mut zerofs_obj := get(name: name)!
console.print_debug("action object:\n${zerofs_obj}") console.print_debug('action object:\n${zerofs_obj}')
if other_action.name == "start"{ if other_action.name == 'start' {
console.print_debug("install action zerofs.${other_action.name}") console.print_debug('install action zerofs.${other_action.name}')
zerofs_obj.start()! zerofs_obj.start()!
} }
if other_action.name == "stop"{
console.print_debug("install action zerofs.${other_action.name}")
zerofs_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action zerofs.${other_action.name}")
zerofs_obj.restart()!
}
}
}
if other_action.name == 'stop' {
console.print_debug('install action zerofs.${other_action.name}')
zerofs_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action zerofs.${other_action.name}')
zerofs_obj.restart()!
}
}
}
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown // unknown
// screen // screen
// zinit // zinit
// tmux // tmux
// systemd // systemd
match cat{ match cat {
.zinit{ .zinit {
console.print_debug("startupmanager: zinit") console.print_debug('startupmanager: zinit')
return startupmanager.get(cat:.zinit)! return startupmanager.get(cat: .zinit)!
} }
.systemd{ .systemd {
console.print_debug("startupmanager: systemd") console.print_debug('startupmanager: systemd')
return startupmanager.get(cat:.systemd)! return startupmanager.get(cat: .systemd)!
}else{ }
console.print_debug("startupmanager: auto") else {
return startupmanager.get()! console.print_debug('startupmanager: auto')
} return startupmanager.get()!
} }
}
} }
pub fn (mut self ZeroFS) start() ! { pub fn (mut self ZeroFS) start() ! {
switch(self.name) switch(self.name)
if self.running()!{ if self.running()! {
return return
} }
console.print_header('zerofs start') console.print_header('zerofs start')
if ! installed()!{ if !installed()! {
install()! install()!
} }
configure()! configure()!
start_pre()! start_pre()!
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting zerofs with ${zprocess.startuptype}...') console.print_debug('starting zerofs with ${zprocess.startuptype}...')
sm.new(zprocess)! sm.new(zprocess)!
sm.start(zprocess.name)! sm.start(zprocess.name)!
} }
start_post()! start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('zerofs did not install properly.')
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('zerofs did not install properly.')
} }
pub fn (mut self ZeroFS) install_start(args InstallArgs) ! { pub fn (mut self ZeroFS) install_start(args InstallArgs) ! {
switch(self.name) switch(self.name)
self.install(args)! self.install(args)!
self.start()! self.start()!
} }
pub fn (mut self ZeroFS) stop() ! { pub fn (mut self ZeroFS) stop() ! {
switch(self.name) switch(self.name)
stop_pre()! stop_pre()!
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)! sm.stop(zprocess.name)!
} }
stop_post()! stop_post()!
} }
pub fn (mut self ZeroFS) restart() ! { pub fn (mut self ZeroFS) restart() ! {
switch(self.name) switch(self.name)
self.stop()! self.stop()!
self.start()! self.start()!
} }
pub fn (mut self ZeroFS) running() !bool { pub fn (mut self ZeroFS) running() !bool {
switch(self.name) switch(self.name)
//walk over the generic processes, if not running return // walk over the generic processes, if not running return
for zprocess in startupcmd()!{ for zprocess in startupcmd()! {
mut sm:=startupmanager_get(zprocess.startuptype)! mut sm := startupmanager_get(zprocess.startuptype)!
r:=sm.running(zprocess.name)! r := sm.running(zprocess.name)!
if r==false{ if r == false {
return false return false
} }
} }
return running()! return running()!
} }
@[params] @[params]
pub struct InstallArgs{ pub struct InstallArgs {
pub mut: pub mut:
reset bool reset bool
} }
pub fn (mut self ZeroFS) install(args InstallArgs) ! { pub fn (mut self ZeroFS) install(args InstallArgs) ! {
switch(self.name) switch(self.name)
if args.reset || (!installed()!) { if args.reset || (!installed()!) {
install()! install()!
} }
} }
pub fn (mut self ZeroFS) build() ! { pub fn (mut self ZeroFS) build() ! {
switch(self.name) switch(self.name)
build()! build()!
} }
pub fn (mut self ZeroFS) destroy() ! { pub fn (mut self ZeroFS) destroy() ! {
switch(self.name) switch(self.name)
self.stop() or {} self.stop() or {}
destroy()! destroy()!
} }
// switch instance to be used for zerofs
//switch instance to be used for zerofs
pub fn switch(name string) { pub fn switch(name string) {
zerofs_default = name zerofs_default = name
} }
// helpers
//helpers
@[params] @[params]
pub struct DefaultConfigArgs{ pub struct DefaultConfigArgs {
instance string = 'default' instance string = 'default'
} }