/
windows.go
336 lines (320 loc) · 10 KB
/
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package windows
import (
_ "embed"
"encoding/base64"
"fmt"
"os"
"github.com/adrianriobo/qenvs/pkg/manager"
qenvsContext "github.com/adrianriobo/qenvs/pkg/manager/context"
infra "github.com/adrianriobo/qenvs/pkg/provider"
"github.com/adrianriobo/qenvs/pkg/provider/aws"
"github.com/adrianriobo/qenvs/pkg/provider/aws/data"
amiCopy "github.com/adrianriobo/qenvs/pkg/provider/aws/modules/ami"
"github.com/adrianriobo/qenvs/pkg/provider/aws/modules/bastion"
"github.com/adrianriobo/qenvs/pkg/provider/aws/modules/ec2/compute"
"github.com/adrianriobo/qenvs/pkg/provider/aws/modules/network"
"github.com/adrianriobo/qenvs/pkg/provider/aws/modules/spot"
amiSVC "github.com/adrianriobo/qenvs/pkg/provider/aws/services/ec2/ami"
"github.com/adrianriobo/qenvs/pkg/provider/aws/services/ec2/keypair"
securityGroup "github.com/adrianriobo/qenvs/pkg/provider/aws/services/ec2/security-group"
"github.com/adrianriobo/qenvs/pkg/provider/util/command"
"github.com/adrianriobo/qenvs/pkg/provider/util/output"
"github.com/adrianriobo/qenvs/pkg/provider/util/security"
"github.com/adrianriobo/qenvs/pkg/util"
"github.com/adrianriobo/qenvs/pkg/util/file"
resourcesUtil "github.com/adrianriobo/qenvs/pkg/util/resources"
"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
"github.com/pulumi/pulumi-random/sdk/v4/go/random"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
// add proxy https://github.com/ptcodes/proxy-server-with-terraform/blob/master/main.tf
type Request struct {
Prefix string
// AMI info. Optional. User and Owner only applied
// if AMIName is set
AMIName string
AMIUser string
AMIOwner string
AMILang string
AMIArch string
Spot bool
Airgap bool
// internal management
// For airgap scenario there is an orchestation of
// a phase with connectivity on the machine (allowing bootstraping)
// a pahase with connectivyt off where the subnet for the target lost the nat gateway
airgapPhaseConnectivity network.Connectivity
// location and price (if Spot is enable)
region string
az string
spotPrice float64
}
type userDataValues struct {
Username string
Password string
AuthorizedKey string
}
//go:embed bootstrap.ps1
var BootstrapScript []byte
// Create orchestrate 3 stacks:
// If spot is enable it will run best spot option to get the best option to spin the machine
// Then it will run the stack for windows dedicated host
func Create(r *Request) error {
if len(r.AMIName) == 0 {
r.AMIName = amiNameDefault
r.AMIUser = amiUserDefault
r.AMIOwner = amiOwnerDefault
r.AMIArch = amiArchDefault
}
if len(r.AMILang) > 0 && r.AMILang == amiLangNonEng {
r.AMIName = amiNonEngNameDefault
}
if r.Spot {
// On windows we use a custom AMI as so we
// do not add it as a requirement for best spot option, we will get the region
// and then wil replicate AMI if needed
sr := spot.SpotOptionRequest{
Prefix: r.Prefix,
ProductDescription: "Windows",
InstaceTypes: requiredInstanceTypes,
}
so, err := sr.Create()
if err != nil {
return err
}
r.region = so.Region
r.az = so.AvailabilityZone
r.spotPrice = so.MaxPrice
} else {
r.region = os.Getenv("AWS_DEFAULT_REGION")
az, err := data.GetRandomAvailabilityZone(r.region)
if err != nil {
return err
}
r.az = *az
}
isAMIOffered, _, err := amiSVC.IsAMIOffered(r.AMIName, r.AMIArch, r.region)
if err != nil {
return err
}
// If it is not offered need to create a copy on the target region
if !isAMIOffered {
acr := amiCopy.CopyAMIRequest{
Prefix: r.Prefix,
ID: awsWindowsDedicatedID,
AMISourceName: r.AMIName,
AMITargetRegion: &r.region,
}
if err := acr.Create(); err != nil {
return err
}
}
// if not only host the mac machine will be created
if !r.Airgap {
return r.createMachine()
}
// Airgap scneario requires orchestration
return r.createAirgapMachine()
}
// Will destroy resources related to machine
func Destroy() (err error) {
err = aws.DestroyStack(stackName)
if err != nil {
return
}
if amiCopy.Exist() {
err = amiCopy.Destroy()
if err != nil {
return
}
}
if spot.Exist() {
return spot.Destroy()
}
return nil
}
func (r *Request) createMachine() error {
cs := manager.Stack{
StackName: qenvsContext.GetStackInstanceName(stackName),
ProjectName: qenvsContext.GetInstanceName(),
BackedURL: qenvsContext.GetBackedURL(),
ProviderCredentials: aws.GetClouProviderCredentials(
map[string]string{
aws.CONFIG_AWS_REGION: r.region}),
DeployFunc: r.deploy,
}
csResult, err := manager.UpStack(cs)
if err != nil {
return err
}
err = r.manageResults(csResult)
if err != nil {
return err
}
return nil
}
// Abstract this with a stackAirgapHandle receives a fn (connectivty on / off) err executes
// first on then off
func (r *Request) createAirgapMachine() error {
r.airgapPhaseConnectivity = network.ON
err := r.createMachine()
if err != nil {
return nil
}
r.airgapPhaseConnectivity = network.OFF
return r.createMachine()
}
// function wil all the logic to deploy resources required by windows
// * create AMI Copy if needed
// * networking
// * key
// * security group
// * compute
// * checks
func (r *Request) deploy(ctx *pulumi.Context) error {
// Get AMI ref
// ami, err := amiSVC.GetAMIByName(ctx, r.AMIName, r.AMIOwner, nil)
ami, err := amiSVC.GetAMIByName(ctx,
fmt.Sprintf("%s*", r.AMIName),
r.AMIOwner, nil)
if err != nil {
return err
}
// Networking
nr := network.NetworkRequest{
Prefix: r.Prefix,
ID: awsWindowsDedicatedID,
Region: r.region,
AZ: r.az,
// LB is required if we use as which is used for spot feature
CreateLoadBalancer: &r.Spot,
Airgap: r.Airgap,
AirgapPhaseConnectivity: r.airgapPhaseConnectivity,
}
// vpc, targetSubnet, targetRouteTableAssociation, bastion, lb, err := nr.Network(ctx)
vpc, targetSubnet, _, bastion, lb, err := nr.Network(ctx)
if err != nil {
return err
}
// Create Keypair
kpr := keypair.KeyPairRequest{
Name: resourcesUtil.GetResourceName(
r.Prefix, awsWindowsDedicatedID, "pk")}
keyResources, err := kpr.Create(ctx)
if err != nil {
return err
}
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputUserPrivateKey),
keyResources.PrivateKey.PrivateKeyPem)
// Security groups
securityGroups, err := r.securityGroups(ctx, vpc)
if err != nil {
return err
}
// Compute
password, userDataB64, err := r.getUserdata(ctx, keyResources)
if err != nil {
return err
}
cr := compute.ComputeRequest{
Prefix: r.Prefix,
ID: awsWindowsDedicatedID,
VPC: vpc,
Subnet: targetSubnet,
AMI: ami,
UserDataAsBase64: userDataB64,
KeyResources: keyResources,
SecurityGroups: securityGroups,
InstaceTypes: requiredInstanceTypes,
DiskSize: &diskSize,
Airgap: r.Airgap,
LB: lb,
LBTargetGroups: []int{22, 3389},
Spot: r.Spot}
c, err := cr.NewCompute(ctx)
if err != nil {
return err
}
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputUsername),
pulumi.String(r.AMIUser))
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputUserPassword),
password.Result)
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputHost),
c.GetHostIP(!r.Airgap))
return c.Readiness(ctx, command.CommandPing, r.Prefix, awsWindowsDedicatedID,
keyResources.PrivateKey, r.AMIUser, bastion, []pulumi.Resource{})
}
// Write exported values in context to files o a selected target folder
func (r *Request) manageResults(stackResult auto.UpResult) error {
results := map[string]string{
fmt.Sprintf("%s-%s", r.Prefix, outputUsername): "username",
fmt.Sprintf("%s-%s", r.Prefix, outputUserPassword): "userpassword",
fmt.Sprintf("%s-%s", r.Prefix, outputUserPrivateKey): "id_rsa",
fmt.Sprintf("%s-%s", r.Prefix, outputHost): "host",
}
if r.Airgap {
err := bastion.WriteOutputs(stackResult, r.Prefix, qenvsContext.GetResultsOutputPath())
if err != nil {
return err
}
}
return output.Write(stackResult, qenvsContext.GetResultsOutputPath(), results)
}
// security group for mac machine with ingress rules for ssh and vnc
func (r *Request) securityGroups(ctx *pulumi.Context,
vpc *ec2.Vpc) (pulumi.StringArray, error) {
// ingress for ssh access from 0.0.0.0
sshIngressRule := securityGroup.SSH_TCP
sshIngressRule.CidrBlocks = infra.NETWORKING_CIDR_ANY_IPV4
rdpIngressRule := securityGroup.RDP_TCP
rdpIngressRule.CidrBlocks = infra.NETWORKING_CIDR_ANY_IPV4
// Create SG with ingress rules
sg, err := securityGroup.SGRequest{
Name: resourcesUtil.GetResourceName(r.Prefix, awsWindowsDedicatedID, "sg"),
VPC: vpc,
Description: fmt.Sprintf("sg for %s", awsWindowsDedicatedID),
IngressRules: []securityGroup.IngressRules{
sshIngressRule, rdpIngressRule},
}.Create(ctx)
if err != nil {
return nil, err
}
// Convert to an array of IDs
sgs := util.ArrayConvert([]*ec2.SecurityGroup{sg.SG},
func(sg *ec2.SecurityGroup) pulumi.StringInput {
return sg.ID()
})
return pulumi.StringArray(sgs[:]), nil
}
// function to template userdata script to be executed on boot
func (r *Request) getUserdata(ctx *pulumi.Context,
keypair *keypair.KeyPairResources) (
*random.RandomPassword, pulumi.StringPtrInput, error) {
password, err := security.CreatePassword(ctx,
resourcesUtil.GetResourceName(
r.Prefix, awsWindowsDedicatedID, "password"))
if err != nil {
return nil, nil, err
}
udBase64 := pulumi.All(password.Result, keypair.PrivateKey.PublicKeyOpenssh).ApplyT(
func(args []interface{}) (string, error) {
password := args[0].(string)
authorizedKey := args[1].(string)
userdata, err := file.Template(
userDataValues{
r.AMIUser,
password,
authorizedKey},
resourcesUtil.GetResourceName(
r.Prefix, awsWindowsDedicatedID, "userdatas"),
string(BootstrapScript[:]))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString([]byte(userdata)), nil
}).(pulumi.StringOutput)
return password, udBase64, nil
}
// Need to add custom listener for RDP or should we use 22 tunneling through the bastion?
// func addCustomListeners(){}