diff --git a/examples/installers/podman.vsh b/examples/installers/podman.vsh new file mode 100755 index 00000000..68f3ef7d --- /dev/null +++ b/examples/installers/podman.vsh @@ -0,0 +1,11 @@ +#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.installers.virt.podman as podman_installer + +mut podman := podman_installer.get()! + +if podman.installed() { + podman.destroy()! +} else { + podman.install()! +} diff --git a/lib/installers/db/postgresql/postgresql_actions.v b/lib/installers/db/postgresql/postgresql_actions.v index d75744ee..de8a4056 100644 --- a/lib/installers/db/postgresql/postgresql_actions.v +++ b/lib/installers/db/postgresql/postgresql_actions.v @@ -1,6 +1,9 @@ module postgresql import freeflowuniverse.herolib.osal +import freeflowuniverse.herolib.ui.console +import freeflowuniverse.herolib.core +import freeflowuniverse.herolib.installers.virt.podman as podman_installer import freeflowuniverse.herolib.osal.zinit fn installed_() !bool { @@ -8,6 +11,18 @@ fn installed_() !bool { } fn install_() ! { + console.print_header('install postgresql') + + if core.platform()! != .ubuntu || core.platform()! != .arch { + return error('only support ubuntu and arch for now') + } + + if osal.done_exists('podman') { + console.print_header('podman binary already installed') + return + } + + podman_installer.install()! osal.execute_silent('podman pull docker.io/library/postgres:latest')! } diff --git a/lib/installers/virt/podman/.heroscript b/lib/installers/virt/podman/.heroscript index a3139ffe..257cb734 100644 --- a/lib/installers/virt/podman/.heroscript +++ b/lib/installers/virt/podman/.heroscript @@ -10,4 +10,4 @@ reset:0 startupmanager:0 hasconfig:0 - build:1 \ No newline at end of file + build:0 \ No newline at end of file diff --git a/lib/installers/virt/podman/podman_actions.v b/lib/installers/virt/podman/podman_actions.v index 28e14f8e..b0497bfe 100644 --- a/lib/installers/virt/podman/podman_actions.v +++ b/lib/installers/virt/podman/podman_actions.v @@ -1,183 +1,87 @@ module podman -import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.core import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.core.texttools -import freeflowuniverse.herolib.installers.lang.golang -import freeflowuniverse.herolib.installers.ulist +import freeflowuniverse.herolib.core import os -// checks if a certain version or above is installed -fn installed_() !bool { - res := os.execute('${osal.profile_path_source_and()!} podman -v') - if res.exit_code != 0 { - return false - } - r := res.output.split_into_lines().filter(it.contains('podman version')) - if r.len != 1 { - return error("couldn't parse herocontainers version, expected 'podman version' on 1 row.\n${res.output}") - } - v := texttools.version(r[0].all_after('version')) - if texttools.version(version) == v { - return true - } - return false +// Check if Podman is installed +fn installed() bool { + console.print_header('Checking if Podman is installed...') + result := os.execute('podman -v') + return result.exit_code == 0 } -fn install_() ! { - console.print_header('install podman') - - destroy()! - - mut url := '' - if core.platform()! == .osx { - mut dest := '/tmp/podman.pkg' - - if core.cputype()! == .arm { - url = 'https://github.com/containers/podman/releases/download/v${version}/podman-installer-macos-arm64.pkg' - } else { - url = 'https://github.com/containers/podman/releases/download/v${version}/podman-installer-macos-amd64.pkg' - } - - console.print_header('download ${url}') - osal.download( - url: url - minsize_kb: 75000 - reset: true - dest: dest - )! - - cmd := ' - sudo installer -pkg ${dest} -target / - ' - osal.execute_interactive(cmd)! - console.print_header(' - pkg installed.') - } else if core.platform()! == .ubuntu { - // is the remote tool to connect to a remote podman - // if core.cputype()! == .arm { - // url = 'https://github.com/containers/podman/releases/download/v${version}/podman-remote-static-linux_arm64.tar.gz' - // } else { - // url = 'https://github.com/containers/podman/releases/download/v${version}/podman-remote-static-linux_amd64.tar.gz' - // } - // console.print_header('download ${url}') - // dest:=osal.download( - // url: url - // minsize_kb: 75000 - // reset: true - // dest: dest - // )! - - build()! - } else { - return error('unsupported platform for podman') +// Install Podman +fn install() ! { + if installed() { + return error('Podman is already installed.') } -} -fn build_() ! { - // mut installer := get()! - - // https://podman.io/docs/installation#building-from-source + console.print_header('Installing Podman...') platform := core.platform()! - if platform != .ubuntu && platform != .arch { - return error('only support ubuntu and arch for now') - } - mut g := golang.get()! - g.install()! - - console.print_header('build buildah') - - osal.package_install('btrfs-progs,crun,git,go-md2man,iptables,libassuan-dev,libbtrfs-dev,libc6-dev,libdevmapper-dev,libglib2.0-dev,libgpgme-dev')! - osal.package_install('libgpg-error-dev,libprotobuf-dev,libprotobuf-c-dev,libseccomp-dev,libselinux1-dev,libsystemd-dev,make,netavark,pkg-config,uidmap')! - osal.package_install('runc')! - - // conmon - cmd0 := ' - cd /tmp - rm -rf conmon - git clone https://github.com/containers/conmon - cd conmon - git checkout v2.1.12 - export GOCACHE="$(mktemp -d)" - make - make install - - cd /tmp - - ' - osal.execute_stdout(cmd0)! - - for x in ['conmon'] { - osal.cmd_add( - cmdname: x - source: '/tmp/conmon/bin/${x}' - )! - } - - cmd := ' - cd /tmp - rm -rf podman - sudo sysctl kernel.unprivileged_userns_clone=1 - git clone https://github.com/containers/podman - cd podman - git checkout v${version} - export GOCACHE="$(mktemp -d)" - - mkdir -p /etc/containers - curl -L -o /etc/containers/registries.conf https://raw.githubusercontent.com/containers/image/main/registries.conf - curl -L -o /etc/containers/policy.json https://raw.githubusercontent.com/containers/image/main/default-policy.json - - make BUILDTAGS="selinux seccomp" PREFIX=/usr - - ' - osal.execute_stdout(cmd)! - - for x in ['podman', 'podman-remote'] { - osal.cmd_add( - cmdname: x - source: '/tmp/podman/bin/${x}' - )! - } - - osal.rm(' - /tmp/podman - /tmp/conmon - ')! + command := get_platform_command(platform, 'install')! + execute_command(command, 'installing Podman')! + console.print_header('Podman installed successfully.') } -// get the Upload List of the files -fn ulist_get() !ulist.UList { - // mut installer := get()! - // optionally build a UList which is all paths which are result of building, is then used e.g. in upload +// Remove Podman +fn destroy() ! { + if !installed() { + return error('Podman is not installed.') + } - // /usr/local/bin/conmon - // /usr/local/bin/podman - - return ulist.UList{} + console.print_header('Removing Podman...') + platform := core.platform()! + command := get_platform_command(platform, 'remove')! + execute_command(command, 'removing Podman')! + console.print_header('Podman removed successfully.') } -fn destroy_() ! { - osal.package_remove(' - podman - conmon - buildah - skopeo - runc - ')! - - // will remove all paths where go/bin is found - osal.profile_path_add_remove(paths2delete: 'go/bin')! - - osal.rm(' - podman - conmon - buildah - skopeo - runc - /var/lib/containers - /var/lib/podman - /var/lib/buildah - /tmp/podman - /tmp/conmon - ')! +// Build Podman (install it) +fn build() ! { + install()! +} + +// Get platform-specific commands for installing/removing Podman +fn get_platform_command(platform core.PlatformType, action string) !string { + return match platform { + .ubuntu { + if action == 'install' { + 'sudo apt-get -y install podman' + } else if action == 'remove' { + 'sudo apt-get -y remove podman' + } else { + return error('Invalid action: ${action}') + } + } + .arch { + if action == 'install' { + 'sudo pacman -S --noconfirm podman' + } else if action == 'remove' { + 'sudo pacman -R --noconfirm podman' + } else { + return error('Invalid action: ${action}') + } + } + .osx { + if action == 'install' { + 'brew install podman' + } else if action == 'remove' { + 'brew uninstall podman' + } else { + return error('Invalid action: ${action}') + } + } + else { + return error('Only Ubuntu, Arch, and macOS are supported.') + } + } +} + +// Execute a shell command and handle errors +fn execute_command(command string, operation string) ! { + result := os.execute(command) + if result.exit_code != 0 { + return error('Failed ${operation}: ${result.output}') + } } diff --git a/lib/installers/virt/podman/podman_factory_.v b/lib/installers/virt/podman/podman_factory_.v index 8a171813..2245f108 100644 --- a/lib/installers/virt/podman/podman_factory_.v +++ b/lib/installers/virt/podman/podman_factory_.v @@ -1,10 +1,8 @@ module podman -import freeflowuniverse.herolib.core.base -import freeflowuniverse.herolib.core.playbook -import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.osal.zinit +import freeflowuniverse.herolib.ui.console import time __global ( @@ -14,9 +12,15 @@ __global ( /////////FACTORY -//////////////////////////////////////////////////////////////////////////////////////////////////// -//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS /////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////// +@[params] +pub struct ArgsGet { +pub mut: + name string = 'default' +} + +pub fn get(args_ ArgsGet) !&PodmanInstaller { + return &PodmanInstaller{} +} @[params] pub struct InstallArgs { @@ -24,19 +28,27 @@ pub mut: reset bool } -pub fn install(args InstallArgs) ! { - if args.reset { - destroy()! - } - if !(installed_()!) { - install_()! - } +pub fn (mut self PodmanInstaller) install(args InstallArgs) ! { + switch(self.name) + install()! } -pub fn destroy() ! { - destroy_()! +pub fn (mut self PodmanInstaller) installed(args InstallArgs) bool { + switch(self.name) + return installed() } -pub fn build() ! { - build_()! +pub fn (mut self PodmanInstaller) build() ! { + switch(self.name) + build()! +} + +pub fn (mut self PodmanInstaller) destroy() ! { + switch(self.name) + destroy()! +} + +// switch instance to be used for podman +pub fn switch(name string) { + podman_default = name } diff --git a/lib/installers/virt/podman/podman_model.v b/lib/installers/virt/podman/podman_model.v index d2518159..e55fbce3 100644 --- a/lib/installers/virt/podman/podman_model.v +++ b/lib/installers/virt/podman/podman_model.v @@ -1,25 +1,10 @@ module podman -import freeflowuniverse.herolib.data.paramsparser -import os - -pub const version = '5.2.3' +pub const version = '4.9.3' const singleton = true const default = true -// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED pub struct PodmanInstaller { pub mut: name string = 'default' } - -fn obj_init(obj_ PodmanInstaller) !PodmanInstaller { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ - return obj -} - -// called before start if done -fn configure() ! { - // mut installer := get()! -} diff --git a/lib/installers/virt/podman/readme.md b/lib/installers/virt/podman/readme.md index 55111fad..a4fd8ec1 100644 --- a/lib/installers/virt/podman/readme.md +++ b/lib/installers/virt/podman/readme.md @@ -1,20 +1,20 @@ -# podman +# Podman +Podman is a lightweight container manager that allows users to manage and run containers without requiring a daemon, providing flexibility and security for containerized applications. +## Using Podman in VLang -To get started +The following example demonstrates how to use the Podman installer in a VLang script. It checks if Podman is installed, removes it if found, or installs it if not. +### Example Code (VLang) ```vlang - - import freeflowuniverse.herolib.installers.virt.podman as podman_installer -mut podman_installer0:= podman_installer.get()! - -podman_installer0.destroy()! //will remove all -podman_installer0.install()! +mut podman := podman_installer.get()! +if podman.installed() { + podman.destroy()! +} else { + podman.install()! +} ``` - - -