-
Notifications
You must be signed in to change notification settings - Fork 90
/
archive.go
91 lines (74 loc) · 2.37 KB
/
archive.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
package upload
import (
"io/ioutil"
"os"
"path"
"strings"
"github.com/mholt/archiver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
"k8s.io/client-go/kubernetes/scheme"
)
func createUploadableArchive(rootPath string) (string, error) {
if strings.HasSuffix(rootPath, string(os.PathSeparator)) {
rootPath = strings.TrimSuffix(rootPath, string(os.PathSeparator))
}
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: true,
},
}
paths := []string{
path.Join(rootPath, "upstream"),
path.Join(rootPath, "base"),
path.Join(rootPath, "overlays"),
}
// the caller of this function is repsonsible for deleting this file
tempDir, err := ioutil.TempDir("", "kots")
if err != nil {
return "", errors.Wrap(err, "failed to create temp dir")
}
if err := tarGz.Archive(paths, path.Join(tempDir, "kots-uploadable-archive.tar.gz")); err != nil {
return "", errors.Wrap(err, "failed to create tar gz")
}
return path.Join(tempDir, "kots-uploadable-archive.tar.gz"), nil
}
func findUpdateCursor(rootPath string) (string, error) {
installationFilePath := path.Join(rootPath, "upstream", "userdata", "installation.yaml")
_, err := os.Stat(installationFilePath)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", errors.Wrap(err, "failed to open file")
}
installationData, err := ioutil.ReadFile(installationFilePath)
if err != nil {
return "", errors.Wrap(err, "failed to read update installation file")
}
kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(installationData), nil, nil)
if err != nil {
return "", errors.Wrap(err, "failed to decode installation data")
}
installation := obj.(*kotsv1beta1.Installation)
return installation.Spec.UpdateCursor, nil
}
func findLicense(rootPath string) (*string, error) {
licenseFilePath := path.Join(rootPath, "upstream", "userdata", "license.yaml")
_, err := os.Stat(licenseFilePath)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrap(err, "failed to open file with license")
}
b, err := ioutil.ReadFile(licenseFilePath)
if err != nil {
return nil, errors.Wrap(err, "failed to read license file")
}
license := string(b)
return &license, nil
}