-
Notifications
You must be signed in to change notification settings - Fork 44
/
SSHRUNRest.go
52 lines (43 loc) · 1.44 KB
/
SSHRUNRest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Rest Runtime Server for VM's SSH and SCP of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2019.10.
package restruntime
import (
sshrun "github.com/cloud-barista/cb-spider/cloud-control-manager/vm-ssh"
"strings"
// REST API (echo)
"net/http"
"github.com/labstack/echo/v4"
)
type SSHRUNReqInfo struct {
UserName string // ex) "root"
PrivateKey []string // ex) ["-----BEGIN RSA PRIVATE KEY-----",
// "MIIEoQIBAAKCAQEArVNOLwMIp5VmZ4VPZotcoCHdEzimKalAsz+ccLfvAA1Y2ELH",
// "..."]
ServerPort string // ex) "node12:22"
Command string // ex) "hostname"
}
//================ SSH RUN
func SSHRun(c echo.Context) error {
cblog.Info("call SSHRun()")
req := &SSHRUNReqInfo{}
if err := c.Bind(req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
strPrivateKey := strings.Join(req.PrivateKey[:], "\n")
sshInfo := sshrun.SSHInfo{
UserName: req.UserName,
PrivateKey: []byte(strPrivateKey),
ServerPort: req.ServerPort,
}
var result string
var err error
if result, err = sshrun.SSHRun(sshInfo, req.Command); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Error while running cmd: "+req.Command+"]"+err.Error())
}
return c.JSON(http.StatusOK, result)
}