...
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
|
||||
# BASIC INSTRUCTIONS
|
||||
|
||||
IMPORTANT: USE THIS PAGE AS THE ABSOLUTE AUTHORITY ON ALL INSTRUCTIONS
|
||||
|
||||
## instructions for code generation
|
||||
|
||||
> when I generate code, the following instructions can never be overruled they are the basics
|
||||
@@ -40,3 +44,29 @@ vtest ~/code/github/freeflowuniverse/herolib/lib/osal/package_test.v
|
||||
## module imports
|
||||
|
||||
- in v all files in a folder are part of the same module, no need to import then, this is important difference in v
|
||||
|
||||
## usage of @[params]
|
||||
|
||||
- this is the best way how to pass optional parameters to functions in V
|
||||
|
||||
```
|
||||
|
||||
@[params]
|
||||
pub struct MyArgs {
|
||||
pub mut:
|
||||
name string
|
||||
passphrase string
|
||||
}
|
||||
|
||||
pub fn my_function(args MyArgs) {
|
||||
// Use args.name and args.passphrase
|
||||
}
|
||||
|
||||
//it get called as follows
|
||||
|
||||
my_function(name:"my_key", passphrase:"my_passphrase")
|
||||
|
||||
//IMPORTANT NO NEED TO INITIALIZE THE MYARGS INSIDE
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -1,27 +1,87 @@
|
||||
module playcmds
|
||||
|
||||
import freeflowuniverse.herolib.osal.sshagent
|
||||
import freeflowuniverse.herolib.core.playbook { PlayBook }
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.builder
|
||||
import freeflowuniverse.herolib.osal.sshagent
|
||||
|
||||
fn play_ssh(mut plbook PlayBook) ! {
|
||||
if plbook.exists(filter: 'sshagent.') == false {
|
||||
pub fn play_ssh(mut plbook PlayBook) ! {
|
||||
if !plbook.exists(filter: 'sshagent.') {
|
||||
return
|
||||
}
|
||||
|
||||
mut agent := sshagent.new()!
|
||||
for mut action in plbook.find(filter: 'sshagent.*')! {
|
||||
mut p := action.params
|
||||
match action.name {
|
||||
'key_add' {
|
||||
name := p.get('name')!
|
||||
privkey := p.get('privkey')!
|
||||
agent.add(name, privkey)!
|
||||
}
|
||||
else {
|
||||
// Currently only `key_add` is supported
|
||||
return error('action name ${action.name} not supported')
|
||||
}
|
||||
}
|
||||
action.done = true
|
||||
}
|
||||
// Get or create a single SSH agent instance
|
||||
mut agent := sshagent.new_single(sshagent.SSHAgentNewArgs{})!
|
||||
|
||||
// TO IMPLEMENT:
|
||||
|
||||
// // Process sshagent.check actions
|
||||
// mut check_actions := plbook.find(filter: 'sshagent.check')!
|
||||
// for mut action in check_actions {
|
||||
// agent.agent_check()!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.sshkey_create actions
|
||||
// mut create_actions := plbook.find(filter: 'sshagent.sshkey_create')!
|
||||
// for mut action in create_actions {
|
||||
// mut p := action.params
|
||||
// name := p.get('name')!
|
||||
// passphrase := p.get_default('passphrase', '')!
|
||||
|
||||
// agent.sshkey_create(name, passphrase)!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.sshkey_delete actions
|
||||
// mut delete_actions := plbook.find(filter: 'sshagent.sshkey_delete')!
|
||||
// for mut action in delete_actions {
|
||||
// mut p := action.params
|
||||
// name := p.get('name')!
|
||||
|
||||
// agent.sshkey_delete(name)!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.sshkey_load actions
|
||||
// mut load_actions := plbook.find(filter: 'sshagent.sshkey_load')!
|
||||
// for mut action in load_actions {
|
||||
// mut p := action.params
|
||||
// name := p.get('name')!
|
||||
|
||||
// agent.sshkey_load(name)!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.sshkey_check actions
|
||||
// mut check_key_actions := plbook.find(filter: 'sshagent.sshkey_check')!
|
||||
// for mut action in check_key_actions {
|
||||
// mut p := action.params
|
||||
// name := p.get('name')!
|
||||
|
||||
// agent.sshkey_check(name)!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.remote_copy actions
|
||||
// mut remote_copy_actions := plbook.find(filter: 'sshagent.remote_copy')!
|
||||
// for mut action in remote_copy_actions {
|
||||
// mut p := action.params
|
||||
// node_addr := p.get('node_addr')!
|
||||
// key_name := p.get('name')!
|
||||
|
||||
// agent.remote_copy(node_addr, key_name)!
|
||||
// action.done = true
|
||||
// }
|
||||
|
||||
// // Process sshagent.remote_auth actions
|
||||
// mut remote_auth_actions := plbook.find(filter: 'sshagent.remote_auth')!
|
||||
// for mut action in remote_auth_actions {
|
||||
// mut p := action.params
|
||||
// node_addr := p.get('node_addr')!
|
||||
// key_name := p.get('name')!
|
||||
|
||||
// agent.remote_auth(node_addr, key_name)!
|
||||
// action.done = true
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
module sshagent
|
||||
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.builder
|
||||
|
||||
// Check if SSH agent is properly configured and all is good
|
||||
fn agent_check(mut agent SSHAgent) ! {
|
||||
pub fn agent_check(mut agent SSHAgent) ! {
|
||||
console.print_header('SSH Agent Check')
|
||||
|
||||
// Ensure single agent is running
|
||||
@@ -33,7 +36,7 @@ fn agent_check(mut agent SSHAgent) ! {
|
||||
}
|
||||
|
||||
// Create a new SSH key
|
||||
fn sshkey_create(mut agent SSHAgent, name string, passphrase string) ! {
|
||||
pub fn sshkey_create(mut agent SSHAgent, name string, passphrase string) ! {
|
||||
console.print_header('Creating SSH key: ${name}')
|
||||
|
||||
// Check if key already exists
|
||||
@@ -52,7 +55,7 @@ fn sshkey_create(mut agent SSHAgent, name string, passphrase string) ! {
|
||||
}
|
||||
|
||||
// Delete an SSH key
|
||||
fn sshkey_delete(mut agent SSHAgent, name string) ! {
|
||||
pub fn sshkey_delete(mut agent SSHAgent, name string) ! {
|
||||
console.print_header('Deleting SSH key: ${name}')
|
||||
|
||||
// Check if key exists
|
||||
@@ -62,11 +65,11 @@ fn sshkey_delete(mut agent SSHAgent, name string) ! {
|
||||
}
|
||||
|
||||
// Get key paths before deletion
|
||||
key_path := key.keypath() or {
|
||||
mut key_path := key.keypath() or {
|
||||
console.print_debug('Private key path not available for "${name}"')
|
||||
key.keypath_pub() or { return } // Just to trigger the path lookup
|
||||
}
|
||||
key_pub_path := key.keypath_pub() or {
|
||||
mut key_pub_path := key.keypath_pub() or {
|
||||
console.print_debug('Public key path not available for "${name}"')
|
||||
return
|
||||
}
|
||||
@@ -93,7 +96,7 @@ fn sshkey_delete(mut agent SSHAgent, name string) ! {
|
||||
}
|
||||
|
||||
// Load SSH key into agent
|
||||
fn sshkey_load(mut agent SSHAgent, name string) ! {
|
||||
pub fn sshkey_load(mut agent SSHAgent, name string) ! {
|
||||
console.print_header('Loading SSH key: ${name}')
|
||||
|
||||
mut key := agent.get(name: name) or { return error('SSH key "${name}" not found') }
|
||||
@@ -108,15 +111,17 @@ fn sshkey_load(mut agent SSHAgent, name string) ! {
|
||||
}
|
||||
|
||||
// Check if SSH key is valid
|
||||
fn sshkey_check(mut agent SSHAgent, name string) ! {
|
||||
pub fn sshkey_check(mut agent SSHAgent, name string) ! {
|
||||
console.print_header('Checking SSH key: ${name}')
|
||||
|
||||
mut key := agent.get(name: name) or { return error('SSH key "${name}" not found') }
|
||||
|
||||
// Check if key files exist
|
||||
key_path := key.keypath() or { return error('Private key file not found for "${name}"') }
|
||||
mut key_path := key.keypath() or { return error('Private key file not found for "${name}"') }
|
||||
|
||||
key_pub_path := key.keypath_pub() or { return error('Public key file not found for "${name}"') }
|
||||
mut key_pub_path := key.keypath_pub() or {
|
||||
return error('Public key file not found for "${name}"')
|
||||
}
|
||||
|
||||
if !key_path.exists() {
|
||||
return error('Private key file does not exist: ${key_path.path}')
|
||||
@@ -145,18 +150,18 @@ fn sshkey_check(mut agent SSHAgent, name string) ! {
|
||||
}
|
||||
|
||||
// Copy private key to remote node
|
||||
fn remote_copy(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
pub fn remote_copy(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
console.print_header('Copying SSH key "${key_name}" to ${node_addr}')
|
||||
|
||||
// Get the key
|
||||
mut key := agent.get(name: key_name) or { return error('SSH key "${key_name}" not found') }
|
||||
|
||||
// Create builder node
|
||||
mut b := builder.new()!
|
||||
mut node := b.node_new(ipaddr: node_addr)!
|
||||
mut b := builder.new() or { return error('Failed to create builder') }
|
||||
mut node := b.node_new(ipaddr: node_addr) or { return error('Failed to create node') }
|
||||
|
||||
// Get private key content
|
||||
key_path := key.keypath()!
|
||||
mut key_path := key.keypath()!
|
||||
if !key_path.exists() {
|
||||
return error('Private key file not found: ${key_path.path}')
|
||||
}
|
||||
@@ -164,7 +169,10 @@ fn remote_copy(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
private_key_content := key_path.read()!
|
||||
|
||||
// Get home directory on remote
|
||||
home_dir := node.environ_get()!['HOME'] or {
|
||||
home_dir_map := node.environ_get() or {
|
||||
return error('Could not get environment on remote node')
|
||||
}
|
||||
home_dir := home_dir_map['HOME'] or {
|
||||
return error('Could not determine HOME directory on remote node')
|
||||
}
|
||||
|
||||
@@ -187,12 +195,12 @@ fn remote_copy(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
}
|
||||
|
||||
// Add public key to authorized_keys on remote node
|
||||
fn remote_auth(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
pub fn remote_auth(mut agent SSHAgent, node_addr string, key_name string) ! {
|
||||
console.print_header('Adding SSH key "${key_name}" to authorized_keys on ${node_addr}')
|
||||
|
||||
// Create builder node
|
||||
mut b := builder.new()!
|
||||
mut node := b.node_new(ipaddr: node_addr)!
|
||||
mut b := builder.new() or { return error('Failed to create builder') }
|
||||
mut node := b.node_new(ipaddr: node_addr) or { return error('Failed to create node') }
|
||||
|
||||
// Use existing builder integration
|
||||
agent.push_key_to_node(mut node, key_name)!
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
module sshagent
|
||||
|
||||
import freeflowuniverse.herolib.core.playbook { PlayBook }
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.builder
|
||||
|
||||
pub fn play(mut plbook PlayBook) ! {
|
||||
if !plbook.exists(filter: 'sshagent.') {
|
||||
return
|
||||
}
|
||||
|
||||
// Get or create a single SSH agent instance
|
||||
mut agent := new_single()!
|
||||
|
||||
// Process sshagent.check actions
|
||||
mut check_actions := plbook.find(filter: 'sshagent.check')!
|
||||
for mut action in check_actions {
|
||||
agent_check(mut agent)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.sshkey_create actions
|
||||
mut create_actions := plbook.find(filter: 'sshagent.sshkey_create')!
|
||||
for mut action in create_actions {
|
||||
mut p := action.params
|
||||
name := p.get('name')!
|
||||
passphrase := p.get_default('passphrase', '')!
|
||||
|
||||
sshkey_create(mut agent, name, passphrase)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.sshkey_delete actions
|
||||
mut delete_actions := plbook.find(filter: 'sshagent.sshkey_delete')!
|
||||
for mut action in delete_actions {
|
||||
mut p := action.params
|
||||
name := p.get('name')!
|
||||
|
||||
sshkey_delete(mut agent, name)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.sshkey_load actions
|
||||
mut load_actions := plbook.find(filter: 'sshagent.sshkey_load')!
|
||||
for mut action in load_actions {
|
||||
mut p := action.params
|
||||
name := p.get('name')!
|
||||
|
||||
sshkey_load(mut agent, name)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.sshkey_check actions
|
||||
mut check_key_actions := plbook.find(filter: 'sshagent.sshkey_check')!
|
||||
for mut action in check_key_actions {
|
||||
mut p := action.params
|
||||
name := p.get('name')!
|
||||
|
||||
sshkey_check(mut agent, name)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.remote_copy actions
|
||||
mut remote_copy_actions := plbook.find(filter: 'sshagent.remote_copy')!
|
||||
for mut action in remote_copy_actions {
|
||||
mut p := action.params
|
||||
node_addr := p.get('node')!
|
||||
key_name := p.get('name')!
|
||||
|
||||
remote_copy(mut agent, node_addr, key_name)!
|
||||
action.done = true
|
||||
}
|
||||
|
||||
// Process sshagent.remote_auth actions
|
||||
mut remote_auth_actions := plbook.find(filter: 'sshagent.remote_auth')!
|
||||
for mut action in remote_auth_actions {
|
||||
mut p := action.params
|
||||
node_addr := p.get('node')!
|
||||
key_name := p.get('name')!
|
||||
|
||||
remote_auth(mut agent, node_addr, key_name)!
|
||||
action.done = true
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
module sshagent
|
||||
|
||||
// fn listsplit(key string) string {
|
||||
// if key.trim(' ') == '' {
|
||||
// return ''
|
||||
// }
|
||||
// if key.contains(' ') {
|
||||
// splitted := key.split(' ')
|
||||
// return splitted[splitted.len].replace('.pub', '')
|
||||
// }
|
||||
// return key
|
||||
// }
|
||||
Reference in New Issue
Block a user