This repository was archived by the owner on Jun 15, 2021. It is now read-only.
forked from kubernetes-retired/kube-aws
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremote_file_loader.go
95 lines (81 loc) · 2.03 KB
/
remote_file_loader.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
package provisioner
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/kubernetes-incubator/kube-aws/logger"
)
type RemoteFileLoader struct {
}
func (loader *RemoteFileLoader) Load(f RemoteFileSpec) (*RemoteFile, error) {
loaded := NewRemoteFile(f)
logger.Debugf("RemoteFileLoader.Load(): loaded RemoteFile: %+v", loaded)
path := f.Source.Path
// TODO
cachePath := path
if path != "" {
if f.Type == "credential" {
path = path + ".enc"
} else {
if _, err := os.Stat(path); os.IsNotExist(err) {
if f.URL != "" {
fmt.Fprintf(os.Stderr, "downloading %s\n", f.URL)
err := download(f.URL, cachePath)
if err != nil {
return nil, fmt.Errorf("failed downloading %s: %v", f.URL, err)
}
mode := f.FileMode()
if mode != nil {
if err := os.Chmod(cachePath, *mode); err != nil {
return nil, fmt.Errorf("failed to chmod %s: %v", path, err)
}
}
} else if len(f.Content.String()) > 0 {
err := ioutil.WriteFile(cachePath, f.Content.bytes, *f.FileMode())
if err != nil {
return nil, fmt.Errorf("failed to write %s: %v", cachePath, err)
}
} else {
return nil, fmt.Errorf("%s not found", path)
}
}
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed loading %s: %v", path, err)
}
loaded.Content = NewBinaryContent(data)
} else {
if f.Template != "" {
loaded.Content = NewStringContent(f.Template)
} else {
loaded.Content = f.Content
}
}
logger.Debugf("RemoteFileLoader.String(): returning loaded remoteFile: %+v", loaded)
return loaded, nil
}
func download(url string, dest string) error {
dir := filepath.Dir(dest)
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("failed creating dir %s: %v", dir, err)
}
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}