forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_server.go
More file actions
179 lines (145 loc) · 4.84 KB
/
debug_server.go
File metadata and controls
179 lines (145 loc) · 4.84 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
package main
import (
"archive/zip"
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"github.com/sourcegraph/sourcegraph/lib/errors"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)
func init() {
usage := `
'src debug server' invokes docker cli diagnostic commands targeting a Sourcegraph server container,
and writes an archive file from their returns.
Usage:
src debug server [command options]
Flags:
-o Specify the name of the output zip archive.
-no-config Don't include Sourcegraph configuration json.
Examples:
$ src debug server -c foo -o debug.zip
$ src -v debug server --no-configs -c ViktorVaughn -o foo.zip
`
flagSet := flag.NewFlagSet("server", flag.ExitOnError)
var base string
var container string
var excludeConfigs bool
flagSet.StringVar(&base, "o", "debug.zip", "The name of the output zip archive")
flagSet.StringVar(&container, "c", "", "The container to target")
flagSet.BoolVar(&excludeConfigs, "no-configs", false, "If true, exclude Sourcegraph configuration files. Defaults to false.")
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
//process -o flag to get zipfile and base directory names, make sure container is targeted
if base == "" {
return fmt.Errorf("empty -o flag")
}
if container == "" {
return fmt.Errorf("empty -c flag, specify a container: src debug server -c foo")
}
base, baseDir := processBaseDir(base)
// init context
ctx := context.Background()
// open pipe to output file
out, err := os.OpenFile(base, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0666)
if err != nil {
return errors.Wrapf(err, "failed to open file %q", base)
}
defer out.Close()
// init zip writer
zw := zip.NewWriter(out)
defer zw.Close()
// Safety check user knows what they are targeting with this debug command
log.Printf("This command will archive docker-cli data for container: %s\n SRC_ENDPOINT: %s\n Output filename: %s", container, cfg.Endpoint, base)
if verified, _ := verify("Do you want to start writing to an archive?"); !verified {
return nil
}
err = archiveServ(ctx, zw, *verbose, !excludeConfigs, container, baseDir)
if err != nil {
return err
}
return nil
}
debugCommands = append(debugCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: func() {
fmt.Println(usage)
},
})
}
// Runs common docker cli commands on a single container
func archiveServ(ctx context.Context, zw *zip.Writer, verbose, archiveConfigs bool, container, baseDir string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// setup channel for slice of archive function outputs
ch := make(chan *archiveFile)
g, ctx := errgroup.WithContext(ctx)
semaphore := semaphore.NewWeighted(int64(runtime.GOMAXPROCS(0)))
run := func(f func() *archiveFile) {
g.Go(func() error {
if err := semaphore.Acquire(ctx, 1); err != nil {
return err
}
defer semaphore.Release(1)
if file := f(); file != nil {
ch <- file
}
return nil
})
}
// start goroutine to run docker ps -o wide
run(func() *archiveFile { return getServLog(ctx, container, baseDir) })
// start goroutine to run docker ps -o wide
run(func() *archiveFile { return getServInspect(ctx, container, baseDir) })
// start goroutine to run docker ps -o wide
run(func() *archiveFile { return getServTop(ctx, container, baseDir) })
// start goroutine to get configs
if archiveConfigs {
run(func() *archiveFile { return getExternalServicesConfig(ctx, baseDir) })
run(func() *archiveFile { return getSiteConfig(ctx, baseDir) })
}
// close channel when wait group goroutines have completed
go func() {
if err := g.Wait(); err != nil {
fmt.Printf("archiveServ failed to open wait group: %s\n", err)
os.Exit(1)
}
close(ch)
}()
// Read binaries from channel and write to archive on host machine
if err := writeChannelContentsToZip(zw, ch, verbose); err != nil {
return errors.Wrap(err, "failed to write archives from channel")
}
return nil
}
// runs archiveFileFromCommand with args container logs $CONTAINER
func getServLog(ctx context.Context, container, baseDir string) *archiveFile {
return archiveFileFromCommand(
ctx,
filepath.Join(baseDir, fmt.Sprintf("%s.log", container)),
"docker", "container", "logs", container,
)
}
// runs archiveFileFromCommand with args container inspect $CONTAINER
func getServInspect(ctx context.Context, container, baseDir string) *archiveFile {
return archiveFileFromCommand(
ctx,
filepath.Join(baseDir, fmt.Sprintf("inspect-%s.txt", container)),
"docker", "container", "inspect", container,
)
}
// runs archiveFileFromCommand with args top $CONTAINER
func getServTop(ctx context.Context, container, baseDir string) *archiveFile {
return archiveFileFromCommand(
ctx,
filepath.Join(baseDir, fmt.Sprintf("top-%s.txt", container)),
"docker", "top", container,
)
}