-
Notifications
You must be signed in to change notification settings - Fork 107
/
post-script.go
139 lines (113 loc) · 3.59 KB
/
post-script.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
// Copyright 2022 Flant JSC
//
// 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 bootstrap
import (
"errors"
"fmt"
"os"
"time"
"github.com/deckhouse/deckhouse/dhctl/pkg/log"
"github.com/deckhouse/deckhouse/dhctl/pkg/system/ssh"
"github.com/deckhouse/deckhouse/dhctl/pkg/system/ssh/frontend"
"github.com/deckhouse/deckhouse/dhctl/pkg/util/fs"
)
type PostBootstrapScriptExecutor struct {
path string
timeout time.Duration
sshClient *ssh.Client
state *State
}
func NewPostBootstrapScriptExecutor(sshClient *ssh.Client, path string, state *State) *PostBootstrapScriptExecutor {
return &PostBootstrapScriptExecutor{
path: path,
sshClient: sshClient,
state: state,
}
}
func (e *PostBootstrapScriptExecutor) WithTimeout(timeout time.Duration) *PostBootstrapScriptExecutor {
e.timeout = timeout
return e
}
func (e *PostBootstrapScriptExecutor) Execute() error {
return log.Process("bootstrap", "Execute post-bootstrap script", func() error {
var err error
resultToSetState, err := e.run()
if err != nil {
msg := fmt.Sprintf("Post execution script was failed: %v", err)
return errors.New(msg)
}
err = e.state.SavePostBootstrapScriptResult(resultToSetState)
if err != nil {
log.ErrorF("Post bootstrap script result was not saved: %v", err)
}
return nil
})
}
func (e *PostBootstrapScriptExecutor) run() (string, error) {
outputFile := fs.RandomNumberSuffix("/tmp/post-bootstrap-script-output")
envs := map[string]string{
"OUTPUT": outputFile,
}
createOUtFileCmd := fmt.Sprintf("touch %s && chmod 644 %s", outputFile, outputFile)
err := frontend.NewCommand(e.sshClient.Settings, createOUtFileCmd).
Sudo().
WithStderrHandler(nil).
WithStdoutHandler(nil).
Run()
if err != nil {
return "", fmt.Errorf("Cannot create output file for script: %v", err)
}
defer func() {
// remove out file on server because it can contain non-safe information
err = frontend.NewCommand(e.sshClient.Settings, fmt.Sprintf("rm %s", outputFile)).
Sudo().
WithStderrHandler(nil).
WithStdoutHandler(nil).
Run()
}()
cmd := e.sshClient.UploadScript(e.path).
WithTimeout(e.timeout).
WithStdoutHandler(func(s string) {
log.InfoLn(s)
}).
WithEnvs(envs).
Sudo()
_, err = cmd.Execute()
if err != nil {
return "", fmt.Errorf("Running %s done with error: %w", e.path, err)
}
content, err := e.sshClient.File().DownloadBytes(outputFile)
if err != nil {
return "", fmt.Errorf("Cannot get output from remote file %s: %w", e.path, err)
}
if err != nil {
log.WarnLn("Post bootstrap output file '%s' did not remove from server", outputFile)
}
return string(content), nil
}
func ValidateScriptFile(path string) error {
info, err := os.Stat(path)
if err != nil {
return fmt.Errorf("Cannot get stats for path %s: %v", path, err)
}
mode := info.Mode()
if !mode.IsRegular() {
return fmt.Errorf("Post bootstrap script should be regular file")
}
perm := info.Mode().Perm()
if perm&0111 != 0111 || perm&0444 != 0444 {
return fmt.Errorf("Post bootstrap script should be readable and executable for user group and other (-r-xr-xr-x)")
}
return nil
}