-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (132 loc) · 3.63 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
package main
import (
"embed"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime"
)
const usage = `Usage of convoy-build:
-c, --consul-location absolute filepath of consul source code on your system, takes precedence over the CONVOY_CONSUL_LOCATION env var
-e, --envoy-version version of envoy to use, defaults to 1.29
-h, --help prints help information
`
//go:embed embeddable
var f embed.FS
func main() {
var consulLocation string
var envoyVersion string
err := parseArgs(&consulLocation, &envoyVersion)
if err != nil {
log.Fatal(err)
}
curDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
defer os.Chdir(curDir)
consulBytes, err := buildConsul(consulLocation, runtime.GOARCH, curDir)
if err != nil {
log.Fatal(err)
}
dir, err := buildTempDir(consulBytes)
if err != nil {
log.Fatal(err)
}
err = buildDockerImage(dir, envoyVersion)
if err != nil {
log.Fatal(err)
}
log.Print("successfully built convoy image, it is available as \"convoy:local\"")
}
func parseArgs(consulLocation, envoyVersion *string) error {
flag.StringVar(consulLocation, "consul-location", "", "absolute filepath of consul source code on your system, takes precedence over the CONVOY_CONSUL_LOCATION env var")
flag.StringVar(consulLocation, "c", "", "absolute filepath of consul source code on your system, takes precedence over the CONVOY_CONSUL_LOCATION env var")
flag.StringVar(envoyVersion, "envoy-version", "", "envoy version to use, defaults to 1.29")
flag.StringVar(envoyVersion, "e", "", "envoy version to use, defaults to 1.29")
flag.Usage = func() { fmt.Print(usage) }
flag.Parse()
if *consulLocation == "" {
*consulLocation = os.Getenv("CONVOY_CONSUL_LOCATION")
}
if *consulLocation == "" {
return errors.New("consul location must be supplied")
}
return nil
}
func buildTempDir(consulBytes []byte) (string, error) {
dir, err := os.MkdirTemp(os.TempDir(), "convoy-build")
if err != nil {
return "", err
}
entryFile, err := f.Open("embeddable/entrypoint.sh")
if err != nil {
return "", err
}
dockerFile, err := f.Open("embeddable/Dockerfile")
if err != nil {
return "", err
}
entryFileDst, err := os.Create(fmt.Sprintf("%s/entrypoint.sh", dir))
if err != nil {
return "", err
}
dockerfileDst, err := os.Create(fmt.Sprintf("%s/Dockerfile", dir))
if err != nil {
return "", err
}
_, err = io.Copy(entryFileDst, entryFile)
if err != nil {
return "", err
}
_, err = io.Copy(dockerfileDst, dockerFile)
if err != nil {
return "", err
}
err = os.WriteFile(fmt.Sprintf("%s/consul", dir), consulBytes, 0o777)
if err != nil {
return "", err
}
return dir, nil
}
func buildConsul(consulLocation, goArch, curDir string) ([]byte, error) {
defer os.Chdir(curDir)
err := os.Chdir(consulLocation)
if err != nil {
return nil, err
}
log.Print("building consul")
cmd := exec.Command("make", "linux")
err = cmd.Run()
if err != nil {
log.Print("failed to build consul")
return nil, err
}
binLocation := fmt.Sprintf("%s/pkg/bin/linux_%s/consul", consulLocation, goArch)
consulBytes, err := os.ReadFile(binLocation)
if err != nil {
return nil, err
}
return consulBytes, nil
}
func buildDockerImage(dir, envoyVersion string) error {
err := os.Chdir(dir)
if err != nil {
log.Fatal(err)
}
dockerBuildArgs := []string{"build", ".", "-t", "convoy:local"}
if envoyVersion != "" {
dockerBuildArgs = append(dockerBuildArgs[:1], append([]string{"--build-arg", fmt.Sprintf("ENVOY_VERSION=v%s-latest", envoyVersion)}, dockerBuildArgs[1:]...)...)
}
cmd := exec.Command("docker", dockerBuildArgs...)
log.Print("building convoy image")
err = cmd.Run()
if err != nil {
return err
}
return nil
}