feat: Improved the pacman installer, added an example

This commit is contained in:
Mahmoud-Emad
2025-02-11 10:32:15 +00:00
parent 6ecd190de8
commit d0b52f40b7
4 changed files with 147 additions and 86 deletions

11
examples/installers/pacman.vsh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.virt.pacman as pacman_installer
mut pacman := pacman_installer.get()!
// To install
pacman.install()!
// To remove
pacman.destroy()!

View File

@@ -1,20 +1,26 @@
module pacman module pacman
import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core import freeflowuniverse.herolib.core
import os import os
// checks if a certain version or above is installed // checks if a certain version or above is installed
fn installed_() !bool { fn installed() !bool {
return osal.done_exists('install_pacman') console.print_header('checking if pacman is installed')
res := os.execute('pacman -v')
if res.exit_code != 0 {
console.print_header('pacman is not installed')
return false
}
console.print_header('pacman is installed')
return true
} }
// use https://archlinux.org/mirrorlist/ // use https://archlinux.org/mirrorlist/
fn install_() ! { fn install() ! {
console.print_header('install pacman') console.print_header('installing pacman')
if core.platform()! == .arch { if core.platform()! == .arch {
return return
@@ -24,78 +30,40 @@ fn install_() ! {
return error('only ubuntu supported for this installer.') return error('only ubuntu supported for this installer.')
} }
cmd := ' mut cmd := 'apt update && apt upgrade -y'
mkdir -p /etc/pacman.d/gnupg
'
osal.execute_stdout(cmd)! osal.execute_stdout(cmd)!
dest := '/etc/pacman.conf' cmd = 'mkdir -p /tmp/pacman'
c := $tmpl('templates/pacman.conf') osal.execute_stdout(cmd)!
os.write_file(dest, c)!
dest2 := '/etc/pacman.d/mirrorlist' cmd = 'cd /tmp/pacman && wget https://gitlab.com/trivoxel/utilities/deb-pacman/-/archive/${version}/deb-pacman-${version}.tar'
c2 := $tmpl('templates/mirrorlist') osal.execute_stdout(cmd)!
pathlib.template_write(c2, dest2, true)!
osal.package_install(' cmd = 'cd /tmp/pacman && tar -xf deb-pacman-v1.0.tar'
arch-install-scripts osal.execute_stdout(cmd)!
pacman-package-manager
')!
url := 'https://gist.githubusercontent.com/despiegk/e56403ecba40f6057251c6cc609c4cf2/raw/1822c921e7282c491d8ac35f3719f51e234f1cbf/gistfile1.txt' cmd = 'cd /tmp/pacman/deb-pacman-v1.0 && chmod +x pacman && sudo mv pacman /usr/local/bin'
mut gpg_dest := osal.download( osal.execute_stdout(cmd)!
url: url
minsize_kb: 1000
reset: true
)!
cmd2 := ' console.print_header('pacman is installed')
mkdir -p /tmp/keyrings
cd /tmp/keyrings
wget https://archlinux.org/packages/core/any/archlinux-keyring/download -O archlinux-keyring.tar.zst
tar -xvf archlinux-keyring.tar.zst
mkdir -p /usr/share/keyrings
cp usr/share/pacman/keyrings/archlinux.gpg /usr/share/keyrings/
pacman-key --init
pacman-key --populate archlinux
gpg --import ${gpg_dest.path}
# Clean up
rm -f pacman_keyring.asc
#pacman-key --refresh-keys
pacman-key --update
rm -rf /tmp/keyrings
'
osal.execute_stdout(cmd2)!
// TODO: is not working well, is like it doesn;t write in right location
osal.done_set('install_pacman', 'OK')!
console.print_header('install done')
} }
fn destroy_() ! { fn destroy() ! {
osal.done_delete('install_pacman')! console.print_header('uninstall pacman')
osal.package_remove(' if core.platform()! == .arch {
arch-install-scripts return
pacman-package-manager }
')!
// TODO: will need to remove more if core.platform()! != .ubuntu {
osal.rm(' return error('only ubuntu supported for this installer.')
pacman }
/etc/pacman.d
/var/cache/pacman mut cmd := 'rm -rf /tmp/pacman'
')! osal.execute_stdout(cmd)!
cmd = 'sudo rm -rf /usr/local/bin/pacman'
osal.execute_stdout(cmd)!
console.print_header('pacman is uninstalled')
} }

View File

@@ -1,11 +1,9 @@
module pacman module pacman
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit import freeflowuniverse.herolib.osal.zinit
import time
__global ( __global (
pacman_global map[string]&PacmanInstaller pacman_global map[string]&PacmanInstaller
@@ -14,25 +12,98 @@ __global (
/////////FACTORY /////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&PacmanInstaller {
return &PacmanInstaller{}
}
@[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 other_actions := plbook.find(filter: 'pacman.')!
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 pacman.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action pacman.install')
install()!
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// //////////////////////////# 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()!
}
}
}
@[params] @[params]
pub struct InstallArgs { pub struct InstallArgs {
pub mut: pub mut:
reset bool reset bool
} }
pub fn install(args InstallArgs) ! { pub fn (mut self PacmanInstaller) install(args InstallArgs) ! {
if args.reset { switch(self.name)
destroy()! if args.reset || (!installed()!) {
} install()!
if !(installed_()!) {
install_()!
} }
} }
pub fn destroy() ! { pub fn (mut self PacmanInstaller) destroy() ! {
destroy_()! switch(self.name)
destroy()!
}
// switch instance to be used for pacman
pub fn switch(name string) {
pacman_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
} }

View File

@@ -1,25 +1,36 @@
module pacman module pacman
import freeflowuniverse.herolib.data.paramsparser import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '' pub const version = 'v1.0'
const singleton = true const singleton = true
const default = true 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]
pub struct PacmanInstaller { pub struct PacmanInstaller {
pub mut: pub mut:
name string = 'default' name string = 'default'
} }
fn obj_init(obj_ PacmanInstaller) !PacmanInstaller { // your checking & initialization code if needed
// never call get here, only thing we can do here is work on object itself fn obj_init(mycfg_ PacmanInstaller) !PacmanInstaller {
mut obj := obj_ mut mycfg := mycfg_
return obj return mycfg
} }
// called before start if done // called before start if done
fn configure() ! { fn configure() ! {
// mut installer := get()! // mut installer := get()!
} }
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj PacmanInstaller) !string {
return encoderhero.encode[PacmanInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !PacmanInstaller {
mut obj := encoderhero.decode[PacmanInstaller](heroscript)!
return obj
}