Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Dec 22, 2024
1 parent 09887ba commit 7cab924
Show file tree
Hide file tree
Showing 80 changed files with 1,323 additions and 1,193 deletions.
10 changes: 5 additions & 5 deletions crystallib/biz/bizmodel/macros.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ fn employee_wiki(p paramsparser.Params, sim BizModel) !string {

// if true{panic("s")}

// theme := 'light'
// theme := 'dark' // Removed unused variable
mut t := $tmpl('./templates/employee.md')
// theme := 'light'
// theme := 'dark' // Removed unused variable
mut t := $tmpl('./templates/employee.md')
return t
}

Expand Down Expand Up @@ -90,9 +90,9 @@ fn department_wiki(p paramsparser.Params, sim BizModel) !string {
}

fn revenues_wiki(p paramsparser.Params, mut sim BizModel) !string {
// mut revs := map[string]string{} // Removed unused variable
// mut revs := map[string]string{} // Removed unused variable

// for name,_ in sim.products{
// for name,_ in sim.products{
// myrow:=sim.sheet.row_get('${name}_rev_total') or { panic("bug in revenues_wiki macro") }
// println(myrow)
// }
Expand Down
4 changes: 2 additions & 2 deletions crystallib/biz/bizmodel/play_product_revenue.v
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn (mut m BizModel) revenue_action(action Action) ! {
product.has_oneoffs = true
}

_ := m.sheet.row_new(
_ := m.sheet.row_new(
name: '${name}_cogs_perc'
growth: action.params.get_default('cogs_perc', '0')!
tags: 'rev name:${name}'
Expand Down Expand Up @@ -205,7 +205,7 @@ fn (mut m BizModel) revenue_action(action Action) ! {
nrmonths: nr_months_recurring
)!

_ := nr_sold.recurring(
_ := nr_sold.recurring(
name: '${name}_nr_sold_recurring'
descr: 'Nr products active because of recurring for ${name2}'
nrmonths: nr_months_recurring
Expand Down
2 changes: 1 addition & 1 deletion crystallib/biz/spreadsheet/factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn sheet_new(args SheetNewArgs) !&Sheet {
pub fn sheet_get(name string) !&Sheet {
rlock sheets {
if name in sheets {
return sheets[name] or { return error('Sheet ${name} not found') }
return sheets[name] or { return error('Sheet ${name} not found') }
}
}
return error("cann't find sheet:'${name}' in global sheets")
Expand Down
2 changes: 1 addition & 1 deletion crystallib/biz/spreadsheet/wiki.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import freeflowuniverse.crystallib.ui.console
pub fn (mut s Sheet) wiki(args_ RowGetArgs) !string {
mut args := args_

_ := match args.period_type {
_ := match args.period_type {
.year { 12 }
.month { 1 }
.quarter { 3 }
Expand Down
2 changes: 1 addition & 1 deletion crystallib/builder/builder_factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct BuilderFactory {
}

pub fn new() !BuilderFactory {
_ := base.context()!
_ := base.context()!
mut bf := BuilderFactory{}
return bf
}
Expand Down
14 changes: 7 additions & 7 deletions crystallib/builder/executor.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Executor = ExecutorLocal | ExecutorSSH

pub struct ExecutorNewArguments {
pub mut:
local bool // if this set then will always be the local machine
ipaddr string
user string = 'root'
debug bool
local bool // if this set then will always be the local machine
ipaddr string
user string = 'root'
debug bool
checkconnect int
}

Expand Down Expand Up @@ -42,9 +42,9 @@ pub fn executor_new(args_ ExecutorNewArguments) !Executor {
return error('can not initialize ip address.\n ${err}')
}
mut e := ExecutorSSH{
ipaddr: ipaddr
user: args.user
debug: args.debug
ipaddr: ipaddr
user: args.user
debug: args.debug
checkconnect: args.checkconnect
}
e.init()!
Expand Down
83 changes: 41 additions & 42 deletions crystallib/builder/executor_ssh.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import freeflowuniverse.crystallib.core.pathlib
import freeflowuniverse.crystallib.data.ipaddress
import freeflowuniverse.crystallib.ui.console
import time

import crypto.md5

@[heap]
pub struct ExecutorSSH {
pub mut:
ipaddr ipaddress.IPAddress
sshkey string
user string = 'root' // default will be root
initialized bool
retry int = 1 // nr of times something will be retried before failing, need to check also what error is, only things which should be retried need to be done
debug bool = true
checkconnect int //if set then will test the ssh connection for that timeout
ipaddr ipaddress.IPAddress
sshkey string
user string = 'root' // default will be root
initialized bool
retry int = 1 // nr of times something will be retried before failing, need to check also what error is, only things which should be retried need to be done
debug bool = true
checkconnect int // if set then will test the ssh connection for that timeout
}

fn (mut executor ExecutorSSH) init() ! {
Expand All @@ -28,7 +28,7 @@ fn (mut executor ExecutorSSH) init() ! {
// return error('port cannot be 0.\n${executor}')
// }
// TODO: need to call code from SSHAGENT do not reimplement here, not nicely done
//os.execute('pgrep -x ssh-agent || eval `ssh-agent -s`')
// os.execute('pgrep -x ssh-agent || eval `ssh-agent -s`')

// if executor.sshkey != '' {
// osal.exec(cmd: 'ssh-add ${executor.sshkey}')!
Expand All @@ -41,32 +41,31 @@ fn (mut executor ExecutorSSH) init() ! {
executor.ipaddr.port = 22
}

if executor.sshkey.len>0{
if ! os.exists(executor.sshkey){
mypath:="/tmp/ssh_${md5.hexhash(executor.sshkey)}"
os.write_file(mypath,executor.sshkey)!
executor.sshkey=mypath
if executor.sshkey.len > 0 {
if !os.exists(executor.sshkey) {
mypath := '/tmp/ssh_${md5.hexhash(executor.sshkey)}'
os.write_file(mypath, executor.sshkey)!
executor.sshkey = mypath
}
}

// TODO: doesn't work with ipv6 after working with ipv4, need better check too, because this slows everything down
// cmd := "sh -c 'ssh-keyscan -H ${executor.ipaddr.addr} -p ${executor.ipaddr.port} -t ecdsa-sha2-nistp256 2>/dev/null >> ~/.ssh/known_hosts'"
// osal.execute_silent(cmd) or { return error('cannot add the ssh keys to known hosts') }

if executor.checkconnect>0{
end:= time.now().unix() + executor.checkconnect
mut now:=i64(0)
for{
if now>end{
if executor.checkconnect > 0 {
end := time.now().unix() + executor.checkconnect
mut now := i64(0)
for {
if now > end {
break
}
if executor.ping()!{
if executor.ping()! {
executor.initialized = true
return
}
time.sleep(time.second)
now = time.now().unix()

}
return error("couldn't establish the ssh connection to ${executor}")
}
Expand All @@ -91,34 +90,34 @@ pub fn (mut executor ExecutorSSH) exec(args_ ExecArgs) !string {
if executor.ipaddr.port > 10 {
port = '-p ${executor.ipaddr.port}'
}
mut sshkey:=""
if os.exists(executor.sshkey){
sshkey = "-i ${executor.sshkey} "
mut sshkey := ''
if os.exists(executor.sshkey) {
sshkey = '-i ${executor.sshkey} '
}

args.cmd = 'ssh -o StrictHostKeyChecking=no ${sshkey}${executor.user}@${executor.ipaddr.addr} ${port} "${args.cmd}"'
res := osal.exec(cmd: args.cmd, stdout: args.stdout, debug: executor.debug)!
return res.output
}

//test if ssh connection works
// test if ssh connection works
pub fn (mut executor ExecutorSSH) ping() !bool {
mut sshkey:=""
if os.exists(executor.sshkey){
sshkey = "-i ${executor.sshkey} "
}
mut sshkey := ''
if os.exists(executor.sshkey) {
sshkey = '-i ${executor.sshkey} '
}
mut port := ''
if executor.ipaddr.port > 10 {
port = '-p ${executor.ipaddr.port}'
}
}
cmd := 'ssh -o StrictHostKeyChecking=no -q -o BatchMode=yes ${sshkey} ${executor.user}@${executor.ipaddr.addr} ${port} exit'
print(cmd)
res:=os.execute(cmd)
if res.exit_code==0{
println("-> ssh ok")
res := os.execute(cmd)
if res.exit_code == 0 {
println('-> ssh ok')
return true
}
println("-> ssh failed")
println('-> ssh failed')
return false
}

Expand All @@ -128,10 +127,10 @@ pub fn (mut executor ExecutorSSH) exec_interactive(args_ ExecArgs) ! {
if executor.ipaddr.port > 10 {
port = '-p ${executor.ipaddr.port}'
}
mut sshkey:=""
if os.exists(executor.sshkey){
sshkey = "-i ${executor.sshkey} "
}
mut sshkey := ''
if os.exists(executor.sshkey) {
sshkey = '-i ${executor.sshkey} '
}
args.cmd = 'ssh -tt -o StrictHostKeyChecking=no ${sshkey}${executor.user}@${executor.ipaddr.addr} ${port} "${args.cmd}"'
osal.execute_interactive(args.cmd)!
}
Expand Down Expand Up @@ -192,7 +191,7 @@ pub fn (mut executor ExecutorSSH) download(args SyncArgs) ! {
ignore: args.ignore
ignore_default: args.ignore_default
stdout: args.stdout
sshkey: executor.sshkey
sshkey: executor.sshkey
}
rsync.rsync(rsargs)!
}
Expand Down Expand Up @@ -242,7 +241,7 @@ pub fn (mut executor ExecutorSSH) upload(args SyncArgs) ! {
ignore_default: args.ignore_default
stdout: args.stdout
fast_rsync: args.fast_rsync
sshkey: executor.sshkey
sshkey: executor.sshkey
}
rsync.rsync(rsargs)!
}
Expand Down
14 changes: 6 additions & 8 deletions crystallib/builder/sshping.v
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module builder


pub struct SSHPingArgs {
pub mut:
ipaddr string
user string = 'root'
timeout int = 60
ipaddr string
user string = 'root'
timeout int = 60
}


//- format ipaddr: 192.168.6.6:7777
//- format ipaddr: 192.168.6.6
//- format ipadd6: [666:555:555:...]
pub fn sshping(args SSHPingArgs)!{
executor_new(ipaddr:args.ipaddr, user:args.user,checkconnect:args.timeout)!
}
pub fn sshping(args SSHPingArgs) ! {
executor_new(ipaddr: args.ipaddr, user: args.user, checkconnect: args.timeout)!
}
11 changes: 5 additions & 6 deletions crystallib/clients/httpconnection/caching.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module httpconnection

import crypto.md5
import json
import net.http { Header, Method }
import net.http { Method }

// https://cassiomolin.com/2016/09/09/which-http-status-codes-are-cacheable/
const default_cacheable_codes = [200, 203, 204, 206, 300, 404, 405, 410, 414, 501]
Expand All @@ -25,7 +25,6 @@ pub mut:
data string
}


// calculate the key for the cache starting from data and url
fn (mut h HTTPConnection) cache_key(req Request) string {
url := h.url(req).split('!')
Expand All @@ -45,22 +44,22 @@ fn (mut h HTTPConnection) cache_get(req Request) !Result {
key := h.cache_key(req)
mut data := h.redis.get(key) or {
assert '${err}' == 'none'
//console.print_debug("cache get: ${key} not in redis")
// console.print_debug("cache get: ${key} not in redis")
return Result{
code: -1
}
}
if data == '' {
//console.print_debug("cache get: ${key} empty data")
// console.print_debug("cache get: ${key} empty data")
return Result{
code: -1
}
}
result := json.decode(Result, data) or {
//console.print_debug("cache get: ${key} coud not decode")
// console.print_debug("cache get: ${key} coud not decode")
return error('failed to decode result with error: ${err}.\ndata:\n${data}')
}
//console.print_debug("cache get: ${key} ok")
// console.print_debug("cache get: ${key} ok")
return result
}

Expand Down
37 changes: 4 additions & 33 deletions crystallib/clients/httpconnection/connection.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module httpconnection

import net.http { Header, Method }
import net.http { Header }
import freeflowuniverse.crystallib.clients.redisclient { Redis }

@[heap]
Expand All @@ -13,39 +13,10 @@ pub mut:
retry int = 5
}

// @[heap]
// pub struct HTTPConnections {
// pub mut:
// connections map[string]&HTTPConnection
// }

// Join headers from httpconnection and Request
fn (mut h HTTPConnection) header(req Request) http.Header {
mut header := req.header or {
return h.default_header
}
fn (mut h HTTPConnection) header(req Request) Header {
mut header := req.header or { return h.default_header }

return h.default_header.join(header)
}

// // get new request
// //
// // ```
// // method Method (.get, .post, .put, ...)
// // prefix string
// // id string
// // params map[string]string
// // data string
// // cache_disable bool = true
// // header Header
// // dict_key string //if the return is a dict, then will take the element out of the dict with the key and process further
// // list_dict_key string //if the output is a list of dicts, then will process each element of the list to take the val with key out of that dict
// // debug bool
// // dataformat DataFormat
// // ```
// // for header see https://modules.vlang.io/net.http.html#Method .
// // for method see https://modules.vlang.io/net.http.html#Header
// pub fn (mut h HTTPConnection) request(args_ Request) !Request {
// mut args := args_

// return args
// }
Loading

0 comments on commit 7cab924

Please sign in to comment.