chore: fix runssh to use -id flag and ssh defaults in main

This commit is contained in:
Jesús Pérez Lorenzo 2021-08-31 21:40:52 +01:00
parent 4bf4f0252c
commit d7918cbcdc
3 changed files with 32 additions and 6 deletions

View File

@ -53,7 +53,7 @@ Short version, can be interpreted as:
## Download Release binary
1 - Copy attachment url from [releases page](https://rlung.librecloud.online/LibreCloud/Upclapi/releases)
1 - Copy <u>attachment url</u> from [releases page](https://rlung.librecloud.online/LibreCloud/Upclapi/releases)
2 - Use this **shell** command changing <u>attachment URL</u>:

View File

@ -23,6 +23,7 @@ const SEPARATOR = "::"
const DATA_CFG = "datacfg.yaml"
const DFLT_COMMAND= "infoserver"
const SSH_KEY_PATH = ".sshkeys"
const DFLT_SSH_USER = "kloudsadm"
const MONTH_HOURS = 672

35
run.go
View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"strconv"
"strings"
"github.com/joho/godotenv"
@ -17,6 +18,21 @@ import (
"github.com/UpCloudLtd/upcloud-go-api/upcloud/service"
)
func defaultSSHAccess(host string) SSHAccess {
sshaccess := SSHAccess{Host: host}
var err error
sshaccess.Port, err = strconv.Atoi(os.Getenv("SSH_PORT"))
if err != nil {
sshaccess.Port = 22
}
sshaccess.User = os.Getenv("SSH_USER")
if len(sshaccess.User) == 0 {
sshaccess.User = DFLT_SSH_USER
}
sshaccess.Password = ""
sshaccess.KeyPath = ""
return sshaccess
}
func cmdexec(command string, args []string) (string, error) {
cmd := &exec.Cmd {
Path: command,
@ -197,12 +213,21 @@ func run() int {
fmt.Fprintln(os.Stderr, "Unable to run ssh command: ", runFlags.command)
return 99
}
datacfg,err := loadDataConfig(runFlags)
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to run ssh command: ", runFlags.command)
return 99
if len(runFlags.id) == 0 {
datacfg,err := loadDataConfig(runFlags)
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to run ssh command: ", runFlags.command)
return 99
}
onServers(s,"runssh",runFlags,datacfg)
} else {
sshAccess := defaultSSHAccess(runFlags.id)
output, err := runSSH(runFlags, sshAccess, runFlags.runCmd)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
}
onServers(s,"runssh",runFlags,datacfg)
case "runcmd":
if len(runFlags.runCmd) == 0 {
fmt.Fprintln(os.Stderr, "Unable to run command: ", runFlags.command)