refactor: improve gitea installer

- Simplify gitea installer logic.
- Remove unnecessary variables and functions.
- Improve code readability and maintainability.
- Update gitea version to 1.23.3.
- Add default values for GiteaServer fields.
- Remove redundant installer.v and server.v files.
This commit is contained in:
Mahmoud Emad
2025-02-12 14:27:18 +00:00
parent 147c889b53
commit 02d4adcff0
5 changed files with 214 additions and 493 deletions

View File

@@ -3,289 +3,277 @@ module gitea
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
gitea_global map[string]&GiteaServer
gitea_default string
gitea_global map[string]&GiteaServer
gitea_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub struct ArgsGet {
pub mut:
name string
name string
}
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&GiteaServer {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := GiteaServer{}
if !(args.name in gitea_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("gitea",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return gitea_global[args.name] or {
println(gitea_global)
//bug if we get here because should be in globals
panic("could not get config for gitea with name, is bug:${args.name}")
}
pub fn get(args_ ArgsGet) !&GiteaServer {
mut context := base.context()!
mut args := args_get(args_)
mut obj := GiteaServer{}
if args.name !in gitea_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('gitea', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return gitea_global[args.name] or {
println(gitea_global)
// bug if we get here because should be in globals
panic('could not get config for gitea with name, is bug:${args.name}')
}
}
//register the config for the future
pub fn set(o GiteaServer)! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("gitea", o.name, heroscript)!
// register the config for the future
pub fn set(o GiteaServer) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('gitea', o.name, heroscript)!
}
//does the config exists?
pub fn exists(args_ ArgsGet)! bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists("gitea", args.name)
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('gitea', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("gitea",args.name)!
if args.name in gitea_global {
//del gitea_global[args.name]
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('gitea', args.name)!
if args.name in gitea_global {
// del gitea_global[args.name]
}
}
//only sets in mem, does not set as config
fn set_in_mem(o GiteaServer)! {
mut o2:=obj_init(o)!
gitea_global[o.name] = &o2
gitea_default = o.name
// only sets in mem, does not set as config
fn set_in_mem(o GiteaServer) ! {
mut o2 := obj_init(o)!
gitea_global[o.name] = &o2
gitea_default = o.name
}
@[params]
pub struct PlayArgs {
pub mut:
heroscript string //if filled in then plbook will be made out of it
plbook ?playbook.PlayBook
reset bool
heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook
reset bool
}
pub fn play(args_ PlayArgs) ! {
mut args:=args_
mut args := args_
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut plbook := args.plbook or {
playbook.new(text: args.heroscript)!
}
mut install_actions := plbook.find(filter: 'gitea.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
mut install_actions := plbook.find(filter: 'gitea.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'gitea.')!
for other_action in other_actions {
if other_action.name in ["destroy","install","build"]{
mut p := other_action.params
reset:=p.get_default_false("reset")
if other_action.name == "destroy" || reset{
console.print_debug("install action gitea.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action gitea.install")
install()!
}
}
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut gitea_obj:=get(name:name)!
console.print_debug("action object:\n${gitea_obj}")
if other_action.name == "start"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action gitea.${other_action.name}")
gitea_obj.restart()!
}
}
}
mut other_actions := plbook.find(filter: 'gitea.')!
for other_action in other_actions {
if other_action.name in ['destroy', 'install', 'build'] {
mut p := other_action.params
reset := p.get_default_false('reset')
if other_action.name == 'destroy' || reset {
console.print_debug('install action gitea.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action gitea.install')
install()!
}
}
if other_action.name in ['start', 'stop', 'restart'] {
mut p := other_action.params
name := p.get('name')!
mut gitea_obj := get(name: name)!
console.print_debug('action object:\n${gitea_obj}')
if other_action.name == 'start' {
console.print_debug('install action gitea.${other_action.name}')
gitea_obj.start()!
}
if other_action.name == 'stop' {
console.print_debug('install action gitea.${other_action.name}')
gitea_obj.stop()!
}
if other_action.name == 'restart' {
console.print_debug('install action gitea.${other_action.name}')
gitea_obj.restart()!
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown
// screen
// zinit
// tmux
// systemd
match cat{
.zinit{
console.print_debug("startupmanager: zinit")
return startupmanager.get(cat:.zinit)!
}
.systemd{
console.print_debug("startupmanager: systemd")
return startupmanager.get(cat:.systemd)!
}else{
console.print_debug("startupmanager: auto")
return startupmanager.get()!
}
}
// unknown
// screen
// zinit
// tmux
// systemd
match cat {
.zinit {
console.print_debug('startupmanager: zinit')
return startupmanager.get(cat: .zinit)!
}
.systemd {
console.print_debug('startupmanager: systemd')
return startupmanager.get(cat: .systemd)!
}
else {
console.print_debug('startupmanager: auto')
return startupmanager.get()!
}
}
}
//load from disk and make sure is properly intialized
// load from disk and make sure is properly intialized
pub fn (mut self GiteaServer) reload() ! {
switch(self.name)
self=obj_init(self)!
switch(self.name)
self = obj_init(self)!
}
pub fn (mut self GiteaServer) start() ! {
switch(self.name)
if self.running()!{
return
}
switch(self.name)
if self.running()! {
return
}
console.print_header('gitea start')
console.print_header('gitea start')
if ! installed()!{
install()!
}
if !installed()! {
install()!
}
configure()!
configure()!
start_pre()!
start_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting gitea with ${zprocess.startuptype}...')
console.print_debug('starting gitea with ${zprocess.startuptype}...')
sm.new(zprocess)!
sm.new(zprocess)!
sm.start(zprocess.name)!
}
sm.start(zprocess.name)!
}
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('gitea did not install properly.')
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('gitea did not install properly.')
}
pub fn (mut self GiteaServer) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self GiteaServer) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)!
}
stop_post()!
switch(self.name)
stop_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)!
}
stop_post()!
}
pub fn (mut self GiteaServer) restart() ! {
switch(self.name)
self.stop()!
self.start()!
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self GiteaServer) running() !bool {
switch(self.name)
switch(self.name)
//walk over the generic processes, if not running return
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
r:=sm.running(zprocess.name)!
if r==false{
return false
}
}
return running()!
// walk over the generic processes, if not running return
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
r := sm.running(zprocess.name)!
if r == false {
return false
}
}
return running()!
}
@[params]
pub struct InstallArgs{
pub struct InstallArgs {
pub mut:
reset bool
reset bool
}
pub fn (mut self GiteaServer) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self GiteaServer) build() ! {
switch(self.name)
build()!
switch(self.name)
build()!
}
pub fn (mut self GiteaServer) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
switch(self.name)
self.stop() or {}
destroy()!
}
//switch instance to be used for gitea
// switch instance to be used for gitea
pub fn switch(name string) {
gitea_default = name
gitea_default = name
}
//helpers
// helpers
@[params]
pub struct DefaultConfigArgs{
instance string = 'default'
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -10,7 +10,7 @@ import freeflowuniverse.herolib.clients.mailclient
import freeflowuniverse.herolib.clients.postgresql_client
import rand
pub const version = '0.0.0'
pub const version = '1.23.3'
const singleton = true
const default = false
@@ -36,6 +36,33 @@ pub fn (obj GiteaServer) config_path() string {
// your checking & initialization code if needed
fn obj_init(mycfg_ GiteaServer) !GiteaServer {
mut mycfg := mycfg_
if mycfg.name == '' {
mycfg.name = 'default'
}
if mycfg.path == '' {
mycfg.path = '${os.home_dir()}/hero/var/gitea'
}
if mycfg.passwd == '' {
mycfg.passwd = rand.hex(12)
}
if mycfg.postgresql_client_name == '' {
mycfg.postgresql_client_name = 'default'
}
if mycfg.domain == '' {
mycfg.domain = 'git.test.com'
}
if mycfg.jwt_secret == '' {
mycfg.jwt_secret = rand.hex(12)
}
if mycfg.mail_client_name == '' {
mycfg.mail_client_name = 'default'
}
return mycfg
}

View File

@@ -1,78 +0,0 @@
module gitea
// import freeflowuniverse.herolib.installers.db.postgresql as postgresinstaller
// import freeflowuniverse.herolib.installers.base
// import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.core
// import freeflowuniverse.herolib.core.pathlib
// import freeflowuniverse.herolib.ui.console
// pub fn install_() ! {
// if core.platform()! != .ubuntu || core.platform()! != .arch {
// return error('only support ubuntu and arch for now')
// }
// if osal.done_exists('gitea_install') {
// console.print_header('gitea binaraies already installed')
// return
// }
// // make sure we install base on the node
// base.install()!
// postgresinstaller.install()!
// version := '1.22.0'
// url := 'https://github.com/go-gitea/gitea/releases/download/v${version}/gitea-${version}-linux-amd64.xz'
// console.print_debug(' download ${url}')
// mut dest := osal.download(
// url: url
// minsize_kb: 40000
// reset: true
// expand_file: '/tmp/download/gitea'
// )!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
// osal.done_set('gitea_install', 'OK')!
// console.print_header('gitea installed properly.')
// }
// pub fn start() ! {
// if core.platform()! != .ubuntu || core.platform()! != .arch {
// return error('only support ubuntu and arch for now')
// }
// if osal.done_exists('gitea_install') {
// console.print_header('gitea binaraies already installed')
// return
// }
// // make sure we install base on the node
// base.install()!
// postgresinstaller.install()!
// version := '1.22.0'
// url := 'https://github.com/go-gitea/gitea/releases/download/v${version}/gitea-${version}-linux-amd64.xz'
// console.print_debug(' download ${url}')
// mut dest := osal.download(
// url: url
// minsize_kb: 40000
// reset: true
// expand_file: '/tmp/download/gitea'
// )!
// binpath := pathlib.get_file(path: '/tmp/download/gitea', create: false)!
// osal.cmd_add(
// cmdname: 'gitea'
// source: binpath.path
// )!
// osal.done_set('gitea_install', 'OK')!
// console.print_header('gitea installed properly.')
// }

View File

@@ -1,208 +0,0 @@
module gitea
// import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.osal.zinit
// import freeflowuniverse.herolib.data.dbfs
// import freeflowuniverse.herolib.core.texttools
// import freeflowuniverse.herolib.core.pathlib
// import freeflowuniverse.herolib.installers.db.postgresql
// import json
// import rand
// import os
// import time
// import freeflowuniverse.herolib.ui.console
// // @[params]
// // pub struct Config {
// // pub mut:
// // name string = 'default'
// // reset bool
// // path string = '/data/gitea'
// // passwd string
// // postgresql_name string = 'default'
// // mail_from string = 'git@meet.tf'
// // smtp_addr string = 'smtp-relay.brevo.com'
// // smtp_login string @[required]
// // smtp_port int = 587
// // smtp_passwd string
// // domain string @[required]
// // jwt_secret string
// // lfs_jwt_secret string
// // internal_token string
// // secret_key string
// // }
// // pub struct Server {
// // pub mut:
// // name string
// // config GiteaServer
// // process ?zinit.ZProcess
// // path_config pathlib.Path
// // }
// // get the gitea server
// //```js
// // name string = 'default'
// // path string = '/data/gitea'
// // passwd string
// //```
// // if name exists already in the config DB, it will load for that name
// // pub fn new_server(args_ GiteaServer) !Server {
// // install()! // make sure it has been build & ready to be used
// // mut args := args_
// // if args.passwd == '' {
// // args.passwd = rand.string(12)
// // }
// // args.name = texttools.name_fix(args.name)
// // key := 'gitea_config_${args.name}'
// // mut kvs := dbfs.new(name: 'config')!
// // if !kvs.exists(key) {
// // // jwt_secret string
// // // lfs_jwt_secret string
// // // internal_token string
// // // secret_key string
// // if args.jwt_secret == '' {
// // r := os.execute_or_panic('gitea generate secret JWT_SECRET')
// // args.jwt_secret = r.output.trim_space()
// // }
// // if args.lfs_jwt_secret == '' {
// // r := os.execute_or_panic('gitea generate secret LFS_JWT_SECRET')
// // args.lfs_jwt_secret = r.output.trim_space()
// // }
// // if args.internal_token == '' {
// // r := os.execute_or_panic('gitea generate secret INTERNAL_TOKEN')
// // args.internal_token = r.output.trim_space()
// // }
// // if args.secret_key == '' {
// // r := os.execute_or_panic('gitea generate secret SECRET_KEY')
// // args.secret_key = r.output.trim_space()
// // }
// // data := json.encode(args)
// // kvs.set(key, data)!
// // }
// // return get_server(args.name)!
// // }
// // pub fn get_server(name_ string) !Server {
// // console.print_header('get gitea server ${name_}')
// // name := texttools.name_fix(name_)
// // key := 'gitea_config_${name}'
// // mut kvs := dbfs.new(name: 'config')!
// // if kvs.exists(key) {
// // data := kvs.get(key)!
// // args := json.decode(Config, data)!
// // mut server := Server{
// // name: name
// // config: args
// // path_config: pathlib.get_dir(path: '${args.path}/cfg', create: true)!
// // }
// // mut z := zinit.new()!
// // processname := 'gitea_${name}'
// // if z.process_exists(processname) {
// // server.process = z.process_get(processname)!
// // }
// // // console.print_debug(" - server get ok")
// // server.start()!
// // return server
// // }
// // return error("can't find server gitea with name ${name}")
// // }
// // // return status
// // // ```
// // // pub enum ZProcessStatus {
// // // unknown
// // // init
// // // ok
// // // error
// // // blocked
// // // spawned
// // // }
// // // ```
// pub fn (mut server GiteaServer) status() zinit.ZProcessStatus {
// mut process := server.process or { return .unknown }
// return process.status() or { return .unknown }
// }
// // run gitea as docker compose
// pub fn (mut server GiteaServer) start() ! {
// // if server.ok(){
// // return
// // }
// console.print_header('start gitea: ${server.name}')
// mut db := postgresql.get(server.config.postgresql_name)!
// // now create the DB
// db.db_create('gitea')!
// // if true{
// // panic("sd")
// // }
// // TODO: postgresql can be on other server, need to fill in all arguments in template
// t1 := $tmpl('templates/app.ini')
// mut config_path := server.path_config.file_get_new('app.ini')!
// config_path.write(t1)!
// // osal.user_add(name: 'git')!
// // osal.exec(
// // cmd: '
// // chown -R git:root ${server.config.path}
// // chmod -R 777 /usr/local/bin
// // '
// // )!
// mut z := zinit.new()!
// processname := 'gitea_${server.name}'
// mut p := z.process_new(
// name: processname
// cmd: '
// /bin/bash -c "gitea --config ${config_path.path}"
// '
// )!
// p.output_wait('Starting new Web server: tcp:0.0.0.0:3000', 120)!
// o := p.log()!
// console.print_debug(o)
// server.check()!
// console.print_header('gitea start ok.')
// }
// pub fn (mut server GiteaServer) restart() ! {
// server.stop()!
// server.start()!
// }
// pub fn (mut server GiteaServer) stop() ! {
// console.print_header('stop gitea: ${server.name}')
// mut process := server.process or { return }
// return process.stop()
// }
// // check health, return true if ok
// pub fn (mut server GiteaServer) check() ! {
// mut p := server.process or { return error("can't find process for server.") }
// p.check()!
// // TODO: need to do some other checks to gitea e.g. rest calls
// }
// // check health, return true if ok
// pub fn (mut server GiteaServer) ok() bool {
// server.check() or { return false }
// return true
// }
// // remove all data
// pub fn (mut server GiteaServer) destroy() ! {
// server.stop()!
// server.path_config.delete()!
// }