-
Notifications
You must be signed in to change notification settings - Fork 88
/
main.go
138 lines (116 loc) · 3.58 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
package main
import "C"
import (
"fmt"
"io/ioutil"
"os"
"path"
"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"
"github.com/replicatedhq/kots/pkg/pull"
"k8s.io/client-go/kubernetes/scheme"
)
//export UpdateCheck
func UpdateCheck(fromArchivePath string) int {
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp path: %s\n", err.Error())
return -1
}
defer os.RemoveAll(tmpRoot)
// extract the current archive to this root
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())
return -1
}
beforeCursor, err := readCursorFromPath(tmpRoot)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
return -1
}
expectedLicenseFile := path.Join(tmpRoot, "upstream", "userdata", "license.yaml")
_, err = os.Stat(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to find license file in archive\n")
return -1
}
licenseData, err := ioutil.ReadFile(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to read license file: %s\n", err.Error())
return -1
}
kotsscheme.AddToScheme(scheme.Scheme)
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())
return -1
}
license := obj.(*kotsv1beta1.License)
pullOptions := pull.PullOptions{
LicenseFile: expectedLicenseFile,
RootDir: tmpRoot,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
}
if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
return 1
}
afterCursor, err := readCursorFromPath(tmpRoot)
if err != nil {
fmt.Printf("failed to read cursor file after update: %s\n", err.Error())
return -1
}
fmt.Printf("Result of checking for updates for %s: Before: %s, After %s\n", license.Spec.AppSlug, beforeCursor, afterCursor)
isUpdateAvailable := string(beforeCursor) != string(afterCursor)
if !isUpdateAvailable {
return 0
}
paths := []string{
path.Join(tmpRoot, "upstream"),
path.Join(tmpRoot, "base"),
path.Join(tmpRoot, "overlays"),
}
err = os.Remove(fromArchivePath)
if err != nil {
fmt.Printf("failed to delete archive to replace: %s\n", err.Error())
return -1
}
if err := tarGz.Archive(paths, fromArchivePath); err != nil {
fmt.Printf("failed to write archive: %s\n", err.Error())
return -1
}
return 1
}
func readCursorFromPath(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 devode installation data")
}
installation := obj.(*kotsv1beta1.Installation)
return installation.Spec.UpdateCursor, nil
}
func main() {}