diff --git a/README.md b/README.md index e509203..4b7f819 100644 --- a/README.md +++ b/README.md @@ -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 attachment url from [releases page](https://rlung.librecloud.online/LibreCloud/Upclapi/releases) 2 - Use this **shell** command changing attachment URL: diff --git a/main.go b/main.go index 1696828..c22854f 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/run.go b/run.go index 124ae5c..bbf3333 100644 --- a/run.go +++ b/run.go @@ -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)