diff --git a/examples/installers/dagu.vsh b/examples/installers/dagu.vsh deleted file mode 100755 index 39704612..00000000 --- a/examples/installers/dagu.vsh +++ /dev/null @@ -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) diff --git a/examples/installers/db/meilisearch.vsh b/examples/installers/db/meilisearch.vsh new file mode 100755 index 00000000..d0a4ccfc --- /dev/null +++ b/examples/installers/db/meilisearch.vsh @@ -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()! diff --git a/examples/installers/db/postgresql.vsh b/examples/installers/db/postgresql.vsh new file mode 100755 index 00000000..7cd5d819 --- /dev/null +++ b/examples/installers/db/postgresql.vsh @@ -0,0 +1,9 @@ +#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.installers.db.postgresql as postgresql_installer + +mut db := postgresql_installer.get()! + +db.install()! +db.start()! +db.destroy()! diff --git a/examples/installers/db/zerodb.vsh b/examples/installers/db/zerodb.vsh new file mode 100755 index 00000000..924f791a --- /dev/null +++ b/examples/installers/db/zerodb.vsh @@ -0,0 +1,9 @@ +#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.installers.db.zerodb as zerodb_installer + +mut db := zerodb_installer.get()! + +db.install()! +db.start()! +db.destroy()! diff --git a/examples/installers/gitea.vsh b/examples/installers/gitea.vsh deleted file mode 100755 index 8c737731..00000000 --- a/examples/installers/gitea.vsh +++ /dev/null @@ -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()! diff --git a/examples/installers/infra/gitea.vsh b/examples/installers/infra/gitea.vsh new file mode 100755 index 00000000..64888a33 --- /dev/null +++ b/examples/installers/infra/gitea.vsh @@ -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()! diff --git a/examples/installers/infra/livekit.vsh b/examples/installers/infra/livekit.vsh new file mode 100755 index 00000000..a89e8fcc --- /dev/null +++ b/examples/installers/infra/livekit.vsh @@ -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()! diff --git a/examples/installers/infra/screen.vsh b/examples/installers/infra/screen.vsh new file mode 100755 index 00000000..a98ca4f9 --- /dev/null +++ b/examples/installers/infra/screen.vsh @@ -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()! diff --git a/examples/installers/zinit_installer.vsh b/examples/installers/infra/zinit_installer.vsh similarity index 82% rename from examples/installers/zinit_installer.vsh rename to examples/installers/infra/zinit_installer.vsh index 418a176d..ac3bce5f 100755 --- a/examples/installers/zinit_installer.vsh +++ b/examples/installers/infra/zinit_installer.vsh @@ -3,5 +3,6 @@ import freeflowuniverse.herolib.installers.infra.zinit_installer mut installer := zinit_installer.get()! -// installer.install()! +installer.install()! installer.start()! +// installer.destroy()! diff --git a/examples/installers/golang.vsh b/examples/installers/lang/golang.vsh similarity index 100% rename from examples/installers/golang.vsh rename to examples/installers/lang/golang.vsh diff --git a/examples/installers/nodejs.vsh b/examples/installers/lang/nodejs.vsh similarity index 100% rename from examples/installers/nodejs.vsh rename to examples/installers/lang/nodejs.vsh diff --git a/examples/installers/python.vsh b/examples/installers/lang/python.vsh similarity index 100% rename from examples/installers/python.vsh rename to examples/installers/lang/python.vsh diff --git a/examples/installers/lang/rust.vsh b/examples/installers/lang/rust.vsh new file mode 100755 index 00000000..cfd2a000 --- /dev/null +++ b/examples/installers/lang/rust.vsh @@ -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()! diff --git a/examples/installers/meilisearch.vsh b/examples/installers/meilisearch.vsh deleted file mode 100755 index 4f563217..00000000 --- a/examples/installers/meilisearch.vsh +++ /dev/null @@ -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()! \ No newline at end of file diff --git a/examples/installers/mycelium.vsh b/examples/installers/net/mycelium.vsh similarity index 57% rename from examples/installers/mycelium.vsh rename to examples/installers/net/mycelium.vsh index 76fa2187..bfa7e367 100755 --- a/examples/installers/mycelium.vsh +++ b/examples/installers/net/mycelium.vsh @@ -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)! diff --git a/examples/installers/net/wireguard.vsh b/examples/installers/net/wireguard.vsh new file mode 100755 index 00000000..5577ddd2 --- /dev/null +++ b/examples/installers/net/wireguard.vsh @@ -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()! diff --git a/examples/installers/postgresql.vsh b/examples/installers/postgresql.vsh deleted file mode 100755 index b826d972..00000000 --- a/examples/installers/postgresql.vsh +++ /dev/null @@ -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()! diff --git a/examples/installers/actrunner.vsh b/examples/installers/sysadmintools/actrunner.vsh similarity index 58% rename from examples/installers/actrunner.vsh rename to examples/installers/sysadmintools/actrunner.vsh index 449736b1..3b5e250f 100755 --- a/examples/installers/actrunner.vsh +++ b/examples/installers/sysadmintools/actrunner.vsh @@ -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()! diff --git a/examples/installers/sysadmintools/garage_s3.vsh b/examples/installers/sysadmintools/garage_s3.vsh new file mode 100755 index 00000000..2ec3ea00 --- /dev/null +++ b/examples/installers/sysadmintools/garage_s3.vsh @@ -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()! diff --git a/examples/installers/sysadmintools/rclone.vsh b/examples/installers/sysadmintools/rclone.vsh new file mode 100755 index 00000000..154d7840 --- /dev/null +++ b/examples/installers/sysadmintools/rclone.vsh @@ -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()! diff --git a/examples/installers/griddriver.vsh b/examples/installers/threefold/griddriver.vsh similarity index 86% rename from examples/installers/griddriver.vsh rename to examples/installers/threefold/griddriver.vsh index 7668d732..da5085c2 100755 --- a/examples/installers/griddriver.vsh +++ b/examples/installers/threefold/griddriver.vsh @@ -4,4 +4,4 @@ import freeflowuniverse.herolib.installers.threefold.griddriver mut griddriver_installer := griddriver.get()! griddriver_installer.install()! -// griddriver_installer.destroy()! +griddriver_installer.destroy()! diff --git a/examples/installers/buildah.vsh b/examples/installers/virt/buildah.vsh similarity index 100% rename from examples/installers/buildah.vsh rename to examples/installers/virt/buildah.vsh diff --git a/examples/installers/virt/dagu.vsh b/examples/installers/virt/dagu.vsh new file mode 100755 index 00000000..2b33db7d --- /dev/null +++ b/examples/installers/virt/dagu.vsh @@ -0,0 +1,9 @@ +#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.installers.sysadmintools.daguserver +import freeflowuniverse.herolib.installers.infra.zinit_installer + +mut ds := daguserver.get()! +ds.install()! +ds.start()! +ds.destroy()! diff --git a/examples/installers/docker.vsh b/examples/installers/virt/docker.vsh similarity index 100% rename from examples/installers/docker.vsh rename to examples/installers/virt/docker.vsh diff --git a/examples/installers/herocontainers.vsh b/examples/installers/virt/herocontainers.vsh similarity index 100% rename from examples/installers/herocontainers.vsh rename to examples/installers/virt/herocontainers.vsh diff --git a/examples/installers/virt/pacman.vsh b/examples/installers/virt/pacman.vsh new file mode 100755 index 00000000..14cc0307 --- /dev/null +++ b/examples/installers/virt/pacman.vsh @@ -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()! diff --git a/examples/installers/podman.vsh b/examples/installers/virt/podman.vsh similarity index 100% rename from examples/installers/podman.vsh rename to examples/installers/virt/podman.vsh diff --git a/examples/installers/dagu_server.vsh b/examples/virt/daguserver/dagu_server.vsh similarity index 100% rename from examples/installers/dagu_server.vsh rename to examples/virt/daguserver/dagu_server.vsh diff --git a/lib/clients/zerodb_client/zdb.v b/lib/clients/zerodb_client/zdb.v index d2c7bbef..e0b5ebd8 100644 --- a/lib/clients/zerodb_client/zdb.v +++ b/lib/clients/zerodb_client/zdb.v @@ -1,4 +1,4 @@ -module zdb +module zerodb_client import freeflowuniverse.herolib.core.redisclient import freeflowuniverse.herolib.ui.console @@ -14,11 +14,12 @@ pub mut: // /tmp/redis-default.sock pub fn get(addr string, auth string, namespace string) !ZDB { console.print_header(' ZDB get: addr:${addr} namespace:${namespace}') - mut redis := redisclient.get(addr)! + mut redis := redisclient.new(addr)! mut zdb := ZDB{ redis: redis } + println('Here..') if auth != '' { zdb.redis.send_expect_ok(['AUTH', auth])! } diff --git a/lib/core/generator/generic/generator.v b/lib/core/generator/generic/generator.v index 7dc56c89..7254ded4 100644 --- a/lib/core/generator/generic/generator.v +++ b/lib/core/generator/generic/generator.v @@ -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) ! { diff --git a/lib/core/generator/generic/templates/objname_factory_.vtemplate b/lib/core/generator/generic/templates/objname_factory_.vtemplate index d05454d0..53420f78 100644 --- a/lib/core/generator/generic/templates/objname_factory_.vtemplate +++ b/lib/core/generator/generic/templates/objname_factory_.vtemplate @@ -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} diff --git a/lib/core/httpconnection/connection_methods.v b/lib/core/httpconnection/connection_methods.v index ee3283d2..5c06fcaa 100644 --- a/lib/core/httpconnection/connection_methods.v +++ b/lib/core/httpconnection/connection_methods.v @@ -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 } diff --git a/lib/installers/db/meilisearchinstaller/.heroscript b/lib/installers/db/meilisearch_installer/.heroscript similarity index 66% rename from lib/installers/db/meilisearchinstaller/.heroscript rename to lib/installers/db/meilisearch_installer/.heroscript index 069a8db1..00732be1 100644 --- a/lib/installers/db/meilisearchinstaller/.heroscript +++ b/lib/installers/db/meilisearch_installer/.heroscript @@ -1,7 +1,7 @@ !!hero_code.generate_installer - name:'meilisearchinstaller' - classname:'MeilisearchServer' + name:'meilisearch_installer' + classname:'MeilisearchInstaller' singleton:0 templates:0 default:1 diff --git a/lib/installers/db/meilisearch_installer/meilisearch_installer_actions.v b/lib/installers/db/meilisearch_installer/meilisearch_installer_actions.v new file mode 100644 index 00000000..d0051092 --- /dev/null +++ b/lib/installers/db/meilisearch_installer/meilisearch_installer_actions.v @@ -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') +} diff --git a/lib/installers/db/meilisearch_installer/meilisearch_installer_factory_.v b/lib/installers/db/meilisearch_installer/meilisearch_installer_factory_.v new file mode 100644 index 00000000..765795c1 --- /dev/null +++ b/lib/installers/db/meilisearch_installer/meilisearch_installer_factory_.v @@ -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' +} diff --git a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_model.v b/lib/installers/db/meilisearch_installer/meilisearch_installer_model.v similarity index 52% rename from lib/installers/db/meilisearchinstaller/meilisearchinstaller_model.v rename to lib/installers/db/meilisearch_installer/meilisearch_installer_model.v index f6dc93f8..6fd6afdf 100644 --- a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_model.v +++ b/lib/installers/db/meilisearch_installer/meilisearch_installer_model.v @@ -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 } diff --git a/lib/installers/db/meilisearchinstaller/readme.md b/lib/installers/db/meilisearch_installer/readme.md similarity index 100% rename from lib/installers/db/meilisearchinstaller/readme.md rename to lib/installers/db/meilisearch_installer/readme.md diff --git a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions.v b/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions.v deleted file mode 100644 index 4c5fd5ae..00000000 --- a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions.v +++ /dev/null @@ -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 - // ")! -} diff --git a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions0.v b/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions0.v deleted file mode 100644 index bd670e0b..00000000 --- a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_actions0.v +++ /dev/null @@ -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(" -// // ")! -// } diff --git a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_.v b/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_.v deleted file mode 100644 index 033a0729..00000000 --- a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_.v +++ /dev/null @@ -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' -} diff --git a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_0.v b/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_0.v deleted file mode 100644 index 0cfe4ee4..00000000 --- a/lib/installers/db/meilisearchinstaller/meilisearchinstaller_factory_0.v +++ /dev/null @@ -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_()! -// } diff --git a/lib/installers/db/postgresql/postgresql_actions.v b/lib/installers/db/postgresql/postgresql_actions.v index 1f9d0a3a..bc3ba663 100644 --- a/lib/installers/db/postgresql/postgresql_actions.v +++ b/lib/installers/db/postgresql/postgresql_actions.v @@ -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') } diff --git a/lib/installers/db/postgresql/postgresql_db.v b/lib/installers/db/postgresql/postgresql_db.v deleted file mode 100644 index f024f13e..00000000 --- a/lib/installers/db/postgresql/postgresql_db.v +++ /dev/null @@ -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.') - } -} diff --git a/lib/installers/db/postgresql/postgresql_factory_.v b/lib/installers/db/postgresql/postgresql_factory_.v index 4c23c67f..36463d61 100644 --- a/lib/installers/db/postgresql/postgresql_factory_.v +++ b/lib/installers/db/postgresql/postgresql_factory_.v @@ -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' } diff --git a/lib/installers/db/postgresql/postgresql_model.v b/lib/installers/db/postgresql/postgresql_model.v index 3b0856c3..61142115 100644 --- a/lib/installers/db/postgresql/postgresql_model.v +++ b/lib/installers/db/postgresql/postgresql_model.v @@ -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 +} diff --git a/lib/installers/db/zerodb/.heroscript b/lib/installers/db/zerodb/.heroscript index 63f693bc..e36bb19f 100644 --- a/lib/installers/db/zerodb/.heroscript +++ b/lib/installers/db/zerodb/.heroscript @@ -1,7 +1,7 @@ !!hero_code.generate_installer name: "zerodb" classname: "ZeroDB" - hasconfig: false + hasconfig: true singleton: true default: true title: "" @@ -9,4 +9,3 @@ build: true startupmanager: true supported_platforms: "" - diff --git a/lib/installers/db/zerodb/zdb_builder.v b/lib/installers/db/zerodb/zdb_builder.v deleted file mode 100644 index 601a0ba7..00000000 --- a/lib/installers/db/zerodb/zdb_builder.v +++ /dev/null @@ -1,29 +0,0 @@ -module zdb - -import freeflowuniverse.herolib.develop.gittools -import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.installers.base -import freeflowuniverse.herolib.ui.console - -// install zdb will return true if it was already installed -pub fn build_() ! { - base.install()! - console.print_header('package_install install zdb') - if !osal.done_exists('install_zdb') && !osal.cmd_exists('zdb') { - mut gs := gittools.new()! - mut repo := gs.get_repo( - url: 'git@github.com:threefoldtech/0-db.git' - reset: false - pull: true - )! - path := repo.path() - cmd := ' - set -ex - cd ${path} - make - sudo rsync -rav ${path}/bin/zdb* /usr/local/bin/ - ' - osal.execute_silent(cmd) or { return error('Cannot install zdb.\n${err}') } - osal.done_set('install_zdb', 'OK')! - } -} diff --git a/lib/installers/db/zerodb/zdb_installer.v b/lib/installers/db/zerodb/zdb_installer.v deleted file mode 100644 index 6ecfae89..00000000 --- a/lib/installers/db/zerodb/zdb_installer.v +++ /dev/null @@ -1,156 +0,0 @@ -module zdb - -import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.core.texttools -import freeflowuniverse.herolib.core.pathlib -import freeflowuniverse.herolib.core.httpconnection -import freeflowuniverse.herolib.crypt.secrets -import freeflowuniverse.herolib.sysadmin.startupmanager -import freeflowuniverse.herolib.clients.zdb -import os -import time - -@[params] -pub struct InstallArgs { -pub mut: - reset bool - secret string - start bool = true - restart bool - sequential bool // if sequential then we autoincrement the keys - datadir string = '${os.home_dir()}/var/zdb/data' - indexdir string = '${os.home_dir()}/var/zdb/index' - rotateperiod int = 1200 // 20 min -} - -pub fn install_(args_ InstallArgs) ! { - mut args := args_ - version := '2.0.7' - - res := os.execute('${osal.profile_path_source_and()!} zdb --version') - if res.exit_code == 0 { - r := res.output.split_into_lines().filter(it.trim_space().len > 0) - if r.len != 3 { - return error("couldn't parse zdb version.\n${res.output}") - } - myversion := r[1].all_after_first('server, v').all_before_last('(').trim_space() - if texttools.version(version) > texttools.version(myversion) { - args.reset = true - } - } else { - args.reset = true - } - - if args.reset { - console.print_header('install zdb') - - mut url := '' - if core.is_linux_intel()! { - url = 'https://github.com/threefoldtech/0-db/releases/download/v${version}/zdb-${version}-linux-amd64-static' - } else { - return error('unsported platform, only linux 64 for zdb for now') - } - - mut dest := osal.download( - url: url - minsize_kb: 1000 - )! - - osal.cmd_add( - cmdname: 'zdb' - source: dest.path - )! - } - - if args.restart { - restart(args)! - return - } - - if args.start { - start(args)! - } -} - -pub fn restart(args_ InstallArgs) ! { - stop(args_)! - start(args_)! -} - -pub fn stop(args_ InstallArgs) ! { - console.print_header('zdb stop') - mut sm := startupmanager.get()! - sm.stop('zdb')! -} - -pub fn start(args_ InstallArgs) ! { - mut args := args_ - - console.print_header('zdb start') - - mut box := secrets.get()! - secret := box.secret(key: 'ZDB.SECRET', default: args.secret)! - - mut sm := startupmanager.get()! - - mut cmd := 'zdb --socket ${os.home_dir()}/hero/var/zdb.sock --port 3355 --admin ${secret} --data ${args.datadir} --index ${args.indexdir} --dualnet --protect --rotate ${args.rotateperiod}' - if args.sequential { - cmd += ' --mode seq' - } - - pathlib.get_dir(path: '${os.home_dir()}/hero/var', create: true)! - - sm.start( - name: 'zdb' - cmd: cmd - )! - - console.print_debug(cmd) - - for _ in 0 .. 50 { - if check()! { - return - } - time.sleep(10 * time.millisecond) - } - return error('zdb not installed properly, check failed.') -} - -pub fn check() !bool { - cmd := 'redis-cli -s /root/hero/var/zdb.sock PING' - - result := os.execute(cmd) - if result.exit_code > 0 { - return error('${cmd} failed with exit code: ${result.exit_code} and error: ${result.output}') - } - - if result.output.trim_space() == 'PONG' { - console.print_debug('zdb is answering.') - // return true - } - - // TODO: need to work on socket version - // mut db := zdb.get('${os.home_dir()}/hero/var/zdb.sock', secret()!, 'test')! - mut db := client()! - - // check info returns info about zdb - info := db.info()! - // console.print_debug(info) - - assert info.contains('server_name: 0-db') - - console.print_debug('zdb is answering.') - return true -} - -pub fn secret() !string { - mut box := secrets.get()! - secret := box.get('ZDB.SECRET')! - return secret -} - -pub fn client() !ZDB { - mut db := zdb.get('localhost:3355', secret()!, 'test')! - return db -} diff --git a/lib/installers/db/zerodb/zdb_test.v b/lib/installers/db/zerodb/zdb_test.v deleted file mode 100644 index 2286d7c4..00000000 --- a/lib/installers/db/zerodb/zdb_test.v +++ /dev/null @@ -1,22 +0,0 @@ -module zdb - -import freeflowuniverse.herolib.clients.zdb - -fn test_get() { - // must set unix domain with --socket argument when running zdb - // run zdb as following: - // mkdir -p ~/.zdb/ && zdb --socket ~/.zdb/socket --admin 1234 - install(secret: 'hamada', start: true) or { panic(err) } - - mut client := zdb.get('/root/hero/var/zdb.sock', 'hamada', 'test') or { panic(err) } - - // check info returns info about zdb - info := client.info()! - assert info.contains('server_name: 0-db') - - nslist := client.nslist()! - assert nslist == ['default', 'test'] - - nsinfo := client.nsinfo('default')! - assert nsinfo['name'] == 'default' -} diff --git a/lib/installers/db/zerodb/zerodb_actions.v b/lib/installers/db/zerodb/zerodb_actions.v index a7db9c95..05f86657 100644 --- a/lib/installers/db/zerodb/zerodb_actions.v +++ b/lib/installers/db/zerodb/zerodb_actions.v @@ -2,51 +2,58 @@ module zerodb import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.core.texttools -import freeflowuniverse.herolib.core.pathlib -import freeflowuniverse.herolib.osal.systemd +import freeflowuniverse.herolib.core import freeflowuniverse.herolib.osal.zinit import freeflowuniverse.herolib.installers.ulist -import freeflowuniverse.herolib.installers.lang.golang -import freeflowuniverse.herolib.installers.lang.rust -import freeflowuniverse.herolib.installers.lang.python +import freeflowuniverse.herolib.develop.gittools +import freeflowuniverse.herolib.installers.base +import freeflowuniverse.herolib.crypt.secrets +import freeflowuniverse.herolib.clients.zerodb_client +import crypto.md5 +import rand import os +import time fn startupcmd() ![]zinit.ZProcessNewArgs { - mut installer := get()! - mut res := []zinit.ZProcessNewArgs{} - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // res << zinit.ZProcessNewArgs{ - // name: 'zerodb' - // cmd: 'zerodb server' - // env: { - // 'HOME': '/root' - // } - // } + mut cfg := get()! + mut cmd := 'zdb --socket ${os.home_dir()}/var/zdb.sock --port ${cfg.port} --admin ${cfg.secret} --data ${cfg.datadir} --index ${cfg.indexdir} --dualnet --protect --rotate ${cfg.rotateperiod}' + if cfg.sequential { + cmd += ' --mode seq' + } + mut res := []zinit.ZProcessNewArgs{} + res << zinit.ZProcessNewArgs{ + name: 'zdb' + cmd: cmd + startuptype: .zinit + } return res } -fn running_() !bool { - mut installer := get()! - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // this checks health of zerodb - // curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works - // url:='http://127.0.0.1:${cfg.port}/api/v1' - // mut conn := httpconnection.new(name: 'zerodb', url: url)! +fn running() !bool { + time.sleep(time.second * 2) + cfg := get()! + cmd := 'redis-cli -s ${os.home_dir()}/var/zdb.sock PING' - // if cfg.secret.len > 0 { - // conn.default_header.add(.authorization, 'Bearer ${cfg.secret}') - // } - // conn.default_header.add(.content_type, 'application/json') - // console.print_debug("curl -X 'GET' '${url}'/tags --oauth2-bearer ${cfg.secret}") - // r := conn.get_json_dict(prefix: 'tags', debug: false) or {return false} - // println(r) - // if true{panic("ssss")} - // tags := r['Tags'] or { return false } - // console.print_debug(tags) - // console.print_debug('zerodb is answering.') - return false + result := os.execute(cmd) + if result.exit_code > 0 { + return error('${cmd} failed with exit code: ${result.exit_code} and error: ${result.output}') + } + + if result.output.trim_space() == 'PONG' { + console.print_debug('zdb is answering.') + return true + } + + mut db := zerodb_client.get('localhost:${cfg.port}', cfg.secret, 'test')! + + // check info returns info about zdb + info := db.info()! + + assert info.contains('server_name: 0-db') + + console.print_debug('zdb is answering.') + return true } fn start_pre() ! { @@ -64,20 +71,9 @@ fn stop_post() ! { //////////////////// following actions are not specific to instance of the object // checks if a certain version or above is installed -fn installed_() !bool { - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // res := os.execute('${osal.profile_path_source_and()!} zerodb version') - // if res.exit_code != 0 { - // return false - // } - // r := res.output.split_into_lines().filter(it.trim_space().len > 0) - // if r.len != 1 { - // return error("couldn't parse zerodb version.\n${res.output}") - // } - // if texttools.version(version) == texttools.version(r[0]) { - // return true - // } - return false +fn installed() !bool { + res := os.execute('zdb --version') + return res.exit_code == 0 } // get the Upload List of the files @@ -87,101 +83,68 @@ fn ulist_get() !ulist.UList { } // uploads to S3 server if configured -fn upload_() ! { +fn upload() ! { // installers.upload( // cmdname: 'zerodb' // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/zerodb' // )! } -fn install_() ! { - console.print_header('install zerodb') - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // mut url := '' - // if core.is_linux_arm()! { - // url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_linux_arm64.tar.gz' - // } else if core.is_linux_intel()! { - // url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_linux_amd64.tar.gz' - // } else if core.is_osx_arm()! { - // url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_darwin_arm64.tar.gz' - // } else if core.is_osx_intel()! { - // url = 'https://github.com/zerodb-dev/zerodb/releases/download/v${version}/zerodb_${version}_darwin_amd64.tar.gz' - // } else { - // return error('unsported platform') - // } +fn install() ! { + console.print_header('install zdb') - // mut dest := osal.download( - // url: url - // minsize_kb: 9000 - // expand_dir: '/tmp/zerodb' - // )! + mut url := '' + if core.is_linux_intel()! { + url = 'https://github.com/threefoldtech/0-db/releases/download/v${version}/zdb-${version}-linux-amd64-static' + } else { + return error('unsported platform, only linux 64 for zdb for now') + } - // //dest.moveup_single_subdir()! + mut dest := osal.download( + url: url + minsize_kb: 1000 + )! - // mut binpath := dest.file_get('zerodb')! - // osal.cmd_add( - // cmdname: 'zerodb' - // source: binpath.path - // )! + osal.cmd_add( + cmdname: 'zdb' + source: dest.path + )! } -fn build_() ! { - // url := 'https://github.com/threefoldtech/zerodb' - - // make sure we install base on the node - // if core.platform()!= .ubuntu { - // return error('only support ubuntu for now') - // } - // golang.install()! - - // console.print_header('build zerodb') - - // gitpath := gittools.get_repo(coderoot: '/tmp/builder', url: url, reset: true, pull: true)! - - // cmd := ' - // cd ${gitpath} - // source ~/.cargo/env - // exit 1 #todo - // ' - // osal.execute_stdout(cmd)! - // - // //now copy to the default bin path - // mut binpath := dest.file_get('...')! - // adds it to path - // osal.cmd_add( - // cmdname: 'griddriver2' - // source: binpath.path - // )! +fn build() ! { + base.install()! + console.print_header('package_install install zdb') + if !osal.done_exists('install_zdb') && !osal.cmd_exists('zdb') { + mut gs := gittools.new()! + mut repo := gs.get_repo( + url: 'git@github.com:threefoldtech/0-db.git' + reset: false + pull: true + )! + path := repo.path() + cmd := ' + set -ex + cd ${path} + make + sudo rsync -rav ${path}/bin/zdb* /usr/local/bin/ + ' + osal.execute_silent(cmd) or { return error('Cannot install zdb.\n${err}') } + osal.done_set('install_zdb', 'OK')! + } } -fn destroy_() ! { - // mut systemdfactory := systemd.new()! - // systemdfactory.destroy("zinit")! +fn destroy() ! { + res := os.execute('sudo rm -rf /usr/local/bin/zdb') + if res.exit_code != 0 { + return error('Could not remove zdb binary due to: ${res.output}') + } - // osal.process_kill_recursive(name:'zinit')! - // osal.cmd_delete('zinit')! - - // osal.package_remove(' - // podman - // conmon - // buildah - // skopeo - // runc - // ')! - - // //will remove all paths where go/bin is found - // osal.profile_path_add_remove(paths2delete:"go/bin")! - - // osal.rm(" - // podman - // conmon - // buildah - // skopeo - // runc - // /var/lib/containers - // /var/lib/podman - // /var/lib/buildah - // /tmp/podman - // /tmp/conmon - // ")! + mut zinit_factory := zinit.new()! + if zinit_factory.exists('zdb') { + zinit_factory.stop('zdb') or { return error('Could not stop zdb service due to: ${err}') } + zinit_factory.delete('zdb') or { + return error('Could not delete zdb service due to: ${err}') + } + } + console.print_header('zdb removed') } diff --git a/lib/installers/db/zerodb/zerodb_factory_.v b/lib/installers/db/zerodb/zerodb_factory_.v index f2dde44e..0efcc962 100644 --- a/lib/installers/db/zerodb/zerodb_factory_.v +++ b/lib/installers/db/zerodb/zerodb_factory_.v @@ -3,219 +3,277 @@ module zerodb import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.data.paramsparser - import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.osal.zinit import time __global ( - zerodb_global map[string]&ZeroDB - zerodb_default string + zerodb_global map[string]&ZeroDB + zerodb_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&ZeroDB { - return &ZeroDB{} +fn args_get(args_ ArgsGet) ArgsGet { + mut args := args_ + if args.name == '' { + args.name = 'default' + } + return args +} + +pub fn get(args_ ArgsGet) !&ZeroDB { + mut context := base.context()! + mut args := args_get(args_) + mut obj := ZeroDB{} + if args.name !in zerodb_global { + if !exists(args)! { + set(obj)! + } else { + heroscript := context.hero_config_get('zerodb', args.name)! + mut obj_ := heroscript_loads(heroscript)! + set_in_mem(obj_)! + } + } + return zerodb_global[args.name] or { + println(zerodb_global) + // bug if we get here because should be in globals + panic('could not get config for zerodb with name, is bug:${args.name}') + } +} + +// register the config for the future +pub fn set(o ZeroDB) ! { + set_in_mem(o)! + mut context := base.context()! + heroscript := heroscript_dumps(o)! + context.hero_config_set('zerodb', o.name, heroscript)! +} + +// does the config exists? +pub fn exists(args_ ArgsGet) !bool { + mut context := base.context()! + mut args := args_get(args_) + return context.hero_config_exists('zerodb', args.name) +} + +pub fn delete(args_ ArgsGet) ! { + mut args := args_get(args_) + mut context := base.context()! + context.hero_config_delete('zerodb', args.name)! + if args.name in zerodb_global { + // del zerodb_global[args.name] + } +} + +// only sets in mem, does not set as config +fn set_in_mem(o ZeroDB) ! { + mut o2 := obj_init(o)! + zerodb_global[o.name] = &o2 + zerodb_default = o.name } @[params] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + heroscript string // if filled in then plbook will be made out of it + plbook ?playbook.PlayBook + reset bool } pub fn play(args_ PlayArgs) ! { - - mut args:=args_ + mut args := args_ - mut plbook := args.plbook or { - playbook.new(text: args.heroscript)! - } - + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut other_actions := plbook.find(filter: 'zerodb.')! - for other_action in other_actions { - if other_action.name in ["destroy","install","build"]{ - mut p := other_action.params - reset:=p.get_default_false("reset") - if other_action.name == "destroy" || reset{ - console.print_debug("install action zerodb.destroy") - destroy()! - } - if other_action.name == "install"{ - console.print_debug("install action zerodb.install") - install()! - } - } - if other_action.name in ["start","stop","restart"]{ - mut p := other_action.params - name := p.get('name')! - mut zerodb_obj:=get(name:name)! - console.print_debug("action object:\n${zerodb_obj}") - if other_action.name == "start"{ - console.print_debug("install action zerodb.${other_action.name}") - zerodb_obj.start()! - } + mut install_actions := plbook.find(filter: 'zerodb.configure')! + if install_actions.len > 0 { + for install_action in install_actions { + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! + } + } - if other_action.name == "stop"{ - console.print_debug("install action zerodb.${other_action.name}") - zerodb_obj.stop()! - } - if other_action.name == "restart"{ - console.print_debug("install action zerodb.${other_action.name}") - zerodb_obj.restart()! - } - } - } + mut other_actions := plbook.find(filter: 'zerodb.')! + for other_action in other_actions { + if other_action.name in ['destroy', 'install', 'build'] { + mut p := other_action.params + reset := p.get_default_false('reset') + if other_action.name == 'destroy' || reset { + console.print_debug('install action zerodb.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action zerodb.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut zerodb_obj := get(name: name)! + console.print_debug('action object:\n${zerodb_obj}') + if other_action.name == 'start' { + console.print_debug('install action zerodb.${other_action.name}') + zerodb_obj.start()! + } + if other_action.name == 'stop' { + console.print_debug('install action zerodb.${other_action.name}') + zerodb_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action zerodb.${other_action.name}') + zerodb_obj.restart()! + } + } + } } - //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { - // unknown - // screen - // zinit - // tmux - // systemd - match cat{ - .zinit{ - console.print_debug("startupmanager: zinit") - return startupmanager.get(cat:.zinit)! - } - .systemd{ - console.print_debug("startupmanager: systemd") - return startupmanager.get(cat:.systemd)! - }else{ - console.print_debug("startupmanager: auto") - return startupmanager.get()! - } - } + // unknown + // screen + // zinit + // tmux + // systemd + match cat { + .zinit { + console.print_debug('startupmanager: zinit') + return startupmanager.get(cat: .zinit)! + } + .systemd { + console.print_debug('startupmanager: systemd') + return startupmanager.get(cat: .systemd)! + } + else { + console.print_debug('startupmanager: auto') + return startupmanager.get()! + } + } } +// load from disk and make sure is properly intialized +pub fn (mut self ZeroDB) reload() ! { + switch(self.name) + self = obj_init(self)! +} pub fn (mut self ZeroDB) start() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('zerodb start') + console.print_header('zerodb start') - if ! installed()!{ - install()! - } + if !installed()! { + install()! + } - configure()! + configure()! - start_pre()! + start_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! - console.print_debug('starting zerodb with ${zprocess.startuptype}...') + console.print_debug('starting zerodb with ${zprocess.startuptype}...') - sm.new(zprocess)! + sm.new(zprocess)! - sm.start(zprocess.name)! - } + sm.start(zprocess.name)! + } - start_post()! - - for _ in 0 .. 50 { - if self.running()! { - return - } - time.sleep(100 * time.millisecond) - } - return error('zerodb did not install properly.') + start_post()! + for _ in 0 .. 50 { + if self.running()! { + return + } + time.sleep(100 * time.millisecond) + } + return error('zerodb did not install properly.') } pub fn (mut self ZeroDB) install_start(args InstallArgs) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self ZeroDB) stop() ! { - switch(self.name) - stop_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - sm.stop(zprocess.name)! - } - stop_post()! + switch(self.name) + stop_pre()! + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! + sm.stop(zprocess.name)! + } + stop_post()! } pub fn (mut self ZeroDB) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self ZeroDB) running() !bool { - switch(self.name) + switch(self.name) - //walk over the generic processes, if not running return - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - r:=sm.running(zprocess.name)! - if r==false{ - return false - } - } - return running()! + // walk over the generic processes, if not running return + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! + r := sm.running(zprocess.name)! + if r == false { + return false + } + } + return running()! } @[params] -pub struct InstallArgs{ +pub struct InstallArgs { pub mut: - reset bool + reset bool } pub fn (mut self ZeroDB) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self ZeroDB) build() ! { - switch(self.name) - build()! + switch(self.name) + build()! } pub fn (mut self ZeroDB) destroy() ! { - switch(self.name) - self.stop() or {} - destroy()! + switch(self.name) + self.stop() or {} + destroy()! } - - -//switch instance to be used for zerodb +// switch instance to be used for zerodb pub fn switch(name string) { - zerodb_default = name + zerodb_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/db/zerodb/zerodb_model.v b/lib/installers/db/zerodb/zerodb_model.v index 8c105b44..4ef18790 100644 --- a/lib/installers/db/zerodb/zerodb_model.v +++ b/lib/installers/db/zerodb/zerodb_model.v @@ -1,27 +1,71 @@ module zerodb -import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.encoderhero import os +import rand +import crypto.md5 +import freeflowuniverse.herolib.crypt.secrets -pub const version = '0.0.0' +pub const version = '2.0.7' const singleton = true const default = true -// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED @[heap] pub struct ZeroDB { pub mut: - name string = 'default' + name string = 'default' + secret string @[secret] + sequential bool // if sequential then we autoincrement the keys + datadir string = '${os.home_dir()}/var/zdb/data' + indexdir string = '${os.home_dir()}/var/zdb/index' + rotateperiod int = 1200 // 20 min + port int = 3355 } -fn obj_init(obj_ ZeroDB) !ZeroDB { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ - panic('implement') - return obj +// your checking & initialization code if needed +fn obj_init(mycfg_ ZeroDB) !ZeroDB { + mut mycfg := mycfg_ + if mycfg.name == '' { + mycfg.name = 'default' + } + + if mycfg.secret == '' { + secret := md5.hexhash(rand.string(16)) + mut box := secrets.get(secret: secret)! + mycfg.secret = box.encrypt(secret)! + } + + if mycfg.datadir == '' { + mycfg.datadir = '${os.home_dir()}/var/zdb/data' + } + + if mycfg.indexdir == '' { + mycfg.indexdir = '${os.home_dir()}/var/zdb/index' + } + + if mycfg.rotateperiod == 0 { + mycfg.rotateperiod = 1200 + } + + if mycfg.port == 0 { + mycfg.port = 3355 + } + + return mycfg } // called before start if done fn configure() ! { // mut installer := get()! } + +/////////////NORMALLY NO NEED TO TOUCH + +pub fn heroscript_dumps(obj ZeroDB) !string { + return encoderhero.encode[ZeroDB](obj)! +} + +pub fn heroscript_loads(heroscript string) !ZeroDB { + mut obj := encoderhero.decode[ZeroDB](heroscript)! + return obj +} diff --git a/lib/installers/db/zerofs/zerofs_factory_.v b/lib/installers/db/zerofs/zerofs_factory_.v index c60f1fb1..5dbd9ee2 100644 --- a/lib/installers/db/zerofs/zerofs_factory_.v +++ b/lib/installers/db/zerofs/zerofs_factory_.v @@ -1,221 +1,208 @@ module zerofs -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.data.paramsparser - import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.osal.zinit import time __global ( - zerofs_global map[string]&ZeroFS - zerofs_default string + zerofs_global map[string]&ZeroFS + zerofs_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&ZeroFS { - return &ZeroFS{} +pub fn get(args_ ArgsGet) !&ZeroFS { + return &ZeroFS{} } @[params] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + heroscript string // if filled in then plbook will be made out of it + plbook ?playbook.PlayBook + reset bool } pub fn play(args_ PlayArgs) ! { - - mut args:=args_ + mut args := args_ - mut plbook := args.plbook or { - playbook.new(text: args.heroscript)! - } - + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut other_actions := plbook.find(filter: 'zerofs.')! - for other_action in other_actions { - if other_action.name in ["destroy","install","build"]{ - mut p := other_action.params - reset:=p.get_default_false("reset") - if other_action.name == "destroy" || reset{ - console.print_debug("install action zerofs.destroy") - destroy()! - } - if other_action.name == "install"{ - console.print_debug("install action zerofs.install") - install()! - } - } - if other_action.name in ["start","stop","restart"]{ - mut p := other_action.params - name := p.get('name')! - mut zerofs_obj:=get(name:name)! - console.print_debug("action object:\n${zerofs_obj}") - if other_action.name == "start"{ - console.print_debug("install action zerofs.${other_action.name}") - zerofs_obj.start()! - } - - if other_action.name == "stop"{ - console.print_debug("install action zerofs.${other_action.name}") - zerofs_obj.stop()! - } - if other_action.name == "restart"{ - console.print_debug("install action zerofs.${other_action.name}") - zerofs_obj.restart()! - } - } - } + mut other_actions := plbook.find(filter: 'zerofs.')! + for other_action in other_actions { + if other_action.name in ['destroy', 'install', 'build'] { + mut p := other_action.params + reset := p.get_default_false('reset') + if other_action.name == 'destroy' || reset { + console.print_debug('install action zerofs.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action zerofs.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut zerofs_obj := get(name: name)! + console.print_debug('action object:\n${zerofs_obj}') + if other_action.name == 'start' { + console.print_debug('install action zerofs.${other_action.name}') + zerofs_obj.start()! + } + if other_action.name == 'stop' { + console.print_debug('install action zerofs.${other_action.name}') + zerofs_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action zerofs.${other_action.name}') + zerofs_obj.restart()! + } + } + } } - //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { - // unknown - // screen - // zinit - // tmux - // systemd - match cat{ - .zinit{ - console.print_debug("startupmanager: zinit") - return startupmanager.get(cat:.zinit)! - } - .systemd{ - console.print_debug("startupmanager: systemd") - return startupmanager.get(cat:.systemd)! - }else{ - console.print_debug("startupmanager: auto") - return startupmanager.get()! - } - } + // unknown + // screen + // zinit + // tmux + // systemd + match cat { + .zinit { + console.print_debug('startupmanager: zinit') + return startupmanager.get(cat: .zinit)! + } + .systemd { + console.print_debug('startupmanager: systemd') + return startupmanager.get(cat: .systemd)! + } + else { + console.print_debug('startupmanager: auto') + return startupmanager.get()! + } + } } - pub fn (mut self ZeroFS) start() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('zerofs start') + console.print_header('zerofs start') - if ! installed()!{ - install()! - } + if !installed()! { + install()! + } - configure()! + configure()! - start_pre()! + start_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! - console.print_debug('starting zerofs with ${zprocess.startuptype}...') + console.print_debug('starting zerofs with ${zprocess.startuptype}...') - sm.new(zprocess)! + sm.new(zprocess)! - sm.start(zprocess.name)! - } + sm.start(zprocess.name)! + } - start_post()! - - for _ in 0 .. 50 { - if self.running()! { - return - } - time.sleep(100 * time.millisecond) - } - return error('zerofs did not install properly.') + start_post()! + for _ in 0 .. 50 { + if self.running()! { + return + } + time.sleep(100 * time.millisecond) + } + return error('zerofs did not install properly.') } pub fn (mut self ZeroFS) install_start(args InstallArgs) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self ZeroFS) stop() ! { - switch(self.name) - stop_pre()! - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - sm.stop(zprocess.name)! - } - stop_post()! + switch(self.name) + stop_pre()! + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! + sm.stop(zprocess.name)! + } + stop_post()! } pub fn (mut self ZeroFS) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self ZeroFS) running() !bool { - switch(self.name) + switch(self.name) - //walk over the generic processes, if not running return - for zprocess in startupcmd()!{ - mut sm:=startupmanager_get(zprocess.startuptype)! - r:=sm.running(zprocess.name)! - if r==false{ - return false - } - } - return running()! + // walk over the generic processes, if not running return + for zprocess in startupcmd()! { + mut sm := startupmanager_get(zprocess.startuptype)! + r := sm.running(zprocess.name)! + if r == false { + return false + } + } + return running()! } @[params] -pub struct InstallArgs{ +pub struct InstallArgs { pub mut: - reset bool + reset bool } pub fn (mut self ZeroFS) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self ZeroFS) build() ! { - switch(self.name) - build()! + switch(self.name) + build()! } pub fn (mut self ZeroFS) destroy() ! { - switch(self.name) - self.stop() or {} - destroy()! + switch(self.name) + self.stop() or {} + destroy()! } - - -//switch instance to be used for zerofs +// switch instance to be used for zerofs pub fn switch(name string) { - zerofs_default = name + zerofs_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/infra/coredns/coredns_factory_.v b/lib/installers/infra/coredns/coredns_factory_.v index d39772bf..ac017843 100644 --- a/lib/installers/infra/coredns/coredns_factory_.v +++ b/lib/installers/infra/coredns/coredns_factory_.v @@ -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' } diff --git a/lib/installers/infra/gitea/gitea_factory_.v b/lib/installers/infra/gitea/gitea_factory_.v index 55014dd2..f928a6c6 100644 --- a/lib/installers/infra/gitea/gitea_factory_.v +++ b/lib/installers/infra/gitea/gitea_factory_.v @@ -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' } diff --git a/lib/installers/infra/gitea/gitea_model.v b/lib/installers/infra/gitea/gitea_model.v index e26f97ec..ccd97edd 100644 --- a/lib/installers/infra/gitea/gitea_model.v +++ b/lib/installers/infra/gitea/gitea_model.v @@ -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 } diff --git a/lib/installers/infra/gitea/installer.v b/lib/installers/infra/gitea/installer.v deleted file mode 100644 index 0b8758ab..00000000 --- a/lib/installers/infra/gitea/installer.v +++ /dev/null @@ -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.') -// } diff --git a/lib/installers/infra/gitea/server.v b/lib/installers/infra/gitea/server.v deleted file mode 100644 index 3080ca0b..00000000 --- a/lib/installers/infra/gitea/server.v +++ /dev/null @@ -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()! -// } diff --git a/lib/installers/infra/livekit/livekit_actions.v b/lib/installers/infra/livekit/livekit_actions.v index 8f5efd8a..d0130b11 100644 --- a/lib/installers/infra/livekit/livekit_actions.v +++ b/lib/installers/infra/livekit/livekit_actions.v @@ -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') } diff --git a/lib/installers/infra/livekit/livekit_factory_.v b/lib/installers/infra/livekit/livekit_factory_.v index 5454ae41..41e6c6f9 100644 --- a/lib/installers/infra/livekit/livekit_factory_.v +++ b/lib/installers/infra/livekit/livekit_factory_.v @@ -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' } diff --git a/lib/installers/infra/livekit/livekit_model.v b/lib/installers/infra/livekit/livekit_model.v index 497cadf6..e7a0c4fb 100644 --- a/lib/installers/infra/livekit/livekit_model.v +++ b/lib/installers/infra/livekit/livekit_model.v @@ -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 +} diff --git a/lib/installers/infra/livekit/templates/config.yaml b/lib/installers/infra/livekit/templates/config.yaml index fafc4e47..738c99ed 100644 --- a/lib/installers/infra/livekit/templates/config.yaml +++ b/lib/installers/infra/livekit/templates/config.yaml @@ -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} diff --git a/lib/installers/infra/screen/screen_actions.v b/lib/installers/infra/screen/screen_actions.v index 616be036..515a36ba 100644 --- a/lib/installers/infra/screen/screen_actions.v +++ b/lib/installers/infra/screen/screen_actions.v @@ -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') diff --git a/lib/installers/infra/screen/screen_factory_.v b/lib/installers/infra/screen/screen_factory_.v index 4b3a06fb..e75089ef 100644 --- a/lib/installers/infra/screen/screen_factory_.v +++ b/lib/installers/infra/screen/screen_factory_.v @@ -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' } diff --git a/lib/installers/infra/screen/screen_model.v b/lib/installers/infra/screen/screen_model.v index 2c727f13..16c13d62 100644 --- a/lib/installers/infra/screen/screen_model.v +++ b/lib/installers/infra/screen/screen_model.v @@ -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 +} diff --git a/lib/installers/infra/zinit_installer/zinit_installer_actions.v b/lib/installers/infra/zinit_installer/zinit_installer_actions.v index 524349cc..da73fcfd 100644 --- a/lib/installers/infra/zinit_installer/zinit_installer_actions.v +++ b/lib/installers/infra/zinit_installer/zinit_installer_actions.v @@ -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')! } diff --git a/lib/installers/infra/zinit_installer/zinit_installer_factory_.v b/lib/installers/infra/zinit_installer/zinit_installer_factory_.v index 9250fcf7..d3e6b21b 100644 --- a/lib/installers/infra/zinit_installer/zinit_installer_factory_.v +++ b/lib/installers/infra/zinit_installer/zinit_installer_factory_.v @@ -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 diff --git a/lib/installers/infra/zinit_installer/zinit_installer_model.v b/lib/installers/infra/zinit_installer/zinit_installer_model.v index 74fcbb0f..458d0bc9 100644 --- a/lib/installers/infra/zinit_installer/zinit_installer_model.v +++ b/lib/installers/infra/zinit_installer/zinit_installer_model.v @@ -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 } diff --git a/lib/installers/lang/python/python_actions.v b/lib/installers/lang/python/python_actions.v index 9d65f6ec..6414118b 100644 --- a/lib/installers/lang/python/python_actions.v +++ b/lib/installers/lang/python/python_actions.v @@ -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 diff --git a/lib/installers/lang/rust/rust_actions.v b/lib/installers/lang/rust/rust_actions.v index cc19be4b..ed8b8db2 100644 --- a/lib/installers/lang/rust/rust_actions.v +++ b/lib/installers/lang/rust/rust_actions.v @@ -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: ' diff --git a/lib/installers/lang/rust/rust_factory_.v b/lib/installers/lang/rust/rust_factory_.v index db4434c4..0eef53de 100644 --- a/lib/installers/lang/rust/rust_factory_.v +++ b/lib/installers/lang/rust/rust_factory_.v @@ -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 diff --git a/lib/installers/lang/rust/rust_model.v b/lib/installers/lang/rust/rust_model.v index 27ec4992..05a7edd2 100644 --- a/lib/installers/lang/rust/rust_model.v +++ b/lib/installers/lang/rust/rust_model.v @@ -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 +} diff --git a/lib/installers/net/mycelium/mycelium_factory_.v b/lib/installers/net/mycelium/mycelium_factory_.v deleted file mode 100644 index fefa2188..00000000 --- a/lib/installers/net/mycelium/mycelium_factory_.v +++ /dev/null @@ -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' -} diff --git a/lib/installers/net/mycelium/tun.v b/lib/installers/net/mycelium/tun.v deleted file mode 100644 index 3efcfc11..00000000 --- a/lib/installers/net/mycelium/tun.v +++ /dev/null @@ -1 +0,0 @@ -module mycelium diff --git a/lib/installers/net/mycelium/.heroscript b/lib/installers/net/mycelium_installer/.heroscript similarity index 88% rename from lib/installers/net/mycelium/.heroscript rename to lib/installers/net/mycelium_installer/.heroscript index 343369ba..c4c7f4a9 100644 --- a/lib/installers/net/mycelium/.heroscript +++ b/lib/installers/net/mycelium_installer/.heroscript @@ -1,5 +1,5 @@ !!hero_code.generate_installer - name: "mycelium" + name: "mycelium_installer" classname: "MyceliumInstaller" hasconfig: true singleton: false diff --git a/lib/installers/net/mycelium/mycelium_actions.v b/lib/installers/net/mycelium_installer/mycelium_installer_actions.v similarity index 95% rename from lib/installers/net/mycelium/mycelium_actions.v rename to lib/installers/net/mycelium_installer/mycelium_installer_actions.v index d08383bc..91cf7f82 100644 --- a/lib/installers/net/mycelium/mycelium_actions.v +++ b/lib/installers/net/mycelium_installer/mycelium_installer_actions.v @@ -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') diff --git a/lib/installers/net/mycelium_installer/mycelium_installer_factory_.v b/lib/installers/net/mycelium_installer/mycelium_installer_factory_.v new file mode 100644 index 00000000..561cdf94 --- /dev/null +++ b/lib/installers/net/mycelium_installer/mycelium_installer_factory_.v @@ -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' +} diff --git a/lib/installers/net/mycelium/mycelium_model.v b/lib/installers/net/mycelium_installer/mycelium_installer_model.v similarity index 98% rename from lib/installers/net/mycelium/mycelium_model.v rename to lib/installers/net/mycelium_installer/mycelium_installer_model.v index ca17d5d2..d0420667 100644 --- a/lib/installers/net/mycelium/mycelium_model.v +++ b/lib/installers/net/mycelium_installer/mycelium_installer_model.v @@ -1,4 +1,4 @@ -module mycelium +module mycelium_installer import freeflowuniverse.herolib.data.encoderhero import freeflowuniverse.herolib.osal.tun diff --git a/lib/installers/net/mycelium/readme.md b/lib/installers/net/mycelium_installer/readme.md similarity index 100% rename from lib/installers/net/mycelium/readme.md rename to lib/installers/net/mycelium_installer/readme.md diff --git a/lib/installers/net/wireguard/wireguard_installer_actions.v b/lib/installers/net/wireguard/wireguard_installer_actions.v deleted file mode 100644 index d2848a89..00000000 --- a/lib/installers/net/wireguard/wireguard_installer_actions.v +++ /dev/null @@ -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 - // ")! - - -} - diff --git a/lib/installers/net/wireguard/wireguard_installer_factory_.v b/lib/installers/net/wireguard/wireguard_installer_factory_.v deleted file mode 100644 index 46c99b94..00000000 --- a/lib/installers/net/wireguard/wireguard_installer_factory_.v +++ /dev/null @@ -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' -} diff --git a/lib/installers/net/wireguard/wireguard_installer_model.v b/lib/installers/net/wireguard/wireguard_installer_model.v deleted file mode 100644 index a0ea9121..00000000 --- a/lib/installers/net/wireguard/wireguard_installer_model.v +++ /dev/null @@ -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 -} diff --git a/lib/installers/net/wireguard/wireguard_model.v b/lib/installers/net/wireguard/wireguard_model.v deleted file mode 100644 index c4c7d5d2..00000000 --- a/lib/installers/net/wireguard/wireguard_model.v +++ /dev/null @@ -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()! -} diff --git a/lib/installers/net/wireguard/.heroscript b/lib/installers/net/wireguard_installer/.heroscript similarity index 100% rename from lib/installers/net/wireguard/.heroscript rename to lib/installers/net/wireguard_installer/.heroscript diff --git a/lib/installers/net/wireguard/readme.md b/lib/installers/net/wireguard_installer/readme.md similarity index 100% rename from lib/installers/net/wireguard/readme.md rename to lib/installers/net/wireguard_installer/readme.md diff --git a/lib/installers/net/wireguard/wireguard_actions.v b/lib/installers/net/wireguard_installer/wireguard_installer_actions.v similarity index 82% rename from lib/installers/net/wireguard/wireguard_actions.v rename to lib/installers/net/wireguard_installer/wireguard_installer_actions.v index 596c63b1..25bbf253 100644 --- a/lib/installers/net/wireguard/wireguard_actions.v +++ b/lib/installers/net/wireguard_installer/wireguard_installer_actions.v @@ -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') } diff --git a/lib/installers/net/wireguard/wireguard_factory_.v b/lib/installers/net/wireguard_installer/wireguard_installer_factory_.v similarity index 63% rename from lib/installers/net/wireguard/wireguard_factory_.v rename to lib/installers/net/wireguard_installer/wireguard_installer_factory_.v index eae7890d..9c0ba093 100644 --- a/lib/installers/net/wireguard/wireguard_factory_.v +++ b/lib/installers/net/wireguard_installer/wireguard_installer_factory_.v @@ -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' +} diff --git a/lib/installers/net/wireguard_installer/wireguard_installer_model.v b/lib/installers/net/wireguard_installer/wireguard_installer_model.v new file mode 100644 index 00000000..c5d10234 --- /dev/null +++ b/lib/installers/net/wireguard_installer/wireguard_installer_model.v @@ -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 +} diff --git a/lib/installers/net/yggdrasil/yggdrasil_factory_.v b/lib/installers/net/yggdrasil/yggdrasil_factory_.v index 0bc63030..f7aeb958 100644 --- a/lib/installers/net/yggdrasil/yggdrasil_factory_.v +++ b/lib/installers/net/yggdrasil/yggdrasil_factory_.v @@ -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' } diff --git a/lib/installers/sysadmintools/actrunner/actrunner_actions.v b/lib/installers/sysadmintools/actrunner/actrunner_actions.v index c8bf2cba..83d18214 100644 --- a/lib/installers/sysadmintools/actrunner/actrunner_actions.v +++ b/lib/installers/sysadmintools/actrunner/actrunner_actions.v @@ -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') } diff --git a/lib/installers/sysadmintools/actrunner/actrunner_factory_.v b/lib/installers/sysadmintools/actrunner/actrunner_factory_.v index 1fdca952..2fc6ce0a 100644 --- a/lib/installers/sysadmintools/actrunner/actrunner_factory_.v +++ b/lib/installers/sysadmintools/actrunner/actrunner_factory_.v @@ -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' } diff --git a/lib/installers/sysadmintools/actrunner/actrunner_model.v b/lib/installers/sysadmintools/actrunner/actrunner_model.v index b38e162a..a0c97e01 100644 --- a/lib/installers/sysadmintools/actrunner/actrunner_model.v +++ b/lib/installers/sysadmintools/actrunner/actrunner_model.v @@ -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 +} diff --git a/lib/installers/sysadmintools/actrunner/install.v b/lib/installers/sysadmintools/actrunner/install.v deleted file mode 100644 index 28d5f6db..00000000 --- a/lib/installers/sysadmintools/actrunner/install.v +++ /dev/null @@ -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 -} diff --git a/lib/installers/sysadmintools/b2/.heroscript b/lib/installers/sysadmintools/b2/.heroscript index 8f2a9219..6fcc05b1 100644 --- a/lib/installers/sysadmintools/b2/.heroscript +++ b/lib/installers/sysadmintools/b2/.heroscript @@ -6,6 +6,6 @@ default: true title: "" templates: false - build: true - startupmanager: true + build: false + startupmanager: false diff --git a/lib/installers/sysadmintools/b2/b2_actions.v b/lib/installers/sysadmintools/b2/b2_actions.v index 3c8a612e..f8f799ac 100644 --- a/lib/installers/sysadmintools/b2/b2_actions.v +++ b/lib/installers/sysadmintools/b2/b2_actions.v @@ -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')! } diff --git a/lib/installers/sysadmintools/b2/b2_factory_.v b/lib/installers/sysadmintools/b2/b2_factory_.v index 6d304e2d..b73cde55 100644 --- a/lib/installers/sysadmintools/b2/b2_factory_.v +++ b/lib/installers/sysadmintools/b2/b2_factory_.v @@ -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' } diff --git a/lib/installers/sysadmintools/b2/b2_install.v b/lib/installers/sysadmintools/b2/b2_install.v deleted file mode 100644 index 78984b3e..00000000 --- a/lib/installers/sysadmintools/b2/b2_install.v +++ /dev/null @@ -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 -} diff --git a/lib/installers/sysadmintools/b2/b2_model.v b/lib/installers/sysadmintools/b2/b2_model.v index da2d4625..4b0b9274 100644 --- a/lib/installers/sysadmintools/b2/b2_model.v +++ b/lib/installers/sysadmintools/b2/b2_model.v @@ -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 +} diff --git a/lib/installers/sysadmintools/daguserver/dagu_test.v b/lib/installers/sysadmintools/daguserver/dagu_test.v deleted file mode 100644 index 8b137891..00000000 --- a/lib/installers/sysadmintools/daguserver/dagu_test.v +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lib/installers/sysadmintools/daguserver/daguserver_actions.v b/lib/installers/sysadmintools/daguserver/daguserver_actions.v index d98e2230..fb1b6832 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_actions.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_actions.v @@ -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}') + } + } } diff --git a/lib/installers/sysadmintools/daguserver/daguserver_factory_.v b/lib/installers/sysadmintools/daguserver/daguserver_factory_.v index 5f9f4e13..a365fc00 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_factory_.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_factory_.v @@ -2,9 +2,9 @@ module daguserver 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 freeflowuniverse.herolib.ui.console import time __global ( @@ -22,9 +22,6 @@ pub mut: fn args_get(args_ ArgsGet) ArgsGet { mut args := args_ - if args.name == '' { - args.name = daguserver_default - } if args.name == '' { args.name = 'default' } @@ -32,43 +29,51 @@ fn args_get(args_ ArgsGet) ArgsGet { } pub fn get(args_ ArgsGet) !&DaguInstaller { + mut context := base.context()! mut args := args_get(args_) + mut obj := DaguInstaller{} if args.name !in daguserver_global { - if args.name == 'default' { - if !config_exists(args) { - if default { - config_save(args)! - } - } - config_load(args)! + if !exists(args)! { + set(obj)! + } else { + heroscript := context.hero_config_get('daguserver', args.name)! + mut obj_ := heroscript_loads(heroscript)! + set_in_mem(obj_)! } } return daguserver_global[args.name] or { println(daguserver_global) - panic('could not get config for daguserver with name:${args.name}') + // bug if we get here because should be in globals + panic('could not get config for daguserver with name, is bug:${args.name}') } } -fn config_exists(args_ ArgsGet) bool { +// register the config for the future +pub fn set(o DaguInstaller) ! { + set_in_mem(o)! + mut context := base.context()! + heroscript := heroscript_dumps(o)! + context.hero_config_set('daguserver', o.name, heroscript)! +} + +// does the config exists? +pub fn exists(args_ ArgsGet) !bool { + mut context := base.context()! mut args := args_get(args_) - mut context := base.context() or { panic('bug') } return context.hero_config_exists('daguserver', args.name) } -fn config_load(args_ ArgsGet) ! { +pub fn delete(args_ ArgsGet) ! { mut args := args_get(args_) mut context := base.context()! - mut heroscript := context.hero_config_get('daguserver', args.name)! - play(heroscript: heroscript)! + context.hero_config_delete('daguserver', args.name)! + if args.name in daguserver_global { + // del daguserver_global[args.name] + } } -fn config_save(args_ ArgsGet) ! { - mut args := args_get(args_) - mut context := base.context()! - context.hero_config_set('daguserver', args.name, heroscript_default()!)! -} - -fn set(o DaguInstaller) ! { +// only sets in mem, does not set as config +fn set_in_mem(o DaguInstaller) ! { mut o2 := obj_init(o)! daguserver_global[o.name] = &o2 daguserver_default = o.name @@ -85,18 +90,14 @@ pub mut: pub fn play(args_ PlayArgs) ! { mut args := args_ - if args.heroscript == '' { - args.heroscript = heroscript_default()! - } mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } mut install_actions := plbook.find(filter: 'daguserver.configure')! if install_actions.len > 0 { for install_action in install_actions { - mut p := install_action.params - mycfg := cfg_play(p)! - console.print_debug('install action daguserver.configure\n${mycfg}') - set(mycfg)! + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! } } @@ -264,3 +265,10 @@ pub fn (mut self DaguInstaller) destroy() ! { pub fn switch(name string) { daguserver_default = name } + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' +} diff --git a/lib/installers/sysadmintools/daguserver/daguserver_model.v b/lib/installers/sysadmintools/daguserver/daguserver_model.v index e9bece7f..b33233dd 100644 --- a/lib/installers/sysadmintools/daguserver/daguserver_model.v +++ b/lib/installers/sysadmintools/daguserver/daguserver_model.v @@ -1,60 +1,79 @@ module daguserver -import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.encoderhero +import freeflowuniverse.herolib.crypt.secrets +import freeflowuniverse.herolib.ui.console +import freeflowuniverse.herolib.core.pathlib import os pub const version = '1.14.3' const singleton = true const default = true +const homedir = os.home_dir() -pub fn heroscript_default() !string { - heroscript := " - !!daguserver.configure - name:'daguserver' - title: 'My Hero DAG' - host: 'localhost' - port: 8888 - " - - return heroscript -} - +// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED +@[heap] pub struct DaguInstaller { pub mut: - name string = 'default' - - dagsdir string - configpath string + name string = 'default' + dagsdir string = '${homedir}/.dagu' + configpath string = '${homedir}/.config/dagu' username string password string @[secret] secret string @[secret] title string - host string - port int + host string = 'localhost' + port int = 8014 } -fn cfg_play(p paramsparser.Params) !DaguInstaller { - // THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above - mut mycfg := DaguInstaller{ - name: p.get_default('name', 'default')! - dagsdir: p.get_default('homedir', '${os.home_dir()}/hero/var/daguserver')! - configpath: p.get_default('configpath', '${os.home_dir()}/hero/cfg/dagu')! - username: p.get_default('username', 'admin')! - password: p.get_default('password', 'secretpassword')! - secret: p.get_default('secret', '')! - title: p.get_default('title', 'HERO DAG')! - host: p.get_default('host', 'localhost')! - port: p.get_int_default('port', 8888)! - } - - if mycfg.password == '' && mycfg.secret == '' { - return error('password or secret needs to be filled in for daguserver') - } +// your checking & initialization code if needed +fn obj_init(mycfg_ DaguInstaller) !DaguInstaller { + mut mycfg := mycfg_ return mycfg } -fn obj_init(obj_ DaguInstaller) !DaguInstaller { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ +// called before start if done +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)! + } + + if cfg.dagsdir == '' { + cfg.dagsdir = '${homedir}/.dagu' + } + + if cfg.configpath == '' { + cfg.configpath = '${homedir}/.config/dagu' + } + + if cfg.host == '' { + cfg.host = 'localhost' + } + + if cfg.port == 0 { + cfg.port = 8014 + } + + 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) +} + +/////////////NORMALLY NO NEED TO TOUCH + +pub fn heroscript_dumps(obj DaguInstaller) !string { + return encoderhero.encode[DaguInstaller](obj)! +} + +pub fn heroscript_loads(heroscript string) !DaguInstaller { + mut obj := encoderhero.decode[DaguInstaller](heroscript)! return obj } diff --git a/lib/installers/sysadmintools/daguserver/model_comms.v b/lib/installers/sysadmintools/daguserver/model_comms.v deleted file mode 100644 index 6a5db486..00000000 --- a/lib/installers/sysadmintools/daguserver/model_comms.v +++ /dev/null @@ -1,48 +0,0 @@ -module daguserver - -import os - -@[params] -pub struct DaguCommunicationConfig { -pub: - log_dir string // directory path to save logs from standard output - history_retention_days int // history retention days (default: 30) - mail_on MailOn // Email notification settings - smtp SMTP // SMTP server settings - error_mail Mail // Error mail configuration - info_mail Mail // Info mail configuration -} - -pub struct SMTP { -pub: - host string - port string - username string - password string - error_mail Mail -} - -pub struct Mail { -pub: - from string - to string - prefix string -} - -pub struct MailOn { -pub: - failure bool - success bool -} - -pub fn (mut self DaguInstaller) comms_configure(config DaguCommunicationConfig) ! { - // mut homedir := self.config()!.homedir - - // config_yaml := $tmpl('./templates/communication.yaml') - // os.write_file('${homedir}/communication.yaml', config_yaml)! - - // dags_dir := '${homedir}/dags' - // if !os.exists(dags_dir) { - // os.mkdir(dags_dir)! - // } -} diff --git a/lib/installers/sysadmintools/fungistor/fungistor_factory_.v b/lib/installers/sysadmintools/fungistor/fungistor_factory_.v index 8c124393..cd7cbaf2 100644 --- a/lib/installers/sysadmintools/fungistor/fungistor_factory_.v +++ b/lib/installers/sysadmintools/fungistor/fungistor_factory_.v @@ -1,6 +1,5 @@ module fungistor -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&FungiStor { + return &FungiStor{} +} + +@[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: 'fungistor.')! + 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 fungistor.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action fungistor.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut fungistor_obj := get(name: name)! + console.print_debug('action object:\n${fungistor_obj}') + if other_action.name == 'start' { + console.print_debug('install action fungistor.${other_action.name}') + fungistor_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action fungistor.${other_action.name}') + fungistor_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action fungistor.${other_action.name}') + fungistor_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self FungiStor) start() ! { console.print_header('fungistor start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self FungiStor) start() ! { return error('fungistor did not install properly.') } -pub fn (mut self FungiStor) install_start(model InstallArgs) ! { +pub fn (mut self FungiStor) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self FungiStor) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self FungiStor) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self FungiStor) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for fungistor +pub fn switch(name string) { + fungistor_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/sysadmintools/garage_s3/_archive/old_install.v b/lib/installers/sysadmintools/garage_s3/_archive/old_install.v deleted file mode 100644 index ade31608..00000000 --- a/lib/installers/sysadmintools/garage_s3/_archive/old_install.v +++ /dev/null @@ -1,56 +0,0 @@ -// module garage_s3 - -// import freeflowuniverse.herolib.osal -// import freeflowuniverse.herolib.core -// import freeflowuniverse.herolib.ui.console -// import freeflowuniverse.herolib.core.texttools -// import os - -// pub fn install(args_ GarageS3) ! { -// mut args := args_ -// version := '1.0.0' - -// res := os.execute('garage --version') -// if res.exit_code == 0 { -// r := res.output.split(' ') -// if r.len < 2 { -// return error("couldn't parse garage version, expected 'garage v*'.\n${res.output}") -// } - -// v := r[1] -// if texttools.version(v) < texttools.version(version) { -// args.reset = true -// } -// } else { -// args.reset = true -// } - -// if args.reset { -// console.print_header('install garage') - -// mut url := '' -// if core.is_linux_arm()! { -// url = 'https://garagehq.deuxfleurs.fr/_releases/v${version}/aarch64-unknown-linux-musl/garage' -// } else if core.is_linux_intel()! { -// url = 'https://garagehq.deuxfleurs.fr/_releases/v${version}/x86_64-unknown-linux-musl/garage' -// } else { -// return error('unsported platform') -// } - -// mut dest := osal.download( -// url: url -// minsize_kb: 15 * 1024 -// dest: '/tmp/garage' -// reset: true -// )! -// console.print_debug('download garage done') -// osal.cmd_add( -// cmdname: 'garage' -// source: '${dest.path}' -// )! -// } - -// if args.start { -// start(args)! -// } -// } diff --git a/lib/installers/sysadmintools/garage_s3/configure.v b/lib/installers/sysadmintools/garage_s3/configure.v deleted file mode 100644 index 710300ec..00000000 --- a/lib/installers/sysadmintools/garage_s3/configure.v +++ /dev/null @@ -1,121 +0,0 @@ -module garage_s3 - -// import freeflowuniverse.herolib.ui.console -// import freeflowuniverse.herolib.core.pathlib -// import freeflowuniverse.herolib.sysadmin.startupmanager -// import freeflowuniverse.herolib.crypt.secrets -// // import freeflowuniverse.herolib.core.texttools -// // import freeflowuniverse.herolib.core.httpconnection -// import os -// import time - -// @[params] -// pub struct S3Config { -// pub mut: -// replication_mode string = '3' -// metadata_dir string = '/var/garage/meta' -// data_dir string = '/var/garage/data' -// sled_cache_capacity u32 = 128 // in MB -// compression_level u8 = 1 - -// rpc_secret string //{GARAGE_RPCSECRET} -// rpc_bind_addr string = '[::]:3901' -// rpc_bind_outgoing bool -// rpc_public_addr string = '127.0.0.1:3901' - -// bootstrap_peers []string - -// api_bind_addr string = '[::]:3900' -// s3_region string = 'garage' -// root_domain string = '.s3.garage' - -// web_bind_addr string = '[::]:3902' -// web_root_domain string = '.web.garage' - -// admin_api_bind_addr string = '[::]:3903' -// admin_metrics_token string //{GARAGE_METRICSTOKEN} -// admin_token string //{GARAGE_ADMINTOKEN} -// admin_trace_sink string = 'http://localhost:4317' - -// reset bool -// config_reset bool -// start bool = true -// restart bool = true -// } - -// pub fn configure(args_ S3Config) !S3Config { -// mut args := args_ - -// if args.rpc_secret == '' { -// args.rpc_secret = secrets.openssl_hex_secret()! -// println('export GARAGE_RPCSECRET=${args.rpc_secret}') -// } - -// if args.admin_metrics_token == '' { -// args.admin_metrics_token = secrets.openssl_base64_secret()! -// println('export GARAGE_METRICSTOKEN=${args.admin_metrics_token}') -// } - -// if args.admin_token == '' { -// args.admin_token = secrets.openssl_base64_secret()! -// println('export GARAGE_ADMINTOKEN=${args.admin_token}') -// } - -// mut config_file := $tmpl('templates/garage.toml') - -// myconfigpath_ := '/etc/garage.toml' -// mut myconfigpath := pathlib.get_file(path: myconfigpath_, create: true)! -// myconfigpath.write(config_file)! - -// console.print_header('garage start') - -// return args -// } - -// pub fn start(args_ S3Config) !S3Config { -// mut args := args_ - -// myconfigpath_ := '/etc/garage.toml' - -// if args.config_reset || !os.exists(myconfigpath_) { -// args = configure(args)! -// } - -// if args.restart { -// stop()! -// } - -// mut sm := startupmanager.get()! - -// sm.new( -// name: 'garage' -// cmd: 'garage -c ${myconfigpath_} server' -// start: true -// )! - -// console.print_debug('garage -c ${myconfigpath_} server') - -// for _ in 0 .. 50 { -// if check(args)! { -// return args -// } -// time.sleep(100 * time.millisecond) -// } - -// return error('garage server did not start properly.') -// } - -// pub fn stop() ! { -// console.print_header('garage stop') -// mut sm := startupmanager.get()! -// sm.stop('garage')! -// } - -// fn check(args S3Config) !bool { -// _ := 'garage status' -// res := os.execute('garage status') -// if res.exit_code == 0 { -// return true -// } -// return false -// } diff --git a/lib/installers/sysadmintools/garage_s3/garage_s3_actions.v b/lib/installers/sysadmintools/garage_s3/garage_s3_actions.v index 3e7a69e2..0b0d5e38 100644 --- a/lib/installers/sysadmintools/garage_s3/garage_s3_actions.v +++ b/lib/installers/sysadmintools/garage_s3/garage_s3_actions.v @@ -5,8 +5,72 @@ import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core import freeflowuniverse.herolib.osal.zinit -// import freeflowuniverse.herolib.osal.systemd +import freeflowuniverse.herolib.installers.ulist +import freeflowuniverse.herolib.core.httpconnection import os +import json + +fn startupcmd() ![]zinit.ZProcessNewArgs { + mut res := []zinit.ZProcessNewArgs{} + res << zinit.ZProcessNewArgs{ + name: 'garage_s3' + cmd: 'garage_s3 -c /var/garage/config.toml server' + startuptype: .zinit + env: { + 'HOME': '/root' + } + } + + return res +} + +struct GarageS3InstanceStatus { + status string + known_nodes int @[json: 'knownNodes'] + connected_nodes int @[json: 'connectedNodes'] + storage_nodes int @[json: 'storageNodes'] + storage_nodes_ok int @[json: 'storageNodesOk'] + partitions int @[json: 'partitions'] + partitions_quorum int @[json: 'partitionsQuorum'] + partitions_all_ok int @[json: 'partitionsAllOk'] +} + +fn running() !bool { + mut installer := get()! + url := 'http://127.0.0.1:3903/' + if installer.admin_token.len < 0 { + return false + } + + mut conn := httpconnection.new(name: 'garage_s3', url: url)! + conn.default_header.add(.authorization, 'Bearer ${installer.admin_token}') + + r := conn.get_json_dict(prefix: 'v1/health', debug: false) or { return false } + if r.len == 0 { + return false + } + + decoded_response := json.decode(GarageS3InstanceStatus, r.str()) or { return false } + + if decoded_response.status != 'healthy' { + return false + } + 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 { @@ -28,114 +92,77 @@ fn installed() !bool { return true } -fn install() ! { - console.print_header('install garage_s3') - // mut installer := get()! - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - mut url := '' - if core.is_linux_arm()! { - url = 'https://github.com/garage_s3-dev/garage_s3/releases/download/v${version}/garage_s3_${version}_linux_arm64.tar.gz' - } else if core.is_linux_intel()! { - url = 'https://github.com/garage_s3-dev/garage_s3/releases/download/v${version}/garage_s3_${version}_linux_amd64.tar.gz' - } else if core.is_osx_arm()! { - url = 'https://github.com/garage_s3-dev/garage_s3/releases/download/v${version}/garage_s3_${version}_darwin_arm64.tar.gz' - } else if core.is_osx_intel()! { - url = 'https://github.com/garage_s3-dev/garage_s3/releases/download/v${version}/garage_s3_${version}_darwin_amd64.tar.gz' - } else { - return error('unsported platform') - } - - mut dest := osal.download( - url: url - minsize_kb: 9000 - expand_dir: '/tmp/garage_s3' - )! - - // dest.moveup_single_subdir()! - - mut binpath := dest.file_get('garage_s3')! - osal.cmd_add( - cmdname: 'garage_s3' - source: binpath.path - )! +// get the Upload List of the files +fn ulist_get() !ulist.UList { + // optionally build a UList which is all paths which are result of building, is then used e.g. in upload + return ulist.UList{} } -fn startupcmd() ![]zinit.ZProcessNewArgs { - mut res := []zinit.ZProcessNewArgs{} - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - res << zinit.ZProcessNewArgs{ - name: 'garage_s3' - cmd: 'garage_s3 server' - env: { - 'HOME': '/root' +// uploads to S3 server if configured +fn upload() ! {} + +fn install() ! { + console.print_header('install garage_s3') + + mut res := os.execute('garage_s3 --version') + if res.exit_code == 0 { + console.print_header('garage_s3 is already installed') + return + } + + p := core.platform()! + + if p != .ubuntu { + return error('unsupported platform') + } + + mut url := '' + if core.is_linux_arm()! { + url = 'https://garagehq.deuxfleurs.fr/_releases/v${version}/aarch64-unknown-linux-musl/garage' + } + if core.is_linux_intel()! { + url = 'https://garagehq.deuxfleurs.fr/_releases/v${version}/x86_64-unknown-linux-musl/garage' + } + + res = os.execute('wget --version') + if res.exit_code == 0 { + console.print_header('wget is already installed') + } else { + osal.package_install('wget') or { + return error('Could not install wget, its required to install rclone.\nerror:\n${err}') } } - return res + // Check if garage_s3 is installed + osal.execute_stdout('sudo wget -O /usr/local/bin/garage_s3 ${url}') or { + return error('cannot install garage_s3 due to: ${err}') + } + + res = os.execute('sudo chmod +x /usr/local/bin/garage_s3') + if res.exit_code != 0 { + return error('failed to install garage_s3: ${res.output}') + } + + console.print_header('garage_s3 is installed') } -fn running_() !bool { - _ := get()! - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - // this checks health of garage_s3 - // 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: 'garage_s3', url: url)! +fn destroy() ! { + console.print_header('uninstall garage_s3') + res := os.execute('sudo rm -rf /usr/local/bin/garage_s3') + if res.exit_code != 0 { + return error('failed to uninstall garage_s3: ${res.output}') + } - // 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('garage_s3 is answering.') - return false -} - -fn start_pre() ! { -} - -fn start_post() ! { -} - -fn stop_pre() ! { -} - -fn stop_post() ! { -} - -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 - // ")! + mut zinit_factory := zinit.new()! + + if zinit_factory.exists('garage_s3') { + zinit_factory.stop('garage_s3') or { + return error('Could not stop garage_s3 service due to: ${err}') + } + zinit_factory.delete('garage_s3') or { + return error('Could not delete garage_s3 service due to: ${err}') + } + } + + console.print_header('garage_s3 is uninstalled') } diff --git a/lib/installers/sysadmintools/garage_s3/garage_s3_factory_.v b/lib/installers/sysadmintools/garage_s3/garage_s3_factory_.v index bcf62fe6..383ea43a 100644 --- a/lib/installers/sysadmintools/garage_s3/garage_s3_factory_.v +++ b/lib/installers/sysadmintools/garage_s3/garage_s3_factory_.v @@ -2,9 +2,9 @@ module garage_s3 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 freeflowuniverse.herolib.ui.console import time __global ( @@ -17,14 +17,11 @@ __global ( @[params] pub struct ArgsGet { pub mut: - name string = 'default' + name string } fn args_get(args_ ArgsGet) ArgsGet { mut args := args_ - if args.name == '' { - args.name = garage_s3_default - } if args.name == '' { args.name = 'default' } @@ -32,74 +29,110 @@ fn args_get(args_ ArgsGet) ArgsGet { } pub fn get(args_ ArgsGet) !&GarageS3 { + mut context := base.context()! mut args := args_get(args_) + mut obj := GarageS3{} if args.name !in garage_s3_global { - if !config_exists() { - if default { - config_save()! - } + if !exists(args)! { + set(obj)! + } else { + heroscript := context.hero_config_get('garage_s3', args.name)! + mut obj_ := heroscript_loads(heroscript)! + set_in_mem(obj_)! } - config_load()! } return garage_s3_global[args.name] or { println(garage_s3_global) - panic('bug in get from factory: ') + // bug if we get here because should be in globals + panic('could not get config for garage_s3 with name, is bug:${args.name}') } } -fn config_exists(args_ ArgsGet) bool { +// register the config for the future +pub fn set(o GarageS3) ! { + set_in_mem(o)! + mut context := base.context()! + heroscript := heroscript_dumps(o)! + context.hero_config_set('garage_s3', o.name, heroscript)! +} + +// does the config exists? +pub fn exists(args_ ArgsGet) !bool { + mut context := base.context()! mut args := args_get(args_) - mut context := base.context() or { panic('bug') } return context.hero_config_exists('garage_s3', args.name) } -fn config_load(args_ ArgsGet) ! { +pub fn delete(args_ ArgsGet) ! { mut args := args_get(args_) mut context := base.context()! - mut heroscript := context.hero_config_get('garage_s3', args.name)! - play(heroscript: heroscript)! + context.hero_config_delete('garage_s3', args.name)! + if args.name in garage_s3_global { + // del garage_s3_global[args.name] + } } -fn config_save(args_ ArgsGet) ! { - mut args := args_get(args_) - mut context := base.context()! - context.hero_config_set('garage_s3', args.name, heroscript_default()!)! -} - -fn set(o GarageS3) ! { +// only sets in mem, does not set as config +fn set_in_mem(o GarageS3) ! { mut o2 := obj_init(o)! - garage_s3_global['default'] = &o2 + garage_s3_global[o.name] = &o2 + garage_s3_default = o.name } @[params] pub struct PlayArgs { pub mut: - name string = 'default' heroscript string // if filled in then plbook will be made out of it plbook ?playbook.PlayBook reset bool - - start bool - stop bool - restart bool - delete bool - configure bool // make sure there is at least one installed } pub fn play(args_ PlayArgs) ! { mut args := args_ - if args.heroscript == '' { - args.heroscript = heroscript_default()! - } mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } mut install_actions := plbook.find(filter: 'garage_s3.configure')! if install_actions.len > 0 { for install_action in install_actions { - mut p := install_action.params - mycfg := cfg_play(p)! - set(mycfg)! + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! + } + } + + mut other_actions := plbook.find(filter: 'garage_s3.')! + 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 garage_s3.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action garage_s3.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut garage_s3_obj := get(name: name)! + console.print_debug('action object:\n${garage_s3_obj}') + if other_action.name == 'start' { + console.print_debug('install action garage_s3.${other_action.name}') + garage_s3_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action garage_s3.${other_action.name}') + garage_s3_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action garage_s3.${other_action.name}') + garage_s3_obj.restart()! + } } } } @@ -206,7 +239,7 @@ pub fn (mut self GarageS3) running() !bool { return false } } - return running_()! + return running()! } @[params] @@ -224,12 +257,18 @@ pub fn (mut self GarageS3) install(args InstallArgs) ! { pub fn (mut self GarageS3) destroy() ! { switch(self.name) - self.stop() or {} - destroy_()! + destroy()! } // switch instance to be used for garage_s3 pub fn switch(name string) { garage_s3_default = name } + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' +} diff --git a/lib/installers/sysadmintools/garage_s3/garage_s3_model.v b/lib/installers/sysadmintools/garage_s3/garage_s3_model.v index 3b299417..cb609f05 100644 --- a/lib/installers/sysadmintools/garage_s3/garage_s3_model.v +++ b/lib/installers/sysadmintools/garage_s3/garage_s3_model.v @@ -1,38 +1,22 @@ module garage_s3 -import freeflowuniverse.herolib.data.paramsparser -import os +import freeflowuniverse.herolib.data.encoderhero +import freeflowuniverse.herolib.ui.console +import freeflowuniverse.herolib.core.pathlib +import rand -pub const version = '1.14.3' +pub const version = '1.0.1' const singleton = false const default = true -// TODO: THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE TO STRUCT BELOW, IS STRUCTURED AS HEROSCRIPT -pub fn heroscript_default() !string { - heroscript := " - !!garage_s3.configure - name:'garage_s3' - homedir: '{HOME}/hero/var/garage_s3' - configpath: '{HOME}/.config/garage_s3/admin.yaml' - username: 'admin' - password: 'secretpassword' - secret: '' - title: 'My Hero DAG' - host: 'localhost' - port: 8888 - - " - - return heroscript -} - // THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED - +@[heap] pub struct GarageS3 { pub mut: name string = 'default' replication_mode string = '3' + config_path string = '/var/garage/config.toml' metadata_dir string = '/var/garage/meta' data_dir string = '/var/garage/data' sled_cache_capacity u32 = 128 // in MB @@ -63,49 +47,102 @@ pub mut: restart bool = true } -fn cfg_play(p paramsparser.Params) !GarageS3 { - mut mycfg := GarageS3{ - name: p.get_default('name', 'default')! - replication_mode: p.get_default('replication_mode', '3')! - metadata_dir: p.get_default('metadata_dir', '/var/garage/meta')! - data_dir: p.get_default('data_dir', '/var/garage/data')! - sled_cache_capacity: p.get_u32_default('sled_cache_capacity', 128)! - compression_level: p.get_u8_default('compression_level', 1)! - rpc_secret: p.get_default('rpc_secret', '')! - rpc_bind_addr: p.get_default('rpc_bind_addr', '[::]:3901')! - rpc_public_addr: p.get_default('rpc_public_addr', '127.0.0.1:3901')! - api_bind_addr: p.get_default('api_bind_addr', '[::]:3900')! - s3_region: p.get_default('s3_region', 'garage')! - root_domain: p.get_default('root_domain', '.s3.garage')! - web_bind_addr: p.get_default('web_bind_addr', '[::]:3902')! - web_root_domain: p.get_default('web_root_domain', '.web.garage')! - admin_api_bind_addr: p.get_default('admin_api_bind_addr', '[::]:3903')! - admin_metrics_token: p.get_default('admin_metrics_token', '')! - admin_token: p.get_default('admin_token', '')! - admin_trace_sink: p.get_default('admin_trace_sink', 'http://localhost:4317')! - bootstrap_peers: p.get_list_default('bootstrap_peers', [])! - rpc_bind_outgoing: p.get_default_false('rpc_bind_outgoing') - reset: p.get_default_false('reset') - config_reset: p.get_default_false('config_reset') - start: p.get_default_true('start') - restart: p.get_default_true('restart') +// your checking & initialization code if needed +fn obj_init(mycfg_ GarageS3) !GarageS3 { + mut mycfg := mycfg_ + + if mycfg.name == '' { + mycfg.name = 'default' } - return mycfg -} + if mycfg.config_path == '' { + mycfg.config_path = '/var/garage/config.toml' + } -fn obj_init(obj_ GarageS3) !GarageS3 { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ - return obj + if mycfg.replication_mode == '' { + mycfg.replication_mode = '3' + } + + if mycfg.metadata_dir == '' { + mycfg.replication_mode = '/var/garage/meta' + } + + if mycfg.data_dir == '' { + mycfg.data_dir = '/var/garage/data' + } + + if mycfg.sled_cache_capacity == 0 { + mycfg.sled_cache_capacity = 128 + } + + if mycfg.compression_level == 0 { + mycfg.compression_level = 1 + } + + if mycfg.rpc_bind_addr == '' { + mycfg.rpc_bind_addr = '[::]:3901' + } + + if mycfg.rpc_public_addr == '' { + mycfg.rpc_public_addr = '127.0.0.1:3901' + } + + if mycfg.api_bind_addr == '' { + mycfg.api_bind_addr = '[::]:3900' + } + + if mycfg.s3_region == '' { + mycfg.s3_region = 'garage' + } + + if mycfg.root_domain == '' { + mycfg.root_domain = '.s3.garage' + } + + if mycfg.web_bind_addr == '' { + mycfg.web_bind_addr = '[::]:3902' + } + + if mycfg.web_root_domain == '' { + mycfg.web_root_domain = '.web.garage' + } + + if mycfg.admin_api_bind_addr == '' { + mycfg.admin_api_bind_addr = '[::]:3903' + } + + if mycfg.admin_trace_sink == '' { + mycfg.admin_trace_sink = 'http://localhost:4317' + } + + if mycfg.admin_token == '' { + mycfg.admin_token = rand.hex(64) + } + + if mycfg.admin_metrics_token == '' { + mycfg.admin_metrics_token = rand.hex(64) + } + + if mycfg.rpc_secret == '' { + mycfg.rpc_secret = rand.hex(64) + } + return mycfg } // 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) + server := get()! + mut mycode := $tmpl('templates/config.ini') + mut path := pathlib.get_file(path: server.config_path, create: true)! + path.write(mycode)! + console.print_debug(mycode) +} + +pub fn heroscript_dumps(obj GarageS3) !string { + return encoderhero.encode[GarageS3](obj)! +} + +pub fn heroscript_loads(heroscript string) !GarageS3 { + mut obj := encoderhero.decode[GarageS3](heroscript)! + return obj } diff --git a/lib/installers/sysadmintools/garage_s3/templates/atemplate.yaml b/lib/installers/sysadmintools/garage_s3/templates/atemplate.yaml deleted file mode 100644 index a4c386dd..00000000 --- a/lib/installers/sysadmintools/garage_s3/templates/atemplate.yaml +++ /dev/null @@ -1,5 +0,0 @@ - - -name: ${cfg.configpath} - - diff --git a/lib/installers/sysadmintools/garage_s3/templates/config.ini b/lib/installers/sysadmintools/garage_s3/templates/config.ini new file mode 100644 index 00000000..2c18b5eb --- /dev/null +++ b/lib/installers/sysadmintools/garage_s3/templates/config.ini @@ -0,0 +1,27 @@ +metadata_dir = "${server.metadata_dir}" +data_dir = "${server.data_dir}" +db_engine = "sqlite" + +replication_factor = ${server.replication_mode} + +rpc_bind_addr = "${server.rpc_bind_addr}" +rpc_public_addr = "${server.rpc_public_addr}" +rpc_secret = "${server.rpc_secret}" + +[s3_api] +s3_region = "${server.s3_region}" +api_bind_addr = "${server.api_bind_addr}" +root_domain = "${server.root_domain}" + +[s3_web] +bind_addr = "${server.web_bind_addr}" +root_domain = "${server.web_root_domain}" +index = "index.html" + +[k2v_api] +api_bind_addr = "${server.api_bind_addr}" + +[admin] +api_bind_addr = "${server.admin_api_bind_addr}" +admin_token = "${server.admin_token}" +metrics_token = "${server.admin_metrics_token}" diff --git a/lib/installers/sysadmintools/grafana/grafana_factory_.v b/lib/installers/sysadmintools/grafana/grafana_factory_.v index 058bc28e..30c27634 100644 --- a/lib/installers/sysadmintools/grafana/grafana_factory_.v +++ b/lib/installers/sysadmintools/grafana/grafana_factory_.v @@ -1,6 +1,5 @@ module grafana -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&Grafana { + return &Grafana{} +} + +@[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: 'grafana.')! + 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 grafana.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action grafana.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut grafana_obj := get(name: name)! + console.print_debug('action object:\n${grafana_obj}') + if other_action.name == 'start' { + console.print_debug('install action grafana.${other_action.name}') + grafana_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action grafana.${other_action.name}') + grafana_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action grafana.${other_action.name}') + grafana_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self Grafana) start() ! { console.print_header('grafana start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self Grafana) start() ! { return error('grafana did not install properly.') } -pub fn (mut self Grafana) install_start(model InstallArgs) ! { +pub fn (mut self Grafana) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self Grafana) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self Grafana) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self Grafana) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for grafana +pub fn switch(name string) { + grafana_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/sysadmintools/prometheus/prometheus_factory_.v b/lib/installers/sysadmintools/prometheus/prometheus_factory_.v index 734182f3..040a2c82 100644 --- a/lib/installers/sysadmintools/prometheus/prometheus_factory_.v +++ b/lib/installers/sysadmintools/prometheus/prometheus_factory_.v @@ -1,6 +1,5 @@ module prometheus -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&Prometheus { + return &Prometheus{} +} + +@[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: 'prometheus.')! + 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 prometheus.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action prometheus.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut prometheus_obj := get(name: name)! + console.print_debug('action object:\n${prometheus_obj}') + if other_action.name == 'start' { + console.print_debug('install action prometheus.${other_action.name}') + prometheus_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action prometheus.${other_action.name}') + prometheus_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action prometheus.${other_action.name}') + prometheus_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self Prometheus) start() ! { console.print_header('prometheus start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self Prometheus) start() ! { return error('prometheus did not install properly.') } -pub fn (mut self Prometheus) install_start(model InstallArgs) ! { +pub fn (mut self Prometheus) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self Prometheus) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self Prometheus) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self Prometheus) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for prometheus +pub fn switch(name string) { + prometheus_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/sysadmintools/rclone/rclone_actions.v b/lib/installers/sysadmintools/rclone/rclone_actions.v index e8551fa2..63cb29d9 100644 --- a/lib/installers/sysadmintools/rclone/rclone_actions.v +++ b/lib/installers/sysadmintools/rclone/rclone_actions.v @@ -1,13 +1,16 @@ module rclone import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.core 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 { +fn installed() !bool { res := os.execute('${osal.profile_path_source_and()!} rclone version') if res.exit_code != 0 { return false @@ -23,58 +26,40 @@ fn installed_() !bool { return true } -fn install_() ! { +// 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 rclone') - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - mut url := '' - if core.is_linux_arm()! { - url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-linux-arm64.zip' - } else if core.is_linux_intel()! { - url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-linux-amd64.zip' - } else if core.is_osx_arm()! { - url = 'https://downloads.rclone.org/rclone-current-osx-amd64.zip' - } else if core.is_osx_intel()! { - url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-osx-amd64.zip' + // Check if curl is installed + mut res := os.execute('curl --version') + if res.exit_code == 0 { + console.print_header('curl is already installed') } else { - return error('unsported platform') + osal.package_install('curl') or { + return error('Could not install curl, its required to install rclone.\nerror:\n${err}') + } } - mut dest := osal.download( - url: url - minsize_kb: 9000 - expand_dir: '/tmp/rclone' - )! - // dest.moveup_single_subdir()! - mut binpath := dest.file_get('rclone')! - osal.cmd_add( - cmdname: 'rclone' - source: binpath.path - )! + // Check if rclone is installed + osal.execute_stdout('sudo -v ; curl https://rclone.org/install.sh | sudo bash') or { + return error('cannot install rclone due to: ${err}') + } + + console.print_header('rclone is installed') } -fn configure() ! { - _ := get()! - - // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED - - _ := $tmpl('templates/rclone.yaml') - // mut path := pathlib.get_file(path: cfg.configpath, create: true)! - // path.write(mycode)! - // console.print_debug(mycode) - // implement if steps need to be done for configuration -} - -fn destroy_() ! { -} - -fn start_pre() ! { -} - -fn start_post() ! { -} - -fn stop_pre() ! { -} - -fn stop_post() ! { +fn destroy() ! { + console.print_header('uninstall rclone') + res := os.execute('sudo rm -rf /usr/local/bin/rclone /usr/local/rclone /usr/bin/rclone /usr/share/man/man1/rclone.1.gz') + if res.exit_code != 0 { + return error('failed to uninstall rclone: ${res.output}') + } + console.print_header('rclone is uninstalled') } diff --git a/lib/installers/sysadmintools/rclone/rclone_factory_.v b/lib/installers/sysadmintools/rclone/rclone_factory_.v index 7dfd9079..cf032caf 100644 --- a/lib/installers/sysadmintools/rclone/rclone_factory_.v +++ b/lib/installers/sysadmintools/rclone/rclone_factory_.v @@ -2,10 +2,9 @@ module rclone 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 freeflowuniverse.herolib.ui.console -import time __global ( rclone_global map[string]&RClone @@ -17,14 +16,11 @@ __global ( @[params] pub struct ArgsGet { pub mut: - name string = 'default' + name string } fn args_get(args_ ArgsGet) ArgsGet { mut args := args_ - if args.name == '' { - args.name = rclone_default - } if args.name == '' { args.name = 'default' } @@ -32,71 +28,91 @@ fn args_get(args_ ArgsGet) ArgsGet { } pub fn get(args_ ArgsGet) !&RClone { + mut context := base.context()! mut args := args_get(args_) + mut obj := RClone{} if args.name !in rclone_global { - if !config_exists() { - if default { - config_save()! - } + if !exists(args)! { + set(obj)! + } else { + heroscript := context.hero_config_get('rclone', args.name)! + mut obj_ := heroscript_loads(heroscript)! + set_in_mem(obj_)! } - config_load()! } return rclone_global[args.name] or { println(rclone_global) - panic('bug in get from factory: ') + // bug if we get here because should be in globals + panic('could not get config for rclone with name, is bug:${args.name}') } } -fn config_exists(args_ ArgsGet) bool { +// register the config for the future +pub fn set(o RClone) ! { + set_in_mem(o)! + mut context := base.context()! + heroscript := heroscript_dumps(o)! + context.hero_config_set('rclone', o.name, heroscript)! +} + +// does the config exists? +pub fn exists(args_ ArgsGet) !bool { + mut context := base.context()! mut args := args_get(args_) - mut context := base.context() or { panic('bug') } return context.hero_config_exists('rclone', args.name) } -fn config_load(args_ ArgsGet) ! { +pub fn delete(args_ ArgsGet) ! { mut args := args_get(args_) mut context := base.context()! - mut heroscript := context.hero_config_get('rclone', args.name)! - play(heroscript: heroscript)! + context.hero_config_delete('rclone', args.name)! + if args.name in rclone_global { + // del rclone_global[args.name] + } } -fn config_save(args_ ArgsGet) ! { - mut args := args_get(args_) - mut context := base.context()! - context.hero_config_set('rclone', args.name, heroscript_default()!)! -} - -fn set(o RClone) ! { +// only sets in mem, does not set as config +fn set_in_mem(o RClone) ! { mut o2 := obj_init(o)! - rclone_global['default'] = &o2 + rclone_global[o.name] = &o2 + rclone_default = o.name } @[params] pub struct PlayArgs { pub mut: - name string = 'default' heroscript string // if filled in then plbook will be made out of it plbook ?playbook.PlayBook reset bool - - delete bool - configure bool // make sure there is at least one installed } pub fn play(args_ PlayArgs) ! { mut args := args_ - if args.heroscript == '' { - args.heroscript = heroscript_default()! - } mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } mut install_actions := plbook.find(filter: 'rclone.configure')! if install_actions.len > 0 { for install_action in install_actions { - mut p := install_action.params - mycfg := cfg_play(p)! - set(mycfg)! + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! + } + } + + mut other_actions := plbook.find(filter: 'rclone.')! + 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 rclone.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action rclone.install') + install()! + } } } } @@ -141,18 +157,24 @@ pub mut: pub fn (mut self RClone) install(args InstallArgs) ! { switch(self.name) - if args.reset || (!installed_()!) { - install_()! + if args.reset || (!installed()!) { + install()! } } pub fn (mut self RClone) destroy() ! { switch(self.name) - - destroy_()! + destroy()! } // switch instance to be used for rclone pub fn switch(name string) { rclone_default = name } + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' +} diff --git a/lib/installers/sysadmintools/rclone/rclone_model.v b/lib/installers/sysadmintools/rclone/rclone_model.v index c995626d..a5c32f47 100644 --- a/lib/installers/sysadmintools/rclone/rclone_model.v +++ b/lib/installers/sysadmintools/rclone/rclone_model.v @@ -1,27 +1,21 @@ module rclone import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.encoderhero +import os pub const version = '1.67.0' const singleton = false const default = false -pub fn heroscript_default() !string { - heroscript := " - !!rclone.configure - name: 'default' - cat: 'b2' - s3_account: '' - s3_key: '' - s3_secret: '' - hard_delete: false - endpoint: '' - " - - return heroscript +// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED +pub enum RCloneCat { + b2 + s3 + ftp } -// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED +@[heap] pub struct RClone { pub mut: name string = 'default' @@ -33,31 +27,32 @@ pub mut: endpoint string } -pub enum RCloneCat { - b2 - s3 - ftp -} - -fn cfg_play(p paramsparser.Params) !RClone { - mut mycfg := RClone{ - name: p.get_default('name', 'default')! - cat: match p.get_default('cat', 'b2')! { - 'b2' { RCloneCat.b2 } - 's3' { RCloneCat.s3 } - 'ftp' { RCloneCat.ftp } - else { return error('Invalid RCloneCat') } - } - s3_account: p.get_default('s3_account', '')! - s3_key: p.get_default('s3_key', '')! - s3_secret: p.get_default('s3_secret', '')! - hard_delete: p.get_default_false('hard_delete') - endpoint: p.get_default('endpoint', '')! - } +// your checking & initialization code if needed +fn obj_init(mycfg_ RClone) !RClone { + mut mycfg := mycfg_ return mycfg } -fn obj_init(obj_ RClone) !RClone { - mut obj := obj_ +// called before start if done +fn configure() ! { + _ := get()! + + // THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED + + _ := $tmpl('templates/rclone.yaml') + // mut path := pathlib.get_file(path: cfg.configpath, create: true)! + // path.write(mycode)! + // console.print_debug(mycode) + // implement if steps need to be done for configuration +} + +/////////////NORMALLY NO NEED TO TOUCH + +pub fn heroscript_dumps(obj RClone) !string { + return encoderhero.encode[RClone](obj)! +} + +pub fn heroscript_loads(heroscript string) !RClone { + mut obj := encoderhero.decode[RClone](heroscript)! return obj } diff --git a/lib/installers/sysadmintools/restic/restic_factory_.v b/lib/installers/sysadmintools/restic/restic_factory_.v index 87d0e0cc..b0eb32dd 100644 --- a/lib/installers/sysadmintools/restic/restic_factory_.v +++ b/lib/installers/sysadmintools/restic/restic_factory_.v @@ -1,6 +1,5 @@ module restic -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&Restic { + return &Restic{} +} + +@[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: 'restic.')! + 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 restic.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action restic.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut restic_obj := get(name: name)! + console.print_debug('action object:\n${restic_obj}') + if other_action.name == 'start' { + console.print_debug('install action restic.${other_action.name}') + restic_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action restic.${other_action.name}') + restic_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action restic.${other_action.name}') + restic_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self Restic) start() ! { console.print_header('restic start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self Restic) start() ! { return error('restic did not install properly.') } -pub fn (mut self Restic) install_start(model InstallArgs) ! { +pub fn (mut self Restic) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self Restic) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self Restic) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self Restic) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for restic +pub fn switch(name string) { + restic_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/sysadmintools/s3/s3_factory_.v b/lib/installers/sysadmintools/s3/s3_factory_.v index c2403bf8..131078f7 100644 --- a/lib/installers/sysadmintools/s3/s3_factory_.v +++ b/lib/installers/sysadmintools/s3/s3_factory_.v @@ -1,6 +1,5 @@ module s3 -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&S3Installer { + return &S3Installer{} +} + +@[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: 's3.')! + 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 s3.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action s3.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut s3_obj := get(name: name)! + console.print_debug('action object:\n${s3_obj}') + if other_action.name == 'start' { + console.print_debug('install action s3.${other_action.name}') + s3_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action s3.${other_action.name}') + s3_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action s3.${other_action.name}') + s3_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self S3Installer) start() ! { console.print_header('s3 start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self S3Installer) start() ! { return error('s3 did not install properly.') } -pub fn (mut self S3Installer) install_start(model InstallArgs) ! { +pub fn (mut self S3Installer) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self S3Installer) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self S3Installer) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self S3Installer) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for s3 +pub fn switch(name string) { + s3_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/threefold/griddriver/griddriver_factory_.v b/lib/installers/threefold/griddriver/griddriver_factory_.v index 1297dfbf..b25d646c 100644 --- a/lib/installers/threefold/griddriver/griddriver_factory_.v +++ b/lib/installers/threefold/griddriver/griddriver_factory_.v @@ -1,128 +1,114 @@ module griddriver -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.data.paramsparser - import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.osal.zinit -import time __global ( - griddriver_global map[string]&GridDriverInstaller - griddriver_default string + griddriver_global map[string]&GridDriverInstaller + griddriver_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&GridDriverInstaller { - return &GridDriverInstaller{} +pub fn get(args_ ArgsGet) !&GridDriverInstaller { + return &GridDriverInstaller{} } @[params] pub struct PlayArgs { pub mut: - heroscript string //if filled in then plbook will be made out of it - plbook ?playbook.PlayBook - reset bool + 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: 'griddriver.')! - for other_action in other_actions { - if other_action.name in ["destroy","install","build"]{ - mut p := other_action.params - reset:=p.get_default_false("reset") - if other_action.name == "destroy" || reset{ - console.print_debug("install action griddriver.destroy") - destroy()! - } - if other_action.name == "install"{ - console.print_debug("install action griddriver.install") - install()! - } - } - } + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } + mut other_actions := plbook.find(filter: 'griddriver.')! + for other_action in other_actions { + if other_action.name in ['destroy', 'install', 'build'] { + mut p := other_action.params + reset := p.get_default_false('reset') + if other_action.name == 'destroy' || reset { + console.print_debug('install action griddriver.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action griddriver.install') + install()! + } + } + } } - //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { - // unknown - // screen - // zinit - // tmux - // systemd - match cat{ - .zinit{ - console.print_debug("startupmanager: zinit") - return startupmanager.get(cat:.zinit)! - } - .systemd{ - console.print_debug("startupmanager: systemd") - return startupmanager.get(cat:.systemd)! - }else{ - console.print_debug("startupmanager: auto") - return startupmanager.get()! - } - } + // 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 GridDriverInstaller) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self GridDriverInstaller) build() ! { - switch(self.name) - build()! + switch(self.name) + build()! } pub fn (mut self GridDriverInstaller) destroy() ! { - switch(self.name) - destroy()! + switch(self.name) + destroy()! } - - -//switch instance to be used for griddriver +// switch instance to be used for griddriver pub fn switch(name string) { - griddriver_default = name + griddriver_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/virt/cloudhypervisor/cloudhypervisor_factory_.v b/lib/installers/virt/cloudhypervisor/cloudhypervisor_factory_.v index aea31c15..c828c5c9 100644 --- a/lib/installers/virt/cloudhypervisor/cloudhypervisor_factory_.v +++ b/lib/installers/virt/cloudhypervisor/cloudhypervisor_factory_.v @@ -1,11 +1,9 @@ module cloudhypervisor -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 ( cloudhypervisor_global map[string]&CloudHypervisor @@ -14,29 +12,103 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&CloudHypervisor { + return &CloudHypervisor{} +} + +@[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: 'cloudhypervisor.')! + 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 cloudhypervisor.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action cloudhypervisor.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 install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self CloudHypervisor) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self CloudHypervisor) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self CloudHypervisor) destroy() ! { + switch(self.name) + destroy()! +} + +// switch instance to be used for cloudhypervisor +pub fn switch(name string) { + cloudhypervisor_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/virt/docker/docker_factory_.v b/lib/installers/virt/docker/docker_factory_.v index ed3cc833..e9cc9147 100644 --- a/lib/installers/virt/docker/docker_factory_.v +++ b/lib/installers/virt/docker/docker_factory_.v @@ -1,217 +1,203 @@ module docker -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 ( - docker_global map[string]&DockerInstaller - docker_default string + docker_global map[string]&DockerInstaller + docker_default string ) /////////FACTORY @[params] -pub struct ArgsGet{ +pub struct ArgsGet { pub mut: - name string + name string } -pub fn get(args_ ArgsGet) !&DockerInstaller { - return &DockerInstaller{} +pub fn get(args_ ArgsGet) !&DockerInstaller { + return &DockerInstaller{} } @[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: 'docker.')! - 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 docker.destroy") - destroy()! - } - if other_action.name == "install"{ - console.print_debug("install action docker.install") - install()! - } - } - if other_action.name in ["start","stop","restart"]{ - mut p := other_action.params - name := p.get('name')! - mut docker_obj:=get(name:name)! - console.print_debug("action object:\n${docker_obj}") - if other_action.name == "start"{ - console.print_debug("install action docker.${other_action.name}") - docker_obj.start()! - } - - if other_action.name == "stop"{ - console.print_debug("install action docker.${other_action.name}") - docker_obj.stop()! - } - if other_action.name == "restart"{ - console.print_debug("install action docker.${other_action.name}") - docker_obj.restart()! - } - } - } + mut other_actions := plbook.find(filter: 'docker.')! + 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 docker.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action docker.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut docker_obj := get(name: name)! + console.print_debug('action object:\n${docker_obj}') + if other_action.name == 'start' { + console.print_debug('install action docker.${other_action.name}') + docker_obj.start()! + } + if other_action.name == 'stop' { + console.print_debug('install action docker.${other_action.name}') + docker_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action docker.${other_action.name}') + docker_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 DockerInstaller) start() ! { - switch(self.name) - if self.running()!{ - return - } + switch(self.name) + if self.running()! { + return + } - console.print_header('docker start') + console.print_header('docker 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 docker with ${zprocess.startuptype}...') + console.print_debug('starting docker 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('docker did not install properly.') + start_post()! + for _ in 0 .. 50 { + if self.running()! { + return + } + time.sleep(100 * time.millisecond) + } + return error('docker did not install properly.') } pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! { - switch(self.name) - self.install(args)! - self.start()! + switch(self.name) + self.install(args)! + self.start()! } pub fn (mut self DockerInstaller) 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 DockerInstaller) restart() ! { - switch(self.name) - self.stop()! - self.start()! + switch(self.name) + self.stop()! + self.start()! } pub fn (mut self DockerInstaller) 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 DockerInstaller) install(args InstallArgs) ! { - switch(self.name) - if args.reset || (!installed()!) { - install()! - } + switch(self.name) + if args.reset || (!installed()!) { + install()! + } } - pub fn (mut self DockerInstaller) destroy() ! { - switch(self.name) - self.stop() or {} - destroy()! + switch(self.name) + self.stop() or {} + destroy()! } - - -//switch instance to be used for docker +// switch instance to be used for docker pub fn switch(name string) { - docker_default = name + docker_default = name } - -//helpers +// helpers @[params] -pub struct DefaultConfigArgs{ - instance string = 'default' +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/virt/pacman/pacman_actions.v b/lib/installers/virt/pacman/pacman_actions.v index c6ceb308..0740282f 100644 --- a/lib/installers/virt/pacman/pacman_actions.v +++ b/lib/installers/virt/pacman/pacman_actions.v @@ -1,20 +1,26 @@ module pacman import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.core.pathlib import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.core import os // checks if a certain version or above is installed -fn installed_() !bool { - return osal.done_exists('install_pacman') +fn installed() !bool { + console.print_header('checking if pacman is installed') + res := os.execute('pacman -v') + if res.exit_code != 0 { + console.print_header('pacman is not installed') + return false + } + console.print_header('pacman is installed') + return true } // use https://archlinux.org/mirrorlist/ -fn install_() ! { - console.print_header('install pacman') +fn install() ! { + console.print_header('installing pacman') if core.platform()! == .arch { return @@ -24,78 +30,40 @@ fn install_() ! { return error('only ubuntu supported for this installer.') } - cmd := ' - - mkdir -p /etc/pacman.d/gnupg - - ' + mut cmd := 'apt update && apt upgrade -y' osal.execute_stdout(cmd)! - dest := '/etc/pacman.conf' - c := $tmpl('templates/pacman.conf') - os.write_file(dest, c)! + cmd = 'mkdir -p /tmp/pacman' + osal.execute_stdout(cmd)! - dest2 := '/etc/pacman.d/mirrorlist' - c2 := $tmpl('templates/mirrorlist') - pathlib.template_write(c2, dest2, true)! + cmd = 'cd /tmp/pacman && wget https://gitlab.com/trivoxel/utilities/deb-pacman/-/archive/${version}/deb-pacman-${version}.tar' + osal.execute_stdout(cmd)! - osal.package_install(' - arch-install-scripts - pacman-package-manager - ')! + cmd = 'cd /tmp/pacman && tar -xf deb-pacman-v1.0.tar' + osal.execute_stdout(cmd)! - url := 'https://gist.githubusercontent.com/despiegk/e56403ecba40f6057251c6cc609c4cf2/raw/1822c921e7282c491d8ac35f3719f51e234f1cbf/gistfile1.txt' - mut gpg_dest := osal.download( - url: url - minsize_kb: 1000 - reset: true - )! + cmd = 'cd /tmp/pacman/deb-pacman-v1.0 && chmod +x pacman && sudo mv pacman /usr/local/bin' + osal.execute_stdout(cmd)! - cmd2 := ' - - mkdir -p /tmp/keyrings - cd /tmp/keyrings - - wget https://archlinux.org/packages/core/any/archlinux-keyring/download -O archlinux-keyring.tar.zst - tar -xvf archlinux-keyring.tar.zst - - mkdir -p /usr/share/keyrings - cp usr/share/pacman/keyrings/archlinux.gpg /usr/share/keyrings/ - - pacman-key --init - pacman-key --populate archlinux - - gpg --import ${gpg_dest.path} - - # Clean up - rm -f pacman_keyring.asc - - #pacman-key --refresh-keys - pacman-key --update - - rm -rf /tmp/keyrings - - ' - osal.execute_stdout(cmd2)! - - // TODO: is not working well, is like it doesn;t write in right location - osal.done_set('install_pacman', 'OK')! - - console.print_header('install done') + console.print_header('pacman is installed') } -fn destroy_() ! { - osal.done_delete('install_pacman')! +fn destroy() ! { + console.print_header('uninstall pacman') - osal.package_remove(' - arch-install-scripts - pacman-package-manager - ')! + if core.platform()! == .arch { + return + } - // TODO: will need to remove more - osal.rm(' - pacman - /etc/pacman.d - /var/cache/pacman - ')! + if core.platform()! != .ubuntu { + return error('only ubuntu supported for this installer.') + } + + mut cmd := 'rm -rf /tmp/pacman' + osal.execute_stdout(cmd)! + + cmd = 'sudo rm -rf /usr/local/bin/pacman' + osal.execute_stdout(cmd)! + + console.print_header('pacman is uninstalled') } diff --git a/lib/installers/virt/pacman/pacman_factory_.v b/lib/installers/virt/pacman/pacman_factory_.v index ff91e19c..644bdf7b 100644 --- a/lib/installers/virt/pacman/pacman_factory_.v +++ b/lib/installers/virt/pacman/pacman_factory_.v @@ -1,11 +1,9 @@ module pacman -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 ( pacman_global map[string]&PacmanInstaller @@ -14,25 +12,98 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&PacmanInstaller { + return &PacmanInstaller{} +} + +@[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: 'pacman.')! + 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 pacman.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action pacman.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 install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self PacmanInstaller) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self PacmanInstaller) destroy() ! { + switch(self.name) + destroy()! +} + +// switch instance to be used for pacman +pub fn switch(name string) { + pacman_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/virt/pacman/pacman_model.v b/lib/installers/virt/pacman/pacman_model.v index 83b10541..d3d01837 100644 --- a/lib/installers/virt/pacman/pacman_model.v +++ b/lib/installers/virt/pacman/pacman_model.v @@ -1,25 +1,36 @@ module pacman -import freeflowuniverse.herolib.data.paramsparser -import os +import freeflowuniverse.herolib.data.encoderhero -pub const version = '' +pub const version = 'v1.0' 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 PacmanInstaller { pub mut: name string = 'default' } -fn obj_init(obj_ PacmanInstaller) !PacmanInstaller { - // 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_ PacmanInstaller) !PacmanInstaller { + 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 PacmanInstaller) !string { + return encoderhero.encode[PacmanInstaller](obj)! +} + +pub fn heroscript_loads(heroscript string) !PacmanInstaller { + mut obj := encoderhero.decode[PacmanInstaller](heroscript)! + return obj +} diff --git a/lib/installers/virt/podman/podman_factory_.v b/lib/installers/virt/podman/podman_factory_.v index e2c1396f..319145a3 100644 --- a/lib/installers/virt/podman/podman_factory_.v +++ b/lib/installers/virt/podman/podman_factory_.v @@ -1,12 +1,9 @@ module podman -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 ( podman_global map[string]&PodmanInstaller diff --git a/lib/installers/virt/youki/youki_factory_.v b/lib/installers/virt/youki/youki_factory_.v index 20bf5cd9..cb1493c3 100644 --- a/lib/installers/virt/youki/youki_factory_.v +++ b/lib/installers/virt/youki/youki_factory_.v @@ -1,11 +1,9 @@ module youki -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 ( youki_global map[string]&YoukiInstaller @@ -14,29 +12,103 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&YoukiInstaller { + return &YoukiInstaller{} +} + +@[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: 'youki.')! + 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 youki.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action youki.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 install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self YoukiInstaller) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self YoukiInstaller) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self YoukiInstaller) destroy() ! { + switch(self.name) + destroy()! +} + +// switch instance to be used for youki +pub fn switch(name string) { + youki_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/web/bun/bun_factory_.v b/lib/installers/web/bun/bun_factory_.v index 96095c9a..03fcc393 100644 --- a/lib/installers/web/bun/bun_factory_.v +++ b/lib/installers/web/bun/bun_factory_.v @@ -1,11 +1,9 @@ module bun -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 ( bun_global map[string]&Bun @@ -24,6 +22,36 @@ pub fn get(args_ ArgsGet) !&Bun { return &Bun{} } +@[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: 'bun.')! + 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 bun.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action bun.install') + install()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -72,3 +100,10 @@ pub fn (mut self Bun) destroy() ! { pub fn switch(name string) { bun_default = name } + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' +} diff --git a/lib/installers/web/caddy2/caddy_factory_.v b/lib/installers/web/caddy2/caddy_factory_.v index 5d63a500..9e1f0bc5 100644 --- a/lib/installers/web/caddy2/caddy_factory_.v +++ b/lib/installers/web/caddy2/caddy_factory_.v @@ -3,7 +3,6 @@ module caddy 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 @@ -15,35 +14,71 @@ __global ( /////////FACTORY -// set the model in mem and the config on the filesystem +@[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) !&CaddyServer { + mut context := base.context()! + mut args := args_get(args_) + mut obj := CaddyServer{} + if args.name !in caddy_global { + if !exists(args)! { + set(obj)! + } else { + heroscript := context.hero_config_get('caddy', args.name)! + mut obj_ := heroscript_loads(heroscript)! + set_in_mem(obj_)! + } + } + return caddy_global[args.name] or { + println(caddy_global) + // bug if we get here because should be in globals + panic('could not get config for caddy with name, is bug:${args.name}') + } +} + +// register the config for the future pub fn set(o CaddyServer) ! { + set_in_mem(o)! + mut context := base.context()! + heroscript := heroscript_dumps(o)! + context.hero_config_set('caddy', 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('caddy', args.name) +} + +pub fn delete(args_ ArgsGet) ! { + mut args := args_get(args_) + mut context := base.context()! + context.hero_config_delete('caddy', args.name)! + if args.name in caddy_global { + // del caddy_global[args.name] + } +} + +// only sets in mem, does not set as config +fn set_in_mem(o CaddyServer) ! { mut o2 := obj_init(o)! caddy_global[o.name] = &o2 caddy_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('caddy', 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('caddy', model.name)! - play(heroscript: heroscript)! -} - -// save the config to the filesystem in the context -pub fn save(o CaddyServer) ! { - mut context := base.context()! - heroscript := encoderhero.encode[CaddyServer](o)! - context.hero_config_set('caddy', model.name, heroscript)! -} - @[params] pub struct PlayArgs { pub mut: @@ -53,21 +88,16 @@ pub mut: } pub fn play(args_ PlayArgs) ! { - mut model := args_ + mut args := args_ - if model.heroscript == '' { - model.heroscript = heroscript_default()! - } - mut plbook := model.plbook or { playbook.new(text: model.heroscript)! } + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut configure_actions := plbook.find(filter: 'caddy.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 caddy.configure\n${mycfg}') - set(mycfg)! - save(mycfg)! + mut install_actions := plbook.find(filter: 'caddy.configure')! + if install_actions.len > 0 { + for install_action in install_actions { + heroscript := install_action.heroscript() + mut obj2 := heroscript_loads(heroscript)! + set(obj2)! } } @@ -78,11 +108,11 @@ pub fn play(args_ PlayArgs) ! { reset := p.get_default_false('reset') if other_action.name == 'destroy' || reset { console.print_debug('install action caddy.destroy') - destroy_()! + destroy()! } if other_action.name == 'install' { console.print_debug('install action caddy.install') - install_()! + install()! } } if other_action.name in ['start', 'stop', 'restart'] { @@ -111,12 +141,6 @@ pub fn play(args_ PlayArgs) ! { //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// -// load from disk and make sure is properly intialized -pub fn (mut self CaddyServer) reload() ! { - switch(self.name) - self = obj_init(self)! -} - fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager { // unknown // screen @@ -139,6 +163,12 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag } } +// load from disk and make sure is properly intialized +pub fn (mut self CaddyServer) reload() ! { + switch(self.name) + self = obj_init(self)! +} + pub fn (mut self CaddyServer) start() ! { switch(self.name) if self.running()! { @@ -147,8 +177,8 @@ pub fn (mut self CaddyServer) start() ! { console.print_header('caddy start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -176,9 +206,9 @@ pub fn (mut self CaddyServer) start() ! { return error('caddy did not install properly.') } -pub fn (mut self CaddyServer) install_start(model InstallArgs) ! { +pub fn (mut self CaddyServer) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -218,19 +248,27 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self CaddyServer) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self CaddyServer) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! } -pub fn build() ! { - build_()! +// switch instance to be used for caddy +pub fn switch(name string) { + caddy_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/web/imagemagick/imagemagick_factory_.v b/lib/installers/web/imagemagick/imagemagick_factory_.v index 034069f9..956c44d5 100644 --- a/lib/installers/web/imagemagick/imagemagick_factory_.v +++ b/lib/installers/web/imagemagick/imagemagick_factory_.v @@ -1,6 +1,5 @@ module imagemagick -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&ImageMagick { + return &ImageMagick{} +} + +@[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: 'imagemagick.')! + 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 imagemagick.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action imagemagick.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut imagemagick_obj := get(name: name)! + console.print_debug('action object:\n${imagemagick_obj}') + if other_action.name == 'start' { + console.print_debug('install action imagemagick.${other_action.name}') + imagemagick_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action imagemagick.${other_action.name}') + imagemagick_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action imagemagick.${other_action.name}') + imagemagick_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self ImageMagick) start() ! { console.print_header('imagemagick start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self ImageMagick) start() ! { return error('imagemagick did not install properly.') } -pub fn (mut self ImageMagick) install_start(model InstallArgs) ! { +pub fn (mut self ImageMagick) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self ImageMagick) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self ImageMagick) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self ImageMagick) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for imagemagick +pub fn switch(name string) { + imagemagick_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/web/lighttpd/lighttpd_factory_.v b/lib/installers/web/lighttpd/lighttpd_factory_.v index f68c9f6c..e1a59199 100644 --- a/lib/installers/web/lighttpd/lighttpd_factory_.v +++ b/lib/installers/web/lighttpd/lighttpd_factory_.v @@ -1,6 +1,5 @@ module lighttpd -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager @@ -14,6 +13,65 @@ __global ( /////////FACTORY +@[params] +pub struct ArgsGet { +pub mut: + name string +} + +pub fn get(args_ ArgsGet) !&LightHttpdInstaller { + return &LightHttpdInstaller{} +} + +@[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: 'lighttpd.')! + 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 lighttpd.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action lighttpd.install') + install()! + } + } + if other_action.name in ['start', 'stop', 'restart'] { + mut p := other_action.params + name := p.get('name')! + mut lighttpd_obj := get(name: name)! + console.print_debug('action object:\n${lighttpd_obj}') + if other_action.name == 'start' { + console.print_debug('install action lighttpd.${other_action.name}') + lighttpd_obj.start()! + } + + if other_action.name == 'stop' { + console.print_debug('install action lighttpd.${other_action.name}') + lighttpd_obj.stop()! + } + if other_action.name == 'restart' { + console.print_debug('install action lighttpd.${other_action.name}') + lighttpd_obj.restart()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,8 +106,8 @@ pub fn (mut self LightHttpdInstaller) start() ! { console.print_header('lighttpd start') - if !installed_()! { - install_()! + if !installed()! { + install()! } configure()! @@ -77,9 +135,9 @@ pub fn (mut self LightHttpdInstaller) start() ! { return error('lighttpd did not install properly.') } -pub fn (mut self LightHttpdInstaller) install_start(model InstallArgs) ! { +pub fn (mut self LightHttpdInstaller) install_start(args InstallArgs) ! { switch(self.name) - self.install(model)! + self.install(args)! self.start()! } @@ -119,19 +177,32 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! +pub fn (mut self LightHttpdInstaller) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! } } -pub fn destroy() ! { - destroy_()! +pub fn (mut self LightHttpdInstaller) build() ! { + switch(self.name) + build()! } -pub fn build() ! { - build_()! +pub fn (mut self LightHttpdInstaller) destroy() ! { + switch(self.name) + self.stop() or {} + destroy()! +} + +// switch instance to be used for lighttpd +pub fn switch(name string) { + lighttpd_default = name +} + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/installers/web/tailwind/tailwind_actions.v b/lib/installers/web/tailwind/tailwind_actions.v index 7317e980..afe2101b 100644 --- a/lib/installers/web/tailwind/tailwind_actions.v +++ b/lib/installers/web/tailwind/tailwind_actions.v @@ -4,12 +4,12 @@ import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.core import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.core.texttools +import freeflowuniverse.herolib.installers.ulist import os - -pub const version = '3.4.12' +//////////////////// 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('tailwind -h') if res.exit_code == 0 { r := res.output.split_into_lines().filter(it.contains('tailwindcss v')) @@ -26,7 +26,21 @@ fn installed_() !bool { return false } -pub fn install_() ! { +// 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: 'tailwind' + // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/tailwind' + // )! +} + +fn install() ! { console.print_header('install tailwind') mut url := '' @@ -54,5 +68,4 @@ pub fn install_() ! { )! } -fn destroy_() ! { -} +fn destroy() ! {} diff --git a/lib/installers/web/tailwind/tailwind_factory_.v b/lib/installers/web/tailwind/tailwind_factory_.v index 07af5f5e..d9e998bb 100644 --- a/lib/installers/web/tailwind/tailwind_factory_.v +++ b/lib/installers/web/tailwind/tailwind_factory_.v @@ -1,11 +1,9 @@ module tailwind -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 freeflowuniverse.herolib.ui.console -import time __global ( tailwind_global map[string]&Tailwind @@ -17,13 +15,43 @@ __global ( @[params] pub struct ArgsGet { pub mut: - name string = 'default' + name string } pub fn get(args_ ArgsGet) !&Tailwind { return &Tailwind{} } +@[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: 'tailwind.')! + 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 tailwind.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action tailwind.install') + install()! + } + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -58,18 +86,24 @@ pub mut: pub fn (mut self Tailwind) install(args InstallArgs) ! { switch(self.name) - if args.reset || (!installed_()!) { - install_()! + if args.reset || (!installed()!) { + install()! } } pub fn (mut self Tailwind) destroy() ! { switch(self.name) - - destroy_()! + destroy()! } // switch instance to be used for tailwind pub fn switch(name string) { tailwind_default = name } + +// helpers + +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' +} diff --git a/lib/installers/web/tailwind/tailwind_model.v b/lib/installers/web/tailwind/tailwind_model.v index 44df9747..307574b1 100644 --- a/lib/installers/web/tailwind/tailwind_model.v +++ b/lib/installers/web/tailwind/tailwind_model.v @@ -1,25 +1,36 @@ module tailwind -import freeflowuniverse.herolib.data.paramsparser -import os +import freeflowuniverse.herolib.data.encoderhero +pub const version = '3.4.12' 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 Tailwind { pub mut: name string = 'default' } -fn obj_init(obj_ Tailwind) !Tailwind { - // 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_ Tailwind) !Tailwind { + 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 Tailwind) !string { + return encoderhero.encode[Tailwind](obj)! +} + +pub fn heroscript_loads(heroscript string) !Tailwind { + mut obj := encoderhero.decode[Tailwind](heroscript)! + return obj +} diff --git a/lib/installers/web/traefik/traefik_factory_.v b/lib/installers/web/traefik/traefik_factory_.v index aa4cb3a3..32a5a6e2 100644 --- a/lib/installers/web/traefik/traefik_factory_.v +++ b/lib/installers/web/traefik/traefik_factory_.v @@ -3,7 +3,6 @@ module traefik 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 diff --git a/lib/installers/web/zola/zola_factory_.v b/lib/installers/web/zola/zola_factory_.v index 30ae6c04..a1d7fb8f 100644 --- a/lib/installers/web/zola/zola_factory_.v +++ b/lib/installers/web/zola/zola_factory_.v @@ -1,11 +1,9 @@ module zola -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 ( zola_global map[string]&ZolaInstaller @@ -20,67 +18,97 @@ pub mut: name string } -fn args_get(args_ ArgsGet) ArgsGet { - mut model := args_ - if model.name == '' { - model.name = zola_default - } - if model.name == '' { - model.name = 'default' - } - return model +pub fn get(args_ ArgsGet) !&ZolaInstaller { + return &ZolaInstaller{} } -pub fn get(args_ ArgsGet) !&ZolaInstaller { - mut args := args_get(args_) - if args.name !in zola_global { - if args.name == 'default' { - if !config_exists(args) { - if default { - mut context := base.context() or { panic('bug') } - context.hero_config_set('zola', model.name, heroscript_default()!)! - } +@[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: 'zola.')! + 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 zola.destroy') + destroy()! + } + if other_action.name == 'install' { + console.print_debug('install action zola.install') + install()! } - load(args)! } } - return zola_global[args.name] or { - println(zola_global) - panic('could not get config for ${args.name} with name:${model.name}') - } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////# 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 ZolaInstaller) install(args InstallArgs) ! { + switch(self.name) + if args.reset || (!installed()!) { + install()! + } +} + +pub fn (mut self ZolaInstaller) build() ! { + switch(self.name) + build()! +} + +pub fn (mut self ZolaInstaller) destroy() ! { + switch(self.name) + destroy()! +} + // switch instance to be used for zola pub fn switch(name string) { zola_default = name } -pub fn (mut self ZolaInstaller) install(args InstallArgs) ! { - switch(self.name) - if args.reset { - destroy_()! - } - if !(installed_()!) { - install_()! - } -} +// helpers -pub fn (mut self ZolaInstaller) build() ! { - switch(self.name) - build_()! -} - -pub fn (mut self ZolaInstaller) destroy() ! { - switch(self.name) - destroy_()! +@[params] +pub struct DefaultConfigArgs { + instance string = 'default' } diff --git a/lib/lang/python/python.v b/lib/lang/python/python.v index 9227c845..1603b7db 100644 --- a/lib/lang/python/python.v +++ b/lib/lang/python/python.v @@ -94,6 +94,31 @@ pub fn (py PythonEnv) update() ! { console.print_debug('Pip update complete') } +// comma separated list of packages to uninstall +pub fn (mut py PythonEnv) pip_uninstall(packages string) ! { + mut to_uninstall := []string{} + for i in packages.split(',') { + pip := i.trim_space() + if !py.pips_done_check(pip)! { + to_uninstall << pip + console.print_debug('Package to uninstall: ${pip}') + } + } + + if to_uninstall.len == 0 { + return + } + + console.print_debug('uninstalling Python packages: ${packages}') + packages2 := to_uninstall.join(' ') + cmd := ' + cd ${py.path.path} + source bin/activate + pip3 uninstall ${packages2} -q + ' + osal.exec(cmd: cmd)! +} + // comma separated list of packages to install pub fn (mut py PythonEnv) pip(packages string) ! { mut to_install := []string{} diff --git a/lib/osal/startupmanager/startupmanager.v b/lib/osal/startupmanager/startupmanager.v index c3730f1c..95b33e1f 100644 --- a/lib/osal/startupmanager/startupmanager.v +++ b/lib/osal/startupmanager/startupmanager.v @@ -222,7 +222,7 @@ pub fn (mut sm StartupManager) delete(name string) ! { } } else { - panic('to implement, startup manager only support screen & systemd for now ${mycat}') + panic('to implement, startup manager only support screen & systemd for now ${sm.cat}') } } } @@ -280,7 +280,7 @@ pub fn (mut sm StartupManager) status(name string) !ProcessStatus { } } else { - panic('to implement, startup manager only support screen & systemd for now ${mycat}') + panic('to implement, startup manager only support screen & systemd for now ${sm.cat}') } } } @@ -303,7 +303,7 @@ pub fn (mut sm StartupManager) output(name string) !string { return systemd.journalctl(service: name)! } else { - panic('to implement, startup manager only support screen & systemd for now ${mycat}') + panic('to implement, startup manager only support screen & systemd for now ${sm.cat}') } } } @@ -326,7 +326,7 @@ pub fn (mut sm StartupManager) exists(name string) !bool { return zinitfactory.exists(name) } else { - panic('to implement. startup manager only support screen & systemd for now ${mycat}') + panic('to implement. startup manager only support screen & systemd for now ${sm.cat}') } } } @@ -347,7 +347,7 @@ pub fn (mut sm StartupManager) list() ![]string { return zinitfactory.names() } else { - panic('to implement. startup manager only support screen & systemd for now: ${mycat}') + panic('to implement. startup manager only support screen & systemd for now: ${sm.cat}') } } } diff --git a/lib/osal/systemd/systemd_process.v b/lib/osal/systemd/systemd_process.v index 78f40ae0..efd1e9e6 100644 --- a/lib/osal/systemd/systemd_process.v +++ b/lib/osal/systemd/systemd_process.v @@ -37,9 +37,6 @@ pub fn (mut self SystemdProcess) write() ! { servicecontent := $tmpl('templates/service.yaml') - println(self) - println(servicecontent) - p.write(servicecontent)! } @@ -54,6 +51,7 @@ pub fn (mut self SystemdProcess) start() ! { _ = osal.execute_silent(cmd)! self.refresh()! + console.print_header('started systemd process: ${self.name}') } // get status from system