feat: Improved rclone installer

This commit is contained in:
Mahmoud Emad
2025-02-16 08:56:28 +00:00
parent f276cdf697
commit a57f53fdb4
4 changed files with 135 additions and 126 deletions

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.sysadmintools.rclone as rclone_installer
mut rclone := rclone_installer.get()!
rclone.install()!
rclone.destroy()!

View File

@@ -1,13 +1,16 @@
module rclone
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.installers.ulist
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed_() !bool {
fn installed() !bool {
res := os.execute('${osal.profile_path_source_and()!} rclone version')
if res.exit_code != 0 {
return false
@@ -23,58 +26,40 @@ fn installed_() !bool {
return true
}
fn install_() ! {
// 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() ! {
console.print_header('install rclone')
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
mut url := ''
if core.is_linux_arm()! {
url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-linux-arm64.zip'
} else if core.is_linux_intel()! {
url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-linux-amd64.zip'
} else if core.is_osx_arm()! {
url = 'https://downloads.rclone.org/rclone-current-osx-amd64.zip'
} else if core.is_osx_intel()! {
url = 'https://github.com/rclone/rclone/releases/download/v${version}/rclone-v${version}-osx-amd64.zip'
// Check if curl is installed
mut res := os.execute('curl --version')
if res.exit_code == 0 {
console.print_header('curl is already installed')
} else {
return error('unsported platform')
osal.package_install('curl') or {
return error('Could not install curl, its required to install rclone.\nerror:\n${err}')
}
}
mut dest := osal.download(
url: url
minsize_kb: 9000
expand_dir: '/tmp/rclone'
)!
// dest.moveup_single_subdir()!
mut binpath := dest.file_get('rclone')!
osal.cmd_add(
cmdname: 'rclone'
source: binpath.path
)!
// Check if rclone is installed
osal.execute_stdout('sudo -v ; curl https://rclone.org/install.sh | sudo bash') or {
return error('cannot install rclone due to: ${err}')
}
console.print_header('rclone is installed')
}
fn configure() ! {
_ := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
_ := $tmpl('templates/rclone.yaml')
// mut path := pathlib.get_file(path: cfg.configpath, create: true)!
// path.write(mycode)!
// console.print_debug(mycode)
// implement if steps need to be done for configuration
}
fn destroy_() ! {
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
fn destroy() ! {
console.print_header('uninstall rclone')
res := os.execute('sudo rm -rf /usr/local/bin/rclone /usr/local/rclone /usr/bin/rclone /usr/share/man/man1/rclone.1.gz')
if res.exit_code != 0 {
return error('failed to uninstall rclone: ${res.output}')
}
console.print_header('rclone is uninstalled')
}

View File

@@ -2,10 +2,9 @@ module rclone
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 (
rclone_global map[string]&RClone
@@ -17,14 +16,11 @@ __global (
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
name string
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = rclone_default
}
if args.name == '' {
args.name = 'default'
}
@@ -32,71 +28,91 @@ fn args_get(args_ ArgsGet) ArgsGet {
}
pub fn get(args_ ArgsGet) !&RClone {
mut context := base.context()!
mut args := args_get(args_)
mut obj := RClone{}
if args.name !in rclone_global {
if !config_exists() {
if default {
config_save()!
}
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('rclone', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
config_load()!
}
return rclone_global[args.name] or {
println(rclone_global)
panic('bug in get from factory: ')
// bug if we get here because should be in globals
panic('could not get config for rclone with name, is bug:${args.name}')
}
}
fn config_exists(args_ ArgsGet) bool {
// register the config for the future
pub fn set(o RClone) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('rclone', o.name, heroscript)!
}
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
mut context := base.context() or { panic('bug') }
return context.hero_config_exists('rclone', args.name)
}
fn config_load(args_ ArgsGet) ! {
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
mut heroscript := context.hero_config_get('rclone', args.name)!
play(heroscript: heroscript)!
context.hero_config_delete('rclone', args.name)!
if args.name in rclone_global {
// del rclone_global[args.name]
}
}
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_set('rclone', args.name, heroscript_default()!)!
}
fn set(o RClone) ! {
// only sets in mem, does not set as config
fn set_in_mem(o RClone) ! {
mut o2 := obj_init(o)!
rclone_global['default'] = &o2
rclone_global[o.name] = &o2
rclone_default = o.name
}
@[params]
pub struct PlayArgs {
pub mut:
name string = 'default'
heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook
reset bool
delete bool
configure bool // make sure there is at least one installed
}
pub fn play(args_ PlayArgs) ! {
mut args := args_
if args.heroscript == '' {
args.heroscript = heroscript_default()!
}
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'rclone.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
mycfg := cfg_play(p)!
set(mycfg)!
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
mut other_actions := plbook.find(filter: 'rclone.')!
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 rclone.destroy')
destroy()!
}
if other_action.name == 'install' {
console.print_debug('install action rclone.install')
install()!
}
}
}
}
@@ -141,18 +157,24 @@ pub mut:
pub fn (mut self RClone) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed_()!) {
install_()!
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self RClone) destroy() ! {
switch(self.name)
destroy_()!
destroy()!
}
// switch instance to be used for rclone
pub fn switch(name string) {
rclone_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,27 +1,21 @@
module rclone
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '1.67.0'
const singleton = false
const default = false
pub fn heroscript_default() !string {
heroscript := "
!!rclone.configure
name: 'default'
cat: 'b2'
s3_account: ''
s3_key: ''
s3_secret: ''
hard_delete: false
endpoint: ''
"
return heroscript
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
pub enum RCloneCat {
b2
s3
ftp
}
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct RClone {
pub mut:
name string = 'default'
@@ -33,31 +27,32 @@ pub mut:
endpoint string
}
pub enum RCloneCat {
b2
s3
ftp
}
fn cfg_play(p paramsparser.Params) !RClone {
mut mycfg := RClone{
name: p.get_default('name', 'default')!
cat: match p.get_default('cat', 'b2')! {
'b2' { RCloneCat.b2 }
's3' { RCloneCat.s3 }
'ftp' { RCloneCat.ftp }
else { return error('Invalid RCloneCat') }
}
s3_account: p.get_default('s3_account', '')!
s3_key: p.get_default('s3_key', '')!
s3_secret: p.get_default('s3_secret', '')!
hard_delete: p.get_default_false('hard_delete')
endpoint: p.get_default('endpoint', '')!
}
// your checking & initialization code if needed
fn obj_init(mycfg_ RClone) !RClone {
mut mycfg := mycfg_
return mycfg
}
fn obj_init(obj_ RClone) !RClone {
mut obj := obj_
// called before start if done
fn configure() ! {
_ := get()!
// THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
_ := $tmpl('templates/rclone.yaml')
// mut path := pathlib.get_file(path: cfg.configpath, create: true)!
// path.write(mycode)!
// console.print_debug(mycode)
// implement if steps need to be done for configuration
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj RClone) !string {
return encoderhero.encode[RClone](obj)!
}
pub fn heroscript_loads(heroscript string) !RClone {
mut obj := encoderhero.decode[RClone](heroscript)!
return obj
}