Skip to content

Commit dc00494

Browse files
committed
added windows supported hosts with userdata based on templating with pulumi outputs
1 parent 25c536a commit dc00494

File tree

12 files changed

+228
-151
lines changed

12 files changed

+228
-151
lines changed
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
package compute
22

33
import (
4-
"fmt"
5-
6-
"github.com/adrianriobo/qenvs/pkg/infra/util/command"
74
"github.com/adrianriobo/qenvs/pkg/util"
85
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ec2"
9-
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
106
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
117
)
128

13-
func (r *Compute) OutputPrivateKey() string {
14-
return fmt.Sprintf("%s-%s", OutputPrivateKey, r.Specs.ID)
15-
}
16-
17-
func (r *Compute) OutputHost() string {
18-
return fmt.Sprintf("%s-%s", OutputHost, r.Specs.ID)
19-
}
20-
21-
func (r *Compute) OutputUsername() string {
22-
return fmt.Sprintf("%s-%s", OutputUsername, r.Specs.ID)
23-
}
24-
259
func (c *Compute) getSecurityGroupsIDs() pulumi.StringArrayInput {
2610
sgs := util.ArrayConvert(c.SG,
2711
func(sg *ec2.SecurityGroup) pulumi.StringInput {
@@ -30,16 +14,16 @@ func (c *Compute) getSecurityGroupsIDs() pulumi.StringArrayInput {
3014
return pulumi.StringArray(sgs[:])
3115
}
3216

33-
func (c *Compute) remoteExec(ctx *pulumi.Context, cmdName, cmd string,
34-
dependecies []pulumi.Resource) (*remote.Command, error) {
35-
instance := command.RemoteInstance{
36-
Instance: c.Instance,
37-
InstanceIP: &c.InstanceIP,
38-
Username: c.Username,
39-
PrivateKey: c.PrivateKey}
40-
return instance.RemoteExec(
41-
ctx,
42-
cmd,
43-
cmdName,
44-
dependecies)
45-
}
17+
// func (c *Compute) remoteExec(ctx *pulumi.Context, cmdName, cmd string,
18+
// dependecies []pulumi.Resource) (*remote.Command, error) {
19+
// instance := command.RemoteInstance{
20+
// Instance: c.Instance,
21+
// InstanceIP: &c.InstanceIP,
22+
// Username: c.Username,
23+
// PrivateKey: c.PrivateKey}
24+
// return instance.RemoteExec(
25+
// ctx,
26+
// cmd,
27+
// cmdName,
28+
// dependecies)
29+
// }
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package compute
22

33
const (
4-
OutputHost string = "Host"
5-
OutputUsername string = "Username"
6-
OutputPrivateKey string = "PrivateKey"
4+
OutputHost string = "Host"
5+
OutputUsername string = "Username"
6+
OutputPrivateKey string = "PrivateKey"
7+
OutputPasswordKey string = "Password"
8+
9+
DefaultRootBlockDeviceName string = "/dev/sda1"
10+
DefaultRootBlockDeviceSize int = 100
711
)

pkg/infra/aws/modules/compute/host/macm1/macm1.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *MacM1Request) GetAMI(ctx *pulumi.Context) (*ec2.LookupAmiResult, error)
2525
return ami.GetAMIByName(ctx, r.Specs.AMI.RegexName, r.Specs.AMI.Owner, r.Specs.AMI.Filters)
2626
}
2727

28-
func (r *MacM1Request) GetUserdata() (pulumi.StringPtrInput, error) {
28+
func (r *MacM1Request) GetUserdata(ctx *pulumi.Context) (pulumi.StringPtrInput, error) {
2929
return nil, nil
3030
}
3131

@@ -55,9 +55,9 @@ func (r *MacM1Request) CustomSecurityGroups(ctx *pulumi.Context) ([]*ec2.Securit
5555
return nil, nil
5656
}
5757

58-
func (r *MacM1Request) GetPostScript() (string, error) {
58+
func (r *MacM1Request) GetPostScript(ctx *pulumi.Context) (string, error) {
5959
return util.Template(
60-
UserDataValues{
60+
scriptDataValues{
6161
r.Specs.AMI.DefaultUser,
6262
r.Password},
6363
"postscript", script)
@@ -67,32 +67,3 @@ func (r *MacM1Request) Create(ctx *pulumi.Context,
6767
computeRequested compute.ComputeRequest) (*compute.Compute, error) {
6868
return r.Request.Create(ctx, r)
6969
}
70-
71-
var script string = `
72-
#!/bin/sh
73-
74-
# Enable remote control (vnc)
75-
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
76-
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
77-
78-
# Set user password
79-
sudo dscl . -passwd /Users/{{.Username}} {{.Password}}
80-
81-
# Autologin
82-
sudo curl -o /tmp/kcpassword https://raw.githubusercontent.com/xfreebird/kcpassword/master/kcpassword
83-
sudo chmod +x /tmp/kcpassword
84-
sudo /tmp/kcpassword {{.Password}}
85-
sudo defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser "{{.Username}}"
86-
87-
sudo defaults write /Library/Preferences/.GlobalPreferences.plist com.apple.securitypref.logoutvalue -int 1200
88-
sudo defaults write /Library/Preferences/.GlobalPreferences.plist com.apple.autologout.AutoLogOutDelay -int 1200
89-
90-
# autologin to take effect
91-
# run reboot on background to successfully finish the remote exec of the script
92-
(sleep 2 && sudo reboot)&
93-
`
94-
95-
type UserDataValues struct {
96-
Username string
97-
Password string
98-
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package macm1
2+
3+
var script string = `
4+
#!/bin/sh
5+
6+
# Allow run x86 binaries on arm64
7+
sudo softwareupdate --install-rosetta --agree-to-license
8+
9+
# Enable remote control (vnc)
10+
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
11+
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
12+
13+
# Set user password
14+
sudo dscl . -passwd /Users/{{.Username}} {{.Password}}
15+
16+
# Autologin
17+
sudo curl -o /tmp/kcpassword https://raw.githubusercontent.com/xfreebird/kcpassword/master/kcpassword
18+
sudo chmod +x /tmp/kcpassword
19+
sudo /tmp/kcpassword {{.Password}}
20+
sudo defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser "{{.Username}}"
21+
22+
sudo defaults write /Library/Preferences/.GlobalPreferences.plist com.apple.securitypref.logoutvalue -int 0
23+
sudo defaults write /Library/Preferences/.GlobalPreferences.plist com.apple.autologout.AutoLogOutDelay -int 0
24+
25+
# autologin to take effect
26+
# run reboot on background to successfully finish the remote exec of the script
27+
(sleep 2 && sudo reboot)&
28+
`
29+
30+
type scriptDataValues struct {
31+
Username string
32+
Password string
33+
}

pkg/infra/aws/modules/compute/host/rhel/rhel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (r *RHELRequest) GetAMI(ctx *pulumi.Context) (*ec2.LookupAmiResult, error)
2222
return ami.GetAMIByName(ctx, amiNameRegex, "", r.Specs.AMI.Filters)
2323
}
2424

25-
func (r *RHELRequest) GetUserdata() (pulumi.StringPtrInput, error) {
25+
func (r *RHELRequest) GetUserdata(ctx *pulumi.Context) (pulumi.StringPtrInput, error) {
2626
userdata, err := util.Template(
2727
UserDataValues{
2828
r.SubscriptionUsername,
@@ -43,7 +43,7 @@ func (r *RHELRequest) CustomSecurityGroups(ctx *pulumi.Context) ([]*ec2.Security
4343
return nil, nil
4444
}
4545

46-
func (r *RHELRequest) GetPostScript() (string, error) {
46+
func (r *RHELRequest) GetPostScript(ctx *pulumi.Context) (string, error) {
4747
return "", nil
4848
}
4949

pkg/infra/aws/modules/compute/host/windows/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package rhel
1+
package windows
22

33
import (
44
"github.com/adrianriobo/qenvs/pkg/infra/aws/modules/compute"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package windows
2+
3+
// type userDataValues struct {
4+
// Username string
5+
// Password string
6+
// AuthorizedKey string
7+
// }
8+
9+
// var userdata string = `
10+
// <powershell>
11+
// # Create local user
12+
// $Password = ConvertTo-SecureString "{{.Password}}" -AsPlainText -Force
13+
// New-LocalUser {{.Username}} -Password $Password
14+
// # Run a process with new local user to create profile, so it will create home folder
15+
// $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList {{.Username}}, $Password
16+
// Start-Process cmd /c -WindowStyle Hidden -Wait -Credential $credential -ErrorAction SilentlyContinue
17+
// # Add user to required groups
18+
// Add-LocalGroupMember -Group "Administrators" -Member {{.Username}}
19+
// # Check if this speed insall of crc...if msi installer checks if no reboot required
20+
// # Non eng Add-LocalGroupMember -Group "Hyper-V Administrators" -Member {{.Username}}
21+
// Add-LocalGroupMember -Member {{.Username}} -SID S-1-5-32-578
22+
// # Set autologon to user to allow start sshd for the user
23+
// # Check requirements for domain user
24+
// # https://docs.microsoft.com/en-us/troubleshoot/windows-server/user-profiles-and-logon/turn-on-automatic-logon
25+
// $RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
26+
// Set-ItemProperty $RegistryPath 'AutoAdminLogon' -Value "1" -Type String
27+
// Set-ItemProperty $RegistryPath 'DefaultUsername' -Value "{{.Username}}" -type String
28+
// Set-ItemProperty $RegistryPath 'DefaultPassword' -Value "{{.Password}}" -type String
29+
// # Install sshd
30+
// Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
31+
// Set-Service -Name sshd -StartupType 'Manual'
32+
// # This generate ssh certs + config file for us
33+
// Start-Service sshd
34+
// # Disable the service as need to start it as a user process on startup
35+
// Stop-Service sshd
36+
// # Add pub key for the user as authorized_key
37+
// New-Item -Path "C:\Users\{{.Username}}\.ssh" -ItemType Directory -Force
38+
// New-Item -Path C:\Users\{{.Username}}\.ssh -Name "authorized_keys" -ItemType "file" -Value "{{.AuthorizedKey}}"
39+
// # Set permissions valid permissions for hyper_user on authorized_keys + host_keys
40+
// $acl = Get-Acl C:\Users\{{.Username}}\.ssh\authorized_keys
41+
// $acl.SetOwner([System.Security.Principal.NTAccount] "{{.Username}}")
42+
// $acl.SetAccessRuleProtection($True, $False)
43+
// $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule([System.Security.Principal.NTAccount] "{{.Username}}","FullControl","Allow")
44+
// $acl.SetAccessRule($AccessRule)
45+
// Set-Acl C:\Users\{{.Username}}\.ssh\authorized_keys $acl
46+
// Set-Acl -Path "C:\ProgramData\ssh\*key" $acl
47+
// # Create bat script to start sshd as a user process on startup
48+
// New-Item -Path "C:\Users\{{.Username}}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" -Name start-openssh.bat -ItemType "file" -Value 'powershell -command "sshd -f C:\ProgramData\ssh\sshd_config"'
49+
// Restart-Computer
50+
// </powershell>
51+
// `
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package rhel
1+
package windows
22

33
import (
44
"github.com/adrianriobo/qenvs/pkg/infra/aws/modules/compute"
@@ -17,55 +17,50 @@ func (r *WindowsRequest) GetAMI(ctx *pulumi.Context) (*ec2.LookupAmiResult, erro
1717
return ami.GetAMIByName(ctx, r.Specs.AMI.RegexName, r.Specs.AMI.Owner, r.Specs.AMI.Filters)
1818
}
1919

20-
func (r *WindowsRequest) GetUserdata() (pulumi.StringPtrInput, error) {
21-
22-
// https://charlesxu.io/wiki/infra-as-code/pulumi/
23-
// https://www.pulumi.com/registry/packages/random/api-docs/randompassword/?utm_source=performance-max&utm_medium=cpc&utm_campaign=&utm_term=&utm_medium=ppc&utm_source=adwords&hsa_grp=&hsa_cam=18353585506&hsa_mt=&hsa_net=adwords&hsa_ver=3&hsa_acc=1926559913&hsa_ad=&hsa_src=x&hsa_tgt=&hsa_kw=&gclid=EAIaIQobChMIwP3C2sqK-wIVPY1oCR0EOgJoEAAYASAAEgJM6vD_BwE
24-
// t := pulumi.All(r.KeyPair.Arn).ApplyT(
25-
// func(args []interface{}) string {
26-
// return args[0].(string)
27-
// }).(pulumi.StringOutput)
28-
29-
// return t, nil
30-
31-
// st := pulumi.String("lalal")
32-
33-
// return st, nil
20+
func (r *WindowsRequest) GetUserdata(ctx *pulumi.Context) (pulumi.StringPtrInput, error) {
3421
return nil, nil
3522
}
3623

24+
// func (r *WindowsRequest) GetUserdata(ctx *pulumi.Context) (pulumi.StringPtrInput, error) {
25+
// password, err := utilInfra.CreatePassword(ctx, r.GetName())
26+
// if err != nil {
27+
// return nil, err
28+
// }
29+
// ctx.Export(r.OutputPrivateKey(), password.Result)
30+
// udBase64 := pulumi.All(password.Result, r.PublicKeyOpenssh).ApplyT(
31+
// func(args []interface{}) string {
32+
// password := args[0].(string)
33+
// authorizedKey := args[1].(string)
34+
// userdata, _ := util.Template(
35+
// userDataValues{
36+
// r.Specs.AMI.DefaultUser,
37+
// password,
38+
// authorizedKey},
39+
// fmt.Sprintf("%s-%s", "userdata", r.GetName()),
40+
// userdata)
41+
// return base64.StdEncoding.EncodeToString([]byte(userdata))
42+
// }).(pulumi.StringOutput)
43+
// return udBase64, nil
44+
// }
45+
3746
func (r *WindowsRequest) GetDedicatedHost(ctx *pulumi.Context) (*ec2.DedicatedHost, error) {
3847
return nil, nil
3948
}
4049

4150
func (r *WindowsRequest) CustomIngressRules() []securityGroup.IngressRules {
42-
return nil
51+
return []securityGroup.IngressRules{
52+
securityGroup.RDP_TCP}
4353
}
4454

4555
func (r *WindowsRequest) CustomSecurityGroups(ctx *pulumi.Context) ([]*ec2.SecurityGroup, error) {
4656
return nil, nil
4757
}
4858

49-
func (r *WindowsRequest) GetPostScript() (string, error) {
59+
func (r *WindowsRequest) GetPostScript(ctx *pulumi.Context) (string, error) {
5060
return "", nil
5161
}
5262

5363
func (r *WindowsRequest) Create(ctx *pulumi.Context,
5464
computeRequested compute.ComputeRequest) (*compute.Compute, error) {
5565
return r.Request.Create(ctx, r)
5666
}
57-
58-
// var cloudConfig string = `
59-
// #cloud-config
60-
// rh_subscription:
61-
// username: {{.SubscriptionUsername}}
62-
// password: {{.SubscriptionPassword}}
63-
// auto-attach: true
64-
// packages:
65-
// - podman
66-
// `
67-
68-
// type UserDataValues struct {
69-
// SubscriptionUsername string
70-
// SubscriptionPassword string
71-
// }

0 commit comments

Comments
 (0)