WIP: Building hero
- The work is still in progress Co-authored-by: supermario <mariobassem12@gmail.com>
This commit is contained in:
13
lib/installers/sysadmintools/daguserver/.heroscript
Normal file
13
lib/installers/sysadmintools/daguserver/.heroscript
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
!!hero_code.generate_installer
|
||||
name:'daguserver'
|
||||
classname:'DaguInstaller'
|
||||
singleton:1
|
||||
templates:1
|
||||
default:1
|
||||
title:''
|
||||
supported_platforms:''
|
||||
reset:0
|
||||
startupmanager:1
|
||||
hasconfig:1
|
||||
build:0
|
||||
1
lib/installers/sysadmintools/daguserver/dagu_test.v
Normal file
1
lib/installers/sysadmintools/daguserver/dagu_test.v
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
152
lib/installers/sysadmintools/daguserver/daguserver_actions.v
Normal file
152
lib/installers/sysadmintools/daguserver/daguserver_actions.v
Normal file
@@ -0,0 +1,152 @@
|
||||
module daguserver
|
||||
|
||||
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.develop.gittools
|
||||
import freeflowuniverse.herolib.osal.zinit
|
||||
import freeflowuniverse.herolib.crypt.secrets
|
||||
import os
|
||||
|
||||
// checks if a certain version or above is installed
|
||||
fn installed() !bool {
|
||||
res := os.execute('${osal.profile_path_source_and()!} dagu version')
|
||||
if res.exit_code == 0 {
|
||||
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
|
||||
if r.len != 1 {
|
||||
return error("couldn't parse dagu version.\n${res.output}")
|
||||
}
|
||||
if texttools.version(version) > texttools.version(r[0]) {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn install() ! {
|
||||
console.print_header('install daguserver')
|
||||
mut url := ''
|
||||
if core.is_linux_arm()! {
|
||||
url = 'https://github.com/dagu-dev/dagu/releases/download/v${version}/dagu_${version}_linux_arm64.tar.gz'
|
||||
} else if core.is_linux_intel()! {
|
||||
url = 'https://github.com/dagu-dev/dagu/releases/download/v${version}/dagu_${version}_linux_amd64.tar.gz'
|
||||
} else if core.is_osx_arm()! {
|
||||
url = 'https://github.com/dagu-dev/dagu/releases/download/v${version}/dagu_${version}_darwin_arm64.tar.gz'
|
||||
} else if core.is_osx_intel()! {
|
||||
url = 'https://github.com/dagu-dev/dagu/releases/download/v${version}/dagu_${version}_darwin_amd64.tar.gz'
|
||||
} else {
|
||||
return error('unsported platform')
|
||||
}
|
||||
|
||||
mut dest := osal.download(
|
||||
url: url
|
||||
minsize_kb: 9000
|
||||
expand_dir: '/tmp/dagu'
|
||||
)!
|
||||
|
||||
mut binpath := dest.file_get('dagu')!
|
||||
osal.cmd_add(
|
||||
cmdname: 'dagu'
|
||||
source: binpath.path
|
||||
)!
|
||||
}
|
||||
|
||||
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
|
||||
systemctl disable daguserver.service
|
||||
systemctl stop daguserver_scheduler.service
|
||||
systemctl stop daguserver.service
|
||||
|
||||
systemctl list-unit-files | grep daguserver
|
||||
|
||||
pkill -9 -f daguserver
|
||||
|
||||
ps aux | grep daguserver
|
||||
|
||||
'
|
||||
|
||||
osal.execute_silent(cmd) or {}
|
||||
}
|
||||
|
||||
fn start_pre() ! {
|
||||
}
|
||||
|
||||
fn start_post() ! {
|
||||
}
|
||||
|
||||
fn stop_pre() ! {
|
||||
}
|
||||
|
||||
fn stop_post() ! {
|
||||
}
|
||||
266
lib/installers/sysadmintools/daguserver/daguserver_factory_.v
Normal file
266
lib/installers/sysadmintools/daguserver/daguserver_factory_.v
Normal file
@@ -0,0 +1,266 @@
|
||||
module daguserver
|
||||
|
||||
import freeflowuniverse.herolib.core.base
|
||||
import freeflowuniverse.herolib.core.playbook
|
||||
import freeflowuniverse.herolib.sysadmin.startupmanager
|
||||
import freeflowuniverse.herolib.osal.zinit
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import time
|
||||
|
||||
__global (
|
||||
daguserver_global map[string]&DaguInstaller
|
||||
daguserver_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 = daguserver_default
|
||||
}
|
||||
if args.name == '' {
|
||||
args.name = 'default'
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
pub fn get(args_ ArgsGet) !&DaguInstaller {
|
||||
mut args := args_get(args_)
|
||||
if args.name !in daguserver_global {
|
||||
if args.name == 'default' {
|
||||
if !config_exists(args) {
|
||||
if default {
|
||||
config_save(args)!
|
||||
}
|
||||
}
|
||||
config_load(args)!
|
||||
}
|
||||
}
|
||||
return daguserver_global[args.name] or {
|
||||
println(daguserver_global)
|
||||
panic('could not get config for daguserver with name:${args.name}')
|
||||
}
|
||||
}
|
||||
|
||||
fn config_exists(args_ ArgsGet) bool {
|
||||
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) ! {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context()!
|
||||
mut heroscript := context.hero_config_get('daguserver', args.name)!
|
||||
play(heroscript: heroscript)!
|
||||
}
|
||||
|
||||
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) ! {
|
||||
mut o2 := obj_init(o)!
|
||||
daguserver_global[o.name] = &o2
|
||||
daguserver_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_
|
||||
|
||||
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)!
|
||||
}
|
||||
}
|
||||
|
||||
mut other_actions := plbook.find(filter: 'daguserver.')!
|
||||
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 daguserver.destroy')
|
||||
destroy()!
|
||||
}
|
||||
if other_action.name == 'install' {
|
||||
console.print_debug('install action daguserver.install')
|
||||
install()!
|
||||
}
|
||||
}
|
||||
if other_action.name in ['start', 'stop', 'restart'] {
|
||||
mut p := other_action.params
|
||||
name := p.get('name')!
|
||||
mut daguserver_obj := get(name: name)!
|
||||
console.print_debug('action object:\n${daguserver_obj}')
|
||||
if other_action.name == 'start' {
|
||||
console.print_debug('install action daguserver.${other_action.name}')
|
||||
daguserver_obj.start()!
|
||||
}
|
||||
|
||||
if other_action.name == 'stop' {
|
||||
console.print_debug('install action daguserver.${other_action.name}')
|
||||
daguserver_obj.stop()!
|
||||
}
|
||||
if other_action.name == 'restart' {
|
||||
console.print_debug('install action daguserver.${other_action.name}')
|
||||
daguserver_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 DaguInstaller) reload() ! {
|
||||
switch(self.name)
|
||||
self = obj_init(self)!
|
||||
}
|
||||
|
||||
pub fn (mut self DaguInstaller) start() ! {
|
||||
switch(self.name)
|
||||
if self.running()! {
|
||||
return
|
||||
}
|
||||
|
||||
console.print_header('daguserver start')
|
||||
|
||||
if !installed()! {
|
||||
install()!
|
||||
}
|
||||
|
||||
configure()!
|
||||
|
||||
start_pre()!
|
||||
|
||||
for zprocess in startupcmd()! {
|
||||
mut sm := startupmanager_get(zprocess.startuptype)!
|
||||
|
||||
console.print_debug('starting daguserver 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('daguserver did not install properly.')
|
||||
}
|
||||
|
||||
pub fn (mut self DaguInstaller) install_start(args InstallArgs) ! {
|
||||
switch(self.name)
|
||||
self.install(args)!
|
||||
self.start()!
|
||||
}
|
||||
|
||||
pub fn (mut self DaguInstaller) 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 DaguInstaller) restart() ! {
|
||||
switch(self.name)
|
||||
self.stop()!
|
||||
self.start()!
|
||||
}
|
||||
|
||||
pub fn (mut self DaguInstaller) 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 DaguInstaller) install(args InstallArgs) ! {
|
||||
switch(self.name)
|
||||
if args.reset || (!installed()!) {
|
||||
install()!
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut self DaguInstaller) destroy() ! {
|
||||
switch(self.name)
|
||||
self.stop() or {}
|
||||
destroy()!
|
||||
}
|
||||
|
||||
// switch instance to be used for daguserver
|
||||
pub fn switch(name string) {
|
||||
daguserver_default = name
|
||||
}
|
||||
60
lib/installers/sysadmintools/daguserver/daguserver_model.v
Normal file
60
lib/installers/sysadmintools/daguserver/daguserver_model.v
Normal file
@@ -0,0 +1,60 @@
|
||||
module daguserver
|
||||
|
||||
import freeflowuniverse.herolib.data.paramsparser
|
||||
import os
|
||||
|
||||
pub const version = '1.14.3'
|
||||
const singleton = true
|
||||
const default = true
|
||||
|
||||
pub fn heroscript_default() !string {
|
||||
heroscript := "
|
||||
!!daguserver.configure
|
||||
name:'daguserver'
|
||||
title: 'My Hero DAG'
|
||||
host: 'localhost'
|
||||
port: 8888
|
||||
"
|
||||
|
||||
return heroscript
|
||||
}
|
||||
|
||||
pub struct DaguInstaller {
|
||||
pub mut:
|
||||
name string = 'default'
|
||||
|
||||
dagsdir string
|
||||
configpath string
|
||||
username string
|
||||
password string @[secret]
|
||||
secret string @[secret]
|
||||
title string
|
||||
host string
|
||||
port int
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
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_
|
||||
return obj
|
||||
}
|
||||
48
lib/installers/sysadmintools/daguserver/model_comms.v
Normal file
48
lib/installers/sysadmintools/daguserver/model_comms.v
Normal file
@@ -0,0 +1,48 @@
|
||||
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)!
|
||||
// }
|
||||
}
|
||||
36
lib/installers/sysadmintools/daguserver/readme.md
Normal file
36
lib/installers/sysadmintools/daguserver/readme.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# daguserver
|
||||
|
||||
|
||||
|
||||
To get started
|
||||
|
||||
```vlang
|
||||
|
||||
|
||||
|
||||
import freeflowuniverse.herolib.installers.something. daguserver
|
||||
|
||||
mut installer:= daguserver.get()!
|
||||
|
||||
installer.start()!
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
## example heroscript
|
||||
|
||||
|
||||
```hero
|
||||
!!daguserver.install
|
||||
homedir: '/home/user/daguserver'
|
||||
username: 'admin'
|
||||
password: 'secretpassword'
|
||||
title: 'Some Title'
|
||||
host: 'localhost'
|
||||
port: 8888
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# directory path to save logs from standard output
|
||||
logDir: @{config.log_dir}
|
||||
|
||||
# history retention days (default: 30)
|
||||
histRetentionDays: @{config.history_retention_days}
|
||||
|
||||
# Email notification settings
|
||||
mailOn:
|
||||
failure: @{config.mail_on.failure}
|
||||
success: @{config.mail_on.success}
|
||||
|
||||
# SMTP server settings
|
||||
smtp:
|
||||
host: @{config.smtp.host}
|
||||
port: @{config.smtp.port}
|
||||
username: @{config.smtp.username}
|
||||
password: @{config.smtp.password}
|
||||
|
||||
# Error mail configuration
|
||||
errorMail:
|
||||
from: @{config.error_mail.from}
|
||||
to: @{config.error_mail.to}
|
||||
prefix: @{config.error_mail.prefix}
|
||||
|
||||
# Info mail configuration
|
||||
infoMail:
|
||||
from: @{config.info_mail.from}
|
||||
to: @{config.info_mail.to}
|
||||
prefix: @{config.info_mail.prefix}
|
||||
27
lib/installers/sysadmintools/daguserver/templates/dagu.yaml
Normal file
27
lib/installers/sysadmintools/daguserver/templates/dagu.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
host: "${cfg.host}" # default: 127.0.0.1
|
||||
port: ${cfg.port}
|
||||
|
||||
# path to the DAGs directory
|
||||
dags: ${cfg.dagsdir}
|
||||
|
||||
# Web UI Color & Title
|
||||
# navbarColor: <ui header color> # header color for web UI (e.g. "#ff0000")
|
||||
navbarTitle: ${cfg.title} # header title for web UI (e.g. "PROD")
|
||||
|
||||
isBasicAuth: true
|
||||
basicAuthUsername: ${cfg.username}
|
||||
basicAuthPassword: ${cfg.password}
|
||||
|
||||
isAuthToken: true # enables API token
|
||||
authToken: ${cfg.secret}
|
||||
|
||||
# Base Config
|
||||
# baseConfig:
|
||||
|
||||
# Working Directory
|
||||
# workDir: # default: DAG location
|
||||
|
||||
# SSL Configuration
|
||||
# tls:
|
||||
# certFile: <path to SSL certificate file>
|
||||
# keyFile: <path to SSL key file>
|
||||
Reference in New Issue
Block a user