Merge branch 'development_installers' of github.com:freeflowuniverse/herolib into development_installers

This commit is contained in:
2025-02-19 05:10:45 +03:00
138 changed files with 5044 additions and 5721 deletions

View File

@@ -1,15 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.sysadmintools.daguserver
import freeflowuniverse.herolib.installers.infra.zinit
// make sure zinit is there and running, will restart it if needed
mut z := zinit.get()!
z.destroy()!
z.start()!
// mut ds := daguserver.get()!
// ds.destroy()!
// ds.start()!
// println(ds)

View File

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

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.postgresql as postgresql_installer
mut db := postgresql_installer.get()!
db.install()!
db.start()!
db.destroy()!

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,16 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.infra.gitea as gitea_installer
mut installer := gitea_installer.get(name: 'test')!
// if you want to configure using heroscript
gitea_installer.play(
heroscript: "
!!gitea.configure name:test
passwd:'something'
domain: 'docs.info.com'
"
)!
installer.start()!

View File

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

View File

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

View File

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

View File

@@ -3,5 +3,6 @@
import freeflowuniverse.herolib.installers.infra.zinit_installer
mut installer := zinit_installer.get()!
// installer.install()!
installer.install()!
installer.start()!
// installer.destroy()!

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.lang.rust as rust_module
mut rust_installer := rust_module.get()!
// rust_installer.install()!
rust_installer.destroy()!

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.db.meilisearchinstaller
meilisearch := meilisearchinstaller.get()!

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.net.mycelium as mycelium_installer
import freeflowuniverse.herolib.installers.net.mycelium_installer
import freeflowuniverse.herolib.clients.mycelium
mut installer := mycelium_installer.get()!
@@ -13,25 +13,27 @@ mut client := mycelium.get()!
// Send a message to a node by public key
// Parameters: public_key, payload, topic, wait_for_reply
msg := client.send_msg('abc123...', // destination public key
'Hello World', // message payload
'greetings', // optional topic
true // wait for reply
)!
msg := client.send_msg(
public_key: 'abc123...' // destination public key
payload: 'Hello World' // message payload
topic: 'greetings' // optional topic
wait: true // wait for reply
)!
println('Sent message ID: ${msg.id}')
// Receive messages
// Parameters: wait_for_message, peek_only, topic_filter
received := client.receive_msg(true, false, 'greetings')!
received := client.receive_msg(wait: true, peek: false, topic: 'greetings')!
println('Received message from: ${received.src_pk}')
println('Message payload: ${received.payload}')
// Reply to a message
client.reply_msg(received.id, // original message ID
received.src_pk, // sender's public key
'Got your message!', // reply payload
'greetings' // topic
)!
client.reply_msg(
id: received.id // original message ID
public_key: received.src_pk // sender's public key
payload: 'Got your message!' // reply payload
topic: 'greetings' // topic
)!
// Check message status
status := client.get_msg_status(msg.id)!

View File

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

View File

@@ -1,13 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import time
import freeflowuniverse.herolib.installers.db.postgresql
mut db := postgresql.get()!
// db.destroy()!
db.start()!
// db.db_create('my_new_db')!
// db.stop()!
// db.start()!

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.sysadmintools.actrunner
import freeflowuniverse.herolib.installers.virt.herocontainers
// import freeflowuniverse.herolib.installers.virt.herocontainers
actrunner.install()!
mut actrunner_ := actrunner.get()!
actrunner_.install()!
// herocontainers.start()!

View File

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

View File

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

View File

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

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.sysadmintools.daguserver
import freeflowuniverse.herolib.installers.infra.zinit_installer
mut ds := daguserver.get()!
ds.install()!
ds.start()!
ds.destroy()!

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.virt.pacman as pacman_installer
mut pacman := pacman_installer.get()!
// To install
pacman.install()!
// To remove
pacman.destroy()!

View File

@@ -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])!
}

View File

