This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
customfiles.go
73 lines (62 loc) · 1.79 KB
/
customfiles.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
package acsengine
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/Azure/dcos-engine/pkg/api"
)
// CustomFileReader takes represents the source text of a file as an io.Reader and
// the desired destination to add it to
type CustomFileReader struct {
Source io.Reader
Dest string
}
func masterCustomFiles(profile *api.Properties) []api.CustomFile {
if profile.MasterProfile.CustomFiles != nil {
return *profile.MasterProfile.CustomFiles
}
return []api.CustomFile{}
}
func customfilesIntoReaders(customFiles []api.CustomFile) ([]CustomFileReader, error) {
customFileReaders := make([]CustomFileReader, len(customFiles))
for idx, customFile := range customFiles {
file, err := os.Open(customFile.Source)
if err != nil {
return []CustomFileReader{}, err
}
customFileReaders[idx] = CustomFileReader{
Source: file,
Dest: customFile.Dest,
}
}
return customFileReaders, nil
}
func substituteConfigStringCustomFiles(input string, customFiles []CustomFileReader, placeholder string) string {
var config string
for _, customFile := range customFiles {
config += buildConfigStringCustomFiles(
customFile.Source,
customFile.Dest)
}
return strings.Replace(input, placeholder, config, -1)
}
func buildConfigStringCustomFiles(source io.Reader, destinationFile string) string {
contents := []string{
fmt.Sprintf("- path: %s", destinationFile),
" permissions: \\\"0644\\\"",
" encoding: gzip",
" owner: \\\"root\\\"",
" content: !!binary |",
fmt.Sprintf(" %s\\n\\n", getBase64CustomFile(source)),
}
return strings.Join(contents, "\\n")
}
func getBase64CustomFile(source io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(source)
cfStr := buf.String()
cfStr = strings.Replace(cfStr, "\r\n", "\n", -1)
return getBase64CustomScriptFromStr(cfStr)
}