refactor: fix meilisearch installler and rename meilisearchinstaller to meilisearch_installer

- Renamed the `meilisearchinstaller` module and related files to  `meilisearch_installer` for consistency.
- Updated import statements and references accordingly.
- Added functionality to install, start, and destroy the  Meilisearch service.
- Improved code readability and organization.
This commit is contained in:
Mahmoud Emad
2025-02-12 11:06:08 +00:00
parent 2953dd0172
commit 3a337b7b0a
7 changed files with 242 additions and 215 deletions

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run #!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.db.meilisearchinstaller import freeflowuniverse.herolib.installers.db.meilisearch_installer
mut meilisearch := meilisearch_installer.get()!
meilisearch := meilisearchinstaller.get()! meilisearch.install()!
meilisearch.start()!
meilisearch.destroy()!

View File

@@ -1,7 +1,7 @@
!!hero_code.generate_installer !!hero_code.generate_installer
name:'meilisearchinstaller' name:'meilisearch_installer'
classname:'MeilisearchServer' classname:'MeilisearchInstaller'
singleton:0 singleton:0
templates:0 templates:0
default:1 default:1

View File

@@ -0,0 +1,182 @@
module meilisearch_installer
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.core.texttools
import os
import rand
import json
fn generate_master_key(length int) !string {
mut key := []rune{}
valid_chars := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for _ in 0 .. length {
random_index := rand.int_in_range(0, valid_chars.len)!
key << valid_chars[random_index]
}
return key.string()
}
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut installer := get()!
mut env := 'development'
if installer.production {
env = 'production'
}
res << zinit.ZProcessNewArgs{
name: 'meilisearch'
cmd: 'meilisearch --no-analytics --http-addr ${installer.host}:${installer.port} --env ${env} --db-path ${installer.path} --master-key ${installer.masterkey}'
startuptype: .zinit
start: true
restart: true
}
return res
}
struct MeilisearchVersionResponse {
version string @[json: 'pkgVersion']
commit_date string @[json: 'commitDate']
commit_sha string @[json: 'commitSha']
}
fn running() !bool {
mut cfg := get()!
url := 'http://${cfg.host}:${cfg.port}'
mut conn := httpconnection.new(name: 'meilisearchinstaller', url: url)!
conn.default_header.add(.authorization, 'Bearer ${cfg.masterkey}')
response := conn.get(prefix: 'version', debug: true) or {
return error('Failed to get meilisearch version: ${err}')
}
decoded_response := json.decode(MeilisearchVersionResponse, response) or {
return error('Failed to decode meilisearch version: ${err}')
}
if decoded_response.version == '' {
console.print_stderr('Meilisearch is not running')
return false
}
console.print_header('Meilisearch is running')
return true
}
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 {
res := os.execute('${osal.profile_path_source_and()!} meilisearch -V')
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 meilisearch version.\n${res.output}")
}
r2 := r[0].all_after('meilisearch').trim(' ')
if texttools.version(version) != texttools.version(r2) {
return false
}
return true
}
// get the Upload List of the files
fn ulist_get() !ulist.UList {
// optionally build a UList which is all paths which are result of building, is then used e.g. in upload
return ulist.UList{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
cfg := get()!
console.print_header('install meilisearch')
// Check if meilisearch is installed
mut res := os.execute('meilisearch --version')
if res.exit_code == 0 {
console.print_header('meilisearch is already installed')
return
}
// Check if curl is installed
res = os.execute('curl --version')
if res.exit_code == 0 {
console.print_header('curl is already installed')
} else {
osal.package_install('curl') or {
return error('Could not install curl, its required to install meilisearch.\nerror:\n${err}')
}
}
if os.exists('${cfg.path}') {
os.rmdir_all('${cfg.path}') or {
return error('Could not remove directory ${cfg.path}.\nerror:\n${err}')
}
}
os.mkdir('${cfg.path}') or {
return error('Could not create directory ${cfg.path}.\nerror:\n${err}')
}
mut cmd := 'cd ${cfg.path} && curl -L https://install.meilisearch.com | sh'
osal.execute_stdout(cmd)!
cmd = 'mv /tmp/meilisearch/meilisearch /usr/local/bin/meilisearch'
osal.execute_stdout(cmd)!
console.print_header('meilisearch is installed')
}
fn build() ! {}
fn destroy() ! {
console.print_header('destroy meilisearch')
mut cfg := get()!
if os.exists('${cfg.path}') {
console.print_header('removing directory ${cfg.path}')
os.rmdir_all('${cfg.path}') or {
return error('Could not remove directory ${cfg.path}.\nerror:\n${err}')
}
}
res := os.execute('meilisearch --version')
if res.exit_code == 0 {
console.print_header('removing meilisearch binary')
osal.execute_silent('sudo rm -rf /usr/local/bin/meilisearch')!
}
mut zinit_factory := zinit.new()!
if zinit_factory.exists('meilisearch') {
zinit_factory.stop('meilisearch') or {
return error('Could not stop meilisearch service due to: ${err}')
}
zinit_factory.delete('meilisearch') or {
return error('Could not delete meilisearch service due to: ${err}')
}
}
// osal.process_kill_recursive(name: 'meilisearch') or {
// return error('Could not kill meilisearch due to: ${err}')
// }
console.print_header('meilisearch is destroyed')
}

View File

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

View File

@@ -1,4 +1,4 @@
module meilisearchinstaller module meilisearch_installer
import freeflowuniverse.herolib.data.encoderhero import freeflowuniverse.herolib.data.encoderhero
@@ -8,7 +8,7 @@ const default = true
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED // THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap] @[heap]
pub struct MeilisearchServer { pub struct MeilisearchInstaller {
pub mut: pub mut:
name string = 'default' name string = 'default'
path string = '/tmp/meilisearch' path string = '/tmp/meilisearch'
@@ -19,10 +19,10 @@ pub mut:
} }
// your checking & initialization code if needed // your checking & initialization code if needed
fn obj_init(mycfg_ MeilisearchServer) !MeilisearchServer { fn obj_init(mycfg_ MeilisearchInstaller) !MeilisearchInstaller {
mut mycfg := mycfg_ mut mycfg := mycfg_
if mycfg.masterkey == '' { if mycfg.masterkey == '' {
return error('masterkey is empty') mycfg.masterkey = generate_master_key(16)!
} }
if mycfg.path == '' { if mycfg.path == '' {
@@ -50,11 +50,11 @@ fn configure() ! {
/////////////NORMALLY NO NEED TO TOUCH /////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj MeilisearchServer) !string { pub fn heroscript_dumps(obj MeilisearchInstaller) !string {
return encoderhero.encode[MeilisearchServer](obj)! return encoderhero.encode[MeilisearchInstaller](obj)!
} }
pub fn heroscript_loads(heroscript string) !MeilisearchServer { pub fn heroscript_loads(heroscript string) !MeilisearchInstaller {
mut obj := encoderhero.decode[MeilisearchServer](heroscript)! mut obj := encoderhero.decode[MeilisearchInstaller](heroscript)!
return obj return obj
} }

View File

@@ -1,157 +0,0 @@
module meilisearchinstaller
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang
import freeflowuniverse.herolib.installers.lang.rust
import freeflowuniverse.herolib.installers.lang.python
import freeflowuniverse.herolib.core.httpconnection
import os
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
mut installer := get()!
mut env := 'development'
if installer.production {
env = 'production'
}
res << zinit.ZProcessNewArgs{
name: 'meilisearch'
cmd: 'meilisearch --no-analytics --http-addr ${installer.host}:${installer.port} --env ${env} --db-path ${installer.path} --master-key ${installer.masterkey}'
}
return res
}
fn running() !bool {
mut meilisearch := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of meilisearchinstaller
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
url := 'http://${meilisearch.host}:${meilisearch.port}/api/v1'
mut conn := httpconnection.new(name: 'meilisearchinstaller', url: url)!
conn.default_header.add(.authorization, 'Bearer ${meilisearch.masterkey}')
conn.default_header.add(.content_type, 'application/json')
console.print_debug("curl -X 'GET' '${url}'/version")
response := conn.get_json_dict(prefix: 'version', debug: false) or { return false }
println('response: ${response}')
// if response
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()!} meilisearchinstaller 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 meilisearchinstaller 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: 'meilisearchinstaller'
// source: '${gitpath}/target/x86_64-unknown-linux-musl/release/meilisearchinstaller'
// )!
}
fn install() ! {
console.print_header('install meilisearchinstaller')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if core.is_linux_arm() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_linux_arm64.tar.gz'
// } else if core.is_linux_intel() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_linux_amd64.tar.gz'
// } else if core.is_osx_arm() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_darwin_arm64.tar.gz'
// } else if osal.is_osx_intel() {
// url = 'https://github.com/meilisearchinstaller-dev/meilisearchinstaller/releases/download/v${version}/meilisearchinstaller_${version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/meilisearchinstaller'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('meilisearchinstaller')!
// osal.cmd_add(
// cmdname: 'meilisearchinstaller'
// source: binpath.path
// )!
}
fn build() ! {
// url := 'https://github.com/threefoldtech/meilisearchinstaller'
// make sure we install base on the node
// if osal.platform() != .ubuntu {
// return error('only support ubuntu for now')
// }
// golang.install()!
// console.print_header('build meilisearchinstaller')
// 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() ! {
osal.process_kill_recursive(name: 'meilisearch')!
osal.cmd_delete('meilisearch')!
osal.package_remove('meilisearch') or { println('') }
}