feat: Add WireGuard installer

- Add a new WireGuard installer to the project.
- This installer handles installation and uninstallation of WireGuard.

Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-02-02 15:20:57 +02:00
parent c27fcc6983
commit 749fa94312
7 changed files with 221 additions and 2 deletions

View File

@@ -1,9 +1,13 @@
#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -d use_openssl -enable-globals run
import freeflowuniverse.herolib.clients.wireguard
import freeflowuniverse.herolib.installers.net.wireguard as wireguard_installer
import time
import os
mut wg_installer := wireguard_installer.get()!
wg_installer.install()!
// Create Wireguard client
mut wg := wireguard.get()!
config_file_path := '${os.dir(@FILE)}/wg0.conf'
@@ -27,3 +31,5 @@ println('public_key: ${public_key}')
wg.down(config_file_path: config_file_path)!
println('${config_file_path} is down')
wg_installer.destroy()!

View File

@@ -64,7 +64,7 @@ pub fn (wg WireGuard) start(args StartArgs) ! {
return error('File ${args.config_file_path} does not exists.')
}
cmd := 'wg-quick up ${args.config_file_path}'
cmd := 'sudo wg-quick up ${args.config_file_path}'
res := os.execute(cmd)
if res.exit_code != 0 {
return error('failed to execute start command due to: ${res.output}')
@@ -82,7 +82,7 @@ pub fn (wg WireGuard) down(args DownArgs) ! {
return error('File ${args.config_file_path} does not exists.')
}
cmd := 'wg-quick down ${args.config_file_path}'
cmd := 'sudo wg-quick down ${args.config_file_path}'
res := os.execute(cmd)
if res.exit_code != 0 {
return error('failed to execute down command due to: ${res.output}')

View File

@@ -0,0 +1,13 @@
!!hero_code.generate_installer
name:'wireguard_installer'
classname:'WireGuard'
singleton:0
templates:0
default:1
title:''
supported_platforms:''
reset:0
startupmanager:0
hasconfig:0
build:0

View File

@@ -0,0 +1,44 @@
# wireguard
To get started
```vlang
import freeflowuniverse.herolib.installers.something.wireguard as wireguard_installer
heroscript:="
!!wireguard.configure name:'test'
password: '1234'
port: 7701
!!wireguard.start name:'test' reset:1
"
wireguard_installer.play(heroscript=heroscript)!
//or we can call the default and do a start with reset
//mut installer:= wireguard_installer.get()!
//installer.start(reset:true)!
```
## example heroscript
```hero
!!wireguard.configure
homedir: '/home/user/wireguard'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
host: 'localhost'
port: 8888
```

View File

@@ -0,0 +1,59 @@
module wireguard
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
osal.execute_silent('wg --version') or { return false }
return true
}
// get the Upload List of the files
fn ulist_get() !ulist.UList {
return ulist.UList{}
}
// uploads to S3 server if configured
fn upload() ! {
}
fn install() ! {
console.print_header('install wireguard')
cmd := match core.platform()! {
.ubuntu {
'sudo apt install -y wireguard'
}
.osx {
'sudo brew install -y wireguard-tools'
}
else {
return error('unsupported platfrom ${core.platform()!}')
}
}
osal.execute_stdout(cmd)!
}
fn destroy() ! {
console.print_header('uninstall wireguard')
cmd := match core.platform()! {
.ubuntu {
'sudo apt remove -y wireguard wireguard-tools'
}
.osx {
'sudo brew uninstall -y wireguard-tools'
}
else {
return error('unsupported platform ${core.platform()!}')
}
}
osal.execute_stdout(cmd)!
}

View File

@@ -0,0 +1,74 @@
module wireguard
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 time
__global (
wireguard_installer_global map[string]&WireGuard
wireguard_installer_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&WireGuard {
return &WireGuard{}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# 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]
pub struct InstallArgs {
pub mut:
reset bool
}
pub fn (mut self WireGuard) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self WireGuard) destroy() ! {
switch(self.name)
destroy()!
}
// switch instance to be used for wireguard_installer
pub fn switch(name string) {
wireguard_installer_default = name
}

View File

@@ -0,0 +1,23 @@
module wireguard
pub const version = '1.14.3'
const singleton = false
const default = true
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct WireGuard {
pub mut:
name string = 'default'
}
fn obj_init(obj_ WireGuard) !WireGuard {
// 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()!
}