-
Notifications
You must be signed in to change notification settings - Fork 0
/
executer.go
143 lines (128 loc) · 3.24 KB
/
executer.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
package flexentry
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/handlename/ssmwrap"
)
type ExecuteOption struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Environ []string
}
type Executer interface {
Execute(ctx context.Context, opt *ExecuteOption, commands ...string) error
}
type ShellExecuter struct {
shell string
shellArgs []string
}
func NewShellExecuter() *ShellExecuter {
shell := "sh"
if s := os.Getenv("FLEXENTRY_SHELL"); s != "" {
shell = s
}
shellArgs := []string{"-c"}
if sArgs := os.Getenv("FLEXENTRY_SHELL_ARGS"); sArgs != "" {
shellArgs = strings.Split(sArgs, " ")
}
return &ShellExecuter{
shell: shell,
shellArgs: shellArgs,
}
}
func (e *ShellExecuter) Execute(ctx context.Context, opt *ExecuteOption, commands ...string) error {
args := make([]string, 0, len(e.shellArgs)+len(commands))
args = append(args, e.shellArgs...)
if os.Getenv("FLEXENTRY_QUOTE_COMMAND") != "" {
args = append(args, `"`+strings.Join(commands, " ")+`"`)
} else {
args = append(args, strings.Join(commands, " "))
}
log.Printf("[debug] $%s %s", e.shell, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, e.shell, args...)
cmd.Env = os.Environ()
p, _ := cmd.StdinPipe()
var stdin io.Reader
if opt != nil {
stdin = opt.Stdin
cmd.Stderr = opt.Stderr
cmd.Stdout = opt.Stdout
cmd.Env = MergeEnv(cmd.Env, opt.Environ)
}
go func() {
defer p.Close()
if stdin == nil {
return
}
if _, err := io.Copy(p, stdin); err != nil {
log.Println("[warn] failed to write stdinPipe:", err)
}
}()
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func (e *ShellExecuter) Clone() *ShellExecuter {
cloned := *e
return &cloned
}
func (e *ShellExecuter) SetShell(shell string, shellArgs []string) *ShellExecuter {
cloned := e.Clone()
cloned.shell = shell
cloned.shellArgs = make([]string, len(shellArgs))
copy(cloned.shellArgs, shellArgs)
return cloned
}
type SSMWrapExecuter struct {
Executer
mu sync.Mutex
lastExported time.Time
ssmCacheExpires time.Duration
}
func NewSSMWrapExecuter(executer Executer, cacheExpires time.Duration) *SSMWrapExecuter {
return &SSMWrapExecuter{
Executer: executer,
ssmCacheExpires: cacheExpires,
}
}
func (e *SSMWrapExecuter) exportEnvWithCache() error {
e.mu.Lock()
defer e.mu.Unlock()
if e.lastExported.IsZero() || e.lastExported.Before(time.Now().Add(-1*e.ssmCacheExpires)) {
defer func() {
e.lastExported = time.Now()
}()
return e.exportEnv()
}
log.Printf("[debug] exportEnv skipped. last exported at %s", e.lastExported.Format(time.RFC3339))
return nil
}
func (e *SSMWrapExecuter) exportEnv() error {
if paths := os.Getenv("SSMWRAP_PATHS"); paths == "" {
return nil
} else {
if err := ssmwrap.Export(ssmwrap.ExportOptions{
Paths: strings.Split(paths, ","),
Retries: 3,
}); err != nil {
return fmt.Errorf("failed to fetch values from SSM paths %s: %w", paths, err)
}
log.Printf("[debug] exportEnv from SSMWRAP_PATHS=%s", paths)
}
return nil
}
func (e *SSMWrapExecuter) Execute(ctx context.Context, opt *ExecuteOption, commands ...string) error {
if err := e.exportEnvWithCache(); err != nil {
return err
}
return e.Executer.Execute(ctx, opt, commands...)
}