Skip to content

Commit 142ba1a

Browse files
authored
Merge pull request #2 from mlafeldt/wipe-state
Allow to wipe state of Chaos Monkey
2 parents 0a49cc6 + 802d302 commit 142ba1a

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ Use the tool to:
4141
chaosmonkey -endpoint http://chaosmonkey.example.com:8080
4242
```
4343

44+
* List available chaos strategies, which you may pass to `-strategy`:
45+
46+
```bash
47+
chaosmonkey -list-strategies
48+
```
49+
4450
* List all auto scaling groups for a given AWS account, which you may then pass to `-group`:
4551

4652
```bash
@@ -50,10 +56,13 @@ Use the tool to:
5056
chaosmonkey -list-groups
5157
```
5258

53-
* List available chaos strategies, which you may pass to `-strategy`:
59+
* Wipe state of Chaos Monkey by deleting its SimpleDB domain (named `SIMIAN_ARMY` by default):
5460

5561
```bash
56-
chaosmonkey -list-strategies
62+
export AWS_ACCESS_KEY_ID=...
63+
export AWS_SECRET_ACCESS_KEY=...
64+
export AWS_REGION=...
65+
chaosmonkey -wipe-state SIMIAN_ARMY
5766
```
5867

5968
As always, invoke `chaosmonkey -h` for a list of all available options.

aws.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/autoscaling"
10+
"github.com/aws/aws-sdk-go/service/simpledb"
11+
)
12+
13+
func autoScalingGroups() ([]string, error) {
14+
var groups []string
15+
svc := autoscaling.New(session.New())
16+
err := svc.DescribeAutoScalingGroupsPages(nil, func(out *autoscaling.DescribeAutoScalingGroupsOutput, last bool) bool {
17+
for _, g := range out.AutoScalingGroups {
18+
groups = append(groups, aws.StringValue(g.AutoScalingGroupName))
19+
}
20+
return !last
21+
})
22+
if err != nil {
23+
return nil, err
24+
}
25+
sort.Strings(groups)
26+
return groups, nil
27+
}
28+
29+
func deleteSimpleDBDomain(domainName string) error {
30+
var domainExists bool
31+
svc := simpledb.New(session.New())
32+
err := svc.ListDomainsPages(nil, func(out *simpledb.ListDomainsOutput, last bool) bool {
33+
for _, n := range out.DomainNames {
34+
if aws.StringValue(n) == domainName {
35+
domainExists = true
36+
}
37+
}
38+
return !last
39+
})
40+
if !domainExists {
41+
return fmt.Errorf("SimpleDB domain %q does not exist", domainName)
42+
}
43+
_, err = svc.DeleteDomain(&simpledb.DeleteDomainInput{
44+
DomainName: aws.String(domainName),
45+
})
46+
return err
47+
}

main.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import (
66
"net/http"
77
"os"
88
"runtime"
9-
"sort"
109
"strings"
1110
"time"
1211

13-
"github.com/aws/aws-sdk-go/aws"
14-
"github.com/aws/aws-sdk-go/aws/session"
15-
"github.com/aws/aws-sdk-go/service/autoscaling"
1612
"github.com/ryanuber/columnize"
1713

1814
chaosmonkey "github.com/mlafeldt/chaosmonkey/lib"
@@ -32,6 +28,7 @@ func main() {
3228

3329
listGroups bool
3430
listStrategies bool
31+
wipeState string
3532
showVersion bool
3633
)
3734

@@ -42,6 +39,7 @@ func main() {
4239
flag.StringVar(&password, "password", "", "HTTP password")
4340
flag.BoolVar(&listGroups, "list-groups", false, "List auto scaling groups")
4441
flag.BoolVar(&listStrategies, "list-strategies", false, "List default chaos strategies")
42+
flag.StringVar(&wipeState, "wipe-state", "", "Wipe Chaos Monkey state by deleting given SimpleDB domain")
4543
flag.BoolVar(&showVersion, "version", false, "Show program version")
4644
flag.Parse()
4745

@@ -58,6 +56,11 @@ func main() {
5856
fmt.Println(s)
5957
}
6058
return
59+
case wipeState != "":
60+
if err := deleteSimpleDBDomain(wipeState); err != nil {
61+
abort("failed to wipe state: %s", err)
62+
}
63+
return
6164
case showVersion:
6265
fmt.Printf("chaosmonkey %s %s/%s %s\n", Version,
6366
runtime.GOOS, runtime.GOARCH, runtime.Version())
@@ -90,22 +93,6 @@ func main() {
9093
}
9194
}
9295

93-
func autoScalingGroups() ([]string, error) {
94-
var groups []string
95-
svc := autoscaling.New(session.New())
96-
err := svc.DescribeAutoScalingGroupsPages(nil, func(out *autoscaling.DescribeAutoScalingGroupsOutput, last bool) bool {
97-
for _, g := range out.AutoScalingGroups {
98-
groups = append(groups, aws.StringValue(g.AutoScalingGroupName))
99-
}
100-
return !last
101-
})
102-
if err != nil {
103-
return nil, err
104-
}
105-
sort.Strings(groups)
106-
return groups, nil
107-
}
108-
10996
func printEvents(event ...chaosmonkey.Event) {
11097
lines := []string{"InstanceID|AutoScalingGroupName|Region|Strategy|TriggeredAt"}
11198
for _, e := range event {

0 commit comments

Comments
 (0)