forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdp_windows.go
108 lines (91 loc) · 2.33 KB
/
rdp_windows.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
package main
import (
"net"
"path/filepath"
"time"
tcclient "github.com/taskcluster/taskcluster/v47/clients/client-go"
"github.com/taskcluster/taskcluster/v47/internal/scopes"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/artifacts"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/fileutil"
)
var (
rdpInfoPath = filepath.Join("generic-worker", "rdp.json")
)
type RDPFeature struct {
}
func (feature *RDPFeature) Name() string {
return "RDP"
}
func (feature *RDPFeature) Initialise() error {
return nil
}
func (feature *RDPFeature) PersistState() error {
return nil
}
// RDP is only enabled when task.payload.rdpInfo is set
func (feature *RDPFeature) IsEnabled(task *TaskRun) bool {
return task.Payload.RdpInfo != ""
}
type RDPTask struct {
task *TaskRun
info *RDPInfo
}
type RDPInfo struct {
Host net.IP `json:"host"`
Port uint16 `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
}
func (feature *RDPFeature) NewTaskFeature(task *TaskRun) TaskFeature {
return &RDPTask{
task: task,
}
}
func (l *RDPTask) RequiredScopes() scopes.Required {
return scopes.Required{
{
"generic-worker:allow-rdp:" + l.task.Definition.ProvisionerID + "/" + l.task.Definition.WorkerType,
},
}
}
func (l *RDPTask) ReservedArtifacts() []string {
return []string{
l.task.Payload.RdpInfo,
}
}
func (l *RDPTask) Start() *CommandExecutionError {
l.createRDPArtifact()
return l.uploadRDPArtifact()
}
func (l *RDPTask) Stop(err *ExecutionErrors) {
time.Sleep(time.Hour * 12)
}
func (l *RDPTask) createRDPArtifact() {
l.info = &RDPInfo{
Host: config.PublicIP,
Port: 3389,
Username: taskContext.User.Name,
Password: taskContext.User.Password,
}
rdpInfoFile := filepath.Join(taskContext.TaskDir, rdpInfoPath)
err := fileutil.WriteToFileAsJSON(l.info, rdpInfoFile)
// if we can't write this, something seriously wrong, so cause worker to
// report an internal-error to sentry and crash!
if err != nil {
panic(err)
}
}
func (l *RDPTask) uploadRDPArtifact() *CommandExecutionError {
return l.task.uploadArtifact(
createDataArtifact(
&artifacts.BaseArtifact{
Name: l.task.Payload.RdpInfo,
// RDP info expires one day after task
Expires: tcclient.Time(time.Now().Add(time.Hour * 24)),
},
rdpInfoPath,
"application/json",
"gzip",
),
)
}