diff --git a/examples/installers/zerodb.vsh b/examples/installers/zerodb.vsh new file mode 100755 index 00000000..924f791a --- /dev/null +++ b/examples/installers/zerodb.vsh @@ -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()! diff --git a/lib/clients/zerodb_client/zdb.v b/lib/clients/zerodb_client/zdb.v index d2c7bbef..e0b5ebd8 100644 --- a/lib/clients/zerodb_client/zdb.v +++ b/lib/clients/zerodb_client/zdb.v @@ -1,4 +1,4 @@ -module zdb +module zerodb_client import freeflowuniverse.herolib.core.redisclient import freeflowuniverse.herolib.ui.console @@ -14,11 +14,12 @@ pub mut: // /tmp/redis-default.sock pub fn get(addr string, auth string, namespace string) !ZDB { console.print_header(' ZDB get: addr:${addr} namespace:${namespace}') - mut redis := redisclient.get(addr)! + mut redis := redisclient.new(addr)! mut zdb := ZDB{ redis: redis } + println('Here..') if auth != '' { zdb.redis.send_expect_ok(['AUTH', auth])! } diff --git a/lib/installers/db/zerodb/.heroscript b/lib/installers/db/zerodb/.heroscript index 63f693bc..e36bb19f 100644 --- a/lib/installers/db/zerodb/.heroscript +++ b/lib/installers/db/zerodb/.heroscript @@ -1,7 +1,7 @@ !!hero_code.generate_installer name: "zerodb" classname: "ZeroDB" - hasconfig: false + hasconfig: true singleton: true default: true title: "" @@ -9,4 +9,3 @@ build: true startupmanager: true supported_platforms: "" - diff --git a/lib/installers/db/zerodb/zdb_builder.v b/lib/installers/db/zerodb/zdb_builder.v deleted file mode 100644 index 601a0ba7..00000000 --- a/lib/installers/db/zerodb/zdb_builder.v +++ /dev/null @@ -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')! - } -} diff --git a/lib/installers/db/zerodb/zdb_installer.v b/lib/installers/db/zerodb/zdb_installer.v deleted file mode 100644 index 6ecfae89..00000000 --- a/lib/installers/db/zerodb/zdb_installer.v +++ /dev/null @@ -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 -} diff --git a/lib/installers/db/zerodb/zdb_test.v b/lib/installers/db/zerodb/zdb_test.v deleted file mode 100644 index 2286d7c4..00000000 --- a/lib/installers/db/zerodb/zdb_test.v +++ /dev/null @@ -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' -} diff --git a/lib/installers/db/zerodb/zerodb_actions.v b/lib/installers/db/zerodb/zerodb_actions.v index a7db9c95..05f86657 100644 --- a/lib/installers/db/zerodb/zerodb_actions.v +++ b/lib/installers/db/zerodb/zerodb_actions.v @@ -2,51 +2,58 @@ module zerodb import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.core.texttools -import freeflowuniverse.herolib.core.pathlib -import freeflowuniverse.herolib.osal.systemd +import freeflowuniverse.herolib.core import freeflowuniverse.herolib.osal.zinit import freeflowuniverse.herolib.installers.ulist -import freeflowuniverse.herolib.installers.lang.golang -import freeflowuniverse.herolib.installers.lang.rust -import freeflowuniverse.herolib.installers.lang.python +import freeflowuniverse.herolib.develop.gittools +import freeflowuniverse.herolib.installers.base +import freeflowuniverse.herolib.crypt.secrets +import freeflowuniverse.herolib.clients.zerodb_client +import crypto.md5 +import rand import os +import time fn startupcmd() ![]zinit.ZProcessNewArgs { - mut installer := get()! - mut res := []zinit.ZProcessNewArgs{} - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // res << zinit.ZProcessNewArgs{ - // name: 'zerodb' - // cmd: 'zerodb server' - // env: { - // 'HOME': '/root' - // } - // } + mut cfg := get()! + 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}' + if cfg.sequential { + cmd += ' --mode seq' + } + mut res := []zinit.ZProcessNewArgs{} + res << zinit.ZProcessNewArgs{ + name: 'zdb' + cmd: cmd + startuptype: .zinit + } return res } -fn running_() !bool { - mut installer := get()! - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // this checks health of zerodb - // 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)! +fn running() !bool { + time.sleep(time.second * 2) + cfg := get()! + cmd := 'redis-cli -s ${os.home_dir()}/var/zdb.sock PING' - // 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('zerodb is answering.') - return false + 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 + } + + mut db := zerodb_client.get('localhost:${cfg.port}', cfg.secret, 'test')! + + // 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() ! { @@ -64,20 +71,9 @@ fn stop_post() ! { //////////////////// following actions are not specific to instance of the object // checks if a certain version or above is installed -fn installed_() !bool { - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // res := os.execute('${osal.profile_path_source_and()!} zerodb version') - // 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 +fn installed() !bool { + res := os.execute('zdb --version') + return res.exit_code == 0 } // get the Upload List of the files @@ -87,101 +83,68 @@ fn ulist_get() !ulist.UList { } // uploads to S3 server if configured -fn upload_() ! { +fn upload() ! { // installers.upload( // cmdname: 'zerodb' // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/zerodb' // )! } -fn install_() ! { - console.print_header('install zerodb') - // 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') - // } +fn install() ! { + console.print_header('install zdb') - // mut dest := osal.download( - // url: url - // minsize_kb: 9000 - // expand_dir: '/tmp/zerodb' - // )! + 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') + } - // //dest.moveup_single_subdir()! + mut dest := osal.download( + url: url + minsize_kb: 1000 + )! - // mut binpath := dest.file_get('zerodb')! - // osal.cmd_add( - // cmdname: 'zerodb' - // source: binpath.path - // )! + osal.cmd_add( + cmdname: 'zdb' + source: dest.path + )! } -fn build_() ! { - // url := 'https://github.com/threefoldtech/zerodb' - - // make sure we install base on the node - // if core.platform()!= .ubuntu { - // return error('only support ubuntu for now') - // } - // golang.install()! - - // console.print_header('build zerodb') - - // 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 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')! + } } -fn destroy_() ! { - // mut systemdfactory := systemd.new()! - // systemdfactory.destroy("zinit")! +fn destroy() ! { + res := os.execute('sudo rm -rf /usr/local/bin/zdb') + if res.exit_code != 0 { + return error('Could not remove zdb binary due to: ${res.output}') + } - // 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 - // ")! + mut zinit_factory := zinit.new()! + if zinit_factory.exists('zdb') { + zinit_factory.stop('zdb') or { return error('Could not stop zdb service due to: ${err}') } + zinit_factory.delete('zdb') or { + return error('Could not delete zdb service due to: ${err}') + } + } + console.print_header('zdb removed') } diff --git a/lib/installers/db/zerodb/zerodb_factory_.v b/lib/installers/db/zerodb/zerodb_factory_.v index f2dde44e..0efcc962 100644 --- a/lib/installers/db/zerodb/zerodb_factory_.v +++ b/lib/installers/db/zerodb/zerodb_factory_.v @@ -3,219 +3,277 @@ module zerodb 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 ( - zerodb_global map[string]&ZeroDB - zerodb_default string + zerodb_global map[string]&ZeroDB + zerodb_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&ZeroDB { - return &ZeroDB{} +fn args_get(args_ ArgsGet) ArgsGet { + 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] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + 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 args := args_ - mut plbook := args.plbook or { - playbook.new(text: args.heroscript)! - } - + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut other_actions := plbook.find(filter: 'zerodb.')! - 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 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()! - } + mut install_actions := plbook.find(filter: 'zerodb.configure')! + if install_actions.len > 0 { + for install_action in install_actions { + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! + } + } - 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()! - } - } - } + mut other_actions := plbook.find(filter: 'zerodb.')! + 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 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 /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// 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()! - } - } + // 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()! + } + } } +// 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() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('zerodb start') + console.print_header('zerodb start') - if ! installed()!{ - install()! - } + if !installed()! { + install()! + } - configure()! + configure()! - start_pre()! + start_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! + for zprocess in startupcmd()! { + 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()! - - for _ in 0 .. 50 { - if self.running()! { - return - } - time.sleep(100 * time.millisecond) - } - return error('zerodb did not install properly.') + start_post()! + 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) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self ZeroDB) stop() ! { - switch(self.name) - stop_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - sm.stop(zprocess.name)! - } - stop_post()! + 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 ZeroDB) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self ZeroDB) running() !bool { - switch(self.name) + 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()! + // 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] -pub struct InstallArgs{ +pub struct InstallArgs { pub mut: - reset bool + reset bool } pub fn (mut self ZeroDB) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self ZeroDB) build() ! { - switch(self.name) - build()! + switch(self.name) + build()! } pub fn (mut self ZeroDB) destroy() ! { - switch(self.name) - self.stop() or {} - destroy()! + switch(self.name) + self.stop() or {} + destroy()! } - - -//switch instance to be used for zerodb +// switch instance to be used for zerodb pub fn switch(name string) { - zerodb_default = name + zerodb_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/db/zerodb/zerodb_model.v b/lib/installers/db/zerodb/zerodb_model.v index 8c105b44..4ef18790 100644 --- a/lib/installers/db/zerodb/zerodb_model.v +++ b/lib/installers/db/zerodb/zerodb_model.v @@ -1,27 +1,71 @@ module zerodb -import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.encoderhero 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 default = true -// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED @[heap] pub struct ZeroDB { 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 { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ - panic('implement') - return obj +// your checking & initialization code if needed +fn obj_init(mycfg_ ZeroDB) !ZeroDB { + mut mycfg := mycfg_ + if mycfg.name == '' { + 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 fn configure() ! { // 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 +} diff --git a/lib/installers/db/zerofs/zerofs_factory_.v b/lib/installers/db/zerofs/zerofs_factory_.v index c60f1fb1..5dbd9ee2 100644 --- a/lib/installers/db/zerofs/zerofs_factory_.v +++ b/lib/installers/db/zerofs/zerofs_factory_.v @@ -1,221 +1,208 @@ module zerofs -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 ( - zerofs_global map[string]&ZeroFS - zerofs_default string + zerofs_global map[string]&ZeroFS + zerofs_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&ZeroFS { - return &ZeroFS{} +pub fn get(args_ ArgsGet) !&ZeroFS { + return &ZeroFS{} } @[params] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + 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 args := args_ - mut plbook := args.plbook or { - playbook.new(text: args.heroscript)! - } - + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut other_actions := plbook.find(filter: 'zerofs.')! - 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 zerofs.destroy") - destroy()! - } - if other_action.name == "install"{ - console.print_debug("install action zerofs.install") - install()! - } - } - if other_action.name in ["start","stop","restart"]{ - mut p := other_action.params - name := p.get('name')! - mut zerofs_obj:=get(name:name)! - console.print_debug("action object:\n${zerofs_obj}") - if other_action.name == "start"{ - console.print_debug("install action zerofs.${other_action.name}") - 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()! - } - } - } + mut other_actions := plbook.find(filter: 'zerofs.')! + 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 zerofs.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action zerofs.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut zerofs_obj := get(name: name)! + console.print_debug('action object:\n${zerofs_obj}') + if other_action.name == 'start' { + console.print_debug('install action zerofs.${other_action.name}') + 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()! + } + } + } } - //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# 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()! - } - } + // 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()! + } + } } - pub fn (mut self ZeroFS) start() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('zerofs start') + console.print_header('zerofs start') - if ! installed()!{ - install()! - } + if !installed()! { + install()! + } - configure()! + configure()! - start_pre()! + start_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! + for zprocess in startupcmd()! { + 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()! - - for _ in 0 .. 50 { - if self.running()! { - return - } - time.sleep(100 * time.millisecond) - } - return error('zerofs did not install properly.') + start_post()! + 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) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self ZeroFS) stop() ! { - switch(self.name) - stop_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - sm.stop(zprocess.name)! - } - stop_post()! + 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 ZeroFS) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self ZeroFS) running() !bool { - switch(self.name) + 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()! + // 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] -pub struct InstallArgs{ +pub struct InstallArgs { pub mut: - reset bool + reset bool } pub fn (mut self ZeroFS) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self ZeroFS) build() ! { - switch(self.name) - build()! + switch(self.name) + build()! } pub fn (mut self ZeroFS) destroy() ! { - switch(self.name) - self.stop() or {} - destroy()! + switch(self.name) + self.stop() or {} + destroy()! } - - -//switch instance to be used for zerofs +// switch instance to be used for zerofs pub fn switch(name string) { - zerofs_default = name + zerofs_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' }