/
chan.go
221 lines (199 loc) · 6.56 KB
/
chan.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
/*
Copyright 2017 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
package config
import (
"fmt"
"io"
"os"
"sort"
"github.com/adobe/butler/internal/stats"
log "github.com/sirupsen/logrus"
)
type ChanEvent interface {
CanCopyFiles() bool
CleanTmpFiles() error
GetTmpFileMap() []TmpFile
SetSuccess(string, string, error) error
SetTmpFile(string, string, string) error
CopyPrimaryConfigFiles(map[string]*ManagerOpts) bool
CopyAdditionalConfigFiles(string) bool
}
// ConfigChanEvent is the object passed around in the channel which contains
// information on whether the file has changed, the path to the tempfile,
// the config name, and repository event information
type ConfigChanEvent struct {
HasChanged bool
TmpFile *os.File
ConfigFile *string
Manager string
Repo map[string]*RepoFileEvent
}
// CanCopyFiles returns a boolean which tells whether or not butler is able to
// copy the files from the repository to the local filesystem. The assumption
// is that ALL files have to pass before butler attempts to copy over.
func (c *ConfigChanEvent) CanCopyFiles() bool {
var (
res bool
)
res = true
log.Debugf("ConfigChanEvent::CanCopyFiles(): seeing if we can copy files")
for _, r := range c.Repo {
for _, v := range r.Success {
if v == false {
res = false
}
}
}
log.Debugf("ConfigChanEvent::CanCopyFiles(): returning %v", res)
return res
}
// CleanTmpFiles returns an error, or not, depending on whether butler was able
// to delete all the tempfiles that were created during the config file
// retrieval from the remote repository
func (c *ConfigChanEvent) CleanTmpFiles() error {
log.Debugf("ConfigChanEvent::CleanTmpFiles(): cleaning up temporary files")
for _, r := range c.Repo {
for _, f := range r.TmpFile {
log.Debugf("ConfigChanEvent::CleanTmpFiles(): removing file %#v", f)
os.Remove(f)
}
}
if c.TmpFile != nil {
os.Remove(c.TmpFile.Name())
}
return nil
}
// GetTmpFileMap returns a slice of SORTED TmpFile objects which contain the
// names of all the temp files creted during the config file retrieval from the
// remote repository.
func (c *ConfigChanEvent) GetTmpFileMap() []TmpFile {
var (
keys []string
res []TmpFile
tmpRes map[string]string
)
tmpRes = make(map[string]string)
for _, r := range c.Repo {
for k, v := range r.TmpFile {
keys = append(keys, k)
tmpRes[k] = v
}
}
// Due to the way that golang handles the ordering of maps (random), we have to
// enforce a sorted ordering, otherwise we may write config files differently,
// but with the same data (eg: the merged primary config file), causing an undesired
// configuration reload
sort.Strings(keys)
for _, v := range keys {
res = append(res, TmpFile{Name: v, File: tmpRes[v]})
}
log.Debugf("ConfigChanEvent::GetTmpFileMap(): res=%#v", res)
return res
}
// SetSuccess sets the value for the file argument in the repo argument to true
func (c *ConfigChanEvent) SetSuccess(repo string, file string, err error) error {
// If c.Repo has not been initialized, do so.
if c.Repo == nil {
c.Repo = make(map[string]*RepoFileEvent)
}
if _, ok := c.Repo[repo]; !ok {
rfe := &RepoFileEvent{}
rfe.Success = make(map[string]bool)
rfe.Error = make(map[string]error)
rfe.TmpFile = make(map[string]string)
c.Repo[repo] = rfe
}
c.Repo[repo].SetSuccess(file, err)
return nil
}
// SetFailure sets the value for the file argument in the repo argument to false
func (c *ConfigChanEvent) SetFailure(repo string, file string, err error) error {
// If c.Repo has not been initialized, do so.
if c.Repo == nil {
c.Repo = make(map[string]*RepoFileEvent)
}
if _, ok := c.Repo[repo]; !ok {
rfe := &RepoFileEvent{}
rfe.Success = make(map[string]bool)
rfe.Error = make(map[string]error)
rfe.TmpFile = make(map[string]string)
c.Repo[repo] = rfe
}
c.Repo[repo].SetFailure(file, err)
return nil
}
func (c *ConfigChanEvent) SetTmpFile(repo string, file string, tmpfile string) error {
if _, ok := c.Repo[repo]; ok {
c.Repo[repo].SetTmpFile(file, tmpfile)
}
return nil
}
func (c *ConfigChanEvent) CopyPrimaryConfigFiles(opts map[string]*ManagerOpts) bool {
var (
primaryConfigs []string
)
// need to get a list of the primary config files, in order from first to last, through each repo
for _, opt := range opts {
for _, config := range opt.PrimaryConfig {
primaryConfigs = append(primaryConfigs, config)
}
}
out, err := os.OpenFile(c.TmpFile.Name(), os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Infof("ConfigChanEvent::CopyPrimaryConfigFiles(): Could not process and merge new %v err=%s.", c.ConfigFile, err.Error())
stats.SetButlerConfigVal(stats.FAILURE, "local", stats.GetStatsLabel(*c.ConfigFile))
c.CleanTmpFiles()
return false
} else {
// we need to go through each fo the primary config files in order, and then find the corresponding tmpMap entry
for _, f := range primaryConfigs {
for _, t := range c.GetTmpFileMap() {
if t.Name == f {
in, err := os.Open(t.File)
if err != nil {
log.Infof("ConfigChanEvent::CopyPrimaryConfigFiles(): Could not process and merge new %v err=%s.", c.ConfigFile, err.Error())
stats.SetButlerConfigVal(stats.FAILURE, "local", stats.GetStatsLabel(t.Name))
c.CleanTmpFiles()
out.Close()
return false
}
_, err = io.Copy(out, in)
if err != nil {
log.Infof("ConfigChanEvent::CopyPrimaryConfigFiles(): Could not process and merge new %v err=%s.", c.ConfigFile, err.Error())
stats.SetButlerConfigVal(stats.FAILURE, "local", stats.GetStatsLabel(t.Name))
c.CleanTmpFiles()
out.Close()
return false
}
in.Close()
break
}
}
}
}
out.Sync()
out.Close()
return CompareAndCopy(c.TmpFile.Name(), *c.ConfigFile, c.Manager)
}
func (c *ConfigChanEvent) CopyAdditionalConfigFiles(destDir string) bool {
var (
IsModified bool
)
log.Debugf("Manager::CopyAdditionalConfigFiles(): entering")
IsModified = false
for _, f := range c.GetTmpFileMap() {
destFile := fmt.Sprintf("%s/%s", destDir, f.Name)
if CompareAndCopy(f.File, destFile, c.Manager) {
IsModified = true
}
}
return IsModified
}