-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
executor_host.go
159 lines (127 loc) · 4.28 KB
/
executor_host.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
154
155
156
157
158
159
/*
Copyright (C) 2020 Daniele Rondina <geaaru@sabayonlinux.org>
Credits goes also to Gogs authors, some code portions and re-implemented design
are also coming from the Gogs project, which is using the go-macaron framework
and was really source of ispiration. Kudos to them!
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package executor
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
helpers "github.com/MottainaiCI/lxd-compose/pkg/helpers"
log "github.com/MottainaiCI/lxd-compose/pkg/logger"
)
func (e *LxdCExecutor) RunHostCommandWithOutput(command string, envs map[string]string, outBuffer, errBuffer io.WriteCloser, entryPoint []string) (int, error) {
ans := 1
entrypoint := []string{"/bin/bash", "-c"}
if len(e.Entrypoint) > 0 {
entrypoint = e.Entrypoint
}
if len(entryPoint) > 0 {
entrypoint = entryPoint
}
if outBuffer == nil {
return 1, errors.New("Invalid outBuffer")
}
if errBuffer == nil {
return 1, errors.New("Invalid errBuffer")
}
cmds := append(entrypoint, command)
hostCommand := exec.Command(cmds[0], cmds[1:]...)
logger := log.GetDefaultLogger()
e.Emitter.DebugLog(true,
logger.Aurora.Bold(
logger.Aurora.BrightYellow(
fmt.Sprintf(" :house_with_garden: - entrypoint: %s", entrypoint))))
e.Emitter.InfoLog(true,
logger.Aurora.Bold(
logger.Aurora.BrightYellow(" :house_with_garden: - "+command)))
// Convert envs to array list
elist := os.Environ()
for k, v := range envs {
elist = append(elist, k+"="+v)
}
if e.ConfigDir != "" {
elist = append(elist, fmt.Sprintf("LXD_CONF=%s", e.ConfigDir))
}
hostCommand.Stdout = outBuffer
hostCommand.Stderr = errBuffer
hostCommand.Env = elist
err := hostCommand.Start()
if err != nil {
logger.Error("Error on start command: " + err.Error())
return 1, err
}
err = hostCommand.Wait()
if err != nil {
logger.Error("Error on waiting command: " + err.Error())
return 1, err
}
ans = hostCommand.ProcessState.ExitCode()
logger.DebugC(logger.Aurora.Bold(
logger.Aurora.BrightYellow(
fmt.Sprintf(" :house_with_garden: Exiting [%d]", ans))))
return ans, nil
}
func (e *LxdCExecutor) RunHostCommand(command string, envs map[string]string, entryPoint []string) (int, error) {
var outBuffer, errBuffer bytes.Buffer
logger := log.GetDefaultLogger()
res, err := e.RunHostCommandWithOutput(command, envs,
helpers.NewNopCloseWriter(&outBuffer), helpers.NewNopCloseWriter(&errBuffer),
entryPoint)
if e.ShowCmdsOutput && len(outBuffer.String()) > 0 {
e.Emitter.InfoLog(false,
logger.Aurora.Bold(
logger.Aurora.BrightYellow(
fmt.Sprintf(">>> [stdout]\n%s", outBuffer.String()))))
}
if e.ShowCmdsOutput && len(errBuffer.String()) > 0 {
e.Emitter.InfoLog(false,
logger.Aurora.Bold(
logger.Aurora.BrightRed(
fmt.Sprintf(">>> [stderr]\n%s", errBuffer.String()))))
}
return res, err
}
func (e *LxdCExecutor) RunHostCommandWithOutput4Var(command, outVar, errVar string, envs *map[string]string, entryPoint []string) (int, error) {
var outBuffer, errBuffer bytes.Buffer
logger := log.GetDefaultLogger()
res, err := e.RunHostCommandWithOutput(command, *envs,
helpers.NewNopCloseWriter(&outBuffer), helpers.NewNopCloseWriter(&errBuffer),
entryPoint)
if err == nil {
if e.ShowCmdsOutput && len(outBuffer.String()) > 0 {
e.Emitter.InfoLog(false,
logger.Aurora.Bold(
logger.Aurora.BrightYellow(
fmt.Sprintf(">>> [stdout]\n%s", outBuffer.String()))))
}
if e.ShowCmdsOutput && len(errBuffer.String()) > 0 {
e.Emitter.InfoLog(false,
logger.Aurora.Bold(
logger.Aurora.BrightRed(
fmt.Sprintf(">>> [stderr]\n%s", errBuffer.String()))))
}
if outVar != "" {
(*envs)[outVar] = outBuffer.String()
}
if errVar != "" {
(*envs)[errVar] = errBuffer.String()
}
}
return res, err
}