-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathami.go
164 lines (149 loc) · 3.3 KB
/
ami.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
package ami
import (
"context"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
log "github.com/cantara/bragi/sbragi"
)
const DateFormat = "2006-01-02T15:04:05Z" //YYYY-MM-DDTHH:MM:SSZ
type Arch int
const (
AMD64 Arch = iota + 1
ARM64
)
func (a Arch) String() string {
switch a {
case AMD64:
return "x86_64"
case ARM64:
return "arm64"
}
return "INVALID ARCH"
}
func StringToArch(s string) (Arch, error) {
switch strings.ToUpper(s) {
case "X86-64":
fallthrough
case "X86_64":
fallthrough
case "AMD64":
return AMD64, nil
case "ARM64":
return ARM64, nil
}
return 0, fmt.Errorf("%s is not a valid arch", s)
}
type Image struct {
Id string
Name string
HName string
Arch Arch
RootDev string
created time.Time
depreated time.Time
got time.Time
}
func nameToAmiName(s string) string {
switch strings.ToLower(s) {
case "amazon linux 2023 minimal":
fallthrough
case "amazon linux 2023 min":
return "al2023-ami-minimal-2023.*"
case "amazon linux 2023":
return "al2023-ami-2023.*"
case "amazon linux 2":
return "amzn2-ami-kernel-*-hvm-2.*"
}
return fmt.Sprintf("%s-*", strings.ReplaceAll(strings.ToLower(s), " ", "-"))
}
func (img Image) Username() string {
n := strings.ToLower(img.HName)
if strings.HasPrefix(n, "amazon") {
return "ec2-user"
}
if strings.HasPrefix(n, "ubuntu") {
return "ubuntu"
}
return ""
}
var imageCache []Image //Needs syncronization
func GetImage(name string, arch Arch, e2 *ec2.Client) (newest Image, err error) {
log.Trace("getting image", "name", name, "arch", arch, "cache", imageCache)
for i, img := range imageCache {
if img.Arch != arch {
continue
}
if img.HName != name {
continue
}
if img.got.Add(time.Hour * 24).After(time.Now()) {
return img, nil
}
imageCache = append(imageCache[:i], imageCache[i:]...)
}
result, err := e2.DescribeImages(context.Background(), &ec2.DescribeImagesInput{
Owners: []string{
"amazon",
},
Filters: []ec2types.Filter{
{
Name: aws.String("architecture"),
Values: []string{
arch.String(),
},
},
{
Name: aws.String("name"),
Values: []string{
nameToAmiName(name),
},
},
},
})
if err != nil {
return
}
if len(result.Images) < 1 {
err = fmt.Errorf("No image with name %s", name)
return
}
/* if len(result.Reservations) > 1 {
err = fmt.Errorf("Too many servers with name %s", name)
} */
images := make([]Image, len(result.Images))
for i, img := range result.Images {
if !strings.HasPrefix(*img.Name, "a") {
continue
}
log.Trace("Looking for image", "name", name, "found", *img.Name)
arch, _ := StringToArch(string(img.Architecture))
created, _ := time.Parse(*img.CreationDate, DateFormat)
depreated, _ := time.Parse(*img.DeprecationTime, DateFormat)
images[i] = Image{
Id: *img.ImageId,
Name: *img.Name,
Arch: arch,
RootDev: aws.ToString(img.RootDeviceName),
//Username:
created: created,
depreated: depreated,
}
}
for _, img := range images {
if img.Arch != arch {
continue
}
if newest.created.After(img.created) {
continue
}
newest = img
}
newest.got = time.Now()
newest.HName = name
imageCache = append(imageCache, newest)
return
}