@@ -2,6 +2,7 @@ module generic
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.osal
fn generate_exec(path string, reset bool) ! {
mut args := args_get(path)!
@@ -43,12 +44,15 @@ fn generate_exec(path string, reset bool) ! {
if args.reset {
path_templ_dir.delete()!
}
if args.templates {
if !path_templ_dir.exists() {
mut templ_6 := $tmpl('templates/atemplate.yaml')
pathlib.template_write(templ_6, '${args.path}/templates/atemplate.yaml', true)!
}
}
console.print_debug('formating dir ${args.path}')
osal.execute_silent('v fmt -w ${args.path}')!
}
fn platform_check(args GeneratorArgs) ! {

View File

@@ -1,15 +1,18 @@
module ${args.name}
@if args.hasconfig
import freeflowuniverse.herolib.core.base
@end
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.data.paramsparser
@if args.cat == .installer
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
@if args.startupmanager
import time
@end
@end
__global (
${args.name}_global map[string]&${args.classname}

View File

@@ -189,7 +189,6 @@ pub fn (mut h HTTPConnection) get(req_ Request) !string {
req.debug = true
req.method = .get
result := h.send(req)!
println(result)
return result.data
}

View File

@@ -1,7 +1,7 @@
!!hero_code.generate_installer
name:'meilisearchinstaller'
classname:'MeilisearchServer'
name:'meilisearch_installer'
classname:'MeilisearchInstaller'
singleton:0
templates:0
default:1

View File

@@ -0,0 +1,178 @@
module meilisearch_installer
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.core.texttools
import os
import rand
import json
fn generate_master_key(length int) !string {
mut key := []rune{}
valid_chars := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for _ in 0 .. length {
random_index := rand.int_in_range(0, valid_chars.len)!
key << valid_chars[random_index]
}
return key.string()
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut installer := get()!
mut env := 'development'
if installer.production {
env = 'production'
}
res << zinit.ZProcessNewArgs{
name: 'meilisearch'
cmd: 'meilisearch --no-analytics --http-addr ${installer.host}:${installer.port} --env ${env} --db-path ${installer.path} --master-key ${installer.masterkey}'
startuptype: .zinit
start: true
restart: true
}
return res
}
struct MeilisearchVersionResponse {
version string @[json: 'pkgVersion']
commit_date string @[json: 'commitDate']
commit_sha string @[json: 'commitSha']
}
fn running() !bool {
mut cfg := get()!
url := 'http://${cfg.host}:${cfg.port}'
mut conn := httpconnection.new(name: 'meilisearchinstaller', url: url)!
conn.default_header.add(.authorization, 'Bearer ${cfg.masterkey}')
response := conn.get(prefix: 'version', debug: true) or {
return error('Failed to get meilisearch version: ${err}')
}
decoded_response := json.decode(MeilisearchVersionResponse, response) or {
return error('Failed to decode meilisearch version: ${err}')
}
if decoded_response.version == '' {
console.print_stderr('Meilisearch is not running')
return false
}
console.print_header('Meilisearch is running')
return true
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
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 {
res := os.execute('${osal.profile_path_source_and()!} meilisearch -V')
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 meilisearch version.\n${res.output}")
}
r2 := r[0].all_after('meilisearch').trim(' ')
if texttools.version(version) != texttools.version(r2) {
return false
}
return true
}
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
cfg := get()!
console.print_header('install meilisearch')
// Check if meilisearch is installed
mut res := os.execute('meilisearch --version')
if res.exit_code == 0 {
console.print_header('meilisearch is already installed')
return
}
// Check if curl is installed
res = os.execute('curl --version')
if res.exit_code == 0 {
console.print_header('curl is already installed')
} else {
osal.package_install('curl') or {
return error('Could not install curl, its required to install meilisearch.\nerror:\n${err}')
}
}
if os.exists('${cfg.path}') {
os.rmdir_all('${cfg.path}') or {
return error('Could not remove directory ${cfg.path}.\nerror:\n${err}')
}
}
os.mkdir('${cfg.path}') or {
return error('Could not create directory ${cfg.path}.\nerror:\n${err}')
}
mut cmd := 'cd ${cfg.path} && curl -L https://install.meilisearch.com | sh'
osal.execute_stdout(cmd)!
cmd = 'mv /tmp/meilisearch/meilisearch /usr/local/bin/meilisearch'
osal.execute_stdout(cmd)!
console.print_header('meilisearch is installed')
}
fn build() ! {}
fn destroy() ! {
console.print_header('destroy meilisearch')
mut cfg := get()!
if os.exists('${cfg.path}') {
console.print_header('removing directory ${cfg.path}')
os.rmdir_all('${cfg.path}') or {
return error('Could not remove directory ${cfg.path}.\nerror:\n${err}')
}
}
res := os.execute('meilisearch --version')
if res.exit_code == 0 {
console.print_header('removing meilisearch binary')
osal.execute_silent('sudo rm -rf /usr/local/bin/meilisearch')!
}
mut zinit_factory := zinit.new()!
if zinit_factory.exists('meilisearch') {
zinit_factory.stop('meilisearch') or {
return error('Could not stop meilisearch service due to: ${err}')
}
zinit_factory.delete('meilisearch') or {
return error('Could not delete meilisearch service due to: ${err}')
}
}
console.print_header('meilisearch is destroyed')
}

View File

@@ -0,0 +1,279 @@
module meilisearch_installer
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
meilisearch_installer_global map[string]&MeilisearchInstaller
meilisearch_installer_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&MeilisearchInstaller {
mut context := base.context()!
mut args := args_get(args_)
mut obj := MeilisearchInstaller{}
if args.name !in meilisearch_installer_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('meilisearch_installer', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return meilisearch_installer_global[args.name] or {
println(meilisearch_installer_global)
// bug if we get here because should be in globals
panic('could not get config for meilisearch_installer with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o MeilisearchInstaller) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('meilisearch_installer', 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('meilisearch_installer', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('meilisearch_installer', args.name)!
if args.name in meilisearch_installer_global {
// del meilisearch_installer_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o MeilisearchInstaller) ! {
mut o2 := obj_init(o)!
meilisearch_installer_global[o.name] = &o2
meilisearch_installer_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
}
pub fn play(args_ PlayArgs) ! {
mut args := args_
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'meilisearch_installer.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'meilisearch_installer.')!
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 meilisearch_installer.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action meilisearch_installer.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut meilisearch_installer_obj := get(name: name)!
console.print_debug('action object:\n${meilisearch_installer_obj}')
if other_action.name == 'start' {
console.print_debug('install action meilisearch_installer.${other_action.name}')
meilisearch_installer_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action meilisearch_installer.${other_action.name}')
meilisearch_installer_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action meilisearch_installer.${other_action.name}')
meilisearch_installer_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()!
}
}
}
// load from disk and make sure is properly intialized
pub fn (mut self MeilisearchInstaller) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self MeilisearchInstaller) start() ! {
switch(self.name)
if self.running()! {
return
}
console.print_header('meilisearch_installer start')
if !installed()! {
install()!
}
configure()!
start_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting meilisearch_installer 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('meilisearch_installer did not install properly.')
}
pub fn (mut self MeilisearchInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self MeilisearchInstaller) 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 MeilisearchInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self MeilisearchInstaller) 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]
pub struct InstallArgs {
pub mut:
reset bool
}
pub fn (mut self MeilisearchInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self MeilisearchInstaller) build() ! {
switch(self.name)
build()!
}
pub fn (mut self MeilisearchInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for meilisearch_installer
pub fn switch(name string) {
meilisearch_installer_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,4 +1,4 @@
module meilisearchinstaller
module meilisearch_installer
import freeflowuniverse.herolib.data.encoderhero
@@ -8,7 +8,7 @@ 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 MeilisearchServer {
pub struct MeilisearchInstaller {
pub mut:
name string = 'default'
path string = '/tmp/meilisearch'
@@ -19,12 +19,27 @@ pub mut:
}
// your checking & initialization code if needed
fn obj_init(mycfg_ MeilisearchServer) !MeilisearchServer {
fn obj_init(mycfg_ MeilisearchInstaller) !MeilisearchInstaller {
mut mycfg := mycfg_
if mycfg.masterkey == '' {
return error('masterkey is empty')
mycfg.masterkey = generate_master_key(16)!
}
if mycfg.path == '' {
mycfg.path = '/tmp/meilisearch'
}
if mycfg.host == '' {
mycfg.host = 'localhost'
}
if mycfg.port == 0 {
mycfg.port = 7700
}
if mycfg.name == '' {
mycfg.name = 'default'
}
return mycfg
}
@@ -35,11 +50,11 @@ fn configure() ! {
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj MeilisearchServer) !string {
return encoderhero.encode[MeilisearchServer](obj)!
pub fn heroscript_dumps(obj MeilisearchInstaller) !string {
return encoderhero.encode[MeilisearchInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !MeilisearchServer {
mut obj := encoderhero.decode[MeilisearchServer](heroscript)!
pub fn heroscript_loads(heroscript string) !MeilisearchInstaller {
mut obj := encoderhero.decode[MeilisearchInstaller](heroscript)!
return obj
}

View File

@@ -1,183 +0,0 @@
module meilisearchinstaller
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.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.core.httpconnection
import os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut installer := get()!
mut env := 'development'
if installer.production {
env = 'production'
}
res << zinit.ZProcessNewArgs{
name: 'meilisearch'
cmd: 'meilisearch --no-analytics --http-addr ${installer.host}:${installer.port} --env ${env} --db-path ${installer.path} --master-key ${installer.masterkey}'
}
return res
}
fn running() !bool {
mut meilisearch := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of meilisearchinstaller
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
url := 'http://${meilisearch.host}:${meilisearch.port}/api/v1'
mut conn := httpconnection.new(name: 'meilisearchinstaller', url: url)!
conn.default_header.add(.authorization, 'Bearer ${meilisearch.masterkey}')
conn.default_header.add(.content_type, 'application/json')
console.print_debug("curl -X 'GET' '${url}'/version")
response := conn.get_json_dict(prefix: 'version', debug: false) or { return false }
println('response: ${response}')
// if response
return false
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
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()!} meilisearchinstaller 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 meilisearchinstaller version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
return false
}
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {
// installers.upload(
// cmdname: 'meilisearchinstaller'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/meilisearchinstaller'
// )!
}
fn install() ! {
console.print_header('install meilisearchinstaller')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_darwin_arm64.tar.gz'
// } else if osal.is_osx_intel() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/meilisearchinstaller'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('meilisearchinstaller')!
// osal.cmd_add(
// cmdname: 'meilisearchinstaller'
// source: binpath.path
// )!
}
fn build() ! {
// url := 'https://github.com/threefoldtech/meilisearchinstaller'
// make sure we install base on the node
// if osal.platform() != .ubuntu {
// return error('only support ubuntu for now')
// }
// golang.install()!
// console.print_header('build meilisearchinstaller')
// 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,168 +0,0 @@
module meilisearchinstaller
// import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.ui.console
// import freeflowuniverse.herolib.core.texttools
// import freeflowuniverse.herolib.osal.zinit
// import freeflowuniverse.herolib.installers.ulist
// // import freeflowuniverse.herolib.installers.lang.rust
// import os
// fn installed_() !bool {
// res := os.execute('${osal.profile_path_source_and()!} meilisearch -V')
// 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 meilisearch version.\n${res.output}")
// }
// r2 := r[0].all_after('meilisearch').trim(' ')
// if texttools.version(version) != texttools.version(r2) {
// return false
// }
// return true
// }
// fn install_() ! {
// console.print_header('install meilisearch')
// mut url := ''
// if core.is_linux_arm()! {
// url = 'https://github.com/meilisearch/meilisearch/releases/download/v${version}/meilisearch-linux-aarch64'
// } else if core.is_linux_intel()! {
// url = 'https://github.com/meilisearch/meilisearch/releases/download/v${version}/meilisearch-linux-amd64'
// } else if core.is_osx_arm()! {
// url = 'https://github.com/meilisearch/meilisearch/releases/download/v${version}/meilisearch-macos-apple-silicon'
// } else if core.is_osx_intel()! {
// url = 'https://github.com/meilisearch/meilisearch/releases/download/v${version}/meilisearch-macos-amd64'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download(
// url: url
// minsize_kb: 100000
// expand_dir: '/tmp/meilisearch'
// )!
// // dest.moveup_single_subdir()!
// mut binpath := dest.file_get('meilisearch')!
// osal.cmd_add(
// cmdname: 'meilisearch'
// source: binpath.path
// )!
// }
// fn build_() ! {
// // mut installer := get()!
// // url := 'https://github.com/threefoldtech/meilisearch'
// // console.print_header('compile meilisearch')
// // rust.install()!
// // mut dest_on_os := '${os.home_dir()}/hero/bin'
// // if core.is_linux()! {
// // dest_on_os = '/usr/local/bin'
// // }
// // console.print_debug(' - dest path for meilisearchs is on: ${dest_on_os}')
// // //osal.package_install('pkg-config,openssl')!
// // cmd := '
// // echo "start meilisearch installer"
// // set +ex
// // source ~/.cargo/env > /dev/null 2>&1
// // //TODO
// // cargo install meilisearch
// // cp ${os.home_dir()}/.cargo/bin/mdb* ${dest_on_os}/
// // '
// // defer {
// // destroy()!
// // }
// // osal.execute_stdout(cmd)!
// // osal.done_set('install_meilisearch', 'OK')!
// // console.print_header('meilisearch installed')
// }
// // 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: 'meilisearch'
// // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/meilisearch'
// // )!
// }
// fn startupcmd() ![]zinit.ZProcessNewArgs {
// mut res := []zinit.ZProcessNewArgs{}
// mut installer := get()!
// mut env := 'development'
// if installer.production {
// env = 'production'
// }
// res << zinit.ZProcessNewArgs{
// name: 'meilisearch'
// cmd: 'meilisearch --no-analytics --http-addr ${installer.host}:${installer.port} --env ${env} --db-path ${installer.path} --master-key ${installer.masterkey}'
// }
// return res
// }
// fn running_() !bool {
// mut installer := get()!
// // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// // this checks health of meilisearch
// // 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: 'meilisearch', 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('meilisearch is answering.')
// return false
// }
// fn start_pre() ! {
// }
// fn start_post() ! {
// }
// fn stop_pre() ! {
// }
// fn stop_post() ! {
// }
// fn destroy_() ! {
// // mut systemdfactory := systemd.new()!
// // systemdfactory.destroy("meilisearch")!
// osal.process_kill_recursive(name: 'meilisearch')!
// osal.cmd_delete('meilisearch')!
// osal.package_remove('
// meilisearch
// ') or { println('') }
// // osal.rm("
// // ")!
// }

View File

@@ -1,291 +0,0 @@
module meilisearchinstaller
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 (
meilisearchinstaller_global map[string]&MeilisearchServer
meilisearchinstaller_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub mut:
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
}
pub fn get(args_ ArgsGet) !&MeilisearchServer {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := MeilisearchServer{}
if !(args.name in meilisearchinstaller_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("meilisearchinstaller",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return meilisearchinstaller_global[args.name] or {
println(meilisearchinstaller_global)
//bug if we get here because should be in globals
panic("could not get config for meilisearchinstaller with name, is bug:${args.name}")
}
}
//register the config for the future
pub fn set(o MeilisearchServer)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("meilisearchinstaller", 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("meilisearchinstaller", args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("meilisearchinstaller",args.name)!
if args.name in meilisearchinstaller_global {
//del meilisearchinstaller_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o MeilisearchServer)! {
mut o2:=obj_init(o)!
meilisearchinstaller_global[o.name] = &o2
meilisearchinstaller_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
}
pub fn play(args_ PlayArgs) ! {
mut args:=args_
mut plbook := args.plbook or {
playbook.new(text: args.heroscript)!
}
mut install_actions := plbook.find(filter: 'meilisearchinstaller.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'meilisearchinstaller.')!
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 meilisearchinstaller.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action meilisearchinstaller.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut meilisearchinstaller_obj:=get(name:name)!
console.print_debug("action object:\n${meilisearchinstaller_obj}")
if other_action.name == "start"{
console.print_debug("install action meilisearchinstaller.${other_action.name}")
meilisearchinstaller_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action meilisearchinstaller.${other_action.name}")
meilisearchinstaller_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action meilisearchinstaller.${other_action.name}")
meilisearchinstaller_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()!
}
}
}
//load from disk and make sure is properly intialized
pub fn (mut self MeilisearchServer) reload() ! {
switch(self.name)
self=obj_init(self)!
}
pub fn (mut self MeilisearchServer) start() ! {
switch(self.name)
if self.running()!{
return
}
console.print_header('meilisearchinstaller start')
if ! installed()!{
install()!
}
configure()!
start_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
console.print_debug('starting meilisearchinstaller 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('meilisearchinstaller did not install properly.')
}
pub fn (mut self MeilisearchServer) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self MeilisearchServer) 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 MeilisearchServer) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self MeilisearchServer) 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]
pub struct InstallArgs{
pub mut:
reset bool
}
pub fn (mut self MeilisearchServer) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self MeilisearchServer) build() ! {
switch(self.name)
build()!
}
pub fn (mut self MeilisearchServer) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for meilisearchinstaller
pub fn switch(name string) {
meilisearchinstaller_default = name
}
//helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
}

View File

@@ -1,281 +0,0 @@
module meilisearchinstaller
// import freeflowuniverse.herolib.core.base
// import freeflowuniverse.herolib.core.playbook
// import freeflowuniverse.herolib.ui.console
// import freeflowuniverse.herolib.data.encoderhero
// import freeflowuniverse.herolib.sysadmin.startupmanager
// import freeflowuniverse.herolib.osal.zinit
// import time
// __global (
// meilisearchinstaller_global map[string]&MeilisearchServer
// meilisearchinstaller_default string
// )
// /////////FACTORY
// @[params]
// pub struct ArgsGet {
// pub mut:
// name string
// }
// fn args_get(args_ ArgsGet) ArgsGet {
// mut model := args_
// if model.name == '' {
// model.name = meilisearchinstaller_default
// }
// if model.name == '' {
// model.name = 'default'
// }
// return model
// }
// pub fn get(args_ ArgsGet) !&MeilisearchServer {
// mut args := args_get(args_)
// if args.name !in meilisearchinstaller_global {
// if args.name == 'default' {
// if !config_exists(args) {
// if default {
// mut context := base.context() or { panic('bug') }
// context.hero_config_set('meilisearchinstaller', model.name, heroscript_default()!)!
// }
// }
// load(args)!
// }
// }
// return meilisearchinstaller_global[args.name] or {
// println(meilisearchinstaller_global)
// panic('could not get config for ${args.name} with name:${model.name}')
// }
// }
// // set the model in mem and the config on the filesystem
// pub fn set(o MeilisearchServer) ! {
// mut o2 := obj_init(o)!
// meilisearchinstaller_global[o.name] = &o2
// meilisearchinstaller_default = o.name
// }
// // check we find the config on the filesystem
// pub fn exists(args_ ArgsGet) bool {
// mut model := args_get(args_)
// mut context := base.context() or { panic('bug') }
// return context.hero_config_exists('meilisearchinstaller', model.name)
// }
// // load the config error if it doesn't exist
// pub fn load(args_ ArgsGet) ! {
// mut model := args_get(args_)
// mut context := base.context()!
// mut heroscript := context.hero_config_get('meilisearchinstaller', model.name)!
// play(heroscript: heroscript)!
// }
// // save the config to the filesystem in the context
// pub fn save(o MeilisearchServer) ! {
// mut context := base.context()!
// heroscript := encoderhero.encode[MeilisearchServer](o)!
// context.hero_config_set('meilisearchinstaller', model.name, heroscript)!
// }
// @[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 model := args_
// if model.heroscript == '' {
// model.heroscript = heroscript_default()!
// }
// mut plbook := model.plbook or { playbook.new(text: model.heroscript)! }
// mut configure_actions := plbook.find(filter: 'meilisearchinstaller.configure')!
// if configure_actions.len > 0 {
// for config_action in configure_actions {
// mut p := config_action.params
// mycfg := cfg_play(p)!
// console.print_debug('install action meilisearchinstaller.configure\n${mycfg}')
// set(mycfg)!
// save(mycfg)!
// }
// }
// mut other_actions := plbook.find(filter: 'meilisearchinstaller.')!
// 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 meilisearchinstaller.destroy')
// destroy_()!
// }
// if other_action.name == 'install' {
// console.print_debug('install action meilisearchinstaller.install')
// install_()!
// }
// }
// if other_action.name in ['start', 'stop', 'restart'] {
// mut p := other_action.params
// name := p.get('name')!
// mut meilisearchinstaller_obj := get(name: name)!
// console.print_debug('action object:\n${meilisearchinstaller_obj}')
// if other_action.name == 'start' {
// console.print_debug('install action meilisearchinstaller.${other_action.name}')
// meilisearchinstaller_obj.start()!
// }
// if other_action.name == 'stop' {
// console.print_debug('install action meilisearchinstaller.${other_action.name}')
// meilisearchinstaller_obj.stop()!
// }
// if other_action.name == 'restart' {
// console.print_debug('install action meilisearchinstaller.${other_action.name}')
// meilisearchinstaller_obj.restart()!
// }
// }
// }
// }
// ////////////////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////////////////////////////
// // load from disk and make sure is properly intialized
// pub fn (mut self MeilisearchServer) reload() ! {
// switch(self.name)
// self = obj_init(self)!
// }
// 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()!
// }
// }
// }
// pub fn (mut self MeilisearchServer) start() ! {
// switch(self.name)
// if self.running()! {
// return
// }
// console.print_header('meilisearchinstaller start')
// if !installed_()! {
// install_()!
// }
// configure()!
// start_pre()!
// for zprocess in startupcmd()! {
// mut sm := startupmanager_get(zprocess.startuptype)!
// console.print_debug('starting meilisearchinstaller 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('meilisearchinstaller did not install properly.')
// }
// pub fn (mut self MeilisearchServer) install_start(model InstallArgs) ! {
// switch(self.name)
// self.install(model)!
// self.start()!
// }
// pub fn (mut self MeilisearchServer) 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 MeilisearchServer) restart() ! {
// switch(self.name)
// self.stop()!
// self.start()!
// }
// pub fn (mut self MeilisearchServer) 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]
// pub struct InstallArgs {
// pub mut:
// reset bool
// }
// // switch instance to be used for meilisearchinstaller
// pub fn switch(name string) {
// meilisearchinstaller_default = name
// }
// pub fn (mut self MeilisearchServer) install(args InstallArgs) ! {
// switch(self.name)
// if args.reset {
// destroy_()!
// }
// if !(installed_()!) {
// install_()!
// }
// }
// pub fn (mut self MeilisearchServer) build() ! {
// switch(self.name)
// build_()!
// }
// pub fn (mut self MeilisearchServer) destroy() ! {
// switch(self.name)
// self.stop() or {}
// destroy_()!
// }

View File

@@ -4,29 +4,9 @@ import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.installers.virt.podman as podman_installer
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist
import os
fn installed_() !bool {
mut cfg := get()!
mut podman := podman_installer.get()!
podman.install()!
cmd := 'podman healthcheck run ${cfg.container_name}'
result := os.execute(cmd)
if result.exit_code != 0 {
return false
}
return true
}
fn install_() ! {
console.print_header('install postgresql')
mut podman := podman_installer.get()!
podman.install()!
osal.execute_silent('podman pull docker.io/library/postgres:latest')!
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut cfg := get()!
mut res := []zinit.ZProcessNewArgs{}
@@ -43,9 +23,14 @@ fn startupcmd() ![]zinit.ZProcessNewArgs {
return res
}
fn running_() !bool {
mut mydb := get()!
mydb.check() or { return false }
fn running() !bool {
cfg := get()!
cmd := 'podman healthcheck run ${cfg.container_name}'
result := os.execute(cmd)
if result.exit_code != 0 {
return false
}
return true
}
@@ -61,7 +46,40 @@ fn stop_pre() ! {
fn stop_post() ! {
}
fn destroy_() ! {
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
mut cfg := get()!
mut podman := podman_installer.get()!
podman.install()!
cmd := 'podman healthcheck run ${cfg.container_name}'
result := os.execute(cmd)
if result.exit_code != 0 {
return false
}
return true
}
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('install postgresql')
mut podman := podman_installer.get()!
podman.install()!
osal.execute_silent('podman pull docker.io/library/postgres:latest')!
}
fn destroy() ! {
// remove the podman postgresql container
mut cfg := get()!
cmd := 'podman rm -f ${cfg.container_name}'
@@ -70,5 +88,20 @@ fn destroy_() ! {
if result.exit_code != 0 {
return error("Postgresql container isn't running: ${result.output}")
}
// Remove podman
mut podman := podman_installer.get()!
podman.destroy()!
// Remove zinit service, Q: Do we really need to run the postgresql inside a zinit service? it's already running in a container
mut zinit_factory := zinit.new()!
if zinit_factory.exists('postgresql') {
zinit_factory.stop('postgresql') or {
return error('Could not stop postgresql service due to: ${err}')
}
zinit_factory.delete('postgresql') or {
return error('Could not delete postgresql service due to: ${err}')
}
}
console.print_header('Postgresql container removed')
}

View File

@@ -1,103 +0,0 @@
module postgresql
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import db.pg
import os
import net
pub fn (mut server Postgresql) path_config() !pathlib.Path {
return pathlib.get_dir(path: '${server.volume_path}/config', create: true)!
}
pub fn (mut server Postgresql) path_data() !pathlib.Path {
return pathlib.get_dir(path: '${server.volume_path}/data', create: true)!
}
pub fn (mut server Postgresql) path_export() !pathlib.Path {
return pathlib.get_dir(path: '${server.volume_path}/exports', create: true)!
}
fn is_port_open(host string, port int) bool {
mut socket := net.dial_tcp('${host}:${port}') or { return false }
socket.close() or { return false }
return true
}
pub fn (mut server Postgresql) db() !pg.DB {
if is_port_open('localhost', 5432) == false {
return error('PostgreSQL is not listening on port 5432')
}
conn_string := 'postgresql://${server.user}:${server.password}@${server.host}:${server.port}/postgres?connect_timeout=5'
mut db := pg.connect_with_conninfo(conn_string)!
// console.print_header("Database connected: ${db}")
return db
}
pub fn (mut server Postgresql) check() ! {
mut db := server.db() or { return error('failed to check server: ${err}') }
db.exec('SELECT version();') or { return error('postgresql could not do select version') }
cmd := 'podman healthcheck run ${server.container_name}'
result := os.execute(cmd)
if result.exit_code != 0 {
return error("Postgresql container isn't healthy: ${result.output}")
}
container_id := 'podman container inspect ${server.container_name} --format {{.Id}}'
container_id_result := os.execute(container_id)
if container_id_result.exit_code != 0 {
return error('Cannot get the container ID: ${result.output}')
}
server.container_id = container_id
console.print_header('Container ID: ${container_id_result.output}')
}
pub fn (mut server Postgresql) db_exists(name_ string) !bool {
mut db := server.db()!
// SELECT datname FROM pg_database WHERE datname='gitea';
r := db.exec("SELECT datname FROM pg_database WHERE datname='${name_}';")!
if r.len == 1 {
console.print_header('db exists: ${name_}')
return true
}
if r.len > 1 {
return error('should not have more than 1 db with name ${name_}')
}
return false
}
pub fn (mut server Postgresql) db_create(name_ string) ! {
name := texttools.name_fix(name_)
server.check()!
mut db := server.db()!
db_exists := server.db_exists(name_)!
if !db_exists {
console.print_header('db create: ${name_}')
db.exec('CREATE DATABASE ${name};')!
}
db_exists2 := server.db_exists(name_)!
if !db_exists2 {
return error('Could not create db: ${name_}, could not find in DB.')
}
}
pub fn (mut server Postgresql) db_delete(name_ string) ! {
name := texttools.name_fix(name_)
server.check()!
mut db := server.db()!
db_exists := server.db_exists(name_)!
if db_exists {
console.print_header('db delete: ${name_}')
db.exec('DROP DATABASE ${name};')!
}
db_exists2 := server.db_exists(name_)!
if db_exists2 {
return error('Could not delete db: ${name_}, could not find in DB.')
}
}

View File

@@ -3,285 +3,272 @@ module postgresql
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 (
postgresql_global map[string]&Postgresql
postgresql_default string
postgresql_global map[string]&Postgresql
postgresql_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&Postgresql {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := Postgresql{}
if !(args.name in postgresql_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("postgresql",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return postgresql_global[args.name] or {
println(postgresql_global)
//bug if we get here because should be in globals
panic("could not get config for postgresql with name, is bug:${args.name}")
}
pub fn get(args_ ArgsGet) !&Postgresql {
mut context := base.context()!
mut args := args_get(args_)
mut obj := Postgresql{}
if args.name !in postgresql_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('postgresql', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return postgresql_global[args.name] or {
println(postgresql_global)
// bug if we get here because should be in globals
panic('could not get config for postgresql with name, is bug:${args.name}')
}
}
//register the config for the future
pub fn set(o Postgresql)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("postgresql", o.name, heroscript)!
// register the config for the future
pub fn set(o Postgresql) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('postgresql', 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("postgresql", args.name)
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('postgresql', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("postgresql",args.name)!
if args.name in postgresql_global {
//del postgresql_global[args.name]
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('postgresql', args.name)!
if args.name in postgresql_global {
// del postgresql_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o Postgresql)! {
mut o2:=obj_init(o)!
postgresql_global[o.name] = &o2
postgresql_default = o.name
// only sets in mem, does not set as config
fn set_in_mem(o Postgresql) ! {
mut o2 := obj_init(o)!
postgresql_global[o.name] = &o2
postgresql_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 install_actions := plbook.find(filter: 'postgresql.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut install_actions := plbook.find(filter: 'postgresql.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'postgresql.')!
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 postgresql.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action postgresql.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut postgresql_obj:=get(name:name)!
console.print_debug("action object:\n${postgresql_obj}")
if other_action.name == "start"{
console.print_debug("install action postgresql.${other_action.name}")
postgresql_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action postgresql.${other_action.name}")
postgresql_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action postgresql.${other_action.name}")
postgresql_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'postgresql.')!
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 postgresql.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action postgresql.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut postgresql_obj := get(name: name)!
console.print_debug('action object:\n${postgresql_obj}')
if other_action.name == 'start' {
console.print_debug('install action postgresql.${other_action.name}')
postgresql_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action postgresql.${other_action.name}')
postgresql_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action postgresql.${other_action.name}')
postgresql_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
// load from disk and make sure is properly intialized
pub fn (mut self Postgresql) reload() ! {
switch(self.name)
self=obj_init(self)!
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self Postgresql) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('postgresql start')
console.print_header('postgresql 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 postgresql with ${zprocess.startuptype}...')
console.print_debug('starting postgresql 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('postgresql did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('postgresql did not install properly.')
}
pub fn (mut self Postgresql) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self Postgresql) 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 Postgresql) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self Postgresql) 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 Postgresql) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self Postgresql) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for postgresql
// switch instance to be used for postgresql
pub fn switch(name string) {
postgresql_default = name
postgresql_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,25 +1,12 @@
module postgresql
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
pub const version = '1.14.3'
pub const version = '0.0.0'
const singleton = true
const default = true
pub fn heroscript_default() !string {
heroscript := "
!!postgresql.configure
name:'postgresql'
user: 'postgres'
password: 'postgres'
host: 'localhost'
port: 5432
volume_path:'/var/lib/postgresql/data'
container_name: 'herocontainer_postgresql'
"
return heroscript
}
@[heap]
pub struct Postgresql {
pub mut:
name string = 'default'
@@ -32,22 +19,56 @@ pub mut:
container_id string
}
fn cfg_play(p paramsparser.Params) !Postgresql {
mut mycfg := Postgresql{
name: p.get_default('name', 'default')!
user: p.get_default('user', 'postgres')!
password: p.get_default('password', 'postgres')!
host: p.get_default('host', 'localhost')!
port: p.get_int_default('port', 5432)!
volume_path: p.get_default('path', '/var/lib/postgresql/data')!
container_name: p.get_default('container_name', 'herocontainer_postgresql')!
// your checking & initialization code if needed
fn obj_init(mycfg_ Postgresql) !Postgresql {
mut mycfg := mycfg_
if mycfg.name == '' {
mycfg.name = 'default'
}
if mycfg.user == '' {
mycfg.user = 'postgres'
}
if mycfg.password == '' {
mycfg.password = 'postgres'
}
if mycfg.host == '' {
mycfg.host = 'localhost'
}
if mycfg.volume_path == '' {
mycfg.volume_path = '/var/lib/postgresql/data'
}
if mycfg.container_name == '' {
mycfg.container_name = 'herocontainer_postgresql'
}
if mycfg.port == 0 {
mycfg.port = 5432
}
return mycfg
}
fn obj_init(obj_ Postgresql) !Postgresql {
mut obj := obj_
return obj
// called before start if done
fn configure() ! {
// mut installer := get()!
// mut mycode := $tmpl('templates/atemplate.yaml')
// mut path := pathlib.get_file(path: cfg.configpath, create: true)!
// path.write(mycode)!
// console.print_debug(mycode)
}
fn configure() ! {}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj Postgresql) !string {
return encoderhero.encode[Postgresql](obj)!
}
pub fn heroscript_loads(heroscript string) !Postgresql {
mut obj := encoderhero.decode[Postgresql](heroscript)!
return obj
}

View File

@@ -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: ""

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.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')
}

View File

@@ -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'
}

View File

@@ -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
}

View File

@@ -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'
}

View File

@@ -3,289 +3,277 @@ module coredns
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 (
coredns_global map[string]&CoreDNS
coredns_default string
coredns_global map[string]&CoreDNS
coredns_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&CoreDNS {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := CoreDNS{}
if !(args.name in coredns_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("coredns",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return coredns_global[args.name] or {
println(coredns_global)
//bug if we get here because should be in globals
panic("could not get config for coredns with name, is bug:${args.name}")
}
pub fn get(args_ ArgsGet) !&CoreDNS {
mut context := base.context()!
mut args := args_get(args_)
mut obj := CoreDNS{}
if args.name !in coredns_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('coredns', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return coredns_global[args.name] or {
println(coredns_global)
// bug if we get here because should be in globals
panic('could not get config for coredns with name, is bug:${args.name}')
}
}
//register the config for the future
pub fn set(o CoreDNS)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("coredns", o.name, heroscript)!
// register the config for the future
pub fn set(o CoreDNS) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('coredns', 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("coredns", args.name)
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('coredns', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("coredns",args.name)!
if args.name in coredns_global {
//del coredns_global[args.name]
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('coredns', args.name)!
if args.name in coredns_global {
// del coredns_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o CoreDNS)! {
mut o2:=obj_init(o)!
coredns_global[o.name] = &o2
coredns_default = o.name
// only sets in mem, does not set as config
fn set_in_mem(o CoreDNS) ! {
mut o2 := obj_init(o)!
coredns_global[o.name] = &o2
coredns_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 install_actions := plbook.find(filter: 'coredns.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut install_actions := plbook.find(filter: 'coredns.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'coredns.')!
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 coredns.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action coredns.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut coredns_obj:=get(name:name)!
console.print_debug("action object:\n${coredns_obj}")
if other_action.name == "start"{
console.print_debug("install action coredns.${other_action.name}")
coredns_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action coredns.${other_action.name}")
coredns_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action coredns.${other_action.name}")
coredns_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'coredns.')!
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 coredns.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action coredns.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut coredns_obj := get(name: name)!
console.print_debug('action object:\n${coredns_obj}')
if other_action.name == 'start' {
console.print_debug('install action coredns.${other_action.name}')
coredns_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action coredns.${other_action.name}')
coredns_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action coredns.${other_action.name}')
coredns_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
// load from disk and make sure is properly intialized
pub fn (mut self CoreDNS) reload() ! {
switch(self.name)
self=obj_init(self)!
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self CoreDNS) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('coredns start')
console.print_header('coredns 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 coredns with ${zprocess.startuptype}...')
console.print_debug('starting coredns 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('coredns did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('coredns did not install properly.')
}
pub fn (mut self CoreDNS) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self CoreDNS) 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 CoreDNS) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self CoreDNS) 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 CoreDNS) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self CoreDNS) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self CoreDNS) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for coredns
// switch instance to be used for coredns
pub fn switch(name string) {
coredns_default = name
coredns_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -3,289 +3,277 @@ module gitea
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 (
gitea_global map[string]&GiteaServer
gitea_default string
gitea_global map[string]&GiteaServer
gitea_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&GiteaServer {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := GiteaServer{}
if !(args.name in gitea_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("gitea",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return gitea_global[args.name] or {
println(gitea_global)
//bug if we get here because should be in globals
panic("could not get config for gitea with name, is bug:${args.name}")
}
pub fn get(args_ ArgsGet) !&GiteaServer {
mut context := base.context()!
mut args := args_get(args_)
mut obj := GiteaServer{}
if args.name !in gitea_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('gitea', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return gitea_global[args.name] or {
println(gitea_global)
// bug if we get here because should be in globals
panic('could not get config for gitea with name, is bug:${args.name}')
}
}
//register the config for the future
pub fn set(o GiteaServer)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("gitea", o.name, heroscript)!
// register the config for the future
pub fn set(o GiteaServer) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('gitea', 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("gitea", args.name)
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('gitea', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("gitea",args.name)!
if args.name in gitea_global {
//del gitea_global[args.name]
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('gitea', args.name)!
if args.name in gitea_global {
// del gitea_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o GiteaServer)! {
mut o2:=obj_init(o)!
gitea_global[o.name] = &o2
gitea_default = o.name
// only sets in mem, does not set as config
fn set_in_mem(o GiteaServer) ! {
mut o2 := obj_init(o)!
gitea_global[o.name] = &o2
gitea_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 install_actions := plbook.find(filter: 'gitea.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut install_actions := plbook.find(filter: 'gitea.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'gitea.')!
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 gitea.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action gitea.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut gitea_obj:=get(name:name)!
console.print_debug("action object:\n${gitea_obj}")
if other_action.name == "start"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'gitea.')!
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 gitea.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action gitea.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut gitea_obj := get(name: name)!
console.print_debug('action object:\n${gitea_obj}')
if other_action.name == 'start' {
console.print_debug('install action gitea.${other_action.name}')
gitea_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action gitea.${other_action.name}')
gitea_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action gitea.${other_action.name}')
gitea_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
// load from disk and make sure is properly intialized
pub fn (mut self GiteaServer) reload() ! {
switch(self.name)
self=obj_init(self)!
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self GiteaServer) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('gitea start')
console.print_header('gitea 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 gitea with ${zprocess.startuptype}...')
console.print_debug('starting gitea 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('gitea did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('gitea did not install properly.')
}
pub fn (mut self GiteaServer) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self GiteaServer) 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 GiteaServer) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self GiteaServer) 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 GiteaServer) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self GiteaServer) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self GiteaServer) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for gitea
// switch instance to be used for gitea
pub fn switch(name string) {
gitea_default = name
gitea_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -10,7 +10,7 @@ import freeflowuniverse.herolib.clients.mailclient
import freeflowuniverse.herolib.clients.postgresql_client
import rand
pub const version = '0.0.0'
pub const version = '1.23.3'
const singleton = true
const default = false
@@ -36,6 +36,33 @@ pub fn (obj GiteaServer) config_path() string {
// your checking & initialization code if needed
fn obj_init(mycfg_ GiteaServer) !GiteaServer {
mut mycfg := mycfg_
if mycfg.name == '' {
mycfg.name = 'default'
}
if mycfg.path == '' {
mycfg.path = '${os.home_dir()}/hero/var/gitea'
}
if mycfg.passwd == '' {
mycfg.passwd = rand.hex(12)
}
if mycfg.postgresql_client_name == '' {
mycfg.postgresql_client_name = 'default'
}
if mycfg.domain == '' {
mycfg.domain = 'git.test.com'
}
if mycfg.jwt_secret == '' {
mycfg.jwt_secret = rand.hex(12)
}
if mycfg.mail_client_name == '' {
mycfg.mail_client_name = 'default'
}
return mycfg
}

View File

@@ -1,78 +0,0 @@
module gitea
// import freeflowuniverse.herolib.installers.db.postgresql as postgresinstaller
// import freeflowuniverse.herolib.installers.base
// import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.core
// import freeflowuniverse.herolib.core.pathlib
// import freeflowuniverse.herolib.ui.console
// pub fn install_() ! {
// if core.platform()! != .ubuntu || core.platform()! != .arch {
// return error('only support ubuntu and arch for now')
// }
// if osal.done_exists('gitea_install') {
// console.print_header('gitea binaraies already installed')
// return
// }
// // make sure we install base on the node
// base.install()!
// postgresinstaller.install()!
// version := '1.22.0'
// url := 'https://github.com/go-gitea/gitea/releases/download/v${version}/gitea-${version}-linux-amd64.xz'
// console.print_debug(' download ${url}')
// mut dest := osal.download(
// url: url
// minsize_kb: 40000
// reset: true
// expand_file: '/tmp/download/gitea'
// )!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
// osal.done_set('gitea_install', 'OK')!
// console.print_header('gitea installed properly.')
// }
// pub fn start() ! {
// if core.platform()! != .ubuntu || core.platform()! != .arch {
// return error('only support ubuntu and arch for now')
// }
// if osal.done_exists('gitea_install') {
// console.print_header('gitea binaraies already installed')
// return
// }
// // make sure we install base on the node
// base.install()!
// postgresinstaller.install()!
// version := '1.22.0'
// url := 'https://github.com/go-gitea/gitea/releases/download/v${version}/gitea-${version}-linux-amd64.xz'
// console.print_debug(' download ${url}')
// mut dest := osal.download(
// url: url
// minsize_kb: 40000
// reset: true
// expand_file: '/tmp/download/gitea'
// )!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
// osal.done_set('gitea_install', 'OK')!
// console.print_header('gitea installed properly.')
// }

View File

@@ -1,208 +0,0 @@
module gitea
// import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.osal.zinit
// import freeflowuniverse.herolib.data.dbfs
// import freeflowuniverse.herolib.core.texttools
// import freeflowuniverse.herolib.core.pathlib
// import freeflowuniverse.herolib.installers.db.postgresql
// import json
// import rand
// import os
// import time
// import freeflowuniverse.herolib.ui.console
// // @[params]
// // pub struct Config {
// // pub mut:
// // name string = 'default'
// // reset bool
// // path string = '/data/gitea'
// // passwd string
// // postgresql_name string = 'default'
// // mail_from string = 'git@meet.tf'
// // smtp_addr string = 'smtp-relay.brevo.com'
// // smtp_login string @[required]
// // smtp_port int = 587
// // smtp_passwd string
// // domain string @[required]
// // jwt_secret string
// // lfs_jwt_secret string
// // internal_token string
// // secret_key string
// // }
// // pub struct Server {
// // pub mut:
// // name string
// // config GiteaServer
// // process ?zinit.ZProcess
// // path_config pathlib.Path
// // }
// // get the gitea server
// //```js
// // name string = 'default'
// // path string = '/data/gitea'
// // passwd string
// //```
// // if name exists already in the config DB, it will load for that name
// // pub fn new_server(args_ GiteaServer) !Server {
// // install()! // make sure it has been build & ready to be used
// // mut args := args_
// // if args.passwd == '' {
// // args.passwd = rand.string(12)
// // }
// // args.name = texttools.name_fix(args.name)
// // key := 'gitea_config_${args.name}'
// // mut kvs := dbfs.new(name: 'config')!
// // if !kvs.exists(key) {
// // // jwt_secret string
// // // lfs_jwt_secret string
// // // internal_token string
// // // secret_key string
// // if args.jwt_secret == '' {
// // r := os.execute_or_panic('gitea generate secret JWT_SECRET')
// // args.jwt_secret = r.output.trim_space()
// // }
// // if args.lfs_jwt_secret == '' {
// // r := os.execute_or_panic('gitea generate secret LFS_JWT_SECRET')
// // args.lfs_jwt_secret = r.output.trim_space()
// // }
// // if args.internal_token == '' {
// // r := os.execute_or_panic('gitea generate secret INTERNAL_TOKEN')
// // args.internal_token = r.output.trim_space()
// // }
// // if args.secret_key == '' {
// // r := os.execute_or_panic('gitea generate secret SECRET_KEY')
// // args.secret_key = r.output.trim_space()
// // }
// // data := json.encode(args)
// // kvs.set(key, data)!
// // }
// // return get_server(args.name)!
// // }
// // pub fn get_server(name_ string) !Server {
// // console.print_header('get gitea server ${name_}')
// // name := texttools.name_fix(name_)
// // key := 'gitea_config_${name}'
// // mut kvs := dbfs.new(name: 'config')!
// // if kvs.exists(key) {
// // data := kvs.get(key)!
// // args := json.decode(Config, data)!
// // mut server := Server{
// // name: name
// // config: args
// // path_config: pathlib.get_dir(path: '${args.path}/cfg', create: true)!
// // }
// // mut z := zinit.new()!
// // processname := 'gitea_${name}'
// // if z.process_exists(processname) {
// // server.process = z.process_get(processname)!
// // }
// // // console.print_debug(" - server get ok")
// // server.start()!
// // return server
// // }
// // return error("can't find server gitea with name ${name}")
// // }
// // // return status
// // // ```
// // // pub enum ZProcessStatus {
// // // unknown
// // // init
// // // ok
// // // error
// // // blocked
// // // spawned
// // // }
// // // ```
// pub fn (mut server GiteaServer) status() zinit.ZProcessStatus {
// mut process := server.process or { return .unknown }
// return process.status() or { return .unknown }
// }
// // run gitea as docker compose
// pub fn (mut server GiteaServer) start() ! {
// // if server.ok(){
// // return
// // }
// console.print_header('start gitea: ${server.name}')
// mut db := postgresql.get(server.config.postgresql_name)!
// // now create the DB
// db.db_create('gitea')!
// // if true{
// // panic("sd")
// // }
// // TODO: postgresql can be on other server, need to fill in all arguments in template
// t1 := $tmpl('templates/app.ini')
// mut config_path := server.path_config.file_get_new('app.ini')!
// config_path.write(t1)!
// // osal.user_add(name: 'git')!
// // osal.exec(
// // cmd: '
// // chown -R git:root ${server.config.path}
// // chmod -R 777 /usr/local/bin
// // '
// // )!
// mut z := zinit.new()!
// processname := 'gitea_${server.name}'
// mut p := z.process_new(
// name: processname
// cmd: '
// /bin/bash -c "gitea --config ${config_path.path}"
// '
// )!
// p.output_wait('Starting new Web server: tcp:0.0.0.0:3000', 120)!
// o := p.log()!
// console.print_debug(o)
// server.check()!
// console.print_header('gitea start ok.')
// }
// pub fn (mut server GiteaServer) restart() ! {
// server.stop()!
// server.start()!
// }
// pub fn (mut server GiteaServer) stop() ! {
// console.print_header('stop gitea: ${server.name}')
// mut process := server.process or { return }
// return process.stop()
// }
// // check health, return true if ok
// pub fn (mut server GiteaServer) check() ! {
// mut p := server.process or { return error("can't find process for server.") }
// p.check()!
// // TODO: need to do some other checks to gitea e.g. rest calls
// }
// // check health, return true if ok
// pub fn (mut server GiteaServer) ok() bool {
// server.check() or { return false }
// return true
// }
// // remove all data
// pub fn (mut server GiteaServer) destroy() ! {
// server.stop()!
// server.path_config.delete()!
// }

View File

@@ -1,55 +1,69 @@
module livekit
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.ulist
import net.http
import json
import os
import regex
import time
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('${osal.profile_path_source_and()!} livekit-server -v')
if res.exit_code != 0 {
return false
}
r := res.output.split_into_lines().filter(it.contains('version'))
if r.len != 1 {
return error("couldn't parse livekit version.\n${res.output}")
}
installedversion := r[0].all_after_first('version')
if texttools.version(version) != texttools.version(installedversion) {
return false
}
return true
}
fn generate_keys() ! {
mut obj := get()!
result := os.execute('livekit-server generate-keys')
fn install() ! {
console.print_header('install livekit')
mut installer := get()!
osal.execute_silent('
curl -s https://livekit.io/install.sh | bash
')!
if result.exit_code != 0 {
return error('Failed to generate LiveKit keys')
}
// Regex pattern to extract API Key and API Secret
api_pattern := r'API Key:\s*([\w\d]+)'
secret_pattern := r'API Secret:\s*([\w\d]+)'
mut api_regex := regex.regex_opt(api_pattern) or { return error('Invalid regex for API Key') }
mut secret_regex := regex.regex_opt(secret_pattern) or {
return error('Invalid regex for API Secret')
}
mut api_key := ''
mut api_secret := ''
mut start, mut end := api_regex.find(result.output)
api_key = result.output.substr(start, end).all_after(':').trim_space()
start, end = secret_regex.find(result.output)
api_secret = result.output.substr(start, end).all_after(':').trim_space()
if api_key == '' || api_secret == '' {
return error('Failed to extract API Key or API Secret')
}
obj.apikey = api_key
obj.apisecret = api_secret
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut installer := get()!
res << zinit.ZProcessNewArgs
{
name: 'livekit'
cmd: 'livekit-server --config ${installer.configpath} --bind 0.0.0.0'
res << zinit.ZProcessNewArgs{
name: 'livekit'
cmd: 'livekit-server --config ${installer.configpath} --bind 0.0.0.0'
startuptype: .zinit
}
return res
}
fn running() !bool {
console.print_header('checking if livekit server is running')
mut installer := get()!
myport := installer.nr * 2 + 7880
endpoint := 'http://localhost:${myport}/api/v1/health'
endpoint := 'http://0.0.0.0:${myport}/'
time.sleep(time.second * 2)
response := http.get(endpoint) or {
console.print_stderr('Error connecting to LiveKit server: ${err}')
@@ -61,37 +75,79 @@ fn running() !bool {
return false
}
health_info := json.decode(map[string]string, response.body) or {
console.print_stderr('Error decoding LiveKit server response: ${err}')
return false
}
if health_info['status'] != 'ok' {
console.print_stderr('LiveKit server health check failed: ${health_info['status']}')
if response.body.to_lower() != 'ok' {
console.print_stderr('LiveKit server health check failed}')
return false
}
console.print_header('the livekit server is running')
return true
}
fn start_pre() ! {
// Pre-start initialization if needed
}
fn start_post() ! {
// Post-start operations if needed
}
fn stop_pre() ! {
// Pre-stop operations if needed
}
fn stop_post() ! {
// Post-stop cleanup if needed
}
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('${osal.profile_path_source_and()!} livekit-server -v')
if res.exit_code != 0 {
return false
}
r := res.output.split_into_lines().filter(it.contains('version'))
if r.len != 1 {
return error("couldn't parse livekit version.\n${res.output}")
}
installedversion := r[0].all_after_first('version')
if texttools.version(version) != texttools.version(installedversion) {
return false
}
return true
}
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('install livekit')
osal.execute_silent('curl -sSL https://get.livekit.io | bash')!
console.print_header('livekit is installed')
console.print_header('generating livekit keys')
generate_keys()!
console.print_header('livekit keys are generated')
}
fn destroy() ! {
mut installer := get()!
os.rm(installer.configpath) or {}
os.rm('livekit-server') or {}
console.print_header('removing livekit')
res := os.execute('sudo rm -rf /usr/local/bin/livekit-server')
if res.exit_code != 0 {
return error('Failed to remove LiveKit server')
}
mut zinit_factory := zinit.new()!
if zinit_factory.exists('livekit') {
zinit_factory.stop('livekit') or {
return error('Could not stop livekit service due to: ${err}')
}
zinit_factory.delete('livekit') or {
return error('Could not delete livekit service due to: ${err}')
}
}
console.print_header('livekit removed')
}

View File

@@ -3,285 +3,272 @@ module livekit
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 (
livekit_global map[string]&LivekitServer
livekit_default string
livekit_global map[string]&LivekitServer
livekit_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&LivekitServer {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := LivekitServer{}
if !(args.name in livekit_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("livekit",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return livekit_global[args.name] or {
println(livekit_global)
//bug if we get here because should be in globals
panic("could not get config for livekit with name, is bug:${args.name}")
}
pub fn get(args_ ArgsGet) !&LivekitServer {
mut context := base.context()!
mut args := args_get(args_)
mut obj := LivekitServer{}
if args.name !in livekit_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('livekit', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return livekit_global[args.name] or {
println(livekit_global)
// bug if we get here because should be in globals
panic('could not get config for livekit with name, is bug:${args.name}')
}
}
//register the config for the future
pub fn set(o LivekitServer)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("livekit", o.name, heroscript)!
// register the config for the future
pub fn set(o LivekitServer) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('livekit', 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("livekit", args.name)
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('livekit', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("livekit",args.name)!
if args.name in livekit_global {
//del livekit_global[args.name]
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('livekit', args.name)!
if args.name in livekit_global {
// del livekit_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o LivekitServer)! {
mut o2:=obj_init(o)!
livekit_global[o.name] = &o2
livekit_default = o.name
// only sets in mem, does not set as config
fn set_in_mem(o LivekitServer) ! {
mut o2 := obj_init(o)!
livekit_global[o.name] = &o2
livekit_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 install_actions := plbook.find(filter: 'livekit.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut install_actions := plbook.find(filter: 'livekit.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'livekit.')!
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 livekit.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action livekit.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut livekit_obj:=get(name:name)!
console.print_debug("action object:\n${livekit_obj}")
if other_action.name == "start"{
console.print_debug("install action livekit.${other_action.name}")
livekit_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action livekit.${other_action.name}")
livekit_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action livekit.${other_action.name}")
livekit_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'livekit.')!
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 livekit.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action livekit.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut livekit_obj := get(name: name)!
console.print_debug('action object:\n${livekit_obj}')
if other_action.name == 'start' {
console.print_debug('install action livekit.${other_action.name}')
livekit_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action livekit.${other_action.name}')
livekit_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action livekit.${other_action.name}')
livekit_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
// load from disk and make sure is properly intialized
pub fn (mut self LivekitServer) reload() ! {
switch(self.name)
self=obj_init(self)!
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self LivekitServer) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('livekit start')
console.print_header('livekit 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 livekit with ${zprocess.startuptype}...')
console.print_debug('starting livekit 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('livekit did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('livekit did not install properly.')
}
pub fn (mut self LivekitServer) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self LivekitServer) 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 LivekitServer) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self LivekitServer) 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 LivekitServer) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self LivekitServer) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for livekit
// switch instance to be used for livekit
pub fn switch(name string) {
livekit_default = name
livekit_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,26 +1,16 @@
module livekit
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
import os
pub const version = '1.7.2'
const singleton = false
const default = true
pub fn heroscript_default() !string {
heroscript := "
!!livekit.configure
name:'default'
apikey: ''
apisecret: ''
nr: 1 // each specific instance onto this server needs to have a unique nr
"
return heroscript
}
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct LivekitServer {
pub mut:
name string = 'default'
@@ -30,42 +20,10 @@ pub mut:
nr int = 0 // each specific instance onto this server needs to have a unique nr
}
fn cfg_play(p paramsparser.Params) !LivekitServer {
mut mycfg := LivekitServer{
name: p.get_default('name', 'default')!
apikey: p.get_default('apikey', '')!
apisecret: p.get_default('apisecret', '')!
nr: p.get_default_int('nr', 0)!
}
return mycfg
}
fn obj_init(obj_ LivekitServer) !LivekitServer {
mut mycfg := obj_
if mycfg.configpath == '' {
mycfg.configpath = '${os.home_dir()}/hero/cfg/livekit_${myconfig.name}.yaml'
}
if mycfg.apikey == '' || mycfg.apisecret == '' {
// Execute the livekit-server generate-keys command
result := os.execute('livekit-server generate-keys')
if result.exit_code != 0 {
return error('Failed to generate LiveKit keys')
}
// Split the output into lines
lines := result.output.split_into_lines()
// Extract API Key and API Secret
for line in lines {
if line.starts_with('API Key:') {
server.apikey = line.all_after('API Key:').trim_space()
} else if line.starts_with('API Secret:') {
server.apisecret = line.all_after('API Secret:').trim_space()
}
}
// Verify that both keys were extracted
if server.apikey == '' || server.apisecret == '' {
return error('Failed to extract API Key or API Secret')
}
mut obj := obj_
if obj.configpath == '' {
obj.configpath = '${os.home_dir()}/hero/cfg/config.yaml'
}
return obj
}
@@ -79,3 +37,14 @@ fn configure() ! {
path.write(mycode)!
console.print_debug(mycode)
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj LivekitServer) !string {
return encoderhero.encode[LivekitServer](obj)!
}
pub fn heroscript_loads(heroscript string) !LivekitServer {
mut obj := encoderhero.decode[LivekitServer](heroscript)!
return obj
}

View File

@@ -11,7 +11,7 @@ rtc:
use_external_ip: true
redis:
# redis is recommended for production deploys
address: localhost:6379
address: 0.0.0.0:6379
keys:
# key-value pairs
${installer.apikey}: ${installer.apisecret}

View File

@@ -5,8 +5,6 @@ import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('screen --version')
@@ -24,8 +22,7 @@ fn ulist_get() !ulist.UList {
}
// uploads to S3 server if configured
fn upload() ! {
}
fn upload() ! {}
fn install() ! {
console.print_header('install screen')

View File

@@ -1,124 +1,109 @@
module screen
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 (
screen_global map[string]&Screen
screen_default string
screen_global map[string]&Screen
screen_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
pub fn get(args_ ArgsGet) !&Screen {
return &Screen{}
pub fn get(args_ ArgsGet) !&Screen {
return &Screen{}
}
@[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 other_actions := plbook.find(filter: 'screen.')!
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 screen.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action screen.install")
install()!
}
}
}
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut other_actions := plbook.find(filter: 'screen.')!
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 screen.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action screen.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()!
}
}
// 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 (mut self Screen) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self Screen) destroy() ! {
switch(self.name)
destroy()!
switch(self.name)
destroy()!
}
//switch instance to be used for screen
// switch instance to be used for screen
pub fn switch(name string) {
screen_default = name
screen_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,5 +1,7 @@
module screen
import freeflowuniverse.herolib.data.encoderhero
const singleton = false
const default = true
@@ -10,6 +12,7 @@ pub mut:
name string = 'default'
}
// your checking & initialization code if needed
fn obj_init(obj_ Screen) !Screen {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
@@ -20,3 +23,14 @@ fn obj_init(obj_ Screen) !Screen {
fn configure() ! {
// mut installer := get()!
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj Screen) !string {
return encoderhero.encode[Screen](obj)!
}
pub fn heroscript_loads(heroscript string) !Screen {
mut obj := encoderhero.decode[Screen](heroscript)!
return obj
}

View File

@@ -8,10 +8,6 @@ import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.osal.zinit as zinit_module
import freeflowuniverse.herolib.installers.ulist
// import freeflowuniverse.herolib.core.pathlib
// import freeflowuniverse.herolib.installers.lang.golang
// import freeflowuniverse.herolib.installers.lang.rust
// import freeflowuniverse.herolib.installers.lang.python
import os
fn startupcmd() ![]zinit_module.ZProcessNewArgs {
@@ -19,7 +15,7 @@ fn startupcmd() ![]zinit_module.ZProcessNewArgs {
res << zinit_module.ZProcessNewArgs{
name: 'zinit'
cmd: '/usr/local/bin/zinit init'
startuptype: .zinit
startuptype: .systemd
start: true
restart: true
}
@@ -130,8 +126,10 @@ fn build() ! {
fn destroy() ! {
mut systemdfactory := systemd.new()!
systemdfactory.destroy('zinit')!
systemdfactory.destroy('zinit') or { return error('Could not destroy zinit due to: ${err}') }
osal.process_kill_recursive(name: 'zinit')!
osal.process_kill_recursive(name: 'zinit') or {
return error('Could not kill zinit due to: ${err}')
}
osal.cmd_delete('zinit')!
}

View File

@@ -7,8 +7,8 @@ import freeflowuniverse.herolib.osal.zinit
import time
__global (
zinit_global map[string]&Zinit
zinit_default string
zinit_installer_global map[string]&ZinitInstaller
zinit_installer_default string
)
/////////FACTORY
@@ -19,8 +19,8 @@ pub mut:
name string
}
pub fn get(args_ ArgsGet) !&Zinit {
return &Zinit{}
pub fn get(args_ ArgsGet) !&ZinitInstaller {
return &ZinitInstaller{}
}
@[params]
@@ -36,37 +36,37 @@ pub fn play(args_ PlayArgs) ! {
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut other_actions := plbook.find(filter: 'zinit.')!
mut other_actions := plbook.find(filter: 'zinit_installer.')!
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 zinit.destroy')
console.print_debug('install action zinit_installer.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action zinit.install')
console.print_debug('install action zinit_installer.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut zinit_obj := get(name: name)!
console.print_debug('action object:\n${zinit_obj}')
mut zinit_installer_obj := get(name: name)!
console.print_debug('action object:\n${zinit_installer_obj}')
if other_action.name == 'start' {
console.print_debug('install action zinit.${other_action.name}')
zinit_obj.start()!
console.print_debug('install action zinit_installer.${other_action.name}')
zinit_installer_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action zinit.${other_action.name}')
zinit_obj.stop()!
console.print_debug('install action zinit_installer.${other_action.name}')
zinit_installer_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action zinit.${other_action.name}')
zinit_obj.restart()!
console.print_debug('install action zinit_installer.${other_action.name}')
zinit_installer_obj.restart()!
}
}
}
@@ -98,14 +98,13 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
}
pub fn (mut self Zinit) start() ! {
pub fn (mut self ZinitInstaller) start() ! {
switch(self.name)
if self.running()! {
return
}
println('Here...')
console.print_header('zinit start')
console.print_header('zinit_installer start')
if !installed()! {
install()!
@@ -118,7 +117,7 @@ pub fn (mut self Zinit) start() ! {
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting zinit with ${zprocess.startuptype}...')
console.print_debug('starting zinit_installer with ${zprocess.startuptype}...')
sm.new(zprocess)!
@@ -133,16 +132,16 @@ pub fn (mut self Zinit) start() ! {
}
time.sleep(100 * time.millisecond)
}
return error('zinit did not install properly.')
return error('zinit_installer did not install properly.')
}
pub fn (mut self Zinit) install_start(args InstallArgs) ! {
pub fn (mut self ZinitInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self Zinit) stop() ! {
pub fn (mut self ZinitInstaller) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()! {
@@ -152,13 +151,13 @@ pub fn (mut self Zinit) stop() ! {
stop_post()!
}
pub fn (mut self Zinit) restart() ! {
pub fn (mut self ZinitInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self Zinit) running() !bool {
pub fn (mut self ZinitInstaller) running() !bool {
switch(self.name)
// walk over the generic processes, if not running return
@@ -178,27 +177,27 @@ pub mut:
reset bool
}
pub fn (mut self Zinit) install(args InstallArgs) ! {
pub fn (mut self ZinitInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self Zinit) build() ! {
pub fn (mut self ZinitInstaller) build() ! {
switch(self.name)
build()!
}
pub fn (mut self Zinit) destroy() ! {
pub fn (mut self ZinitInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for zinit
// switch instance to be used for zinit_installer
pub fn switch(name string) {
zinit_default = name
zinit_installer_default = name
}
// helpers

View File

@@ -8,13 +8,13 @@ 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 Zinit {
pub struct ZinitInstaller {
pub mut:
name string = 'default'
}
// your checking & initialization code if needed
fn obj_init(mycfg_ Zinit) !Zinit {
fn obj_init(mycfg_ ZinitInstaller) !ZinitInstaller {
mut mycfg := mycfg_
return mycfg
}
@@ -26,11 +26,11 @@ fn configure() ! {
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj Zinit) !string {
return encoderhero.encode[Zinit](obj)!
pub fn heroscript_dumps(obj ZinitInstaller) !string {
return encoderhero.encode[ZinitInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !Zinit {
mut obj := encoderhero.decode[Zinit](heroscript)!
pub fn heroscript_loads(heroscript string) !ZinitInstaller {
mut obj := encoderhero.decode[ZinitInstaller](heroscript)!
return obj
}

View File

@@ -8,7 +8,6 @@ import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed

View File

@@ -3,15 +3,16 @@ module rust
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed_() !bool {
fn installed() !bool {
res := os.execute('${osal.profile_path_source_and()!} rustc -V')
if res.exit_code != 0 {
return false
@@ -34,14 +35,14 @@ fn ulist_get() !ulist.UList {
}
// uploads to S3 server if configured
fn upload_() ! {
fn upload() ! {
// installers.upload(
// cmdname: 'rust'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/rust'
// )!
}
fn install_() ! {
fn install() ! {
console.print_header('install rust')
base.install()!
@@ -58,14 +59,14 @@ fn install_() ! {
}
osal.profile_path_add_remove(paths2add: '${os.home_dir()}/.cargo/bin')!
return
console.print_header('rust is installed')
}
fn destroy_() ! {
osal.package_remove('
rust
')!
fn destroy() ! {
pl := core.platform()!
if pl == .arch {
osal.package_remove('rust')!
}
osal.exec(
cmd: '

View File

@@ -1,12 +1,9 @@
module rust
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 (
rust_global map[string]&RustInstaller
@@ -45,11 +42,11 @@ pub fn play(args_ PlayArgs) ! {
reset := p.get_default_false('reset')
if other_action.name == 'destroy' || reset {
console.print_debug('install action rust.destroy')
// destroy()!
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action rust.install')
// install()!
install()!
}
}
}
@@ -89,14 +86,14 @@ pub mut:
pub fn (mut self RustInstaller) install(args InstallArgs) ! {
switch(self.name)
// if args.reset || (!installed()!) {
// install()!
// }
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self RustInstaller) destroy() ! {
switch(self.name)
// destroy()!
destroy()!
}
// switch instance to be used for rust

View File

@@ -1,6 +1,7 @@
module rust
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '1.83.0'
@@ -14,13 +15,24 @@ pub mut:
name string = 'default'
}
fn obj_init(obj_ RustInstaller) !RustInstaller {
// 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_ RustInstaller) !RustInstaller {
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 RustInstaller) !string {
return encoderhero.encode[RustInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !RustInstaller {
mut obj := encoderhero.decode[RustInstaller](heroscript)!
return obj
}

View File

@@ -1,291 +0,0 @@
module mycelium
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 (
mycelium_global map[string]&MyceliumInstaller
mycelium_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub mut:
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
}
pub fn get(args_ ArgsGet) !&MyceliumInstaller {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := MyceliumInstaller{}
if !(args.name in mycelium_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("mycelium",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return mycelium_global[args.name] or {
println(mycelium_global)
//bug if we get here because should be in globals
panic("could not get config for mycelium with name, is bug:${args.name}")
}
}
//register the config for the future
pub fn set(o MyceliumInstaller)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("mycelium", 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("mycelium", args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("mycelium",args.name)!
if args.name in mycelium_global {
//del mycelium_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o MyceliumInstaller)! {
mut o2:=obj_init(o)!
mycelium_global[o.name] = &o2
mycelium_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
}
pub fn play(args_ PlayArgs) ! {
mut args:=args_
mut plbook := args.plbook or {
playbook.new(text: args.heroscript)!
}
mut install_actions := plbook.find(filter: 'mycelium.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'mycelium.')!
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 mycelium.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action mycelium.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut mycelium_obj:=get(name:name)!
console.print_debug("action object:\n${mycelium_obj}")
if other_action.name == "start"{
console.print_debug("install action mycelium.${other_action.name}")
mycelium_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action mycelium.${other_action.name}")
mycelium_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action mycelium.${other_action.name}")
mycelium_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()!
}
}
}
//load from disk and make sure is properly intialized
pub fn (mut self MyceliumInstaller) reload() ! {
switch(self.name)
self=obj_init(self)!
}
pub fn (mut self MyceliumInstaller) start() ! {
switch(self.name)
if self.running()!{
return
}
console.print_header('mycelium start')
if ! installed()!{
install()!
}
configure()!
start_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
console.print_debug('starting mycelium 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('mycelium did not install properly.')
}
pub fn (mut self MyceliumInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self MyceliumInstaller) 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 MyceliumInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self MyceliumInstaller) 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]
pub struct InstallArgs{
pub mut:
reset bool
}
pub fn (mut self MyceliumInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self MyceliumInstaller) build() ! {
switch(self.name)
build()!
}
pub fn (mut self MyceliumInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for mycelium
pub fn switch(name string) {
mycelium_default = name
}
//helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
}

View File

@@ -1 +0,0 @@
module mycelium

View File

@@ -1,5 +1,5 @@
!!hero_code.generate_installer
name: "mycelium"
name: "mycelium_installer"
classname: "MyceliumInstaller"
hasconfig: true
singleton: false

View File

@@ -1,11 +1,11 @@
module mycelium
module mycelium_installer
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.sysadmintools.zinit as zinit_installer
import freeflowuniverse.herolib.installers.infra.zinit_installer
import freeflowuniverse.herolib.clients.mycelium
import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.zinit
@@ -78,8 +78,8 @@ fn ulist_get() !ulist.UList {
// uploads to S3 server if configured
fn upload() ! {
// installers.upload(
// cmdname: 'mycelium'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/mycelium'
// cmdname: 'mycelium_installer'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/mycelium_installer'
// )!
}
@@ -125,7 +125,9 @@ fn build() ! {
if myplatform != .ubuntu {
return error('only support ubuntu for now')
}
rust.install()!
mut rs := rust.get()!
rs.install()!
console.print_header('build mycelium')

View File

@@ -0,0 +1,279 @@
module mycelium_installer
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
mycelium_installer_global map[string]&MyceliumInstaller
mycelium_installer_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&MyceliumInstaller {
mut context := base.context()!
mut args := args_get(args_)
mut obj := MyceliumInstaller{}
if args.name !in mycelium_installer_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('mycelium_installer', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return mycelium_installer_global[args.name] or {
println(mycelium_installer_global)
// bug if we get here because should be in globals
panic('could not get config for mycelium_installer with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o MyceliumInstaller) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('mycelium_installer', 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('mycelium_installer', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('mycelium_installer', args.name)!
if args.name in mycelium_installer_global {
// del mycelium_installer_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o MyceliumInstaller) ! {
mut o2 := obj_init(o)!
mycelium_installer_global[o.name] = &o2
mycelium_installer_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
}
pub fn play(args_ PlayArgs) ! {
mut args := args_
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'mycelium_installer.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'mycelium_installer.')!
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 mycelium_installer.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action mycelium_installer.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut mycelium_installer_obj := get(name: name)!
console.print_debug('action object:\n${mycelium_installer_obj}')
if other_action.name == 'start' {
console.print_debug('install action mycelium_installer.${other_action.name}')
mycelium_installer_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action mycelium_installer.${other_action.name}')
mycelium_installer_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action mycelium_installer.${other_action.name}')
mycelium_installer_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()!
}
}
}
// load from disk and make sure is properly intialized
pub fn (mut self MyceliumInstaller) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self MyceliumInstaller) start() ! {
switch(self.name)
if self.running()! {
return
}
console.print_header('mycelium_installer start')
if !installed()! {
install()!
}
configure()!
start_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting mycelium_installer 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('mycelium_installer did not install properly.')
}
pub fn (mut self MyceliumInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self MyceliumInstaller) 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 MyceliumInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self MyceliumInstaller) 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]
pub struct InstallArgs {
pub mut:
reset bool
}
pub fn (mut self MyceliumInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self MyceliumInstaller) build() ! {
switch(self.name)
build()!
}
pub fn (mut self MyceliumInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for mycelium_installer
pub fn switch(name string) {
mycelium_installer_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,4 +1,4 @@
module mycelium
module mycelium_installer
import freeflowuniverse.herolib.data.encoderhero
import freeflowuniverse.herolib.osal.tun

View File

@@ -1,114 +0,0 @@
module wireguard_installer
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// 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()!} wireguard_installer 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 wireguard_installer version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
return false
}
//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{}
}
//uploads to S3 server if configured
fn upload() ! {
// installers.upload(
// cmdname: 'wireguard_installer'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/wireguard_installer'
// )!
}
fn install() ! {
console.print_header('install wireguard_installer')
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm() {
// url = 'https://github.com/wireguard_installer-dev/wireguard_installer/releases/download/v${version}/wireguard_installer_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel() {
// url = 'https://github.com/wireguard_installer-dev/wireguard_installer/releases/download/v${version}/wireguard_installer_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm() {
// url = 'https://github.com/wireguard_installer-dev/wireguard_installer/releases/download/v${version}/wireguard_installer_${version}_darwin_arm64.tar.gz'
// } else if osal.is_osx_intel() {
// url = 'https://github.com/wireguard_installer-dev/wireguard_installer/releases/download/v${version}/wireguard_installer_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/wireguard_installer'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('wireguard_installer')!
// osal.cmd_add(
// cmdname: 'wireguard_installer'
// 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,124 +0,0 @@
module wireguard_installer
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 (
wireguard_installer_global map[string]&WireGuard
wireguard_installer_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&WireGuard {
return &WireGuard{}
}
@[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: 'wireguard_installer.')!
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 wireguard_installer.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action wireguard_installer.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 mut:
reset bool
}
pub fn (mut self WireGuard) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self WireGuard) destroy() ! {
switch(self.name)
destroy()!
}
//switch instance to be used for wireguard_installer
pub fn switch(name string) {
wireguard_installer_default = name
}
//helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
}

View File

@@ -1,43 +0,0 @@
module wireguard_installer
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '0.0.0'
const singleton = false
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 WireGuard {
pub mut:
name string = 'default'
}
//your checking & initialization code if needed
fn obj_init(mycfg_ WireGuard)!WireGuard{
mut mycfg:=mycfg_
if mycfg.password == '' && mycfg.secret == '' {
return error('password or secret needs to be filled in for ${mycfg.name}')
}
return mycfg
}
//called before start if done
fn configure() ! {
//mut installer := get()!
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj WireGuard) !string {
return encoderhero.encode[WireGuard ](obj)!
}
pub fn heroscript_loads(heroscript string) !WireGuard {
mut obj := encoderhero.decode[WireGuard](heroscript)!
return obj
}

View File

@@ -1,23 +0,0 @@
module wireguard
pub const version = '1.14.3'
const singleton = false
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 WireGuard {
pub mut:
name string = 'default'
}
fn obj_init(obj_ WireGuard) !WireGuard {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
}
// called before start if done
fn configure() ! {
// mut installer := get()!
}

View File

@@ -1,4 +1,4 @@
module wireguard
module wireguard_installer
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.ulist
@@ -15,16 +15,15 @@ fn installed() !bool {
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {
}
fn upload() ! {}
fn install() ! {
console.print_header('install wireguard')
cmd := match core.platform()! {
.ubuntu {
'sudo apt install -y wireguard'
@@ -38,6 +37,7 @@ fn install() ! {
}
osal.execute_stdout(cmd)!
console.print_header('wireguard is installed')
}
fn destroy() ! {
@@ -56,4 +56,5 @@ fn destroy() ! {
}
osal.execute_stdout(cmd)!
console.print_header('wireguard is uninstalled')
}

View File

@@ -1,11 +1,9 @@
module wireguard
module wireguard_installer
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
wireguard_installer_global map[string]&WireGuard
@@ -24,6 +22,36 @@ pub fn get(args_ ArgsGet) !&WireGuard {
return &WireGuard{}
}
@[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: 'wireguard_installer.')!
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 wireguard_installer.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action wireguard_installer.install')
install()!
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -72,3 +100,10 @@ pub fn (mut self WireGuard) destroy() ! {
pub fn switch(name string) {
wireguard_installer_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -0,0 +1,36 @@
module wireguard_installer
import freeflowuniverse.herolib.data.encoderhero
pub const version = '1.14.3'
const singleton = false
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 WireGuard {
pub mut:
name string = 'default'
}
// your checking & initialization code if needed
fn obj_init(mycfg_ WireGuard) !WireGuard {
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 WireGuard) !string {
return encoderhero.encode[WireGuard](obj)!
}
pub fn heroscript_loads(heroscript string) !WireGuard {
mut obj := encoderhero.decode[WireGuard](heroscript)!
return obj
}

View File

@@ -1,221 +1,208 @@
module yggdrasil
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 (
yggdrasil_global map[string]&YggdrasilInstaller
yggdrasil_default string
yggdrasil_global map[string]&YggdrasilInstaller
yggdrasil_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
pub fn get(args_ ArgsGet) !&YggdrasilInstaller {
return &YggdrasilInstaller{}
pub fn get(args_ ArgsGet) !&YggdrasilInstaller {
return &YggdrasilInstaller{}
}
@[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: 'yggdrasil.')!
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 yggdrasil.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action yggdrasil.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut yggdrasil_obj:=get(name:name)!
console.print_debug("action object:\n${yggdrasil_obj}")
if other_action.name == "start"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action yggdrasil.${other_action.name}")
yggdrasil_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'yggdrasil.')!
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 yggdrasil.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action yggdrasil.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut yggdrasil_obj := get(name: name)!
console.print_debug('action object:\n${yggdrasil_obj}')
if other_action.name == 'start' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action yggdrasil.${other_action.name}')
yggdrasil_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 YggdrasilInstaller) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('yggdrasil start')
console.print_header('yggdrasil 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 yggdrasil with ${zprocess.startuptype}...')
console.print_debug('starting yggdrasil 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('yggdrasil did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('yggdrasil did not install properly.')
}
pub fn (mut self YggdrasilInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self YggdrasilInstaller) 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 YggdrasilInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self YggdrasilInstaller) 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 YggdrasilInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self YggdrasilInstaller) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self YggdrasilInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for yggdrasil
// switch instance to be used for yggdrasil
pub fn switch(name string) {
yggdrasil_default = name
yggdrasil_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,51 +1,34 @@
module actrunner
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.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.osal
import freeflowuniverse.herolib.core
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: 'actrunner'
// cmd: 'actrunner server'
// env: {
// 'HOME': '/root'
// }
// }
res << zinit.ZProcessNewArgs{
name: 'actrunner'
cmd: 'actrunner daemon'
startuptype: .zinit
env: {
'HOME': '/root'
}
}
return res
}
fn running_() !bool {
mut installer := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of actrunner
// 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: 'actrunner', 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('actrunner is answering.')
fn running() !bool {
mut zinit_factory := zinit.new()!
if zinit_factory.exists('actrunner') {
is_running := zinit_factory.get('actrunner')!
println('is_running: ${is_running}')
return true
}
return false
}
@@ -64,19 +47,19 @@ 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 {
fn installed() !bool {
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res := os.execute('${osal.profile_path_source_and()!} actrunner 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 actrunner version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
res := os.execute('actrunner --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 actrunner version.\n${res.output}")
}
if texttools.version(version) == texttools.version(r[0]) {
return true
}
return false
}
@@ -87,101 +70,55 @@ fn ulist_get() !ulist.UList {
}
// uploads to S3 server if configured
fn upload_() ! {
// installers.upload(
// cmdname: 'actrunner'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/actrunner'
// )!
}
fn upload() ! {}
fn install_() ! {
fn install() ! {
console.print_header('install actrunner')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm()! {
// url = 'https://github.com/actrunner-dev/actrunner/releases/download/v${version}/actrunner_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel()! {
// url = 'https://github.com/actrunner-dev/actrunner/releases/download/v${version}/actrunner_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm()! {
// url = 'https://github.com/actrunner-dev/actrunner/releases/download/v${version}/actrunner_${version}_darwin_arm64.tar.gz'
// } else if core.is_osx_intel()! {
// url = 'https://github.com/actrunner-dev/actrunner/releases/download/v${version}/actrunner_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
mut url := ''
if core.is_linux_arm()! {
url = 'https://gitea.com/gitea/act_runner/releases/download/v${version}/act_runner-${version}-linux-arm64'
} else if core.is_linux_intel()! {
url = 'https://gitea.com/gitea/act_runner/releases/download/v${version}/act_runner-${version}-linux-amd64'
} else if core.is_osx_arm()! {
url = 'https://gitea.com/gitea/act_runner/releases/download/v${version}/act_runner-${version}-darwin-arm64'
} else if core.is_osx_intel()! {
url = 'https://gitea.com/gitea/act_runner/releases/download/v${version}/act_runner-${version}-darwin-amd64'
} else {
return error('unsported platform')
}
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/actrunner'
// )!
osal.package_install('wget') or { return error('Could not install wget due to: ${err}') }
// //dest.moveup_single_subdir()!
mut res := os.execute('sudo wget -O /usr/local/bin/actrunner ${url}')
if res.exit_code != 0 {
return error('failed to install actrunner: ${res.output}')
}
// mut binpath := dest.file_get('actrunner')!
// osal.cmd_add(
// cmdname: 'actrunner'
// source: binpath.path
// )!
res = os.execute('sudo chmod +x /usr/local/bin/actrunner')
if res.exit_code != 0 {
return error('failed to install actrunner: ${res.output}')
}
}
fn build_() ! {
// url := 'https://github.com/threefoldtech/actrunner'
fn build() ! {}
// make sure we install base on the node
// if core.platform()!= .ubuntu {
// return error('only support ubuntu for now')
// }
// golang.install()!
fn destroy() ! {
console.print_header('uninstall actrunner')
mut zinit_factory := zinit.new()!
// console.print_header('build actrunner')
if zinit_factory.exists('actrunner') {
zinit_factory.stop('actrunner') or {
return error('Could not stop actrunner service due to: ${err}')
}
zinit_factory.delete('actrunner') or {
return error('Could not delete actrunner service due to: ${err}')
}
}
// 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
// ")!
res := os.execute('sudo rm -rf /usr/local/bin/actrunner')
if res.exit_code != 0 {
return error('failed to uninstall actrunner: ${res.output}')
}
console.print_header('actrunner is uninstalled')
}

View File

@@ -1,221 +1,208 @@
module actrunner
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 (
actrunner_global map[string]&ActRunner
actrunner_default string
actrunner_global map[string]&ActRunner
actrunner_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
pub fn get(args_ ArgsGet) !&ActRunner {
return &ActRunner{}
pub fn get(args_ ArgsGet) !&ActRunner {
return &ActRunner{}
}
@[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: 'actrunner.')!
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 actrunner.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action actrunner.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut actrunner_obj:=get(name:name)!
console.print_debug("action object:\n${actrunner_obj}")
if other_action.name == "start"{
console.print_debug("install action actrunner.${other_action.name}")
actrunner_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action actrunner.${other_action.name}")
actrunner_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action actrunner.${other_action.name}")
actrunner_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'actrunner.')!
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 actrunner.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action actrunner.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut actrunner_obj := get(name: name)!
console.print_debug('action object:\n${actrunner_obj}')
if other_action.name == 'start' {
console.print_debug('install action actrunner.${other_action.name}')
actrunner_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action actrunner.${other_action.name}')
actrunner_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action actrunner.${other_action.name}')
actrunner_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 ActRunner) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('actrunner start')
console.print_header('actrunner 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 actrunner with ${zprocess.startuptype}...')
console.print_debug('starting actrunner 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('actrunner did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('actrunner did not install properly.')
}
pub fn (mut self ActRunner) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self ActRunner) 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 ActRunner) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self ActRunner) 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 ActRunner) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self ActRunner) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self ActRunner) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for actrunner
// switch instance to be used for actrunner
pub fn switch(name string) {
actrunner_default = name
actrunner_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,9 +1,8 @@
module actrunner
import freeflowuniverse.herolib.data.paramsparser
import os
import freeflowuniverse.herolib.data.encoderhero
pub const version = '0.0.0'
pub const version = '0.2.11'
const singleton = true
const default = true
@@ -14,14 +13,24 @@ pub mut:
name string = 'default'
}
fn obj_init(obj_ ActRunner) !ActRunner {
// 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_ ActRunner) !ActRunner {
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 ActRunner) !string {
return encoderhero.encode[ActRunner](obj)!
}
pub fn heroscript_loads(heroscript string) !ActRunner {
mut obj := encoderhero.decode[ActRunner](heroscript)!
return obj
}

View File

@@ -1,59 +0,0 @@
module actrunner
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import os
pub fn installl(args_ InstallArgs) ! {
mut args := args_
version := '0.2.10'
res := os.execute('${osal.profile_path_source_and()!} actrunner -v')
if res.exit_code == 0 {
r := res.output.split_into_lines().filter(it.contains('act_runner version'))
if r.len != 1 {
return error("couldn't parse actrunner version, expected 'actrunner 0' on 1 row.\n${res.output}")
}
v := texttools.version(r[0].all_after('act_runner version'))
if v < texttools.version(version) {
args.reset = true
}
} else {
args.reset = true
}
if args.reset == false {
return
}
console.print_header('install actrunner')
mut url := ''
if core.is_linux_arm()! {
url = 'https://dl.gitea.com/act_runner/${version}/act_runner-${version}-linux-arm64'
} else if core.is_linux_intel()! {
url = 'https://dl.gitea.com/act_runner/${version}/act_runner-${version}-linux-amd64'
} else if core.is_osx_arm()! {
url = 'https://dl.gitea.com/act_runner/${version}/act_runner-${version}-darwin-arm64'
} else if core.is_osx_intel()! {
url = 'https://dl.gitea.com/act_runner/${version}/act_runner-${version}-darwin-amd64'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 15000
)!
// console.print_debug(dest)
osal.cmd_add(
cmdname: 'actrunner'
source: dest.path
)!
return
}

View File

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

View File

@@ -2,82 +2,16 @@ module b2
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.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.lang.python
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
// 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()!} b2 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 b2 version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
return false
fn installed() !bool {
res := os.execute('b2 version')
return res.exit_code == 0
}
// get the Upload List of the files
@@ -87,101 +21,20 @@ fn ulist_get() !ulist.UList {
}
// uploads to S3 server if configured
fn upload_() ! {
// installers.upload(
// cmdname: 'b2'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/b2'
// )!
}
fn upload() ! {}
fn install_() ! {
fn install() ! {
console.print_header('install b2')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm()! {
// 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 py := python.new()!
py.update()!
py.pip('b2')!
// mut dest := osal.download(
// 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
// )!
osal.done_set('install_b2', 'OK')!
}
fn build_() ! {
// url := 'https://github.com/threefoldtech/b2'
// 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 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
// ")!
fn destroy() ! {
console.print_header('uninstall b2')
// mut py := python.new()! // Should be get function, skiping for now
// py.update()!
// py.pip_uninstall('b2')!
}

View File

@@ -1,11 +1,9 @@
module b2
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
b2_global map[string]&BackBase
@@ -14,6 +12,46 @@ __global (
/////////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 ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -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]
pub struct InstallArgs {
pub mut:
reset bool
}
pub fn install(args InstallArgs) ! {
if args.reset {
destroy()!
}
if !(installed_()!) {
install_()!
pub fn (mut self BackBase) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn destroy() ! {
destroy_()!
pub fn (mut self BackBase) destroy() ! {
switch(self.name)
destroy()!
}
pub fn build() ! {
build_()!
// switch instance to be used for b2
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
import freeflowuniverse.herolib.data.paramsparser
import os
import freeflowuniverse.herolib.data.encoderhero
pub const version = '0.0.0'
pub const version = '4.3.0'
const singleton = true
const default = true
@@ -14,14 +13,24 @@ pub mut:
name string = 'default'
}
fn obj_init(obj_ BackBase) !BackBase {
// 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_ BackBase) !BackBase {
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 BackBase) !string {
return encoderhero.encode[BackBase](obj)!
}
pub fn heroscript_loads(heroscript string) !BackBase {
mut obj := encoderhero.decode[BackBase](heroscript)!
return obj
}

View File

@@ -4,16 +4,71 @@ import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.installers.ulist
// import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.crypt.secrets
import os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut cfg := get()!
res << zinit.ZProcessNewArgs{
name: 'dagu'
cmd: 'dagu server'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath // config for dagu is called admin.yml and is in this dir
}
}
res << zinit.ZProcessNewArgs{
name: 'dagu_scheduler'
cmd: 'dagu scheduler'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath
}
}
return res
}
fn running() !bool {
mut cfg := get()!
url := 'http://${cfg.host}:${cfg.port}/api/v1'
mut conn := httpconnection.new(name: 'dagu', url: url)!
if cfg.secret.len > 0 {
conn.default_header.add(.authorization, 'Bearer ${cfg.secret}')
}
console.print_debug("curl -X 'GET' '${url}'/tags --oauth2-bearer ${cfg.secret}")
r := conn.get_json_dict(prefix: 'tags', debug: false) or { return false }
tags := r['Tags'] or { return false }
console.print_debug(tags)
console.print_debug('Dagu is answering.')
return true
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
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 {
res := os.execute('${osal.profile_path_source_and()!} dagu version')
res := os.execute('dagu version')
if res.exit_code == 0 {
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
if r.len != 1 {
@@ -28,6 +83,15 @@ fn installed() !bool {
return true
}
// 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{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('install daguserver')
mut url := ''
@@ -56,71 +120,6 @@ fn install() ! {
)!
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut cfg := get()!
res << zinit.ZProcessNewArgs{
name: 'dagu'
cmd: 'dagu server'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath // config for dagu is called admin.yml and is in this dir
}
}
res << zinit.ZProcessNewArgs{
name: 'dagu_scheduler'
cmd: 'dagu scheduler'
env: {
'HOME ': os.home_dir()
'DAGU_HOME ': cfg.configpath
}
}
return res
}
// user needs to us switch to make sure we get the right object
fn configure() ! {
mut cfg := get()!
if cfg.password == '' {
cfg.password = secrets.hex_secret()!
}
// TODO:use DAGU_SECRET from env variables in os if not set then empty string
if cfg.secret == '' {
cfg.secret = secrets.openssl_hex_secret(input: cfg.password)!
}
mut mycode := $tmpl('templates/dagu.yaml')
mut path := pathlib.get_file(path: '${cfg.configpath}/admin.yaml', create: true)!
path.write(mycode)!
console.print_debug(mycode)
}
fn running() !bool {
mut cfg := get()!
// this checks health of dagu
// 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: 'dagu', 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('Dagu is answering.')
return true
}
fn destroy() ! {
cmd := '
systemctl disable daguserver_scheduler.service
@@ -137,16 +136,21 @@ fn destroy() ! {
'
osal.execute_silent(cmd) or {}
}
mut zinit_factory := zinit.new()!
fn start_pre() ! {
}
if zinit_factory.exists('dagu') {
zinit_factory.stop('dagu') or { return error('Could not stop dagu service due to: ${err}') }
zinit_factory.delete('dagu') or {
return error('Could not delete dagu service due to: ${err}')
}
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
if zinit_factory.exists('dagu_scheduler') {
zinit_factory.stop('dagu_scheduler') or {
return error('Could not stop dagu_scheduler service due to: ${err}')
}
zinit_factory.delete('dagu_scheduler') or {
return error('Could not delete dagu_scheduler service due to: ${err}')
}
}
}

Some files were not shown because too many files have changed in this diff Show More