-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
200 lines (163 loc) · 5.08 KB
/
main.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
package main
import (
"bytes"
"crypto/sha1"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"light-stemcell-builder/collection"
"light-stemcell-builder/config"
"light-stemcell-builder/driverset"
"light-stemcell-builder/manifest"
"light-stemcell-builder/publisher"
"light-stemcell-builder/resources"
)
func usage(message string) {
fmt.Fprintln(os.Stderr, message) //nolint:errcheck
fmt.Fprintln(os.Stderr, "Usage of light-stemcell-builder/main.go") //nolint:errcheck
flag.PrintDefaults()
os.Exit(1)
}
func main() {
sharedWriter := &logWriter{
writer: os.Stderr,
}
logger := log.New(sharedWriter, "", log.LstdFlags)
configPath := flag.String("c", "", "Path to the JSON configuration file")
machineImagePath := flag.String("image", "", "Path to the input machine image (root.img)")
machineImageFormat := flag.String("format", resources.VolumeRawFormat, "Format of the input machine image (RAW or vmdk). Defaults to RAW.")
imageVolumeSize := flag.Int("volume-size", 0, "Block device size (in GB) of the input machine image")
manifestPath := flag.String("manifest", "", "Path to the input stemcell.MF")
flag.Parse()
if *configPath == "" {
usage("-c flag is required")
}
if *machineImagePath == "" {
usage("--image flag is required")
}
if *manifestPath == "" {
usage("--manifest flag is required")
}
if *imageVolumeSize == 0 && *machineImageFormat != resources.VolumeRawFormat {
usage("--volume-size flag is required for formats other than RAW")
}
configFile, err := os.Open(*configPath)
if err != nil {
logger.Fatalf("Error opening config file: %s", err)
}
defer func() {
closeErr := configFile.Close()
if closeErr != nil {
logger.Fatalf("Error closing config file: %s", closeErr)
}
}()
if err != nil {
logger.Fatalf("Error opening config file: %s", err)
}
c, err := config.NewFromReader(configFile)
if err != nil {
logger.Fatalf("Error parsing config file: %s. Message: %s", *configPath, err)
}
if _, err := os.Stat(*machineImagePath); os.IsNotExist(err) {
logger.Fatalf("machine image not found at: %s", *machineImagePath)
}
if _, err := os.Stat(*manifestPath); os.IsNotExist(err) {
logger.Fatalf("manifest not found at: %s", *manifestPath)
}
manifestBytes, err := os.ReadFile(*manifestPath)
if err != nil {
logger.Fatalf("opening manifest: %s", err)
}
m, err := manifest.NewFromReader(bytes.NewReader(manifestBytes))
if err != nil {
logger.Fatalf("reading manifest: %s", err)
}
if c.AmiConfiguration.Tags == nil {
c.AmiConfiguration.Tags = map[string]string{
"version": m.Version,
"distro": m.OperatingSystem,
}
}
if c.AmiConfiguration.KmsKeyId != "" && c.AmiConfiguration.KmsKeyAliasName == "" {
aliasName := "light-stemcell-builder"
logger.Printf("Kms key alias not set - using default value: %s", aliasName)
c.AmiConfiguration.KmsKeyAliasName = aliasName
}
if c.AmiConfiguration.KmsKeyAliasName != "" {
if !strings.HasPrefix(c.AmiConfiguration.KmsKeyAliasName, "alias/") {
c.AmiConfiguration.KmsKeyAliasName = "alias/" + c.AmiConfiguration.KmsKeyAliasName
}
}
amiCollection := collection.Ami{}
errCollection := collection.Error{}
var wg sync.WaitGroup
wg.Add(len(c.AmiRegions))
imageConfig := publisher.MachineImageConfig{
LocalPath: *machineImagePath,
FileFormat: *machineImageFormat,
VolumeSizeGB: int64(*imageVolumeSize),
}
for i := range c.AmiRegions {
go func(regionConfig config.AmiRegion) {
defer wg.Done()
switch {
case regionConfig.IsolatedRegion:
ds := driverset.NewIsolatedRegionDriverSet(sharedWriter, regionConfig.Credentials)
p := publisher.NewIsolatedRegionPublisher(sharedWriter, publisher.Config{
AmiRegion: regionConfig,
AmiConfiguration: c.AmiConfiguration,
})
amis, err := p.Publish(ds, imageConfig)
if err != nil {
errCollection.Add(fmt.Errorf("publishing AMIs to %s: %s", regionConfig.RegionName, err))
} else {
amiCollection.Merge(amis)
}
default:
ds := driverset.NewStandardRegionDriverSet(sharedWriter, regionConfig.Credentials)
p := publisher.NewStandardRegionPublisher(sharedWriter, publisher.Config{
AmiRegion: regionConfig,
AmiConfiguration: c.AmiConfiguration,
})
amis, err := p.Publish(ds, imageConfig)
if err != nil {
errCollection.Add(fmt.Errorf("publishing AMIs to %s: %s", regionConfig.RegionName, err))
} else {
amiCollection.Merge(amis)
}
}
}(c.AmiRegions[i])
}
logger.Println("Waiting for publishers to finish...")
wg.Wait()
combinedErr := errCollection.Error()
if combinedErr != nil {
logger.Fatal(combinedErr)
}
m.PublishedAmis = amiCollection.GetAll()
m.Sha1 = shasum([]byte{})
err = m.Write(os.Stdout)
if err != nil {
logger.Fatalf("writing manifest: %s", err)
}
logger.Println("Publishing finished successfully")
}
func shasum(content []byte) string {
h := sha1.New()
h.Write(content)
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs)
}
type logWriter struct {
sync.Mutex
writer io.Writer
}
func (l *logWriter) Write(message []byte) (int, error) {
l.Lock()
defer l.Unlock()
return l.writer.Write(message)
}