forked from rancher/giddyup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
153 lines (135 loc) · 3.08 KB
/
exec.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
package app
import (
"bufio"
"io/ioutil"
"os"
"path"
"strings"
"syscall"
"time"
"os/exec"
"github.com/Sirupsen/logrus"
"github.com/rancher/go-rancher-metadata/metadata"
"github.com/urfave/cli"
)
func ExecCommand() cli.Command {
return cli.Command{
Name: "exec",
Usage: "exec out to a command",
Action: execCommand,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "secret-envs",
Usage: "reads /run/secrets and sets env vars",
},
cli.BoolFlag{
Name: "cloud-init",
Usage: "Process /self/service/metadata/cloud-init (currently only write_files)",
},
cli.StringSliceFlag{
Name: "wait-for-file",
Usage: "wait for a file to exist, assumes something else is creating it. This flag can be used more then once for multiple files",
},
cli.StringSliceFlag{
Name: "source-file",
Usage: "Source an environment file before executing. Can use the flag multiple times",
},
},
}
}
func execCommand(c *cli.Context) error {
if c.Bool("secret-envs") {
envs, err := filesToMap("/run/secrets")
if err != nil {
logrus.Error(err)
return err
}
for key, val := range envs {
os.Setenv(strings.ToUpper(key), val)
}
}
if c.Bool("cloud-init") {
mdClient, err := metadata.NewClientAndWait(c.GlobalString("metadata-url"))
if err != nil {
return err
}
ProcessCloudInit(mdClient)
}
// This is a fairly naive approach for now.
if len(c.StringSlice("wait-for-file")) > 0 {
err := waitForFiles(c.StringSlice("wait-for-file"))
if err != nil {
return err
}
}
if len(c.StringSlice("source-file")) > 0 {
envs, err := readSourceFiles(c.StringSlice("source-file"))
if err != nil {
return err
}
for key, val := range envs {
os.Setenv(key, val)
}
}
name, err := exec.LookPath(c.Args().Get(0))
if err != nil {
return err
}
return syscall.Exec(name, c.Args(), os.Environ())
}
func filesToMap(dirPath string) (map[string]string, error) {
vals := map[string]string{}
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return vals, err
}
for _, file := range files {
content, err := ioutil.ReadFile(path.Join(dirPath, file.Name()))
if err != nil {
return vals, err
}
vals[file.Name()] = string(content)
}
return vals, nil
}
func waitForFiles(files []string) error {
for true {
seenCount := 0
for idx, file := range files {
if file != "seen" {
if _, err := os.Stat(file); os.IsNotExist(err) {
break
}
// set the seen value and increment the counter
files[idx] = "seen"
seenCount++
continue
}
seenCount++
}
if seenCount == len(files) {
break
}
time.Sleep(1 * time.Second)
}
return nil
}
func readSourceFiles(files []string) (map[string]string, error) {
envs := map[string]string{}
for _, file := range files {
// the Close() will not be deferred to avoid large number of open files...
f, err := os.Open(file)
if err != nil {
return envs, err
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
pair := strings.SplitN(scanner.Text(), "=", 2)
if len(pair) == 2 {
envs[pair[0]] = pair[1]
}
}
f.Close()
}
return envs, nil
}