commetbft

This commit is contained in:
2025-03-03 17:32:28 -05:00
parent 6bbaa0d1f7
commit fb9c9b8070
25 changed files with 735 additions and 352 deletions

View File

@@ -0,0 +1,13 @@
!!hero_code.generate_installer
name:'cometbft'
classname:'CometBFT'
singleton:0
templates:1
default:1
title:''
supported_platforms:''
reset:0
startupmanager:1
hasconfig:1
build:1

View File

@@ -0,0 +1,184 @@
module cometbft
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 os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut installer := get()!
mut res := []zinit.ZProcessNewArgs{}
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res << zinit.ZProcessNewArgs{
// name: 'cometbft'
// cmd: 'cometbft server'
// env: {
// 'HOME': '/root'
// }
// }
return res
}
fn running() !bool {
mut installer := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of cometbft
// 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: 'cometbft', 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('cometbft 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()!} cometbft 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 cometbft version.\n${res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
// }
return false
}
fn ulist_get() !ulist.UList {
return ulist.UList{}
}
// uploads to S3 server if configured
fn upload() ! {
// installers.upload(
// cmdname: 'cometbft'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/cometbft'
// )!
}
fn install() ! {
console.print_header('install cometbft')
mut url := ''
if core.is_linux_arm() {
url = 'https://github.com/cometbft/cometbft/releases/download/v${version}/cometbft_${version}_linux_arm64.tar.gz'
} else if core.is_linux_intel() {
url = 'https://github.com/cometbft/cometbft/releases/download/v${version}/cometbft_${version}_linux_amd64.tar.gz'
} else if core.is_osx_arm() {
url = 'https://github.com/cometbft/cometbft/releases/download/v${version}/cometbft_${version}_darwin_arm64.tar.gz'
} else if osal.is_osx_intel() {
url = 'https://github.com/cometbft/cometbft/releases/download/v${version}/cometbft_${version}_darwin_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 9000
expand_dir: '/tmp/cometbft'
)!
//dest.moveup_single_subdir()!
mut binpath := dest.file_get('cometbft')!
osal.cmd_add(
cmdname: 'cometbft'
source: binpath.path
)!
}
fn build() ! {
// url := 'https://github.com/threefoldtech/cometbft'
// 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 cometbft')
// gitpath := gittools.get_repo(coderoot: '/tmp/builder', url: url, reset: true, pull: true)!
// cmd := '
// cd ${gitpath}
// source ~/.cargo/env
// exit 1 #todo
// '
// osal.execute_stdout(cmd)!
//
// //now copy to the default bin path
// mut binpath := dest.file_get('...')!
// adds it to path
// osal.cmd_add(
// cmdname: 'griddriver2'
// source: binpath.path
// )!
}
fn destroy() ! {
// mut systemdfactory := systemd.new()!
// systemdfactory.destroy("zinit")!
// osal.process_kill_recursive(name:'zinit')!
// osal.cmd_delete('zinit')!
// osal.package_remove('
// podman
// conmon
// buildah
// skopeo
// runc
// ')!
// //will remove all paths where go/bin is found
// osal.profile_path_add_remove(paths2delete:"go/bin")!
// osal.rm("
// podman
// conmon
// buildah
// skopeo
// runc
// /var/lib/containers
// /var/lib/podman
// /var/lib/buildah
// /tmp/podman
// /tmp/conmon
// ")!
}

View File

