-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathcommand.go
More file actions
223 lines (200 loc) · 5.71 KB
/
Copy pathcommand.go
File metadata and controls
223 lines (200 loc) · 5.71 KB
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 apmservertest
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
)
// TODO(axw): add support for building/running the OSS apm-server.
// ServerCommand returns a ServerCmd (wrapping os/exec) for running
// apm-server with args.
func ServerCommand(ctx context.Context, subcommand string, args ...string) *ServerCmd {
binary, buildErr := BuildServerBinary(runtime.GOOS, runtime.GOARCH)
if buildErr != nil {
// Dummy command; Start etc. will return the build error.
binary = "/usr/bin/false"
}
args = append([]string{subcommand}, args...)
cmd := exec.CommandContext(ctx, binary, args...)
cmd.SysProcAttr = serverCommandSysProcAttr
return &ServerCmd{
Cmd: cmd,
buildError: buildErr,
}
}
// ServerCmd wraps an os/exec.Cmd, taking care of building apm-server
// and cleaning up on close.
type ServerCmd struct {
*exec.Cmd
buildError error
tempdir string
}
// Run runs the apm-server command, and waits for it to exit.
func (c *ServerCmd) Run() error {
if err := c.Start(); err != nil {
return err
}
return c.Wait()
}
// Output runs the apm-server command, waiting for it to exit
// and returning its stdout.
func (c *ServerCmd) Output() ([]byte, error) {
if err := c.prestart(); err != nil {
return nil, err
}
defer c.cleanup()
return c.Cmd.Output()
}
// CombinedOutput runs the apm-server command, waiting for it to exit
// and returning its combined stdout/stderr.
func (c *ServerCmd) CombinedOutput() ([]byte, error) {
if err := c.prestart(); err != nil {
return nil, err
}
defer c.cleanup()
return c.Cmd.CombinedOutput()
}
// Start starts the apm-server command, and returns immediately.
func (c *ServerCmd) Start() error {
if err := c.prestart(); err != nil {
return err
}
if err := c.Cmd.Start(); err != nil {
c.cleanup()
return err
}
return nil
}
// Wait waits for the previously started apm-server command to exit.
func (c *ServerCmd) Wait() error {
defer c.cleanup()
return c.Cmd.Wait()
}
// InterruptProcess sends an interrupt signal
func (c *ServerCmd) InterruptProcess() error {
return interruptProcess(c.Process)
}
func (c *ServerCmd) prestart() error {
if c.buildError != nil {
return c.buildError
}
if c.Dir == "" {
if err := c.createTempDir(); err != nil {
return err
}
}
return nil
}
func (c *ServerCmd) createTempDir() error {
tempdir, err := os.MkdirTemp("", "apm-server-systemtest")
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(tempdir, "apm-server.yml"), nil, 0644); err != nil {
os.RemoveAll(tempdir)
return err
}
// Symlink ingest/pipeline/definition.json into the temporary directory.
pipelineDir := filepath.Join(tempdir, "ingest", "pipeline")
if err := os.MkdirAll(pipelineDir, 0755); err != nil {
os.RemoveAll(tempdir)
return err
}
pipelineDefinitionFile := filepath.Join(filepath.Dir(c.Cmd.Path), "ingest", "pipeline", "definition.json")
pipelineDefinitionSymlink := filepath.Join(pipelineDir, "definition.json")
if err := os.Symlink(pipelineDefinitionFile, pipelineDefinitionSymlink); err != nil {
if !os.IsExist(err) {
os.RemoveAll(tempdir)
return err
}
}
c.tempdir = tempdir
c.Dir = tempdir
return nil
}
func (c *ServerCmd) cleanup() {
if c.tempdir != "" {
os.RemoveAll(c.tempdir)
}
}
// BuildServerBinary builds the apm-server binary for the given GOOS
// and GOARCH, returning its absolute path.
func BuildServerBinary(goos, goarch string) (string, error) {
apmServerBinaryMu.Lock()
defer apmServerBinaryMu.Unlock()
if binary := apmServerBinary[goos]; binary != "" {
return binary, nil
}
repoRoot, err := getRepoRoot()
if err != nil {
return "", err
}
relpath := filepath.Join("build", fmt.Sprintf("apm-server-%s-%s", goos, goarch))
if goos == "windows" {
relpath += ".exe"
}
abspath := filepath.Join(repoRoot, relpath)
log.Println("Building apm-server...")
cmd := exec.Command("make", relpath)
cmd.Dir = repoRoot
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}
log.Println("Built", abspath)
apmServerBinary[goos] = abspath
return abspath, nil
}
func getRepoRoot() (string, error) {
repoRootMu.Lock()
defer repoRootMu.Unlock()
if repoRoot != "" {
return repoRoot, nil
}
// Build apm-server binary in the repo root.
output, err := exec.Command("go", "list", "-m", "-f={{.Dir}}/..").Output()
if err != nil {
return "", err
}
// When go workspaces is enabled then go list lists all the module paths in
// the go workspace. This is hack to ensure we take what we required.
// TODO (lahsivjar): Better way to find repo root?
allPaths := strings.Split(strings.TrimSpace(string(output)), "\n")
var targetPath string
for _, path := range allPaths {
if strings.HasSuffix(path, "systemtest/..") {
targetPath = path
break
}
}
repoRoot = filepath.Clean(targetPath)
return repoRoot, nil
}
var (
apmServerBinaryMu sync.Mutex
apmServerBinary = make(map[string]string)
repoRootMu sync.Mutex
repoRoot string
)