-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
336 lines (305 loc) · 8.4 KB
/
main.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//usr/bin/env go run $0 "$@"; exit
// Nexus Continuous Delivery Victor
// Cleans all but the latest release for a GAV
// return codes:
// -1: number of artifacts found exceeds expected result size
package main
import (
"bufio"
"bytes"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
)
const (
defaultProtocol = "http"
defaultServer = "localhost"
defaultPort = "8081"
defaultContextroot = "nexus/"
defaultUsername = "admin"
defaultPassword = "admin123"
defaultArtifact = ""
defaultVersion = ""
defaultRepository = "releases"
defaultCount = 200
)
// NexusInstance holds coordinates of a Nexus installation
type NexusInstance struct {
Protocol string
Server string
Port string
Contextroot string
Username string
Password string
}
// NexusRepository holds coordinates of a Nexus repository
type NexusRepository struct {
NexusInstance
RepositoryID string
}
func baseUrl(repo NexusRepository) *url.URL {
s := fmt.Sprintf("%s://%s:%s/%s",
repo.Protocol, repo.Server, repo.Port, repo.Contextroot)
u, err := url.Parse(s)
if err != nil {
log.Fatalf("cannot parse URL %s: %v\n", s, err)
}
return u
}
// Fqa holds coordinates to a fully qualified artifact
type Fqa struct {
NexusRepository
Gav
}
type searchNGResponse struct {
Count int `xml:"count"`
From int `xml:"from"`
TotalCount int `xml:"totalCount"`
TooManyResults bool `xml:"tooManyResults"`
Artifacts []Gav `xml:"data>artifact"`
}
// Gav is the standard Maven coordinates: group, artifact, version, extended by
// a custom Nexus field for the latest release version
type Gav struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
LatestRelease string `xml:"latestRelease"`
}
// ConciseNotation returns group:artifact:version
func (a Gav) ConciseNotation() string {
return fmt.Sprintf("%v:%v:%v",
a.GroupID,
a.ArtifactID,
a.Version)
}
// DefaultLayout supports group, artifact and version in GAVs.
// For complete GAV support including extension and classifier, the whole
// XML result has to be parsed. This is not necessary for our use case.
func (a Gav) DefaultLayout() string {
return fmt.Sprintf("%s/%s/%s",
strings.Replace(a.GroupID, ".", "/", -1),
a.ArtifactID,
a.Version)
}
func main() {
flag.Usage = usage
var repo NexusRepository
flag.StringVar(&repo.Protocol, "protocol", defaultProtocol,
"Nexus protocol (http/https)")
flag.StringVar(&repo.Server, "server", defaultServer,
"Nexus server name")
flag.StringVar(&repo.Port, "port", defaultPort, "Nexus port")
flag.StringVar(&repo.Contextroot, "contextroot", defaultContextroot,
"Nexus context root")
flag.StringVar(&repo.Username, "username", defaultUsername,
"Nexus credentials")
flag.StringVar(&repo.Password, "password", defaultPassword,
"Nexus credentials")
flag.StringVar(&repo.RepositoryID, "repository", defaultRepository,
"Nexus repository ID, empty for global search")
// Parse commandline parameter
count := flag.Int("count", defaultCount,
"Nexus count parameter in REST interface")
delete := flag.Bool("delete", false,
"delete search results (otherwise only display them)")
keepLatest := flag.Bool("keepLatest", true, "keep lastest version")
expect := flag.Int("expect", 1, "expected number of results")
throttle := flag.Int("throttle", 1, "throttle number of actions")
artifact := flag.String("artifact", defaultArtifact,
"Limit search to an artifact")
version := flag.String("version", defaultVersion,
"Limit search to specific version (may include wildcards)")
flag.Parse()
// Need at least one group, or exactly one @filename
if len(flag.Args()) == 0 {
flag.Usage()
}
groups := resolveGroups()
// If multiple instances are running, avoid syncing on generating Maven
// metadata
rand.Seed(time.Now().UnixNano())
Shuffle(groups)
actions := 0
truncated := false
perf := []int{}
for _, group := range groups {
gav := Gav{ArtifactID: *artifact,
GroupID: group,
Version: *version}
log.Printf("processing %+v\n", gav)
found := search(Fqa{repo, gav}, *count)
n := len(found.Artifacts)
// Display on stdout
for i := 0; i < n; i++ {
fmt.Printf("%v\n", found.Artifacts[i].ConciseNotation())
}
log.Printf("search returned %v artifacts out of %v\n",
n, found.TotalCount)
// Check expected result size
if n > *expect {
msg := "Found %v artifacts but expect is %v, aborting\n"
fmt.Fprintf(os.Stderr, msg, n, *expect)
os.Exit(1)
}
for i := 0; i < n; i++ {
a := found.Artifacts[i]
if *keepLatest && a.Version == a.LatestRelease {
log.Printf("keeping latest version %v for %v\n",
a.LatestRelease, a.ConciseNotation())
continue
}
// Check if throttle limit reached
if actions >= *throttle {
s := "throttle=%v reached, exiting"
log.Printf(s, actions)
os.Exit(0)
}
if *delete {
now := time.Now()
deleted := deleteGav(Fqa{repo, a})
if deleted {
actions++
elapsed := time.Since(now)
// normalize ns (1e-9) to ms (1e-3)
ms := int(elapsed.Nanoseconds() / 1e6)
perf = append(perf, ms)
log.Printf("[PERF] %v"+
", average: %v ms"+
", median: %v ms"+
"\n",
elapsed,
ArithmeticMean(perf),
Median(perf))
}
}
}
// Sometimes Nexus returns 200 (maximum) out of 473, sometimes
// 73 out of 247 items for whatever reason
if found.TooManyResults || (n != found.TotalCount) {
truncated = true
}
}
if truncated {
log.Printf("Truncated batch, consider re-running")
}
}
// delete removes artifacts via REST from Nexus
// DELETE on http://${server}:${port}/service/local/repositories/${repo}/
// content/${group}/${artifact}/${version}/${artifact}-${version}.jar
func deleteGav(fqa Fqa) bool {
u := fmt.Sprintf("%sservice/local/repositories/%s/content/%s",
baseUrl(fqa.NexusRepository).String(),
fqa.RepositoryID, fqa.Gav.DefaultLayout())
log.Printf("HTTP DELETE %v", u)
req, err := http.NewRequest("DELETE", u, nil)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(fqa.NexusInstance.Username, fqa.NexusInstance.Password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
rc := resp.StatusCode
if rc == 204 {
return true
}
if rc == 404 {
// Nexus is bent out of shape
log.Printf("%s is already gone", fqa.Gav.ConciseNotation())
return false
}
log.Fatalf("Expected HTTP status code 204 but got %v", rc)
var mu bool
return mu
}
func groupIsFile(group string) bool {
return strings.HasPrefix(group, "@")
}
// read groups from file
func read(filename string) (groups []string, err error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(bytes.NewReader(buf))
for scanner.Scan() {
groups = append(groups, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return
}
func resolveGroups() []string {
if groupIsFile(flag.Arg(0)) {
if len(flag.Args()) > 1 {
flag.Usage()
}
groups, err := read(flag.Arg(0)[1:])
if err != nil {
log.Fatal(err)
}
return groups
}
return flag.Args()
}
// search executes a REST search against Nexus
func search(fqa Fqa, count int) searchNGResponse {
var sb strings.Builder
sb.WriteString(baseUrl(fqa.NexusRepository).String())
sb.WriteString(
fmt.Sprintf("service/local/lucene/search?g=%s&count=%d",
fqa.Gav.GroupID, count))
if fqa.RepositoryID != "" {
sb.WriteString(
fmt.Sprintf("&repositoryId=%s",
fqa.RepositoryID))
}
if fqa.Gav.ArtifactID != "" {
sb.WriteString(fmt.Sprintf("&a=%s", fqa.Gav.ArtifactID))
}
if fqa.Gav.Version != "" {
sb.WriteString(fmt.Sprintf("&v=%s", fqa.Gav.Version))
}
u := sb.String()
response, err := http.Get(u)
if err != nil {
log.Fatalf("Cannot read url %v: %v\n", u, err)
}
log.Printf("%v returns HTTP status code %v\n",
u, response.StatusCode)
if response.StatusCode != 200 {
log.Fatalf("Expected status 200 but got %v\n",
response.StatusCode)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var found searchNGResponse
err = xml.Unmarshal(body, &found)
if err != nil {
log.Fatal(err)
}
return found
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [group...|@filename]\n",
path.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "\t@ references a file with groups separated "+
" by newline\n")
flag.PrintDefaults()
}