-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathpackage.go
212 lines (182 loc) · 9.02 KB
/
package.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
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/base64"
"errors"
"fmt"
"github.com/camunda/camunda/c8run/internal/archive"
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
)
func Clean(camundaVersion string, elasticsearchVersion string) {
os.RemoveAll("elasticsearch-" + elasticsearchVersion)
os.RemoveAll("camunda-zeebe-" + camundaVersion)
logFiles := []string{"camunda.log", "connectors.log", "elasticsearch.log"}
for _, logFile := range logFiles {
_, err := os.Stat(filepath.Join("log", logFile))
if !errors.Is(err, os.ErrNotExist) {
os.Remove(filepath.Join("log", logFile))
}
}
}
func downloadAndExtract(filePath, url, extractDir string, authToken string, extractFunc func(string, string) error) error {
err := archive.DownloadFile(filePath, url, authToken)
if err != nil {
return fmt.Errorf("downloadAndExtract: failed to download file at url %s\n%w\n%s", url, err, debug.Stack())
}
_, err = os.Stat(extractDir)
if errors.Is(err, os.ErrNotExist) {
err = extractFunc(filePath, ".")
if err != nil {
return fmt.Errorf("downloadAndExtract: failed to extract from archive at %s\n%w\n%s", filePath, err, debug.Stack())
}
}
return nil
}
func downloadGHArtifact(camundaVersion string, camundaFilePath string) error {
_, err := exec.LookPath("gh")
if err != nil {
// This is not an error because there is another way to download camunda releases
return nil
}
cmd := exec.Command("gh", "release", "download", "--repo", "camunda/camunda", camundaVersion, "-p", camundaFilePath)
err = cmd.Run()
if err != nil {
return fmt.Errorf("downloadGHArtifact: failed to download artifact %w\n%s", err, debug.Stack())
}
return nil
}
func PackageWindows(camundaVersion string, elasticsearchVersion string, connectorsVersion string, camundaReleaseTag string, composeTag string) error {
elasticsearchUrl := "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-" + elasticsearchVersion + "-windows-x86_64.zip"
elasticsearchFilePath := "elasticsearch-" + elasticsearchVersion + ".zip"
camundaFilePath := "camunda-zeebe-" + camundaVersion + ".zip"
camundaUrl := "https://github.com/camunda/camunda/releases/download/" + camundaReleaseTag + "/" + camundaFilePath
connectorsFilePath := "connector-runtime-bundle-" + connectorsVersion + "-with-dependencies.jar"
connectorsUrl := "https://repository.nexus.camunda.cloud/content/groups/internal/io/camunda/connector/connector-runtime-bundle/" + connectorsVersion + "/" + connectorsFilePath
composeUrl := "https://github.com/camunda/camunda-platform/archive/refs/tags/" + composeTag + ".zip"
composeFilePath := composeTag + ".zip"
composeExtractionPath := "camunda-platform-" + composeTag
authToken := os.Getenv("GH_TOKEN")
javaArtifactsUser := os.Getenv("JAVA_ARTIFACTS_USER")
javaArtifactsPassword := os.Getenv("JAVA_ARTIFACTS_PASSWORD")
if javaArtifactsUser == "" || javaArtifactsPassword == "" {
return fmt.Errorf("PackageWindows: JAVA_ARTIFACTS_USER or JAVA_ARTIFACTS_PASSWORD env vars are not set")
}
javaArtifactsToken := "Basic " + base64.StdEncoding.EncodeToString([]byte(javaArtifactsUser + ":" + javaArtifactsPassword))
Clean(camundaVersion, elasticsearchVersion)
err := downloadAndExtract(elasticsearchFilePath, elasticsearchUrl, "elasticsearch-"+elasticsearchVersion, "", archive.UnzipSource)
if err != nil {
return fmt.Errorf("PackageWindows: failed to fetch elasticsearch: %w\n%s", err, debug.Stack())
}
err = downloadGHArtifact(camundaVersion, camundaFilePath)
if err != nil {
return fmt.Errorf("PackageWindows: failed to download camunda with gh: %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(camundaFilePath, camundaUrl, "camunda-zeebe-"+camundaVersion, authToken, archive.UnzipSource)
if err != nil {
return fmt.Errorf("PackageWindows: failed to fetch camunda: %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(connectorsFilePath, connectorsUrl, connectorsFilePath, javaArtifactsToken, func(_, _ string) error { return nil })
if err != nil {
return fmt.Errorf("PackageWindows: failed to fetch connectors: %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(composeFilePath, composeUrl, composeExtractionPath, authToken, archive.UnzipSource)
if err != nil {
return fmt.Errorf("PackageWindows: failed to fetch compose release %w\n%s", err, debug.Stack())
}
os.Chdir("..")
filesToArchive := []string{
filepath.Join("c8run", "README.md"),
filepath.Join("c8run", "connectors-application.properties"),
filepath.Join("c8run", connectorsFilePath),
filepath.Join("c8run", "elasticsearch-"+elasticsearchVersion),
filepath.Join("c8run", "custom_connectors"),
filepath.Join("c8run", "configuration"),
filepath.Join("c8run", "c8run.exe"),
filepath.Join("c8run", "endpoints.txt"),
filepath.Join("c8run", "log"),
filepath.Join("c8run", "camunda-zeebe-"+camundaVersion),
filepath.Join("c8run", "package.bat"),
filepath.Join("c8run", composeExtractionPath),
}
err = archive.ZipSource(filesToArchive, filepath.Join("c8run", "camunda8-run-"+camundaVersion+"-windows-x86_64.zip"))
if err != nil {
return fmt.Errorf("PackageWindows: failed to create c8run package %w\n%s", err, debug.Stack())
}
os.Chdir("c8run")
return nil
}
func PackageUnix(camundaVersion string, elasticsearchVersion string, connectorsVersion string, camundaReleaseTag string, composeTag string) error {
var architecture string
if runtime.GOARCH == "amd64" {
architecture = "x86_64"
} else if runtime.GOARCH == "arm64" {
architecture = "aarch64"
}
elasticsearchUrl := "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-" + elasticsearchVersion + "-" + runtime.GOOS + "-" + architecture + ".tar.gz"
elasticsearchFilePath := "elasticsearch-" + elasticsearchVersion + ".tar.gz"
camundaFilePath := "camunda-zeebe-" + camundaVersion + ".tar.gz"
camundaUrl := "https://github.com/camunda/camunda/releases/download/" + camundaReleaseTag + "/" + camundaFilePath
connectorsFilePath := "connector-runtime-bundle-" + connectorsVersion + "-with-dependencies.jar"
connectorsUrl := "https://repository.nexus.camunda.cloud/content/groups/internal/io/camunda/connector/connector-runtime-bundle/" + connectorsVersion + "/" + connectorsFilePath
composeUrl := "https://github.com/camunda/camunda-platform/archive/refs/tags/" + composeTag + ".tar.gz"
composeFilePath := composeTag + ".tar.gz"
composeExtractionPath := "camunda-platform-" + composeTag
authToken := os.Getenv("GH_TOKEN")
javaArtifactsUser := os.Getenv("JAVA_ARTIFACTS_USER")
javaArtifactsPassword := os.Getenv("JAVA_ARTIFACTS_PASSWORD")
if javaArtifactsUser == "" || javaArtifactsPassword == "" {
return fmt.Errorf("PackageUnix: JAVA_ARTIFACTS_USER or JAVA_ARTIFACTS_PASSWORD env vars are not set")
}
javaArtifactsToken := "Basic " + base64.StdEncoding.EncodeToString([]byte(javaArtifactsUser + ":" + javaArtifactsPassword))
Clean(camundaVersion, elasticsearchVersion)
err := downloadAndExtract(elasticsearchFilePath, elasticsearchUrl, "elasticsearch-"+elasticsearchVersion, "", archive.ExtractTarGzArchive)
if err != nil {
return fmt.Errorf("PackageUnix: failed to fetch elasticsearch %w\n%s", err, debug.Stack())
}
err = downloadGHArtifact(camundaVersion, camundaFilePath)
if err != nil {
return fmt.Errorf("PackageWindows: failed to download camunda with gh: %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(camundaFilePath, camundaUrl, "camunda-zeebe-"+camundaVersion, authToken, archive.ExtractTarGzArchive)
if err != nil {
return fmt.Errorf("PackageUnix: failed to fetch camunda %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(connectorsFilePath, connectorsUrl, connectorsFilePath, javaArtifactsToken, func(_, _ string) error { return nil })
if err != nil {
return fmt.Errorf("PackageUnix: failed to fetch connectors %w\n%s", err, debug.Stack())
}
err = downloadAndExtract(composeFilePath, composeUrl, composeExtractionPath, authToken, archive.ExtractTarGzArchive)
if err != nil {
return fmt.Errorf("PackageUnix: failed to fetch compose release %w\n%s", err, debug.Stack())
}
os.Chdir("..")
filesToArchive := []string{
filepath.Join("c8run", "README.md"),
filepath.Join("c8run", "connectors-application.properties"),
filepath.Join("c8run", connectorsFilePath),
filepath.Join("c8run", "elasticsearch-"+elasticsearchVersion),
filepath.Join("c8run", "custom_connectors"),
filepath.Join("c8run", "configuration"),
filepath.Join("c8run", "c8run"),
filepath.Join("c8run", "endpoints.txt"),
filepath.Join("c8run", "log"),
filepath.Join("c8run", "camunda-zeebe-"+camundaVersion),
filepath.Join("c8run", "start.sh"),
filepath.Join("c8run", "shutdown.sh"),
filepath.Join("c8run", "package.sh"),
filepath.Join("c8run", composeExtractionPath),
}
outputArchive, err := os.Create(filepath.Join("c8run", "camunda8-run-"+camundaVersion+"-"+runtime.GOOS+"-"+architecture+".tar.gz"))
if err != nil {
return fmt.Errorf("PackageUnix: failed to create empty archive file %w\n%s", err, debug.Stack())
}
err = archive.CreateTarGzArchive(filesToArchive, outputArchive)
if err != nil {
return fmt.Errorf("PackageUnix: failed to fill camunda archive %w\n%s", err, debug.Stack())
}
os.Chdir("c8run")
return nil
}