-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.go
233 lines (205 loc) · 6.25 KB
/
common.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2018-2019, CS Systemes d'Information, http://www.c-s.fr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nfs
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"text/template"
"github.com/CS-SI/SafeScale/lib/utils/scerr"
"github.com/CS-SI/SafeScale/lib/utils/temporal"
log "github.com/sirupsen/logrus"
"github.com/CS-SI/SafeScale/lib/system"
"github.com/CS-SI/SafeScale/lib/utils"
"github.com/CS-SI/SafeScale/lib/utils/retry"
rice "github.com/GeertJohan/go.rice"
)
//go:generate rice embed-go
//templateProvider is the instance of TemplateProvider used by package nfs
var tmplBox *rice.Box
//getTemplateProvider returns the instance of TemplateProvider
func getTemplateBox() (*rice.Box, error) {
if tmplBox == nil {
var err error
tmplBox, err = rice.FindBox("../nfs/scripts")
if err != nil {
return nil, err
}
}
return tmplBox, nil
}
// executeScript executes a script template with parameters in data map
// Returns retcode, stdout, stderr, error
// If error == nil && retcode != 0, the script ran but failed.
func executeScript(sshconfig system.SSHConfig, name string, data map[string]interface{}) (int, string, string, error) {
bashLibrary, err := system.GetBashLibrary()
if err != nil {
return 255, "", "", err
}
data["reserved_BashLibrary"] = bashLibrary
scriptHeader := "set -u -o pipefail"
if suffixCandidate := os.Getenv("SAFESCALE_SCRIPTS_FAIL_FAST"); suffixCandidate != "" {
if strings.EqualFold("True", strings.TrimSpace(suffixCandidate)) {
scriptHeader = "set -Eeuxo pipefail"
}
if strings.EqualFold("1", strings.TrimSpace(suffixCandidate)) {
scriptHeader = "set -Eeuxo pipefail"
}
}
data["BashHeader"] = scriptHeader
tmplBox, err := getTemplateBox()
if err != nil {
return 255, "", "", err
}
// get file content as string
tmplContent, err := tmplBox.String(name)
if err != nil {
return 255, "", "", err
}
// Prepare the template for execution
tmplPrepared, err := template.New(name).Parse(tmplContent)
if err != nil {
return 255, "", "", err
}
var buffer bytes.Buffer
if err := tmplPrepared.Execute(&buffer, data); err != nil {
return 255, "", "", fmt.Errorf("failed to execute template: %s", err.Error())
}
content := buffer.String()
// Copy script to remote host with retries if needed
f, err := system.CreateTempFileFromString(content, 0600)
if err != nil {
return 255, "", "", fmt.Errorf("failed to create temporary file: %s", err.Error())
}
filename := "/opt/safescale/var/tmp/" + name
retryErr := retry.WhileUnsuccessfulDelay5Seconds(
func() error {
retcode, stdout, stderr, err := sshconfig.Copy(filename, f.Name(), true)
if err != nil {
return fmt.Errorf("ssh operation failed: %s", err.Error())
}
if retcode != 0 {
return fmt.Errorf("script copy failed: %s, %s", stdout, stderr)
}
return nil
},
temporal.GetHostTimeout(),
)
if retryErr != nil {
return 255, "", "", fmt.Errorf("failed to copy script to remote host: %s", retryErr.Error())
}
k, uperr := sshconfig.Command("which scp")
if uperr != nil && k != nil {
_, uptext, _, kerr := k.RunWithTimeout(nil, temporal.GetBigDelay())
if kerr == nil {
connected := strings.Contains(uptext, "/scp")
if !connected {
log.Warn("SSH problem ?")
}
}
}
k, uperr = sshconfig.SudoCommand("which scp")
if uperr != nil && k != nil {
_, uptext, _, kerr := k.RunWithTimeout(nil, temporal.GetBigDelay())
if kerr == nil {
connected := strings.Contains(uptext, "/scp")
if !connected {
log.Warn("SUDO problem ?")
}
}
}
nerr := utils.LazyRemove(f.Name())
if nerr != nil {
log.Warnf("Error deleting file: %v", nerr)
}
// Execute script on remote host with retries if needed
var (
cmd, stdout, stderr string
retcode int
)
// cmd = fmt.Sprintf("chmod u+rwx %s; bash -c %s; rc=$?; if [[ rc -eq 0 ]]; then rm -f %s; fi; exit $rc", filename, filename, filename)
cmd = fmt.Sprintf("chmod u+rwx %s; bash -c %s; rc=$?; exit $rc", filename, filename)
retryErr = retry.Action(
func() error {
stdout = ""
stderr = ""
retcode = 0
sshCmd, err := sshconfig.SudoCommand(cmd)
if err != nil {
return err
}
cmdResult, err := sshCmd.Output()
stdout = string(cmdResult)
stderr = ""
retcode = 0
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
if status, ok := ee.Sys().(syscall.WaitStatus); ok {
retcode = status.ExitStatus()
}
stderr = string(ee.Stderr)
}
}
return err
},
retry.PrevailDone(retry.UnsuccessfulWhereRetcode255(), retry.Timeout(temporal.GetContextTimeout())),
retry.Constant(temporal.GetDefaultDelay()),
nil, nil, nil,
)
if retryErr != nil {
switch retryErr.(type) {
case retry.ErrTimeout:
log.Errorf("Timeout running remote script '%s'", name)
return 255, stdout, stderr, retryErr
default:
return 255, stdout, stderr, retryErr
}
}
/*
k, uperr = sshconfig.SudoCommand("ping -c4 google.com")
if uperr != nil {
log.Warn("Network problem...")
} else {
_, uptext, _, kerr := k.Run()
if kerr == nil {
log.Warnf("Network working !!: %s", uptext)
}
}
*/
return retcode, stdout, stderr, err
}
func handleExecuteScriptReturn(retcode int, stdout string, stderr string, err error, msg string) error {
if err != nil {
log.Debugf("Standard output: [%s]", stdout)
log.Debugf("Standard error: [%s]", stderr)
// TODO Simplification of error message
collected := ""
errLines := strings.Split(stderr, "\n")
for _, errline := range errLines {
if strings.Contains(errline, "An error occurred in line") {
collected += errline + ";"
}
}
return scerr.Wrap(err, fmt.Sprintf("%s: std error [%s]", msg, collected))
}
if retcode != 0 {
return fmt.Errorf("%s: Errorcode [%d], std error [%s], std output [%s]", msg, retcode, stderr, stdout)
}
return nil
}