wip: fix gitea & postgresql installers

Co-authored-by: omda <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-01-05 19:29:54 +02:00
parent d5d5eab855
commit f7309f2433
10 changed files with 585 additions and 480 deletions

View File

@@ -2,10 +2,9 @@ module postgresql
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 freeflowuniverse.herolib.ui.console
import time
__global (
@@ -15,94 +14,91 @@ __global (
/////////FACTORY
// set the model in mem and the config on the filesystem
pub fn set(o Postgresql) ! {
mut o2 := obj_init(o)!
postgresql_global[o.name] = &o2
postgresql_default = o.name
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
}
// check we find the config on the filesystem
pub fn exists(args_ ArgsGet) bool {
mut model := args_get(args_)
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = postgresql_default
}
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&Postgresql {
mut args := args_get(args_)
if args.name !in postgresql_global {
if !config_exists() {
if default {
config_save()!
}
}
config_load()!
}
return postgresql_global[args.name] or {
println(postgresql_global)
panic('bug in get from factory: ')
}
}
fn config_exists(args_ ArgsGet) bool {
mut args := args_get(args_)
mut context := base.context() or { panic('bug') }
return context.hero_config_exists('postgresql', model.name)
return context.hero_config_exists('postgresql', args.name)
}
// load the config error if it doesn't exist
pub fn load(args_ ArgsGet) ! {
mut model := args_get(args_)
fn config_load(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
mut heroscript := context.hero_config_get('postgresql', model.name)!
mut heroscript := context.hero_config_get('postgresql', args.name)!
play(heroscript: heroscript)!
}
// save the config to the filesystem in the context
pub fn save(o Postgresql) ! {
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
heroscript := encoderhero.encode[Postgresql](o)!
context.hero_config_set('postgresql', model.name, heroscript)!
context.hero_config_set('postgresql', args.name, heroscript_default()!)!
}
fn set(o Postgresql) ! {
mut o2 := obj_init(o)!
postgresql_global['default'] = &o2
}
@[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 model := args_
mut args := args_
if model.heroscript == '' {
model.heroscript = heroscript_default()!
if args.heroscript == '' {
args.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: 'postgresql.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 postgresql.configure\n${mycfg}')
set(mycfg)!
save(mycfg)!
}
}
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 install_actions := plbook.find(filter: 'postgresql.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
cfg_play(p)!
}
}
}
@@ -111,12 +107,6 @@ pub fn play(args_ PlayArgs) ! {
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// load from disk and make sure is properly intialized
pub fn (mut self Postgresql) reload() ! {
switch(self.name)
self = obj_init(self)!
}
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown
// screen
@@ -139,6 +129,12 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
}
// load from disk and make sure is properly intialized
pub fn (mut self Postgresql) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self Postgresql) start() ! {
switch(self.name)
if self.running()! {
@@ -176,9 +172,9 @@ pub fn (mut self Postgresql) start() ! {
return error('postgresql did not install properly.')
}
pub fn (mut self Postgresql) install_start(model InstallArgs) ! {
pub fn (mut self Postgresql) install_start(args InstallArgs) ! {
switch(self.name)
self.install(model)!
self.install(args)!
self.start()!
}
@@ -209,7 +205,7 @@ pub fn (mut self Postgresql) running() !bool {
return false
}
}
return running()!
return running_()!
}
@[params]
@@ -218,15 +214,21 @@ pub mut:
reset bool
}
pub fn install(args InstallArgs) ! {
if args.reset {
destroy()!
}
if !(installed_()!) {
pub fn (mut self Postgresql) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed_()!) {
install_()!
}
}
pub fn destroy() ! {
pub fn (mut self Postgresql) destroy() ! {
switch(self.name)
self.stop() or {}
destroy_()!
}
// switch instance to be used for postgresql
pub fn switch(name string) {
postgresql_default = name
}

View File

@@ -3,6 +3,7 @@ module postgresql
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core
import os
pub const version = '0.0.0'

View File

@@ -7,7 +7,8 @@ To get started
```vlang
import freeflowuniverse.herolib.installers.db.postgresql
import freeflowuniverse.crystallib.installers.something. postgresql
mut installer:= postgresql.get()!
@@ -20,37 +21,16 @@ installer.start()!
## example heroscript
```hero
!!postgresql.install
path: ''
passwd: 'asecret'
```
## use psql
uses our hero configure output and jq command line trick
```bash
#default is the instance name
export PGPASSWORD=`hero configure -c postgres -i default -s | jq -r '.passwd'`
psql -U "root" -h localhost
```
## to use in other installer
```v
//e.g. in server configure function
import freeflowuniverse.herolib.installers.db.postgresql
mut mydbinstaller:=postgresql.get()!
mydbinstaller.start()!
// now create the DB
mydbinstaller.db_create('gitea')!
homedir: '/home/user/postgresql'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
host: 'localhost'
port: 8888
```

View File

@@ -1,11 +1,11 @@
!!hero_code.generate_installer
name: "gitea"
classname: "GiteaInstaller"
hasconfig: false
classname: "GiteaServer"
hasconfig: true
singleton: true
default: true
default: false
title: ""
templates: false
templates: true
build: true
startupmanager: true
supported_platforms: ""

View File

@@ -1,20 +1,117 @@
module gitea
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core
// import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.ulist
// import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.installers.base
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.installers.db.postgresql as postgresinstaller
import os
// checks if a certain version or above is installed
fn installed() !bool {
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
cfg := get()!
res := os.execute('/bin/bash -c "gitea --version"')
if res.exit_code != 0 {
return false
}
r := res.output.split(' ')
if r.len < 3 {
return error("couldn't parse gitea version.\n${res.output}")
}
if texttools.version(cfg.version) > texttools.version(r[2]) {
return false
}
return false
}
fn install() ! {
console.print_header('install gitea')
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()!
cfg := get()!
version := cfg.version
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: dest.path, create: false)!
osal.cmd_add(
cmdname: 'gitea'
source: binpath.path
)!
osal.done_set('gitea_install', 'OK')!
console.print_header('gitea installed properly.')
}
fn build() ! {
install()!
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}
fn destroy() ! {
server := get()!
server.stop()!
osal.process_kill_recursive(name: 'gitea')!
osal.cmd_delete('gitea')!
}
// 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: 'gitea'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/gitea'
// )!
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut installer := get()!
mut res := []zinit.ZProcessNewArgs{}
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res << zinit.ZProcessNewArgs{
@@ -28,7 +125,7 @@ fn startupcmd() ![]zinit.ZProcessNewArgs {
return res
}
fn running_() !bool {
fn running() !bool {
mut installer := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of gitea
@@ -49,140 +146,3 @@ fn running_() !bool {
// console.print_debug('gitea 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()!} gitea 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 gitea 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: 'gitea'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/gitea'
// )!
}
fn install_() ! {
console.print_header('install gitea')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm()! {
// url = 'https://github.com/gitea-dev/gitea/releases/download/v${version}/gitea_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel()! {
// url = 'https://github.com/gitea-dev/gitea/releases/download/v${version}/gitea_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm()! {
// url = 'https://github.com/gitea-dev/gitea/releases/download/v${version}/gitea_${version}_darwin_arm64.tar.gz'
// } else if core.is_osx_intel()! {
// url = 'https://github.com/gitea-dev/gitea/releases/download/v${version}/gitea_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/gitea'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('gitea')!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
}
fn build_() ! {
// url := 'https://github.com/threefoldtech/gitea'
// 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 gitea')
// 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

@@ -2,18 +2,107 @@ module gitea
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 (
gitea_global map[string]&GiteaInstaller
gitea_global map[string]&GiteaServer
gitea_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = gitea_default
}
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&GiteaServer {
mut args := args_get(args_)
if args.name !in gitea_global {
if !config_exists() {
if default {
config_save()!
}
}
config_load()!
}
return gitea_global[args.name] or {
println(gitea_global)
panic('failed to get gitea server for ${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('gitea', args.name)
}
fn config_load(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
mut heroscript := context.hero_config_get('gitea', args.name)!
play(heroscript: heroscript)!
}
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_set('gitea', args.name, heroscript_default()!)!
}
fn set(o GiteaServer) ! {
mut o2 := obj_init(o)!
gitea_global['default'] = &o2
}
@[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: 'gitea.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
cfg_play(p)!
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -40,7 +129,13 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
}
pub fn (mut self GiteaInstaller) start() ! {
// load from disk and make sure is properly intialized
pub fn (mut self GiteaServer) reload() ! {
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self GiteaServer) start() ! {
switch(self.name)
if self.running()! {
return
@@ -48,8 +143,8 @@ pub fn (mut self GiteaInstaller) start() ! {
console.print_header('gitea start')
if !installed_()! {
install_()!
if !installed()! {
install()!
}
configure()!
@@ -77,13 +172,13 @@ pub fn (mut self GiteaInstaller) start() ! {
return error('gitea did not install properly.')
}
pub fn (mut self GiteaInstaller) install_start(model InstallArgs) ! {
pub fn (mut self GiteaServer) install_start(args InstallArgs) ! {
switch(self.name)
self.install(model)!
self.install(args)!
self.start()!
}
pub fn (mut self GiteaInstaller) stop() ! {
pub fn (mut self GiteaServer) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()! {
@@ -93,13 +188,13 @@ pub fn (mut self GiteaInstaller) stop() ! {
stop_post()!
}
pub fn (mut self GiteaInstaller) restart() ! {
pub fn (mut self GiteaServer) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self GiteaInstaller) running() !bool {
pub fn (mut self GiteaServer) running() !bool {
switch(self.name)
// walk over the generic processes, if not running return
@@ -119,19 +214,26 @@ pub mut:
reset bool
}
pub fn install(args InstallArgs) ! {
if args.reset {
pub fn (mut self GiteaServer) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self GiteaServer) build() ! {
switch(self.name)
build()!
}
pub fn (mut self GiteaServer) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
if !(installed_()!) {
install_()!
}
}
pub fn destroy() ! {
destroy_()!
}
pub fn build() ! {
build_()!
// switch instance to be used for gitea
pub fn switch(name string) {
gitea_default = name
}

View File

@@ -1,20 +1,84 @@
module gitea
import freeflowuniverse.herolib.data.paramsparser
import os
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.core.pathlib
pub const version = '0.0.0'
const singleton = true
const default = true
const default = false
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct GiteaInstaller {
pub mut:
name string = 'default'
// 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 := "
!!gitea.configure
name:'gitea'
path: '/data/gitea'
passwd: '12345678'
postgresql_name: 'default'
mail_from: 'git@meet.tf'
smtp_addr: 'smtp-relay.brevo.com'
smtp_login: 'admin'
smpt_port: 587
smtp_passwd: '12345678'
domain: 'meet.tf'
jwt_secret: ''
lfs_jwt_secret: ''
internal_token: ''
secret_key: ''
"
return heroscript
}
fn obj_init(obj_ GiteaInstaller) !GiteaInstaller {
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
pub struct GiteaServer {
pub mut:
name string = 'default'
// reset bool
version string = '1.22.4'
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
process ?zinit.ZProcess
path_config pathlib.Path
}
fn cfg_play(p paramsparser.Params) !GiteaServer {
// THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
mut mycfg := GiteaServer{
name: p.get_default('name', 'default')!
path: p.get_default('path', '/data/gitea')!
passwd: p.get('passwd')!
postgresql_name: p.get_default('postgresql_name', 'default')!
mail_from: p.get_default('mail_from', 'git@meet.tf')!
smtp_addr: p.get_default('smtp_addr', 'smtp-relay.brevo.com')!
smtp_login: p.get('smtp_login')!
smpt_port: p.get_int_default('smpt_port', 587)!
smtp_passwd: p.get('smtp_passwd')!
domain: p.get('domain')!
jwt_secret: p.get('jwt_secret')!
lfs_jwt_secret: p.get('lfs_jwt_secret')!
internal_token: p.get('internal_token')!
secret_key: p.get('secret_key')!
}
return mycfg
}
fn obj_init(obj_ GiteaServer) !GiteaServer {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
@@ -23,4 +87,9 @@ fn obj_init(obj_ GiteaInstaller) !GiteaInstaller {
// 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)
}

View File

@@ -1,77 +1,78 @@
module gitea
import freeflowuniverse.herolib.installers.db.postgresql as postgresinstaller
import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
// 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')
}
// 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
}
// 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()!
// // 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'
)!
// 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
)!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
osal.done_set('gitea_install', 'OK')!
// osal.done_set('gitea_install', 'OK')!
console.print_header('gitea installed properly.')
}
// 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')
}
// 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
}
// 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()!
// // 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'
)!
// 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
)!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
osal.done_set('gitea_install', 'OK')!
// osal.done_set('gitea_install', 'OK')!
console.print_header('gitea installed properly.')
}
// console.print_header('gitea installed properly.')
// }

View File

@@ -7,21 +7,12 @@ To get started
```vlang
import freeflowuniverse.herolib.installers.something.gitea as gitea_installer
heroscript:="
!!gitea.configure name:'test'
password: '1234'
port: 7701
import freeflowuniverse.crystallib.installers.something. gitea
!!gitea.start name:'test' reset:1
"
mut installer:= gitea.get()!
gitea_installer.play(heroscript=heroscript)!
//or we can call the default and do a start with reset
//mut installer:= gitea_installer.get()!
//installer.start(reset:true)!
installer.start()!
@@ -30,8 +21,9 @@ gitea_installer.play(heroscript=heroscript)!
## example heroscript
```hero
!!gitea.configure
!!gitea.install
homedir: '/home/user/gitea'
username: 'admin'
password: 'secretpassword'

View File

@@ -5,40 +5,40 @@ import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.data.dbfs
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.postgresql
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]
smpt_port int = 587
smtp_passwd string
domain string @[required]
jwt_secret string
lfs_jwt_secret string
internal_token string
secret_key string
}
// @[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]
// smpt_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 Config
process ?zinit.ZProcess
path_config pathlib.Path
}
// pub struct Server {
// pub mut:
// name string
// config GiteaServer
// process ?zinit.ZProcess
// path_config pathlib.Path
// }
// get the gitea server
//```js
@@ -47,89 +47,89 @@ pub mut:
// passwd string
//```
// if name exists already in the config DB, it will load for that name
pub fn new(args_ Config) !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(args.name)!
}
pub fn get(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 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)
// }
// ```
pub fn (mut server Server) status() zinit.ZProcessStatus {
// 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 Server) start() ! {
pub fn (mut server GiteaServer) start() ! {
// if server.ok(){
// return
// }
@@ -149,22 +149,21 @@ pub fn (mut server Server) start() ! {
mut config_path := server.path_config.file_get_new('app.ini')!
config_path.write(t1)!
osal.user_add(name: 'git')!
// osal.user_add(name: 'git')!
osal.exec(
cmd: '
chown -R git:root ${server.config.path}
chmod -R 777 /usr/local/bin
'
)!
// 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: '
cd /tmp
sudo -u git bash -c \'gitea web --config ${config_path.path} --verbose\'
/bin/bash -c "gitea --config ${config_path.path}"
'
)!
@@ -178,33 +177,32 @@ pub fn (mut server Server) start() ! {
console.print_header('gitea start ok.')
}
pub fn (mut server Server) restart() ! {
pub fn (mut server GiteaServer) restart() ! {
server.stop()!
server.start()!
}
pub fn (mut server Server) stop() ! {
print_backtrace()
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 Server) check() ! {
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 Server) ok() bool {
pub fn (mut server GiteaServer) ok() bool {
server.check() or { return false }
return true
}
// remove all data
pub fn (mut server Server) destroy() ! {
pub fn (mut server GiteaServer) destroy() ! {
server.stop()!
server.path_config.delete()!
}