-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.go
149 lines (127 loc) · 5.05 KB
/
runner.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package command
import (
"fmt"
"github.com/pulumi/pulumi-command/sdk/go/command/local"
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/DataDog/test-infra-definitions/common/config"
"github.com/DataDog/test-infra-definitions/common/namer"
"github.com/DataDog/test-infra-definitions/common/utils"
)
type Args struct {
Create pulumi.StringInput
Update pulumi.StringInput
Delete pulumi.StringInput
Triggers pulumi.ArrayInput
Stdin pulumi.StringPtrInput
Environment pulumi.StringMap
RequirePasswordFromStdin bool
Sudo bool
}
func (args *Args) toLocalCommandArgs(config runnerConfiguration, osCommand OSCommand) *local.CommandArgs {
return &local.CommandArgs{
Create: osCommand.BuildCommandString(args.Create, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Update: osCommand.BuildCommandString(args.Update, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Delete: osCommand.BuildCommandString(args.Delete, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Triggers: args.Triggers,
Stdin: args.Stdin,
}
}
func (args *Args) toRemoteCommandArgs(config runnerConfiguration, osCommand OSCommand) *remote.CommandArgs {
return &remote.CommandArgs{
Connection: config.connection,
Create: osCommand.BuildCommandString(args.Create, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Update: osCommand.BuildCommandString(args.Update, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Delete: osCommand.BuildCommandString(args.Delete, args.Environment, args.Sudo, args.RequirePasswordFromStdin, config.user),
Triggers: args.Triggers,
Stdin: args.Stdin,
}
}
// Transformer is a function that can be used to modify the command name and args.
// Examples: swapping `args.Delete` with `args.Create`, or adding `args.Triggers`, or editing the name
type Transformer func(name string, args Args) (string, Args)
type runnerConfiguration struct {
user string
connection remote.ConnectionInput
}
type Runner struct {
e config.Env
namer namer.Namer
waitCommand *remote.Command
config runnerConfiguration
osCommand OSCommand
options []pulumi.ResourceOption
}
type RunnerArgs struct {
ParentResource pulumi.Resource
ConnectionName string
Connection remote.ConnectionInput
ReadyFunc ReadyFunc
User string
OSCommand OSCommand
}
func NewRunner(e config.Env, args RunnerArgs) (*Runner, error) {
runner := &Runner{
e: e,
namer: namer.NewNamer(e.Ctx(), "remote").WithPrefix(args.ConnectionName),
config: runnerConfiguration{
connection: args.Connection,
user: args.User,
},
osCommand: args.OSCommand,
options: []pulumi.ResourceOption{
e.WithProviders(config.ProviderCommand),
},
}
if args.ParentResource != nil {
runner.options = append(runner.options, pulumi.Parent(args.ParentResource), pulumi.DeletedWith(args.ParentResource))
}
if args.ReadyFunc != nil {
var err error
runner.waitCommand, err = args.ReadyFunc(runner)
if err != nil {
return nil, err
}
runner.options = append(runner.options, utils.PulumiDependsOn(runner.waitCommand))
}
return runner, nil
}
func (r *Runner) Command(name string, args *Args, opts ...pulumi.ResourceOption) (*remote.Command, error) {
if args.Sudo && r.config.user != "" {
r.e.Ctx().Log.Info(fmt.Sprintf("warning: running sudo command on a runner with user %s, discarding user", r.config.user), nil)
}
return remote.NewCommand(r.e.Ctx(), r.namer.ResourceName("cmd", name), args.toRemoteCommandArgs(r.config, r.osCommand), utils.MergeOptions(r.options, opts...)...)
}
func (r *Runner) NewCopyFile(localPath, remotePath string, opts ...pulumi.ResourceOption) (*remote.CopyFile, error) {
return remote.NewCopyFile(r.e.Ctx(), r.namer.ResourceName("copy", remotePath), &remote.CopyFileArgs{
Connection: r.config.connection,
LocalPath: pulumi.String(localPath),
RemotePath: pulumi.String(remotePath),
Triggers: pulumi.Array{pulumi.String(localPath), pulumi.String(remotePath)},
}, utils.MergeOptions(r.options, opts...)...)
}
type LocalRunner struct {
e config.Env
namer namer.Namer
config runnerConfiguration
osCommand OSCommand
}
type LocalRunnerArgs struct {
User string
OSCommand OSCommand
}
func NewLocalRunner(e config.Env, args LocalRunnerArgs) *LocalRunner {
localRunner := &LocalRunner{
e: e,
namer: namer.NewNamer(e.Ctx(), "local"),
osCommand: args.OSCommand,
config: runnerConfiguration{
user: args.User,
},
}
return localRunner
}
func (r *LocalRunner) Command(name string, args *Args, opts ...pulumi.ResourceOption) (*local.Command, error) {
opts = utils.MergeOptions[pulumi.ResourceOption](opts, r.e.WithProviders(config.ProviderCommand))
return local.NewCommand(r.e.Ctx(), r.namer.ResourceName("cmd", name), args.toLocalCommandArgs(r.config, r.osCommand), opts...)
}