Skip to content

Commit 700b158

Browse files
committed
updating to latest dep versions, removing exported constants, and adding ability to specify port and acl token
1 parent b134a7b commit 700b158

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
/.idea
33
/*.iml
4-
/consul-zombie
4+
/consul-zombie
5+
/zombie

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# consul-zombie
2-
Find and kill consul zombies.
2+
Find and kill consul zombie services.
33

44
If you fail to deregister consul services and health checks it can be nice to
55
have a tool to prune the dead from the living. This is especially true if you
66
are deploying in a container environment where every service gets a fresh
77
and unique ID.
88

9-
Install to your `$GOPATH` by cloning the repo and running `./build.sh`
9+
Install by cloning the repo and running `./build.sh`
1010

1111
There are three invocations of this tool:
1212

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/myENA/consul-zombie
33
go 1.13
44

55
require (
6-
github.com/hashicorp/consul/api v1.2.0
7-
github.com/mattn/go-runewidth v0.0.4 // indirect
8-
github.com/olekukonko/tablewriter v0.0.1
6+
github.com/hashicorp/consul/api v1.3.0
7+
github.com/olekukonko/tablewriter v0.0.4
98
)

go.sum

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
99
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
1010
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw=
1111
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
12-
github.com/hashicorp/consul/api v1.2.0 h1:oPsuzLp2uk7I7rojPKuncWbZ+m5TMoD4Ivs+2Rkeh4Y=
13-
github.com/hashicorp/consul/api v1.2.0/go.mod h1:1SIkFYi2ZTXUE5Kgt179+4hH33djo11+0Eo2XgTAtkw=
14-
github.com/hashicorp/consul/sdk v0.2.0 h1:GWFYFmry/k4b1hEoy7kSkmU8e30GAyI4VZHk0fRxeL4=
15-
github.com/hashicorp/consul/sdk v0.2.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
12+
github.com/hashicorp/consul/api v1.3.0 h1:HXNYlRkkM/t+Y/Yhxtwcy02dlYwIaoxzvxPnS+cqy78=
13+
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
14+
github.com/hashicorp/consul/sdk v0.3.0 h1:UOxjlb4xVNF93jak1mzzoBatyFju9nrkxpVwIp/QqxQ=
15+
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
1616
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
1717
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
1818
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
@@ -42,8 +42,8 @@ github.com/hashicorp/serf v0.8.2 h1:YZ7UKsJv+hKjqGVUUbtE3HNj79Eln2oQ75tniF6iPt0=
4242
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
4343
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
4444
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
45-
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
46-
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
45+
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
46+
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
4747
github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA=
4848
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
4949
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -56,8 +56,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
5656
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
5757
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
5858
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
59-
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
60-
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
59+
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
60+
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
6161
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs=
6262
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
6363
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=

utilities.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import (
88
)
99

1010
// get a client handle for a specified address (or the local agent if "")
11-
func getClient(address string) (*api.Client, error) {
11+
func getClient(address, token string) (*api.Client, error) {
1212
config := api.DefaultConfig()
1313
if address != "" {
1414
config.Address = address
1515
}
16+
if token != "" {
17+
config.Token = token
18+
}
1619
return api.NewClient(config)
1720
}
1821

1922
// get a list of all services, limit to those matching the search criteria
20-
func getList(serviceString string, tag string) []*api.ServiceEntry {
21-
client, err := getClient("")
23+
func getList(serviceString, token, tag string) []*api.ServiceEntry {
24+
client, err := getClient("", token)
2225
if err != nil {
2326
log.Fatalf("Unable to get a consul client connection: %s\n", err)
2427
}

zombie.go

+21-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// this is the default port for talking to remote consul agents
15-
const ConsulPort = 8500
15+
const defaultPort = 8500
1616

1717
type verbosityLevel uint8
1818

@@ -25,10 +25,10 @@ func (v verbosityLevel) allows(other verbosityLevel) bool {
2525
}
2626

2727
const (
28-
V0 verbosityLevel = iota
29-
V1
30-
V2
31-
V3
28+
verbosityLevel0 verbosityLevel = iota
29+
verbosityLevel1
30+
verbosityLevel2
31+
verbosityLevel3
3232
)
3333

3434
func usage(code int) {
@@ -42,18 +42,23 @@ func main() {
4242
serviceString := fs.String("s", "", "Limit search by service address (regexp)")
4343
tag := fs.String("t", "", "Limit search by tag")
4444
force := fs.Bool("f", false, "Force killing of all matches, including healthy services")
45+
port := fs.Int("port", defaultPort, "Port to use when connecting to remote agents")
46+
token := fs.String("token", "", "Token to use when connecting to remote agents")
4547
v1 := fs.Bool("v", false, "Verbose")
4648
v2 := fs.Bool("vv", false, "Increased Verbosity")
4749
v3 := fs.Bool("vvv", false, "Super Verbosity")
48-
fs.Parse(os.Args[1:])
50+
if err := fs.Parse(os.Args[1:]); err != nil {
51+
log.Printf("Error parsing args: %s", err)
52+
os.Exit(1)
53+
}
4954

5055
var verbosity verbosityLevel
5156
if *v3 {
52-
verbosity = V3
57+
verbosity = verbosityLevel3
5358
} else if *v2 {
54-
verbosity = V2
59+
verbosity = verbosityLevel2
5560
} else if *v1 {
56-
verbosity = V1
61+
verbosity = verbosityLevel1
5762
}
5863

5964
// show usage if there are not command line args
@@ -66,12 +71,12 @@ func main() {
6671
switch cmd {
6772
// define a couple synonyms to "hunt" as well
6873
case "hunt", "find", "search":
69-
serviceList := getList(*serviceString, *tag)
74+
serviceList := getList(*serviceString, *token, *tag)
7075
printList(serviceList, verbosity)
7176

7277
case "kill":
73-
serviceList := getList(*serviceString, *tag)
74-
deregister(serviceList, *force)
78+
serviceList := getList(*serviceString, *token, *tag)
79+
deregister(serviceList, *port, *token, *force)
7580

7681
default:
7782
usage(1)
@@ -105,7 +110,7 @@ func printList(serviceList []*api.ServiceEntry, v verbosityLevel) {
105110
}
106111

107112
switch true {
108-
case v.allows(V3), v.allows(V2), v.allows(V1):
113+
case v.allows(verbosityLevel3), v.allows(verbosityLevel2), v.allows(verbosityLevel1):
109114
table.Append([]string{
110115
se.Node.Node,
111116
se.Service.ID,
@@ -137,12 +142,12 @@ func printList(serviceList []*api.ServiceEntry, v verbosityLevel) {
137142
}
138143

139144
// kill those services that are failing in the passed list, or all if force is true
140-
func deregister(serviceList []*api.ServiceEntry, force bool) {
145+
func deregister(serviceList []*api.ServiceEntry, port int, token string, force bool) {
141146
for _, se := range serviceList {
142147
if !isHealthy(se) || force {
143-
fullAddress := fmt.Sprintf("%s:%d", se.Node.Address, ConsulPort)
148+
fullAddress := fmt.Sprintf("%s:%d", se.Node.Address, port)
144149
fmt.Printf("Deregistering %s: %s (%s)\n", se.Service.Service, se.Service.ID, fullAddress)
145-
client, err := getClient(fullAddress)
150+
client, err := getClient(fullAddress, token)
146151
if err != nil {
147152
log.Fatalf("Unable to get consul client: %s\n", err)
148153
}

0 commit comments

Comments
 (0)