@@ -1,4 +1,4 @@
module caddy
module cometbft
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
@@ -8,8 +8,8 @@ import freeflowuniverse.herolib.osal.zinit
import time
__global (
caddy_global map[string]&CaddyServer
caddy_default string
cometbft_global map[string]&CometBFT
cometbft_default string
)
/////////FACTORY
@@ -28,55 +28,55 @@ fn args_get(args_ ArgsGet) ArgsGet {
return args
}
pub fn get(args_ ArgsGet) !&CaddyServer {
pub fn get(args_ ArgsGet) !&CometBFT {
mut context := base.context()!
mut args := args_get(args_)
mut obj := CaddyServer{}
if args.name !in caddy_global {
mut obj := CometBFT{}
if args.name !in cometbft_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('caddy', args.name)!
heroscript := context.hero_config_get('cometbft', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return caddy_global[args.name] or {
println(caddy_global)
return cometbft_global[args.name] or {
println(cometbft_global)
// bug if we get here because should be in globals
panic('could not get config for caddy with name, is bug:${args.name}')
panic('could not get config for cometbft with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o CaddyServer) ! {
pub fn set(o CometBFT) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('caddy', o.name, heroscript)!
context.hero_config_set('cometbft', 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)
return context.hero_config_exists('cometbft', 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]
context.hero_config_delete('cometbft', args.name)!
if args.name in cometbft_global {
// del cometbft_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o CaddyServer) ! {
fn set_in_mem(o CometBFT) ! {
mut o2 := obj_init(o)!
caddy_global[o.name] = &o2
caddy_default = o.name
cometbft_global[o.name] = &o2
cometbft_default = o.name
}
@[params]
@@ -92,7 +92,7 @@ pub fn play(args_ PlayArgs) ! {
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'caddy.configure')!
mut install_actions := plbook.find(filter: 'cometbft.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
@@ -101,37 +101,37 @@ pub fn play(args_ PlayArgs) ! {
}
}
mut other_actions := plbook.find(filter: 'caddy.')!
mut other_actions := plbook.find(filter: 'cometbft.')!
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 caddy.destroy')
console.print_debug('install action cometbft.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action caddy.install')
console.print_debug('install action cometbft.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut caddy_obj := get(name: name)!
console.print_debug('action object:\n${caddy_obj}')
mut cometbft_obj := get(name: name)!
console.print_debug('action object:\n${cometbft_obj}')
if other_action.name == 'start' {
console.print_debug('install action caddy.${other_action.name}')
caddy_obj.start()!
console.print_debug('install action cometbft.${other_action.name}')
cometbft_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action caddy.${other_action.name}')
caddy_obj.stop()!
console.print_debug('install action cometbft.${other_action.name}')
cometbft_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action caddy.${other_action.name}')
caddy_obj.restart()!
console.print_debug('install action cometbft.${other_action.name}')
cometbft_obj.restart()!
}
}
}
@@ -164,18 +164,18 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
// load from disk and make sure is properly intialized
pub fn (mut self CaddyServer) reload() ! {
pub fn (mut self CometBFT) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self CaddyServer) start() ! {
pub fn (mut self CometBFT) start() ! {
switch(self.name)
if self.running()! {
return
}
console.print_header('caddy start')
console.print_header('cometbft start')
if !installed()! {
install()!
@@ -188,7 +188,7 @@ pub fn (mut self CaddyServer) start() ! {
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting caddy with ${zprocess.startuptype}...')
console.print_debug('starting cometbft with ${zprocess.startuptype}...')
sm.new(zprocess)!
@@ -203,16 +203,16 @@ pub fn (mut self CaddyServer) start() ! {
}
time.sleep(100 * time.millisecond)
}
return error('caddy did not install properly.')
return error('cometbft did not install properly.')
}
pub fn (mut self CaddyServer) install_start(args InstallArgs) ! {
pub fn (mut self CometBFT) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self CaddyServer) stop() ! {
pub fn (mut self CometBFT) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()! {
@@ -222,13 +222,13 @@ pub fn (mut self CaddyServer) stop() ! {
stop_post()!
}
pub fn (mut self CaddyServer) restart() ! {
pub fn (mut self CometBFT) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self CaddyServer) running() !bool {
pub fn (mut self CometBFT) running() !bool {
switch(self.name)
// walk over the generic processes, if not running return
@@ -248,22 +248,27 @@ pub mut:
reset bool
}
pub fn (mut self CaddyServer) install(args InstallArgs) ! {
pub fn (mut self CometBFT) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self CaddyServer) destroy() ! {
pub fn (mut self CometBFT) build() ! {
switch(self.name)
build()!
}
pub fn (mut self CometBFT) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for caddy
// switch instance to be used for cometbft
pub fn switch(name string) {
caddy_default = name
cometbft_default = name
}
// helpers

View File

@@ -0,0 +1,52 @@
module cometbft
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 CometBFT {
pub mut:
name string = 'default'
// homedir string
// configpath string
// username string
// password string @[secret]
// title string
// host string
// port int
}
// your checking & initialization code if needed
fn obj_init(mycfg_ CometBFT) !CometBFT {
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()!
// mut mycode := $tmpl('templates/atemplate.yaml')
// mut path := pathlib.get_file(path: cfg.configpath, create: true)!
// path.write(mycode)!
// console.print_debug(mycode)
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj CometBFT) !string {
return encoderhero.encode[CometBFT](obj)!
}
pub fn heroscript_loads(heroscript string) !CometBFT {
mut obj := encoderhero.decode[CometBFT](heroscript)!
return obj
}

View File

@@ -0,0 +1,44 @@
# cometbft
To get started
```vlang
import freeflowuniverse.herolib.installers.db.cometbft as cometbft_installer
heroscript:="
!!cometbft.configure name:'test'
password: '1234'
port: 7701
!!cometbft.start name:'test' reset:1
"
cometbft_installer.play(heroscript=heroscript)!
//or we can call the default and do a start with reset
//mut installer:= cometbft_installer.get()!
//installer.start(reset:true)!
```
## example heroscript
```hero
!!cometbft.configure
homedir: '/home/user/cometbft'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
host: 'localhost'
port: 8888
```

View File

@@ -0,0 +1,5 @@
name: ${cfg.configpath}

View File

@@ -7,7 +7,6 @@
default:1
title:''
supported_platforms:''
reset:0
startupmanager:0
build:1
hasconfig:0

View File

@@ -9,7 +9,7 @@ import os
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('/bin/bash -c "griddriver --version"')
res := os.execute("griddriver --version")
if res.exit_code != 0 {
return false
}
@@ -26,7 +26,6 @@ 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{}
@@ -37,7 +36,7 @@ fn upload() ! {}
fn install() ! {
console.print_header('install griddriver')
build()!
panic("to implement")
console.print_header('install griddriver OK')
}

View File

@@ -1,11 +0,0 @@
!!hero_code.generate_installer
name:'caddy'
classname:'CaddyServer'
singleton:1 //there can only be 1 object in the globals, is called 'default'
templates:1 //are there templates for the installer
default:1 //can we create a default when the factory is used
title:''
supported_platforms:'' //osx, ... (empty means all)
reset:0 // regenerate all, dangerous !!!
startupmanager:1 //managed by a startup manager, default true

View File

@@ -1,172 +0,0 @@
module caddy
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.zinit
import os
// 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()!} caddy 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 caddy version.\n${res.output}")
// }
// if texttools.version(version) > texttools.version(r[0]) {
// return false
// }
return true
}
fn install_() ! {
console.print_header('install caddy')
mut cfg := get()!
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_arm64.tar.gz'
} else if core.is_linux_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_amd64.tar.gz'
} else if core.is_osx_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_arm64.tar.gz'
} else if core.is_osx_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 1000
expand_dir: '/tmp/xcaddy_dir'
)!
mut binpath := dest.file_get('xcaddy')!
osal.cmd_add(
cmdname: 'xcaddy'
source: binpath.path
)!
console.print_header('Installing Caddy with xcaddy')
plugins_str := cfg.plugins.map('--with ${it}').join(' ')
// Define the xcaddy command to build Caddy with plugins
path := '/tmp/caddyserver/caddy'
cmd := 'source ${osal.profile_path()!} && xcaddy build v${caddy_version} ${plugins_str} --output ${path}'
osal.exec(cmd: cmd)!
osal.cmd_add(
cmdname: 'caddy'
source: path
reset: true
)!
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
res << zinit.ZProcessNewArgs{
name: 'caddy'
cmd: 'caddy run --config /etc/caddy/Caddyfile'
}
return res
}
// user needs to us switch to make sure we get the right object
fn configure() ! {
mut cfg := get()!
if !os.exists('/etc/caddy/Caddyfile') {
// set the default caddyfile
configure_examples(path: cfg.path)!
}
}
// configure caddy as default webserver & start
// node, path, domain
// path e.g. /var/www
// domain e.g. www.myserver.com
pub fn configure_examples(config WebConfig) ! {
mut config_file := $tmpl('templates/caddyfile_default')
if config.domain.len > 0 {
config_file = $tmpl('templates/caddyfile_domain')
}
os.mkdir_all(config.path)!
default_html := '
<!DOCTYPE html>
<html>
<head>
<title>Caddy has now been installed.</title>
</head>
<body>
Caddy has been installed and is working in /var/www.
</body>
</html>
'
osal.file_write('${config.path}/index.html', default_html)!
configuration_set(content: config_file)!
}
fn running_() !bool {
mut cfg := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of caddy
// 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: 'caddy', 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('caddy is answering.')
return true
}
fn destroy_() ! {
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// cmd:="
// systemctl disable caddy_scheduler.service
// systemctl disable caddy.service
// systemctl stop caddy_scheduler.service
// systemctl stop caddy.service
// systemctl list-unit-files | grep caddy
// pkill -9 -f caddy
// ps aux | grep caddy
// "
// osal.exec(cmd: cmd, stdout:true, debug: false)!
}
fn obj_init() ! {
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}

View File

@@ -1,45 +0,0 @@
module caddy
import freeflowuniverse.herolib.data.paramsparser
pub const xcaddy_version = '0.4.2'
pub const caddy_version = '2.8.4'
const singleton = true
const default = true
const heroscript_default = "
!!caddy.configure
path: ''
domain: ''
plugins: ''
"
// CaddyServer represents a Caddy server configuration.
pub struct CaddyServer {
pub mut:
// name is the name of the Caddy server.
name string = 'default'
// path is the path to the server's root directory.
path string = '/var/www'
// domain is the default domain for the server.
domain string // sort of default domain
// plugins is a list of plugins to be used by the server.
plugins []string
}
fn cfg_play(p paramsparser.Params) ! {
mut mycfg := CaddyServer{
path: p.get_default('homedir', '{HOME}/hero/var/caddy')!
domain: p.get_default('configpath', '{HOME}/hero/var/caddy/admin.yaml')!
plugins: p.get_list_default('plugins', []string{})!
}
if mycfg.path == '' && mycfg.domain == '' {
return error('path or domain needs to be filled in for caddy')
}
set(mycfg)!
}

View File

@@ -1,129 +0,0 @@
module caddy
import freeflowuniverse.herolib.core.playbook
// import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.ui.console
import time
@[params]
pub struct InstallPlayArgs {
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_ InstallPlayArgs) ! {
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: 'caddy.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
cfg_play(p)!
}
}
}
// load from disk and make sure is properly intialized
// pub fn (mut self CaddyServer) reload() ! {
// switch(self.name)
// obj_init()!
// }
// pub fn (mut self CaddyServer) start() ! {
// switch(self.name)
// if self.running()! {
// return
// }
// console.print_header('caddy start')
// configure()!
// start_pre()!
// mut sm := startupmanager.get()!
// for zprocess in startupcmd()! {
// sm.start(zprocess.name)!
// }
// start_post()!
// for _ in 0 .. 50 {
// if self.running()! {
// return
// }
// time.sleep(100 * time.millisecond)
// }
// return error('caddy did not install properly.')
// }
// pub fn (mut self CaddyServer) install_start(args RestartArgs) ! {
// switch(self.name)
// self.install(args)!
// self.start()!
// }
// pub fn (mut self CaddyServer) stop() ! {
// switch(self.name)
// stop_pre()!
// mut sm := startupmanager.get()!
// for zprocess in startupcmd()! {
// sm.stop(zprocess.name)!
// }
// stop_post()!
// }
// pub fn (mut self CaddyServer) restart() ! {
// switch(self.name)
// self.stop()!
// self.start()!
// }
// pub fn (mut self CaddyServer) running() !bool {
// switch(self.name)
// mut sm := startupmanager.get()!
// // walk over the generic processes, if not running return
// for zprocess in startupcmd()! {
// r := sm.running(zprocess.name)!
// if r == false {
// return false
// }
// }
// return running()!
// }
// @[params]
// pub struct RestartArgs {
// pub mut:
// reset bool
// }
// pub fn (mut self CaddyServer) install(args RestartArgs) ! {
// switch(self.name)
// if args.reset || (!installed()!) {
// install()!
// }
// }
// pub fn (mut self CaddyServer) destroy() ! {
// switch(self.name)
// self.stop()!
// destroy()!
// }

View File

@@ -1,292 +0,0 @@
module caddy
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.installers.lang.golang
import os
pub const version = '2.8.4'
// @[params]
// pub struct InstallArgs {
// pub mut:
// reset bool
// start bool
// restart bool
// stop bool
// homedir string = '/var/www'
// file_path string // path to caddyfile
// file_url string // path to caddyfile
// xcaddy bool // wether to install caddy with xcaddy
// plugins []string // list of plugins to build caddy with
// }
// // install caddy will return true if it was already installed
// pub fn install_(args_ InstallArgs) ! {
// mut args := args_
// version := '2.8.4'
// installed := is_installed(version)!
// if args.reset || !installed {
// console.print_header('install caddy')
// if args.xcaddy || args.plugins.len > 0 {
// golang.install()!
// install_caddy_with_xcaddy(args.plugins)!
// } else {
// install_caddy_from_release()!
// }
// } else if args.plugins.any(!plugin_is_installed(it)!) {
// golang.install()!
// install_caddy_with_xcaddy(args.plugins)!
// }
// if args.restart {
// restart(args)!
// return
// }
// if args.start {
// start(args)!
// }
// }
pub fn is_installed(version string) !bool {
res := os.execute('${osal.profile_path_source_and()!} caddy version')
if res.exit_code == 0 {
mut r := res.output.split_into_lines().filter(it.trim_space().len > 0)
if r.len > 1 {
r = r.filter(it.starts_with('v'))
}
if r.len != 1 {
return error("couldn't parse caddy version.\n${r}")
}
if texttools.version(version) > texttools.version(r[0]) {
return false
}
return true
}
return false
}
pub fn install_caddy_from_release() ! {
version := '2.8.4'
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_linux_arm64.tar.gz'
} else if core.is_linux_intel()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_linux_amd64.tar.gz'
} else if core.is_osx_arm()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_darwin_arm64.tar.gz'
} else if core.is_osx_intel()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_darwin_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 10000
expand_dir: '/tmp/caddyserver'
)!
mut binpath := dest.file_get('caddy')!
osal.cmd_add(
cmdname: 'caddy'
source: binpath.path
)!
}
pub fn plugin_is_installed(plugin_ string) !bool {
plugin := plugin_.trim_space()
result := osal.exec(cmd: 'caddy list-modules --packages')!
mut lines := result.output.split('\n')
mut standardard_packages := []string{}
mut nonstandardard_packages := []string{}
mut standard := true
for mut line in lines {
line = line.trim_space()
if line == '' {
continue
}
if line.starts_with('Standard modules') {
standard = false
continue
}
package := line.all_after(' ')
if standard {
standardard_packages << package
} else {
nonstandardard_packages << package
}
}
return plugin in standardard_packages || plugin in nonstandardard_packages
}
pub fn install_caddy_with_xcaddy(plugins []string) ! {
xcaddy_version := '0.4.2'
caddy_version := '2.8.4'
console.print_header('Installing xcaddy')
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_arm64.tar.gz'
} else if core.is_linux_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_amd64.tar.gz'
} else if core.is_osx_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_arm64.tar.gz'
} else if core.is_osx_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 1000
expand_dir: '/tmp/xcaddy_dir'
)!
mut binpath := dest.file_get('xcaddy')!
osal.cmd_add(
cmdname: 'xcaddy'
source: binpath.path
)!
console.print_header('Installing Caddy with xcaddy')
plugins_str := plugins.map('--with ${it}').join(' ')
// Define the xcaddy command to build Caddy with plugins
path := '/tmp/caddyserver/caddy'
cmd := 'source ${osal.profile_path()!} && xcaddy build v${caddy_version} ${plugins_str} --output ${path}'
osal.exec(cmd: cmd)!
osal.cmd_add(
cmdname: 'caddy'
source: path
reset: true
)!
}
@[params]
pub struct WebConfig {
pub mut:
path string = '/var/www'
domain string
}
// configure caddy as default webserver & start
// node, path, domain
// path e.g. /var/www
// domain e.g. www.myserver.com
pub fn configure_examples(config WebConfig) ! {
mut config_file := $tmpl('templates/caddyfile_default')
if config.domain.len > 0 {
config_file = $tmpl('templates/caddyfile_domain')
}
install()!
os.mkdir_all(config.path)!
default_html := '
<!DOCTYPE html>
<html>
<head>
<title>Caddy has now been installed.</title>
</head>
<body>
Caddy has been installed and is working in /var/www.
</body>
</html>
'
osal.file_write('${config.path}/index.html', default_html)!
configuration_set(content: config_file)!
}
pub fn configuration_get() !string {
c := osal.file_read('/etc/caddy/Caddyfile')!
return c
}
@[params]
pub struct ConfigurationArgs {
pub mut:
content string // caddyfile content
path string
restart bool = true
}
pub fn configuration_set(args_ ConfigurationArgs) ! {
console.print_header('Caddy config set')
mut args := args_
if args.content == '' && args.path == '' {
return error('need to specify content or path.')
}
if args.content.len > 0 {
args.content = texttools.dedent(args.content)
if !os.exists('/etc/caddy') {
os.mkdir_all('/etc/caddy')!
}
osal.file_write('/etc/caddy/Caddyfile', args.content)!
} else {
mut p := pathlib.get_file(path: args.path, create: true)!
content := p.read()!
if !os.exists('/etc/caddy') {
os.mkdir_all('/etc/caddy')!
}
osal.file_write('/etc/caddy/Caddyfile', content)!
}
if args.restart {
restart()!
}
}
@[params]
pub struct StartArgs {
pub mut:
zinit bool
}
// start caddy
pub fn start(args_ InstallArgs) ! {
mut args := args_
console.print_header('caddy start')
if args.homedir == '' {
args.homedir = '/var/www'
}
if !os.exists('/etc/caddy/Caddyfile') {
// set the default caddyfile
configure_examples(path: args.homedir)!
}
cmd := 'caddy run --config /etc/caddy/Caddyfile'
mut sm := startupmanager.get()!
sm.new(
name: 'caddy'
cmd: cmd
start: true
)!
}
pub fn stop() ! {
console.print_header('Caddy Stop')
mut sm := startupmanager.get()!
sm.stop('caddy')!
}
pub fn restart(args InstallArgs) ! {
stop()!
start(args)!
}

