/
step_prepare.go
151 lines (136 loc) · 4.06 KB
/
step_prepare.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
// Copyright (c) 2021 Terminus, Inc.
//
// 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 actionagent
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/pkg/filehelper"
)
const (
EnvStdErrRegexpList = "ACTIONAGENT_STDERR_REGEXP_LIST"
)
// 对于 custom action,需要将 commands 转换为 script 来执行
func (agent *Agent) prepare() {
// 1. create contextDir/workDir/dir(metaFile)/uploadDir/tempTarUploadDir
if err := os.MkdirAll(agent.EasyUse.ContainerContext, 0755); err != nil {
agent.AppendError(err)
}
if err := os.MkdirAll(agent.EasyUse.ContainerWd, 0755); err != nil {
agent.AppendError(err)
}
if err := os.MkdirAll(filepath.Dir(agent.EasyUse.ContainerMetaFile), 0755); err != nil {
agent.AppendError(err)
}
if agent.EasyUse.ContainerUploadDir != "" {
if err := os.MkdirAll(agent.EasyUse.ContainerUploadDir, 0755); err != nil {
agent.AppendError(err)
}
}
if err := os.Mkdir(agent.EasyUse.ContainerTempTarUploadDir, 0755); err != nil {
agent.AppendError(err)
}
// {
// "name":"a.cert",
// "value":"/.pipeline/container/context/.cms/dice_files/a.cert",
// "labels":{
// "diceFileUUID":"d31b0b31e85c467c8a54e4a9786363b7"
// }
// }
for _, f := range agent.Arg.Context.CmsDiceFiles {
if err := os.MkdirAll(filepath.Dir(f.Value), 0755); err != nil {
agent.AppendError(err)
}
}
// 2. create custom script
if len(agent.Arg.Commands) > 0 {
if err := agent.setupScript(); err != nil {
agent.AppendError(err)
return
}
}
// 3. compatible when_sigterm -> when_sig_15
const oldSigTERMScript = "/opt/action/when_sigterm"
if err := filehelper.CheckExist(oldSigTERMScript, false); err == nil {
if err := filehelper.Copy(oldSigTERMScript, getSigScriptPath(syscall.SIGTERM)); err != nil {
agent.AppendError(err)
return
}
}
// 4. multiWriter of stdout/stderr
if f, err := filehelper.CreateFile3(agent.EasyUse.RunMultiStdoutFilePath, bytes.NewBufferString(""), 0644); err != nil {
logrus.Printf("failed to create multi stdout, err: %v\n", err)
} else {
agent.EasyUse.RunMultiStdout = f
}
if f, err := filehelper.CreateFile3(agent.EasyUse.RunMultiStderrFilePath, bytes.NewBufferString(""), 0644); err != nil {
logrus.Printf("failed to create multi stderr, err: %v\n", err)
} else {
agent.EasyUse.RunMultiStderr = f
}
// 5. set stderr regexp list
envStdErrRegexpStr := os.Getenv(EnvStdErrRegexpList)
regexpStrList := []string{}
if err := json.Unmarshal([]byte(envStdErrRegexpStr), ®expStrList); err != nil {
agent.AppendError(err)
return
}
for i := range regexpStrList {
reg, err := regexp.Compile(regexpStrList[i])
if err != nil {
agent.AppendError(err)
} else {
agent.StdErrRegexpList = append(agent.StdErrRegexpList, reg)
}
}
}
func (agent *Agent) setupScript() error {
var buf bytes.Buffer
for _, command := range agent.Arg.Commands {
escaped := fmt.Sprintf("%q", command)
escaped = strings.Replace(escaped, `$`, `\$`, -1)
buf.WriteString(fmt.Sprintf(
traceScript,
escaped,
command,
))
}
script := fmt.Sprintf(
buildScript,
buf.String(),
)
if err := filehelper.CreateFile(agent.EasyUse.RunScript, script, 0777); err != nil {
return err
}
return nil
}
// buildScript is a helper script which add a shebang
// to the generated script.
const buildScript = `#!/bin/sh
set -e
%s
`
// traceScript is a helper script which is added to the
// generated script to trace each command.
const traceScript = `
echo + %s
%s || ((echo "- FAIL! exit code: $?") && false)
echo
`