forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injection.go
100 lines (93 loc) · 2.89 KB
/
injection.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
package util
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/openshift/source-to-image/pkg/api"
)
// FixInjectionsWithRelativePath fixes the injections that does not specify the
// destination directory or the directory is relative to use the provided
// working directory.
func FixInjectionsWithRelativePath(workdir string, injections api.VolumeList) api.VolumeList {
if len(injections) == 0 {
return injections
}
newList := api.VolumeList{}
for _, injection := range injections {
changed := false
if filepath.Clean(injection.Destination) == "." {
injection.Destination = workdir
changed = true
}
if !filepath.IsAbs(injection.Destination) {
injection.Destination = filepath.Join(workdir, injection.Destination)
changed = true
}
if changed {
glog.V(5).Infof("Using %q as a destination for injecting %q", injection.Destination, injection.Source)
}
newList = append(newList, injection)
}
return newList
}
// ExpandInjectedFiles returns a flat list of all files that are injected into a
// container. All files from nested directories are returned in the list.
func ExpandInjectedFiles(injections api.VolumeList) ([]string, error) {
result := []string{}
for _, s := range injections {
if _, err := os.Stat(s.Source); err != nil {
return nil, err
}
err := filepath.Walk(s.Source, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
newPath := filepath.Join(s.Destination, strings.TrimPrefix(path, s.Source))
result = append(result, newPath)
return nil
})
if err != nil {
return nil, err
}
}
return result, nil
}
// CreateInjectedFilesRemovalScript creates a shell script that contains truncation
// of all files we injected into the container. The path to the script is returned.
// When the scriptName is provided, it is also truncated together with all
// secrets.
func CreateInjectedFilesRemovalScript(files []string, scriptName string) (string, error) {
rmScript := "set -e\n"
for _, s := range files {
rmScript += fmt.Sprintf("truncate -s0 %q\n", s)
}
f, err := ioutil.TempFile("", "s2i-injection-remove")
if err != nil {
return "", err
}
if len(scriptName) > 0 {
rmScript += fmt.Sprintf("truncate -s0 %q\n", scriptName)
}
rmScript += "set +e\n"
err = ioutil.WriteFile(f.Name(), []byte(rmScript), 0700)
return f.Name(), err
}
// HandleInjectionError handles the error caused by injection and provide
// reasonable suggestion to users.
func HandleInjectionError(p api.VolumeSpec, err error) error {
if err == nil {
return nil
}
if strings.Contains(err.Error(), "no such file or directory") {
glog.Errorf("The destination directory for %q injection must exist in container (%q)", p.Source, p.Destination)
return err
}
glog.Errorf("Error occured during injecting %q to %q: %v", p.Source, p.Destination, err)
return err
}