commetbft
This commit is contained in:
@@ -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
|
||||
@@ -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() ! {
|
||||
}
|
||||
@@ -1,274 +0,0 @@
|
||||
module caddy
|
||||
|
||||
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 (
|
||||
caddy_global map[string]&CaddyServer
|
||||
caddy_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) !&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
|
||||
}
|
||||
|
||||
@[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: 'caddy.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: 'caddy.')!
|
||||
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')
|
||||
destroy()!
|
||||
}
|
||||
if other_action.name == 'install' {
|
||||
console.print_debug('install action caddy.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}')
|
||||
if other_action.name == 'start' {
|
||||
console.print_debug('install action caddy.${other_action.name}')
|
||||
caddy_obj.start()!
|
||||
}
|
||||
|
||||
if other_action.name == 'stop' {
|
||||
console.print_debug('install action caddy.${other_action.name}')
|
||||
caddy_obj.stop()!
|
||||
}
|
||||
if other_action.name == 'restart' {
|
||||
console.print_debug('install action caddy.${other_action.name}')
|
||||
caddy_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 CaddyServer) reload() ! {
|
||||
switch(self.name)
|
||||
self = obj_init(self)!
|
||||
}
|
||||
|
||||
pub fn (mut self CaddyServer) start() ! {
|
||||
switch(self.name)
|
||||
if self.running()! {
|
||||
return
|
||||
}
|
||||
|
||||
console.print_header('caddy start')
|
||||
|
||||
if !installed()! {
|
||||
install()!
|
||||
}
|
||||
|
||||
configure()!
|
||||
|
||||
start_pre()!
|
||||
|
||||
for zprocess in startupcmd()! {
|
||||
mut sm := startupmanager_get(zprocess.startuptype)!
|
||||
|
||||
console.print_debug('starting caddy 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('caddy did not install properly.')
|
||||
}
|
||||
|
||||
pub fn (mut self CaddyServer) install_start(args InstallArgs) ! {
|
||||
switch(self.name)
|
||||
self.install(args)!
|
||||
self.start()!
|
||||
}
|
||||
|
||||
pub fn (mut self CaddyServer) 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 CaddyServer) restart() ! {
|
||||
switch(self.name)
|
||||
self.stop()!
|
||||
self.start()!
|
||||
}
|
||||
|
||||
pub fn (mut self CaddyServer) 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 CaddyServer) install(args InstallArgs) ! {
|
||||
switch(self.name)
|
||||
if args.reset || (!installed()!) {
|
||||
install()!
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut self CaddyServer) destroy() ! {
|
||||
switch(self.name)
|
||||
self.stop() or {}
|
||||
destroy()!
|
||||
}
|
||||
|
||||
// switch instance to be used for caddy
|
||||
pub fn switch(name string) {
|
||||
caddy_default = name
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
@[params]
|
||||
pub struct DefaultConfigArgs {
|
||||
instance string = 'default'
|
||||
}
|
||||
@@ -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)!
|
||||
}
|
||||
@@ -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()!
|
||||
// }
|
||||
@@ -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)!
|
||||
}
|
||||
@@ -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
|
||||
)!
|
||||
}
|
||||
@@ -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
|
||||
)!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
:80
|
||||
|
||||
root * @config.path
|
||||
|
||||
file_server
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user