forked from d2iq-archive/cake-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.23 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
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
log.Println("Running in " + currentDir)
dryRun := flag.Bool("dry-run", false, "Resolves templates and calculates checksums without build or pushing images")
releaseTag := flag.String("release-tag", "latest", "Additional tag to republish checksum based images with e.g. a release tag")
outputFile := flag.String("out", currentDir+"/cake-report.json", "A file to save build report to")
registryUrl := flag.String("registry", "https://index.docker.io", "Docker registry URL")
dockerUser := flag.String("username", "", "Username to authenticate with Docker registry")
dockerPassword := flag.String("password", "", "Password to authenticate with Docker registry")
flag.Parse()
var config BuildConfig
err = config.loadConfigFromFile(currentDir + "/cake.yaml")
if err != nil {
log.Fatal(err)
}
config.BaseDir = currentDir
config.ReleaseTag = *releaseTag
config.OutputFile = *outputFile
authConfig := AuthConfig{
DockerRegistryUrl: *registryUrl,
Username: *dockerUser,
Password: *dockerPassword,
}
config.AuthConfig = authConfig
log.Println(config.Images)
log.Println(fmt.Sprintf("[build] dry run: %t, release tag: %s, output file: %s", *dryRun, *releaseTag, *outputFile))
images, err := transformConfigToImages(config)
if err != nil {
log.Fatal(err)
}
buildGraph, err := createImageBuildGraph(images)
if err != nil {
log.Fatal(err)
}
walkBuildGraph(buildGraph, func(image *Image) {
err = image.renderDockerfileFromTemplate(config)
if err != nil {
log.Fatal(err)
}
err = image.calculateChecksum()
if err != nil {
log.Fatal(err)
}
})
dockerClient := NewExternalDockerClient(config.AuthConfig)
if !*dryRun {
walkBuildGraph(buildGraph, func(image *Image) {
exists, err := imageExists(dockerClient, image, config)
if err != nil {
log.Fatal(err)
}
if !exists {
err = buildImage(dockerClient, image, config)
if err != nil {
log.Fatal(err)
}
err = pushImage(dockerClient, image, config)
if err != nil {
log.Fatal(err)
}
}
})
err = generateReport(buildGraph, config)
if err != nil {
log.Fatal(err)
}
}
}