View File

@@ -1,73 +0,0 @@
module caddy
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
pub fn install_caddy_from_release() ! {
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_linux_arm64.tar.gz'
} else if core.is_linux_intel()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_linux_amd64.tar.gz'
} else if core.is_osx_arm()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_darwin_arm64.tar.gz'
} else if core.is_osx_intel()! {
url = 'https://github.com/caddyserver/caddy/releases/download/v${version}/caddy_${version}_darwin_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 10000
expand_dir: '/tmp/caddyserver'
)!
mut binpath := dest.file_get('caddy')!
osal.cmd_add(
cmdname: 'caddy'
source: binpath.path
)!
}
pub fn install_caddy_with_xcaddy(plugins []string) ! {
console.print_header('Installing xcaddy')
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_arm64.tar.gz'
} else if core.is_linux_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_linux_amd64.tar.gz'
} else if core.is_osx_arm()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_arm64.tar.gz'
} else if core.is_osx_intel()! {
url = 'https://github.com/caddyserver/xcaddy/releases/download/v${xcaddy_version}/xcaddy_${xcaddy_version}_mac_amd64.tar.gz'
} else {
return error('unsported platform')
}
mut dest := osal.download(
url: url
minsize_kb: 1000
expand_dir: '/tmp/xcaddy_dir'
)!
mut binpath := dest.file_get('xcaddy')!
osal.cmd_add(
cmdname: 'xcaddy'
source: binpath.path
)!
console.print_header('Installing Caddy with xcaddy')
plugins_str := plugins.map('--with ${it}').join(' ')
// Define the xcaddy command to build Caddy with plugins
path := '/tmp/caddyserver/caddy'
cmd := 'source ${osal.profile_path()!} && xcaddy build v${caddy_version} ${plugins_str} --output ${path}'
osal.exec(cmd: cmd)!
osal.cmd_add(
cmdname: 'caddy'
source: path
reset: true
)!
}

