This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbug.go
More file actions
203 lines (161 loc) · 4.29 KB
/
Copy pathbug.go
File metadata and controls
203 lines (161 loc) · 4.29 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
package bug
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"os"
"os/exec"
"time"
"koding/kites/config"
"koding/klient/uploader"
konfig "koding/klientctl/config"
"koding/klientctl/endpoint/machine"
"koding/klientctl/helper"
"koding/klientctl/stream"
"github.com/skratchdot/open-golang/open"
)
//go:generate $GOPATH/bin/go-bindata -mode 420 -modtime 1475345133 -pkg bug -prefix ../../../../../.github/ -o issue.md.go ../../../../../.github/ISSUE_TEMPLATE.md
type BugMetadata struct {
KiteID string `json:"kiteID"`
Konfig *config.Konfig `json:"konfig"`
Version int `json:"version"`
Environment string `json:"environment"`
Files []*machine.UploadedFile `json:"files"`
CreatedAt time.Time `json:"createdAt"`
}
func (bm *BugMetadata) ToFile() *machine.UploadedFile {
p, err := jsonMarshal(bm)
if err != nil {
panic(err)
}
hash := sha1.Sum(p)
return &machine.UploadedFile{
File: "bug/" + hex.EncodeToString(hash[:]) + ".json",
Content: p,
}
}
func Bug(s stream.Streamer) error {
meta := metadata()
resp, err := machine.ListMount(&machine.ListMountOptions{})
if err != nil {
return err
}
for id, mounts := range resp {
for _, m := range mounts {
opts := &machine.InspectMountOptions{
Identifier: string(m.ID),
Sync: true,
Filesystem: true,
}
records, err := machine.InspectMount(opts)
if err != nil {
return err
}
if len(records.Sync) > 0 {
p, err := json.Marshal(records.Sync)
if err != nil {
return err
}
meta.Files = append(meta.Files, &machine.UploadedFile{
File: "sync/" + id + "/" + string(m.ID) + ".json",
Content: p,
})
}
if len(records.Filesystem) > 0 {
p, err := json.Marshal(records.Filesystem)
if err != nil {
return err
}
meta.Files = append(meta.Files, &machine.UploadedFile{
File: "filesystem/" + id + "/" + string(m.ID) + ".json",
Content: p,
})
}
}
}
// Best-effort attempt of uploading system files
// for troubleshooting.
_ = machine.UploadForce(meta.Files...)
report := meta.ToFile()
if err := machine.Upload(report); err != nil {
return err
}
sign := signature(report.URL)
var t string
if u, ok := askScreencast(s); ok {
t = fmt.Sprintf(asciicastBody, u, sign)
} else {
t = fmt.Sprintf(issueBody, sign)
}
if err := open.Start("https://github.com/koding/koding/issues/new?body=" + url.QueryEscape(t)); err != nil {
return err
}
fmt.Fprintln(s.Out(), s)
return nil
}
func metadata() *BugMetadata {
// Strip KiteKey from config.
cfg := *konfig.Konfig
cfg.KiteKey = ""
return &BugMetadata{
KiteID: konfig.Konfig.KiteConfig().Id,
Konfig: &cfg,
Version: konfig.VersionNum(),
Environment: konfig.Environment,
Files: systemFiles(),
CreatedAt: time.Now(),
}
}
func systemFiles() (files []*machine.UploadedFile) {
for _, log := range uploader.LogFiles {
if _, err := os.Stat(log); os.IsNotExist(err) {
continue
}
files = append(files, &machine.UploadedFile{
File: log,
})
}
return files
}
func jsonMarshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func signature(uri string) string {
u, err := url.Parse(uri)
if err != nil {
panic(err)
}
return hex.EncodeToString([]byte(u.Path))
}
func askScreencast(s stream.Streamer) (*url.URL, bool) {
if _, err := exec.LookPath("asciinema"); err != nil {
return nil, false
}
link, err := helper.Fask(s.In(), s.Out(), "Did you record your shell with asciinema? If yes, please provide a URL of the recording [none]: ")
if err != nil {
return nil, false
}
if link == "" {
return nil, false
}
u, err := url.Parse(link)
if err != nil || u.Host != "asciinema.org" {
fmt.Fprintln(s.Err(), "Invalid asciinema link, ignoring.")
return nil, false
}
return u, true
}
const sign = "\n### Signature\n```\n%s\n```\n"
var (
issueBody = string(append(MustAsset("ISSUE_TEMPLATE.md"), []byte(sign)...))
asciicastBody = "<!--- Provide a general summary of the issue in the Title above -->\n\n## Steps to reproduce\n[](%[1]s)\n" + sign
)