-
Notifications
You must be signed in to change notification settings - Fork 88
/
write.go
143 lines (120 loc) · 3.99 KB
/
write.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
package upstream
import (
"bytes"
"io/ioutil"
"os"
"path"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/upstream/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
func WriteUpstream(u *types.Upstream, options types.WriteOptions) error {
renderDir := options.RootDir
if options.CreateAppDir {
renderDir = path.Join(renderDir, u.Name)
}
renderDir = path.Join(renderDir, "upstream")
if options.IncludeAdminConsole {
adminConsoleFiles, err := generateAdminConsoleFiles(renderDir, options.SharedPassword)
if err != nil {
return errors.Wrap(err, "failed to generate admin console")
}
u.Files = append(u.Files, adminConsoleFiles...)
}
var previousInstallationContent []byte
_, err := os.Stat(renderDir)
if err == nil {
_, err = os.Stat(path.Join(renderDir, "userdata", "installation.yaml"))
if err == nil {
c, err := ioutil.ReadFile(path.Join(renderDir, "userdata", "installation.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read existing installation")
}
previousInstallationContent = c
}
if err := os.RemoveAll(renderDir); err != nil {
return errors.Wrap(err, "failed to remove previous content in upstream")
}
}
var prevInstallation *kotsv1beta1.Installation
if previousInstallationContent != nil {
decode := scheme.Codecs.UniversalDeserializer().Decode
prevObj, _, err := decode(previousInstallationContent, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to decode previous installation")
}
prevInstallation = prevObj.(*kotsv1beta1.Installation)
}
for _, file := range u.Files {
fileRenderPath := path.Join(renderDir, file.Path)
d, _ := path.Split(fileRenderPath)
if _, err := os.Stat(d); os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
if err := ioutil.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write upstream file")
}
}
// Write the installation status (update cursor, etc)
// but preserving the encryption key, if there already is one
encryptionKey, err := getEncryptionKey(prevInstallation)
if err != nil {
return errors.Wrap(err, "failed to get encryption key")
}
var channelName string
if prevInstallation != nil && options.PreserveInstallation {
channelName = prevInstallation.Spec.ChannelName
} else {
channelName = u.ChannelName
}
installation := kotsv1beta1.Installation{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "Installation",
},
ObjectMeta: metav1.ObjectMeta{
Name: u.Name,
},
Spec: kotsv1beta1.InstallationSpec{
UpdateCursor: u.UpdateCursor,
ChannelName: channelName,
VersionLabel: u.VersionLabel,
ReleaseNotes: u.ReleaseNotes,
EncryptionKey: encryptionKey,
},
}
if _, err := os.Stat(path.Join(renderDir, "userdata")); os.IsNotExist(err) {
if err := os.MkdirAll(path.Join(renderDir, "userdata"), 0755); err != nil {
return errors.Wrap(err, "failed to create userdata dir")
}
}
err = ioutil.WriteFile(path.Join(renderDir, "userdata", "installation.yaml"), mustMarshalInstallation(&installation), 0644)
if err != nil {
return errors.Wrap(err, "failed to write installation")
}
return nil
}
func getEncryptionKey(prevInstallation *kotsv1beta1.Installation) (string, error) {
if prevInstallation == nil {
cipher, err := crypto.NewAESCipher()
if err != nil {
return "", errors.Wrap(err, "failed to create new AES cipher")
}
return cipher.ToString(), nil
}
return prevInstallation.Spec.EncryptionKey, nil
}
func mustMarshalInstallation(installation *kotsv1beta1.Installation) []byte {
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(installation, &b); err != nil {
panic(err)
}
return b.Bytes()
}