-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhelper.go
67 lines (58 loc) · 1.76 KB
/
helper.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
package exec
import (
"github.com/viant/endly"
"github.com/viant/endly/model"
"github.com/viant/endly/model/location"
)
var sessionsKey = (*model.Sessions)(nil)
// TerminalSessions returns system sessions
func TerminalSessions(context *endly.Context) model.Sessions {
var result *model.Sessions
if !context.Contains(sessionsKey) {
var sessions model.Sessions = make(map[string]*model.Session)
result = &sessions
context.AsyncUnsafeKeys[sessionsKey] = true
_ = context.Put(sessionsKey, result)
} else {
context.GetInto(sessionsKey, &result)
}
return *result
}
// SessionID returns session I
func SessionID(context *endly.Context, target *location.Resource) string {
username := ""
if cred, _ := context.Secrets.GetCredentials(context.Background(), target.Credentials); cred != nil {
username = cred.Username
}
return username + "@" + target.Hostname()
}
// TerminalSession returns Session for passed in target resource.
func TerminalSession(context *endly.Context, target *location.Resource) (*model.Session, error) {
sessions := TerminalSessions(context)
var sessionID = SessionID(context, target)
if !sessions.Has(sessionID) {
service, err := context.Service(ServiceID)
if err != nil {
return nil, err
}
response := service.Run(context, &OpenSessionRequest{
Target: target,
})
if response.Err != nil {
return nil, response.Err
}
}
return sessions[sessionID], nil
}
// Os returns operating system for provide session
func OperatingSystem(context *endly.Context, sessionName string) *model.OperatingSystem {
var sessions = TerminalSessions(context)
if session, has := sessions[sessionName]; has {
os := model.OperatingSystem{
OSInfo: session.OsInfo(),
HardwareInfo: session.HardwareInfo(),
}
return &os
}
return nil
}