fix: improve package management and screen status
- Fix issues in package installation and removal across different platforms (Ubuntu, macOS, Alpine, Arch). - Improve error handling and add sudo support where necessary. - Enhance screen status check to accurately reflect process activity. - Address minor bugs in `db.v`, `done.v`, and `net_test.v`. - Correct minor inconsistencies in package names.
This commit is contained in:
@@ -169,7 +169,7 @@ function os_update {
|
||||
fi
|
||||
#apt install apt-transport-https ca-certificates curl software-properties-common -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes
|
||||
package_install "apt-transport-https ca-certificates curl wget software-properties-common tmux"
|
||||
package_install "rclone rsync mc redis-server screen net-tools git htop ca-certificates lsb-release binutils pkg-config"
|
||||
package_install "rclone rsync mc redis-server screen net-tools git htop ca-certificates screen lsb-release binutils pkg-config"
|
||||
|
||||
elif [[ "${OSNAME}" == "darwin"* ]]; then
|
||||
if command -v brew >/dev/null 2>&1; then
|
||||
|
||||
@@ -248,8 +248,11 @@ pub fn (mut db DB) destroy() ! {
|
||||
// get all keys of the db (e.g. per session) can be with a prefix
|
||||
pub fn (mut db DB) keys(prefix_ string) ![]string {
|
||||
// TODO: see get, to fix this one
|
||||
mut files := db.path.list()!
|
||||
if prefix_.len == 0 {
|
||||
return []
|
||||
}
|
||||
|
||||
mut files := db.path.list()!
|
||||
panic('implement ${files}')
|
||||
prefix := texttools.name_fix(prefix_)
|
||||
mut r := db.path.list(recursive: false)!
|
||||
|
||||
@@ -44,7 +44,9 @@ pub fn done_exists(key string) bool {
|
||||
pub fn done_print() ! {
|
||||
mut db := donedb()!
|
||||
mut output := 'DONE:\n'
|
||||
for key in db.keys('')! {
|
||||
kyes := db.keys('')!
|
||||
println('kyes: ${kyes}')
|
||||
for key in kyes {
|
||||
output += '\t${key} = ${done_get_str(key)}\n'
|
||||
}
|
||||
console.print_debug('${output}')
|
||||
|
||||
@@ -6,13 +6,16 @@ fn test_ipaddr_pub_get() {
|
||||
}
|
||||
|
||||
fn test_ping() {
|
||||
assert ping(address: '127.0.0.1', count: 1) == .ok
|
||||
x := ping(address: '127.0.0.1', count: 1)!
|
||||
assert x == .ok
|
||||
}
|
||||
|
||||
fn test_ping_timeout() ! {
|
||||
assert ping(address: '192.168.145.154', count: 5, timeout: 1) == .timeout
|
||||
x := ping(address: '192.168.145.154', count: 5, timeout: 1)!
|
||||
assert x == .timeout
|
||||
}
|
||||
|
||||
fn test_ping_unknownhost() ! {
|
||||
assert ping(address: '12.902.219.1', count: 1, timeout: 1) == .unknownhost
|
||||
x := ping(address: '12.902.219.1', count: 1, timeout: 1)!
|
||||
assert x == .unknownhost
|
||||
}
|
||||
|
||||
@@ -2,112 +2,166 @@ module osal
|
||||
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.core.texttools
|
||||
import os
|
||||
|
||||
// update the package list
|
||||
pub fn package_refresh() ! {
|
||||
platform_ := platform()
|
||||
|
||||
if cmd_exists('nix-env') {
|
||||
// means nix package manager is installed
|
||||
// nix package manager is installed
|
||||
// nothing to do
|
||||
return
|
||||
}
|
||||
|
||||
if platform_ == .ubuntu {
|
||||
exec(cmd: 'apt-get update') or { return error('Could not update packages\nerror:\n${err}') }
|
||||
// Refresh the package list for Ubuntu/Debian
|
||||
exec(cmd: 'sudo apt-get update') or {
|
||||
return error('Could not update packages for Ubuntu\nerror:\n${err}')
|
||||
}
|
||||
return
|
||||
} else if platform_ == .osx {
|
||||
exec(cmd: 'brew update') or { return error('Could not update packages\nerror:\n${err}') }
|
||||
// Refresh the package list for macOS
|
||||
exec(cmd: 'brew update') or {
|
||||
return error('Could not update packages for macOS\nerror:\n${err}')
|
||||
}
|
||||
return
|
||||
} else if platform_ == .alpine {
|
||||
exec(cmd: 'apk update') or { return error('Could not update packages\nerror:\n${err}') }
|
||||
// Refresh the package list for Alpine Linux
|
||||
exec(cmd: 'apk update') or {
|
||||
return error('Could not update packages for Alpine\nerror:\n${err}')
|
||||
}
|
||||
return
|
||||
} else if platform_ == .arch {
|
||||
exec(cmd: 'pacman -Syu --noconfirm') or {
|
||||
return error('Could not update packages\nerror:\n${err}')
|
||||
// Refresh the package list for Arch Linux
|
||||
exec(cmd: 'sudo pacman -Syu --noconfirm') or {
|
||||
return error('Could not update packages for Arch Linux\nerror:\n${err}')
|
||||
}
|
||||
return
|
||||
}
|
||||
return error("Only ubuntu, alpine, arch and osx is supported for now. Found \"${platform_}\"")
|
||||
|
||||
return error("Only ubuntu, alpine, arch, and osx are supported for now. Found \"${platform_}\"")
|
||||
}
|
||||
|
||||
// install a package will use right commands per platform
|
||||
// install a package using the right commands per platform
|
||||
pub fn package_install(name_ string) ! {
|
||||
names := texttools.to_array(name_)
|
||||
|
||||
// if cmd_exists('nix-env') {
|
||||
// // means nix package manager is installed
|
||||
// names_list := names.join(' ')
|
||||
// console.print_header('package install: ${names_list}')
|
||||
// exec(cmd: 'nix-env --install ${names_list}') or {
|
||||
// return error('could not install package using nix:${names_list}\nerror:\n${err}')
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
name := names.join(' ')
|
||||
console.print_header('package install: ${name}')
|
||||
|
||||
platform_ := platform()
|
||||
cpu := cputype()
|
||||
|
||||
if platform_ == .osx {
|
||||
if cpu == .arm {
|
||||
exec(cmd: 'arch --arm64 brew install ${name}') or {
|
||||
return error('could not install package: ${name}\nerror:\n${err}')
|
||||
return error('could not install package on macOS (ARM): ${name}\nerror:\n${err}')
|
||||
}
|
||||
} else {
|
||||
exec(cmd: 'brew install ${name}') or {
|
||||
return error('could not install package:${name}\nerror:\n${err}')
|
||||
return error('could not install package on macOS: ${name}\nerror:\n${err}')
|
||||
}
|
||||
}
|
||||
} else if platform_ == .ubuntu {
|
||||
exec(
|
||||
cmd: '
|
||||
export TERM=xterm
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
apt install -y ${name} -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --allow-downgrades --allow-remove-essential --allow-change-held-packages
|
||||
'
|
||||
) or { return error('could not install package:${name}\nerror:\n${err}') }
|
||||
// Use sudo if required (based on user's permissions)
|
||||
use_sudo := is_sudo_required()
|
||||
|
||||
cmd := if use_sudo {
|
||||
'sudo apt install -y ${name} -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --allow-downgrades --allow-remove-essential --allow-change-held-packages'
|
||||
} else {
|
||||
'apt install -y ${name} -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --allow-downgrades --allow-remove-essential --allow-change-held-packages'
|
||||
}
|
||||
exec(cmd: cmd) or {
|
||||
return error('could not install package on Ubuntu: ${name}\nerror:\n${err}')
|
||||
}
|
||||
} else if platform_ == .alpine {
|
||||
exec(cmd: 'apk add ${name}') or {
|
||||
return error('could not install package:${name}\nerror:\n${err}')
|
||||
// Use sudo if required
|
||||
use_sudo := is_sudo_required()
|
||||
cmd := if use_sudo {
|
||||
'sudo apk add ${name}'
|
||||
} else {
|
||||
'apk add ${name}'
|
||||
}
|
||||
exec(cmd: cmd) or {
|
||||
return error('could not install package on Alpine: ${name}\nerror:\n${err}')
|
||||
}
|
||||
} else if platform_ == .arch {
|
||||
exec(cmd: 'pacman --noconfirm -Su ${name}') or {
|
||||
return error('could not install package:${name}\nerror:\n${err}')
|
||||
// Use sudo if required
|
||||
use_sudo := is_sudo_required()
|
||||
cmd := if use_sudo {
|
||||
'sudo pacman --noconfirm -Su ${name}'
|
||||
} else {
|
||||
'pacman --noconfirm -Su ${name}'
|
||||
}
|
||||
exec(cmd: cmd) or {
|
||||
return error('could not install package on Arch: ${name}\nerror:\n${err}')
|
||||
}
|
||||
} else {
|
||||
return error('Only ubuntu, alpine and osx supported for now')
|
||||
return error('Only ubuntu, alpine, arch, and osx supported for now')
|
||||
}
|
||||
}
|
||||
|
||||
// Remove a package using the appropriate command for each platform
|
||||
// Method to check if sudo is required (i.e., if the user is root or has sudo privileges)
|
||||
fn is_sudo_required() bool {
|
||||
// Check if the user is root
|
||||
if os.getenv('USER') == 'root' {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if the user has sudo privileges (test with `sudo -v`)
|
||||
sudo_check := os.execute('sudo -v')
|
||||
return sudo_check.exit_code == 0
|
||||
}
|
||||
|
||||
// remove a package using the right commands per platform
|
||||
pub fn package_remove(name_ string) ! {
|
||||
names := texttools.to_array(name_)
|
||||
name := names.join(' ')
|
||||
console.print_header('package remove: ${name}')
|
||||
|
||||
platform_ := platform()
|
||||
cpu := cputype()
|
||||
|
||||
// Debugging: print out platform and cpu type
|
||||
println('Platform: ${platform_}, CPU: ${cpu}')
|
||||
|
||||
// Check if name is empty
|
||||
if name == '' {
|
||||
return error('Package name is empty')
|
||||
}
|
||||
|
||||
// Determine if sudo is required by checking if the user has sudo privileges
|
||||
use_sudo := is_sudo_required()
|
||||
|
||||
// Platform-specific package removal logic
|
||||
if platform_ == .osx {
|
||||
if cpu == .arm {
|
||||
exec(cmd: 'arch --arm64 brew uninstall ${name}', ignore_error: true)!
|
||||
exec(cmd: 'arch --arm64 brew uninstall ${name}', ignore_error: false)!
|
||||
} else {
|
||||
exec(cmd: 'brew uninstall ${name}', ignore_error: true)!
|
||||
exec(cmd: 'brew uninstall ${name}', ignore_error: false)!
|
||||
}
|
||||
} else if platform_ == .ubuntu {
|
||||
exec(
|
||||
cmd: '
|
||||
export TERM=xterm
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
apt remove -y ${name} --allow-change-held-packages
|
||||
apt autoremove -y
|
||||
'
|
||||
ignore_error: true
|
||||
)!
|
||||
// Use sudo if required
|
||||
cmd := if use_sudo {
|
||||
'sudo apt remove -y ${name} --allow-change-held-packages'
|
||||
} else {
|
||||
'apt remove -y ${name} --allow-change-held-packages'
|
||||
}
|
||||
exec(cmd: cmd, ignore_error: false)!
|
||||
exec(cmd: 'sudo apt autoremove -y', ignore_error: false)!
|
||||
} else if platform_ == .alpine {
|
||||
exec(cmd: 'apk del ${name}', ignore_error: true)!
|
||||
// Use sudo if required
|
||||
cmd := if use_sudo { 'sudo apk del ${name}' } else { 'apk del ${name}' }
|
||||
exec(cmd: cmd, ignore_error: false)!
|
||||
} else if platform_ == .arch {
|
||||
exec(cmd: 'pacman --noconfirm -R ${name}', ignore_error: true)!
|
||||
// Use sudo if required
|
||||
cmd := if use_sudo {
|
||||
'sudo pacman --noconfirm -R ${name}'
|
||||
} else {
|
||||
'pacman --noconfirm -R ${name}'
|
||||
}
|
||||
exec(cmd: cmd, ignore_error: false)!
|
||||
} else {
|
||||
return error('Only ubuntu, alpine and osx supported for now')
|
||||
return error('Only ubuntu, alpine, and osx supported for now')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,35 +47,39 @@ pub enum ScreenStatus {
|
||||
|
||||
// Method to check the status of a screen process
|
||||
pub fn (self Screen) status() !ScreenStatus {
|
||||
panic('implement')
|
||||
// // Command to list screen sessions
|
||||
// cmd := 'screen -ls'
|
||||
// response := osal.execute_silent(cmd)!
|
||||
// Command to list screen sessions
|
||||
cmd := 'screen -ls'
|
||||
response := osal.execute_silent(cmd)!
|
||||
|
||||
// // Check if the screen session exists
|
||||
// if !response.contains(self.name) {
|
||||
// return .inactive
|
||||
// }
|
||||
// Check if the screen session exists by looking for the session name in the output
|
||||
if !response.contains(self.name) {
|
||||
return .inactive
|
||||
}
|
||||
|
||||
// // Command to send a dummy command to the screen session and check response
|
||||
// cmd_check := 'screen -S ${self.name} -X eval "stuff \\"\\003\\"; sleep 0.1; stuff \\"ps\\n\\""'
|
||||
// osal.execute_silent(cmd_check)!
|
||||
// Command to send a dummy command to the screen session and check response
|
||||
cmd_check := "screen -S ${self.name} -X eval \"stuff \\\"\\003\\\"; sleep 0.1; stuff \\\"ps\\n\\\"\""
|
||||
osal.execute_silent(cmd_check)!
|
||||
|
||||
// // Check if the process is running in the screen session
|
||||
// cmd_ps := 'screen -S ${self.name} -X hardcopy -h /tmp/screen_output; cat /tmp/screen_output | grep "${self.name}"'
|
||||
// ps_response := osal.execute_silent(cmd_ps)!
|
||||
// Command to check if there is an active process in the screen session
|
||||
cmd_ps := 'screen -S ${self.name} -X hardcopy -h /tmp/screen_output; cat /tmp/screen_output'
|
||||
ps_response := osal.execute_silent(cmd_ps)!
|
||||
|
||||
// return parse_screen_process_status(ps_response)
|
||||
// Parse the response and determine if there's an active process
|
||||
return parse_screen_process_status(ps_response)
|
||||
}
|
||||
|
||||
// Function to parse screen process status output
|
||||
// Function to parse the screen process status output
|
||||
fn parse_screen_process_status(output string) ScreenStatus {
|
||||
lines := output.split_into_lines()
|
||||
|
||||
// Check the output for active processes
|
||||
for line in lines {
|
||||
if line.contains('SCREEN') || line.contains('PID') {
|
||||
return .active
|
||||
}
|
||||
}
|
||||
|
||||
// If no active process is found, return inactive
|
||||
return .inactive
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ pub fn testsuite_begin() ! {
|
||||
|
||||
pub fn test_screen_status() ! {
|
||||
mut screen_factory := new()!
|
||||
mut screen := screen_factory.add(name: 'testservice', cmd: 'redis-server')!
|
||||
mut screen := screen_factory.add(name: 'testservice', cmd: 'redis-server --port 1234')!
|
||||
status := screen.status()!
|
||||
// assert status == .active
|
||||
assert status == .active
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user