-
Notifications
You must be signed in to change notification settings - Fork 4
/
execute.go
116 lines (102 loc) · 2.63 KB
/
execute.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package server
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"golang.org/x/crypto/ssh"
)
type executeConfiguration struct {
privateKeyFilename string
payloadFilename string
targetPayloadDir string
targetPayloadFilename string
userScriptPathFilename string
relayNodeName string
remoteServer string
dataDir string
homeDir string
webhookID string
payload []byte
username string
groupname string
password string
}
// CopyFile copies a source file to a destination file.
// Any existing file will be overwritten and will not copy file attributes.
func CopyFile(src string, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func triggerQsubCommand(c Connector, client *ssh.Client, conf executeConfiguration) error {
session, err := c.NewSession(client)
if err != nil {
return err
}
defer c.CloseSession(session)
// Grab the path to the user script
contents, err := ioutil.ReadFile(conf.userScriptPathFilename)
if err != nil {
return err
}
userScriptFilename := string(contents)
// Go the correct folder and run the qsub command from there
command := fmt.Sprintf("cd ~/.qaas/%s/ && qsub -F %s %s", conf.webhookID, conf.targetPayloadFilename, userScriptFilename)
fmt.Println(command)
err = c.Run(session, command)
if err != nil {
return err
}
return err
}
// ExecuteScript triggers a qsub command on the HPC cluster
func ExecuteScript(c Connector, conf executeConfiguration) error {
// Configure the SSH connection
privateKey, err := ioutil.ReadFile(conf.privateKeyFilename)
if err != nil {
return err
}
signer, _ := ssh.ParsePrivateKey(privateKey)
clientConfig := &ssh.ClientConfig{
User: conf.username,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
// Start an SSH session on the relay node
remoteServer := fmt.Sprintf("%s:22", conf.relayNodeName)
client, err := c.NewClient(remoteServer, clientConfig)
if err != nil {
return err
}
// Copy the payload to QaaS folder
err = os.MkdirAll(conf.targetPayloadDir, os.ModePerm)
if err != nil {
return err
}
err = CopyFile(conf.payloadFilename, conf.targetPayloadFilename)
if err != nil {
return err
}
// Trigger the qsub command
err = triggerQsubCommand(c, client, conf)
if err != nil {
return err
}
return err
}