-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchtemplate.go
151 lines (126 loc) · 3.32 KB
/
launchtemplate.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
package launchtemplate
import (
"fmt"
"log"
"math/rand"
"os"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/psanford/aws-buddy/config"
"github.com/spf13/cobra"
)
func Command() *cobra.Command {
cmd := cobra.Command{
Use: "launch_template <name>",
Short: "Launch template command",
Run: launchTemplateAction,
}
return &cmd
}
func launchTemplateAction(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatalf("usage: launch_template <name>")
}
name := args[0]
fname := fmt.Sprintf("%s.yml", name)
f, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
log.Fatalf("create file %s err: %s", fname, err)
}
defer f.Close()
svc := ec2.New(config.Session())
var securityGroups []string
var defaultSG string
err = svc.DescribeSecurityGroupsPages(&ec2.DescribeSecurityGroupsInput{}, func(dsgo *ec2.DescribeSecurityGroupsOutput, b bool) bool {
for _, sg := range dsgo.SecurityGroups {
var name string
if sg.GroupName != nil {
name = *sg.GroupName
}
securityGroups = append(securityGroups, fmt.Sprintf("%s (%s)", *sg.GroupId, name))
if name == "allow-ssh" {
defaultSG = *sg.GroupId
}
}
return true
})
if err != nil {
log.Fatalf("DescribeSecurityGroups err: %s", err)
}
var subnets []string
err = svc.DescribeSubnetsPages(&ec2.DescribeSubnetsInput{}, func(dso *ec2.DescribeSubnetsOutput, b bool) bool {
for _, s := range dso.Subnets {
var name string
for _, t := range s.Tags {
if *t.Key == "Name" {
name = *t.Value
}
}
subnets = append(subnets, fmt.Sprintf("%s %s %s %s", *s.SubnetId, *s.AvailabilityZone, *s.CidrBlock, name))
}
return true
})
if err != nil {
log.Fatalf("DescribeSubnets err: %s", err)
}
var keyPairs []string
kps, err := svc.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{})
if err != nil {
log.Fatalf("DescribeKeyPairs err: %s", err)
}
for _, kp := range kps.KeyPairs {
keyPairs = append(keyPairs, *kp.KeyName)
}
cfg := tmplConfig{
Name: name,
SecurityGroups: securityGroups,
Subnets: subnets,
KeyPairs: keyPairs,
DefaultSubnet: strings.Fields(subnets[rand.Intn(len(subnets))])[0],
DefaultSecurityGroup: defaultSG,
}
if len(keyPairs) > 0 {
cfg.DefaultKeyPair = keyPairs[0]
}
err = tmpl.Execute(f, cfg)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(os.Stderr, "wrote %s\n", fname)
}
type tmplConfig struct {
Name string
SecurityGroups []string
DefaultSecurityGroup string
Subnets []string
DefaultSubnet string
KeyPairs []string
DefaultKeyPair string
}
var tmpl = template.Must(template.New("tmpl").Parse(tmplText))
var tmplText = `name: {{.Name}}
instance_type: t4g.small
{{range .SecurityGroups}}
# {{.}}
{{- end}}
security_group: {{.DefaultSecurityGroup}}
{{range .Subnets}}
# {{.}}
{{- end}}
subnet: {{.DefaultSubnet}}
{{range .KeyPairs}}
# {{.}}
{{- end}}
key_pair: {{.DefaultKeyPair}}
ubuntu_release: 22.04
# user_data script content
# see CloudInit docs for details
# user_data: |
# #!/bin/bash
# set -x
# mkdir -p /home/ubuntu/.ssh/
# curl https://api.sanford.io/sshkeys/ > /home/ubuntu/.ssh/authorized_keys
# chmod 400 /home/ubuntu/.ssh/authorized_keys
# chown ubuntu:ubuntu /home/ubuntu/.ssh/authorized_keys
`