refactor: improve gitea installer

- Refactor the Gitea installer to use a more robust and.
- configurable approach.  The installer now uses a more.
- flexible configuration system and handles different.
- platforms more effectively.  The installation process.
- is now more modular and easier to maintain.  The.
- previous method was overly simplistic and lacked.
- essential configuration options.

Co-authored-by: mariobassem12 <mariobassem12@gmail.com>
This commit is contained in:
Mahmoud Emad
2025-01-08 11:43:01 +02:00
parent d631cc5af7
commit e2a2f22571
5 changed files with 248 additions and 211 deletions

View File

@@ -1,13 +1,32 @@
#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.infra.gitea
import freeflowuniverse.herolib.installers.infra.gitea as gitea_installer
mut g := gitea.new(
passwd: '123'
postgresql_path: '/tmp/db'
postgresql_reset: true
domain: 'git.meet.tf'
appname: 'ourworld'
// First of all, we need to set the gitea configuration
heroscript := "
!!gitea.configure
name:'default'
version:'1.22.6'
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: ''
"
gitea_installer.play(
name: 'default'
heroscript: heroscript
)!
// postgresql will be same passwd
g.restart()!
// Then we need to get an instace of the installer and call the install
mut gitea := gitea_installer.get(name: 'default')!
gitea.install()!

View File

@@ -12,11 +12,10 @@ import os
// checks if a certain version or above is installed
fn installed() !bool {
mut cfg := get()!
mut podman := podman_installer.get()!
podman.install()!
cmd := 'podman healthcheck run ${cfg.container_name}'
cmd := 'gitea version'
result := os.execute(cmd)
if result.exit_code != 0 {
@@ -26,17 +25,12 @@ fn installed() !bool {
}
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') {
if installed()! {
console.print_header('gitea binaraies already installed')
return
}
console.print_header('install gitea')
// make sure we install base on the node
base.install()!
@@ -44,10 +38,32 @@ fn install() ! {
postgres.install()!
cfg := get()!
mut podman := podman_installer.get()!
podman.install()!
platform := core.platform()!
mut download_link := ''
is_linux_intel := core.is_linux_intel()!
is_osx_arm := core.is_osx_arm()!
if is_linux_intel {
download_link = 'https://dl.gitea.com/gitea/${cfg.version}/gitea-${cfg.version}-linux-amd64'
}
if is_osx_arm {
download_link = 'https://dl.gitea.com/gitea/${cfg.version}/gitea-${cfg.version}-darwin-10.12-amd64'
}
if download_link.len == 0 {
return error('unsupported platform')
}
binary := osal.download(url: download_link, name: 'gitea', dest: '/tmp/gitea') or {
return error('failed to download gitea due to: ${err}')
}
osal.cmd_add(cmdname: 'gitea', source: binary.path) or {
return error('failed to add gitea to the path due to: ${err}')
}
osal.execute_silent('podman pull docker.io/gitea/gitea:${cfg.version}')!
console.print_header('gitea installed properly.')
}

View File

@@ -98,7 +98,8 @@ pub fn play(args_ PlayArgs) ! {
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
cfg_play(p)!
cfg := cfg_play(p)!
set(cfg)!
}
}
}

View File

@@ -12,6 +12,7 @@ pub fn heroscript_default() !string {
heroscript := "
!!gitea.configure
name:'gitea'
version:'1.22.6'
path: '/data/gitea'
passwd: '12345678'
postgresql_name: 'default'
@@ -37,8 +38,7 @@ pub mut:
name string = 'default'
// reset bool
container_name string = 'herocontainer_gitea'
version string = 'latest'
version string = '1.22.6'
path string = '/data/gitea'
passwd string
postgresql_name string = 'default'
@@ -61,6 +61,7 @@ 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')!
version: p.get_default('version', '1.22.6')!
path: p.get_default('path', '/data/gitea')!
passwd: p.get('passwd')!
postgresql_name: p.get_default('postgresql_name', 'default')!

View File

@@ -1,208 +1,208 @@
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
// 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]
// 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 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
// // @[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 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
// }
// // pub struct Server {
// // pub mut:
// // name string
// // config GiteaServer
// // process ?zinit.ZProcess
// // path_config pathlib.Path
// // }
console.print_header('start gitea: ${server.name}')
mut db := postgresql.get(server.config.postgresql_name)!
// // 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
// now create the DB
db.db_create('gitea')!
// // 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()
// // }
// if true{
// panic("sd")
// }
// // data := json.encode(args)
// // kvs.set(key, data)!
// // }
// // return get_server(args.name)!
// // }
// 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)!
// // 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)!
// osal.user_add(name: 'git')!
// // mut server := Server{
// // name: name
// // config: args
// // path_config: pathlib.get_dir(path: '${args.path}/cfg', create: true)!
// // }
// osal.exec(
// cmd: '
// chown -R git:root ${server.config.path}
// chmod -R 777 /usr/local/bin
// '
// )!
// // 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}")
// // }
mut z := zinit.new()!
processname := 'gitea_${server.name}'
mut p := z.process_new(
name: processname
cmd: '
/bin/bash -c "gitea --config ${config_path.path}"
'
)!
// // // 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 }
// }
p.output_wait('Starting new Web server: tcp:0.0.0.0:3000', 120)!
// // run gitea as docker compose
// pub fn (mut server GiteaServer) start() ! {
// // if server.ok(){
// // return
// // }
o := p.log()!
console.print_debug(o)
// console.print_header('start gitea: ${server.name}')
// mut db := postgresql.get(server.config.postgresql_name)!
server.check()!
// // now create the DB
// db.db_create('gitea')!
console.print_header('gitea start ok.')
}
// // if true{
// // panic("sd")
// // }
pub fn (mut server GiteaServer) restart() ! {
server.stop()!
server.start()!
}
// // 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)!
pub fn (mut server GiteaServer) stop() ! {
console.print_header('stop gitea: ${server.name}')
mut process := server.process or { return }
return process.stop()
}
// // osal.user_add(name: 'git')!
// 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
}
// // osal.exec(
// // cmd: '
// // chown -R git:root ${server.config.path}
// // chmod -R 777 /usr/local/bin
// // '
// // )!
// check health, return true if ok
pub fn (mut server GiteaServer) ok() bool {
server.check() or { return false }
return true
}
// mut z := zinit.new()!
// processname := 'gitea_${server.name}'
// mut p := z.process_new(
// name: processname
// cmd: '
// /bin/bash -c "gitea --config ${config_path.path}"
// '
// )!
// remove all data
pub fn (mut server GiteaServer) destroy() ! {
server.stop()!
server.path_config.delete()!
}
// 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()!
// }