-
Notifications
You must be signed in to change notification settings - Fork 90
/
online.go
264 lines (229 loc) · 7.52 KB
/
online.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import "C"
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mholt/archiver"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/rewrite"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
//export PullFromLicense
func PullFromLicense(socket string, licenseData string, downstream string, namespace string, outputFile string) {
go func() {
var ffiResult *FFIResult
statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()
license, err := loadLicense(licenseData)
if err != nil {
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
licenseFile, err := writeLicenseFileFromLicenseData(licenseData)
if err != nil {
fmt.Printf("failed to write license file: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
defer os.Remove(licenseFile)
// pull to a tmp dir
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp root path: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)
pullOptions := pull.PullOptions{
Downstreams: []string{downstream},
LicenseFile: licenseFile,
Namespace: namespace,
ExcludeKotsKinds: true,
RootDir: tmpRoot,
ExcludeAdminConsole: true,
CreateAppDir: false,
ReportWriter: statusClient.getOutputWriter(),
}
if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
// make an archive
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: true,
},
}
paths := []string{
filepath.Join(tmpRoot, "upstream"),
filepath.Join(tmpRoot, "base"),
filepath.Join(tmpRoot, "overlays"),
}
if err := tarGz.Archive(paths, outputFile); err != nil {
fmt.Printf("failed to write archive: %s", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
ffiResult = NewFFIResult(0)
}()
}
//export RewriteVersion
func RewriteVersion(socket, fromArchivePath, outputFile, downstreamsStr, k8sNamespace, registryJson string, copyImages, isAirgap bool, marshalledConfigValues string) {
go func() {
var ffiResult *FFIResult
statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()
registryInfo := struct {
Host string `json:"registryHostname"`
Username string `json:"registryUsername"`
Password string `json:"registryPassword"`
Namespace string `json:"namespace"`
}{}
if err := json.Unmarshal([]byte(registryJson), ®istryInfo); err != nil {
fmt.Printf("failed to unmarshal registry info: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
donwstreams := []string{}
err = json.Unmarshal([]byte(downstreamsStr), &donwstreams)
if err != nil {
if err != nil {
fmt.Printf("failed to decode downstreams: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
}
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp root path: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(fromArchivePath, tmpRoot); err != nil {
fmt.Printf("failed to unarchive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
installationFilePath := filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml")
installation, err := loadInstallationFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
expectedLicenseFile := filepath.Join(tmpRoot, "upstream", "userdata", "license.yaml")
_, err = os.Stat(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to find license file in archive\n")
ffiResult = NewFFIResult(-1).WithError(err)
return
}
licenseData, err := ioutil.ReadFile(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to read license file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)
var configValues *kotsv1beta1.ConfigValues
if marshalledConfigValues != "" {
obj, _, err := decode([]byte(marshalledConfigValues), nil, nil)
if err != nil {
fmt.Printf("failed to decode marshaled config values: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
configValues = obj.(*kotsv1beta1.ConfigValues)
err = ioutil.WriteFile(filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"), mustMarshalConfigValues(configValues), 0644)
if err != nil {
fmt.Printf("failed to write marshaled config values: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
} else {
configValues, err = parseConfigValuesFromFile(filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"))
if err != nil {
fmt.Printf("failed to decode config values from release archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
}
options := rewrite.RewriteOptions{
RootDir: tmpRoot,
UpstreamURI: fmt.Sprintf("replicated://%s", license.Spec.AppSlug),
UpstreamPath: filepath.Join(tmpRoot, "upstream"),
Installation: installation,
Downstreams: donwstreams,
Silent: true,
CreateAppDir: false,
ExcludeKotsKinds: true,
License: license,
ConfigValues: configValues,
K8sNamespace: k8sNamespace,
ReportWriter: statusClient.getOutputWriter(),
CopyImages: copyImages,
IsAirgap: isAirgap,
RegistryEndpoint: registryInfo.Host,
RegistryUsername: registryInfo.Username,
RegistryPassword: registryInfo.Password,
RegistryNamespace: registryInfo.Namespace,
}
if err := rewrite.Rewrite(options); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
paths := []string{
filepath.Join(tmpRoot, "upstream"),
filepath.Join(tmpRoot, "base"),
filepath.Join(tmpRoot, "overlays"),
}
if err := tarGz.Archive(paths, outputFile); err != nil {
fmt.Printf("failed to write archive: %s", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
ffiResult = NewFFIResult(0)
}()
}
func mustMarshalConfigValues(configValues *kotsv1beta1.ConfigValues) []byte {
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(configValues, &b); err != nil {
panic(err)
}
return b.Bytes()
}