Skip to content

Commit

Permalink
Merge pull request #2 from mlafeldt/wipe-state
Browse files Browse the repository at this point in the history
Allow to wipe state of Chaos Monkey
  • Loading branch information
mlafeldt authored Jun 14, 2016
2 parents 0a49cc6 + 802d302 commit 142ba1a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Use the tool to:
chaosmonkey -endpoint http://chaosmonkey.example.com:8080
```

* List available chaos strategies, which you may pass to `-strategy`:

```bash
chaosmonkey -list-strategies
```

* List all auto scaling groups for a given AWS account, which you may then pass to `-group`:

```bash
Expand All @@ -50,10 +56,13 @@ Use the tool to:
chaosmonkey -list-groups
```

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

```bash
chaosmonkey -list-strategies
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_REGION=...
chaosmonkey -wipe-state SIMIAN_ARMY
```

As always, invoke `chaosmonkey -h` for a list of all available options.
Expand Down
47 changes: 47 additions & 0 deletions aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/simpledb"
)

func autoScalingGroups() ([]string, error) {
var groups []string
svc := autoscaling.New(session.New())
err := svc.DescribeAutoScalingGroupsPages(nil, func(out *autoscaling.DescribeAutoScalingGroupsOutput, last bool) bool {
for _, g := range out.AutoScalingGroups {
groups = append(groups, aws.StringValue(g.AutoScalingGroupName))
}
return !last
})
if err != nil {
return nil, err
}
sort.Strings(groups)
return groups, nil
}

func deleteSimpleDBDomain(domainName string) error {
var domainExists bool
svc := simpledb.New(session.New())
err := svc.ListDomainsPages(nil, func(out *simpledb.ListDomainsOutput, last bool) bool {
for _, n := range out.DomainNames {
if aws.StringValue(n) == domainName {
domainExists = true
}
}
return !last
})
if !domainExists {
return fmt.Errorf("SimpleDB domain %q does not exist", domainName)
}
_, err = svc.DeleteDomain(&simpledb.DeleteDomainInput{
DomainName: aws.String(domainName),
})
return err
}
27 changes: 7 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import (
"net/http"
"os"
"runtime"
"sort"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/ryanuber/columnize"

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

listGroups bool
listStrategies bool
wipeState string
showVersion bool
)

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

Expand All @@ -58,6 +56,11 @@ func main() {
fmt.Println(s)
}
return
case wipeState != "":
if err := deleteSimpleDBDomain(wipeState); err != nil {
abort("failed to wipe state: %s", err)
}
return
case showVersion:
fmt.Printf("chaosmonkey %s %s/%s %s\n", Version,
runtime.GOOS, runtime.GOARCH, runtime.Version())
Expand Down Expand Up @@ -90,22 +93,6 @@ func main() {
}
}

func autoScalingGroups() ([]string, error) {
var groups []string
svc := autoscaling.New(session.New())
err := svc.DescribeAutoScalingGroupsPages(nil, func(out *autoscaling.DescribeAutoScalingGroupsOutput, last bool) bool {
for _, g := range out.AutoScalingGroups {
groups = append(groups, aws.StringValue(g.AutoScalingGroupName))
}
return !last
})
if err != nil {
return nil, err
}
sort.Strings(groups)
return groups, nil
}

func printEvents(event ...chaosmonkey.Event) {
lines := []string{"InstanceID|AutoScalingGroupName|Region|Strategy|TriggeredAt"}
for _, e := range event {
Expand Down

0 comments on commit 142ba1a

Please sign in to comment.