chore: fix runssh include -sid flag and ssh key parse
This commit is contained in:
parent
a8b8152d98
commit
cf5947fc19
@ -127,7 +127,9 @@ Usage of upclapi:
|
||||
-kdr string
|
||||
use coder
|
||||
-o string
|
||||
output format (json|yaml)
|
||||
output format (attached for infofloatip)
|
||||
-sid string
|
||||
ssh id for ssh commands (or defaul id_rsa)
|
||||
-t string
|
||||
target item for command
|
||||
```
|
||||
@ -168,7 +170,7 @@ Usage of upclapi:
|
||||
(yaml-config-file has to include *ssh** config section)
|
||||
|
||||
```bash
|
||||
./build/upclapi -c runssh -f [yaml-config-file] -ssh remote-command-to-execute
|
||||
./build/upclapi -c runssh -f [yaml-config-file] -ssh remote-command-to-execute [-sid ssh/key_filename (or id_pub in HOME by default)]
|
||||
```
|
||||
|
||||
- Create Servers [Ansible Inventory](https://docs.ansible.com/ansible/latest/index.html)
|
||||
|
7
main.go
7
main.go
@ -45,6 +45,7 @@ type RunFlags struct {
|
||||
encdr string
|
||||
out string
|
||||
target string
|
||||
sid string
|
||||
}
|
||||
type CoderConfig struct {
|
||||
cmd string
|
||||
@ -496,6 +497,7 @@ func ParseFlags() (RunFlags,error) {
|
||||
encdr: os.Getenv("UPCLAPI_ENCODER"),
|
||||
out: os.Getenv("UPCLAPI_OUT"),
|
||||
target: os.Getenv("UPCLAPI_TARGET"),
|
||||
sid: os.Getenv("UPCLAPI_SID"),
|
||||
}
|
||||
if runFlags.command == "" {
|
||||
runFlags.command = DFLT_COMMAND
|
||||
@ -523,8 +525,9 @@ func ParseFlags() (RunFlags,error) {
|
||||
flag.StringVar(&runFlags.id, "id", runFlags.id, "resource name or uuid")
|
||||
flag.StringVar(&runFlags.runCmd, "cmd", runFlags.runCmd, "run [ssh] command")
|
||||
flag.StringVar(&runFlags.encdr, "kdr", runFlags.encdr, "use coder ")
|
||||
flag.StringVar(&runFlags.out, "o", runFlags.out, "output format ")
|
||||
flag.StringVar(&runFlags.target, "t", runFlags.out, "target item for command ")
|
||||
flag.StringVar(&runFlags.out, "o", runFlags.out, "output format (attached for infofloatip)")
|
||||
flag.StringVar(&runFlags.target, "t", runFlags.target, "target item for command ")
|
||||
flag.StringVar(&runFlags.sid, "sid", runFlags.sid, "ssh id for ssh commands (or defaul id_rsa)")
|
||||
|
||||
// Actually parse the flags
|
||||
flag.Parse()
|
||||
|
2
run.go
2
run.go
@ -432,7 +432,7 @@ func onServers(s *service.Service, tsksrvc string, runFlags RunFlags, datacfg *D
|
||||
}
|
||||
case "runssh":
|
||||
if sshAccess.Host != "" {
|
||||
output, err := runSSH(sshAccess, runCommand)
|
||||
output, err := runSSH(runFlags, sshAccess, runCommand)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
20
ssh.go
20
ssh.go
@ -15,7 +15,7 @@ import (
|
||||
// "github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func publicKeyAuthFunc(kPath string) ssh.AuthMethod {
|
||||
func publicKeyAuthGet(kPath string) ssh.AuthMethod {
|
||||
// keyPath, err := homedir.Expand(kPath)
|
||||
// if err != nil {
|
||||
// log.Fatal("find key's home dir failed", err)
|
||||
@ -23,16 +23,18 @@ func publicKeyAuthFunc(kPath string) ssh.AuthMethod {
|
||||
// key, err := ioutil.ReadFile(keyPath )
|
||||
key, err := ioutil.ReadFile(kPath )
|
||||
if err != nil {
|
||||
log.Fatal("ssh key file read failed", err)
|
||||
log.Fatal("ssh key file read failed: " , err)
|
||||
return nil
|
||||
}
|
||||
// Create the Signer for this private key.
|
||||
signer, err := ssh.ParsePrivateKey(key)
|
||||
if err != nil {
|
||||
log.Fatal("ssh key signer failed", err)
|
||||
log.Fatal("ssh key signer failed: ", err)
|
||||
return nil
|
||||
}
|
||||
return ssh.PublicKeys(signer)
|
||||
}
|
||||
func runSSH(cfg SSHAccess, cmds ...string ) ([]byte, error) {
|
||||
func runSSH(runFlags RunFlags, cfg SSHAccess, cmds ...string ) ([]byte, error) {
|
||||
// fmt.Fprintf(os.Stderr, "SSH: %#v\n", cfg)
|
||||
fmt.Fprintf(os.Stderr, "%s - running : %s\n\n", cfg.Host, cmds)
|
||||
|
||||
@ -46,7 +48,15 @@ func runSSH(cfg SSHAccess, cmds ...string ) ([]byte, error) {
|
||||
if cfg.UType == "password" {
|
||||
config.Auth = []ssh.AuthMethod{ssh.Password(cfg.Password)}
|
||||
} else {
|
||||
config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(cfg.KeyPath)}
|
||||
sshkey := cfg.KeyPath
|
||||
if len(sshkey) == 0 {
|
||||
sid := runFlags.sid
|
||||
if len(sid) == 0 {
|
||||
sid="id_rsa"
|
||||
}
|
||||
sshkey = fmt.Sprintf("%s/.ssh/%s", os.Getenv("HOME"),sid)
|
||||
}
|
||||
config.Auth = []ssh.AuthMethod{publicKeyAuthGet(sshkey)}
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
||||
conn, err := ssh.Dial("tcp", addr,config)
|
||||
|
Loading…
Reference in New Issue
Block a user