View File

@@ -1,50 +0,0 @@
module caddy
import freeflowuniverse.herolib.core.playbook
import os
pub fn play(mut plbook playbook.PlayBook) ! {
// base.play(plbook)!
caddy_actions := plbook.find(filter: 'caddy.')!
if caddy_actions.len == 0 {
return
}
mut install_actions := plbook.find(filter: 'caddy.install')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
reset := p.get_default_false('reset')
start := p.get_default_true('start')
restart := p.get_default_false('restart')
stop := p.get_default_false('stop')
homedir := p.get_default('homedir', '${os.home_dir()}/hero/www')!
file_path := p.get_default('file_path', '')!
file_url := p.get_default('file_url', '')!
xcaddy := p.get_default_false('xcaddy')
mut plugins := p.get_list_default('plugins', [])!
if xcaddy {
if plugins == [] {
plugins = ['github.com/mholt/caddy-webdav', 'github.com/mohammed90/caddy-git-fs',
'github.com/abiosoft/caddy-exec', 'github.com/greenpau/caddy-security']
}
}
install(
reset: reset
start: start
restart: restart
stop: stop
homedir: homedir
file_path: file_path
file_url: file_url
xcaddy: xcaddy
plugins: plugins
)!
}
}
}

View File

@@ -1,31 +0,0 @@
module caddy
pub fn plugin_is_installed(plugin_ string) !bool {
plugin := plugin_.trim_space()
result := osal.exec(cmd: 'caddy list-modules --packages')!
mut lines := result.output.split('\n')
mut standardard_packages := []string{}
mut nonstandardard_packages := []string{}
mut standard := true
for mut line in lines {
line = line.trim_space()
if line == '' {
continue
}
if line.starts_with('Standard modules') {
standard = false
continue
}
package := line.all_after(' ')
if standard {
standardard_packages << package
} else {
nonstandardard_packages << package
}
}
return plugin in standardard_packages || plugin in nonstandardard_packages
}

View File

@@ -1,36 +0,0 @@
# caddy
To get started
```vlang
import freeflowuniverse.herolib.installers.web.caddy
mut installer:= caddy.get()!
installer.start()!
```
## example heroscript
```hero
!!caddy.install
homedir: '/home/user/caddy'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
host: 'localhost'
port: 8888
```

View File

@@ -1,5 +0,0 @@
:80
root * @config.path
file_server

View File

@@ -1,13 +0,0 @@
@config.domain
encode zstd gzip
# Set this path to your site's directory.
root * @config.path
# Enable the static file server.
file_server