Skip to content

Commit

Permalink
added selection by tag with '--default-selection' option
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBonacci committed Aug 3, 2019
1 parent 57a4fab commit f74d985
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 32 deletions.
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
# bad-boy
[![Clojars Project](https://img.shields.io/clojars/v/com.brunobonacci/bad-boy.svg)](https://clojars.org/com.brunobonacci/bad-boy) ![CircleCi](https://img.shields.io/circleci/project/BrunoBonacci/bad-boy.svg) ![last-commit](https://img.shields.io/github/last-commit/BrunoBonacci/bad-boy.svg) [![Dependencies Status](https://jarkeeper.com/com.brunobonacci/bad-boy/status.svg)](https://jarkeeper.com/BrunoBonacci/bad-boy)

A cool library designed to ... well, that part is up to you.
Chaos testing and infrastructure hardening tool.

## Usage

In order to use the library add the dependency to your `project.clj`
![bad boy](./doc/badboy.png)

``` clojure
;; Leiningen project
[com.brunobonacci/bad-boy "0.1.0-SNAPSHOT"]

;; deps.edn format
{:deps { com.brunobonacci/bad-boy "0.1.0-SNAPSHOT" }}
```

Current version: [![Clojars Project](https://img.shields.io/clojars/v/com.brunobonacci/bad-boy.svg)](https://clojars.org/com.brunobonacci/bad-boy)


Then require the namespace:

``` clojure
(ns foo.bar
(:require [com.brunobonacci.bad-boy :as x]))
```

Then it's up to you...
Bad stuff in progress, more to come...!

## License

Expand Down
Binary file added doc/badboy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

:scm {:name "git" :url "https://github.com/BrunoBonacci/bad-boy.git"}

:dependencies [[org.clojure/clojure "1.10.0"]
:dependencies [[org.clojure/clojure "1.10.1"]
[com.cognitect.aws/api "0.8.301"]
[com.cognitect.aws/endpoints "1.1.11.537"]
[com.cognitect.aws/ec2 "714.2.430.0"]
[com.cognitect.aws/autoscaling "712.2.426.0"]
[com.brunobonacci/where "0.5.1"]]
[com.brunobonacci/where "0.5.1"]
[instaparse "1.4.10"]]

:global-vars {*warn-on-reflection* true}

Expand Down
2 changes: 1 addition & 1 deletion resources/bad-boy.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.2
0.1.3
18 changes: 18 additions & 0 deletions resources/cli-grammar.ebnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
command = atom? (<ws> atom)*
<atom> = global-options | target

<global-options> = global-option ( <ws> global-option )*
<global-option> = help | version | dryrun
help = <'-h'> | <'--help'>
version = <'-v'> | <'--version'>
dryrun = <'--dryrun'> | <'--dry-run'>

target = (target-name / tag)
target-name = glob

tag = <'tag:'> tag-name <'='> tag-value
tag-name = #'[a-zA-Z0-9_-]+'
tag-value = (<"'"> #"[^\']*" <"'">) | (<'"'> #'[^\"]*' <'"'>) | #'[a-zA-Z0-9_-]+'

<glob> = #'[a-zA-Z][a-zA-Z0-9*?_-]+'
<ws> = #'\s+'
17 changes: 10 additions & 7 deletions src/com/brunobonacci/bad_boy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(:gen-class))

;;(def creds (credentials/system-property-credentials-provider))

;;(reset! dry-run 1)


(def ec2 (aws/client {:api :ec2}))
Expand Down Expand Up @@ -74,9 +74,9 @@
#(aws-request asg
(cond-> {:op :DescribeAutoScalingGroups :request {:MaxRecords 100}}
% (assoc-in [:request :NextToken] %))))
(filter filters)
(map #(select-keys % [:AutoScalingGroupName :MinSize :MaxSize :DesiredCapacity :Instances :Tags]))
(map #(update % :Tags tags-map))))
(map #(update % :Tags tags-map))
(filter filters)))



Expand All @@ -89,6 +89,7 @@
(when (> (count c) 0)
(clojure.core/rand-nth c)))


(defn find-and-kill-one
[asg ec2 filters]
(let [groups (auto-scaling-groups asg filters)
Expand Down Expand Up @@ -131,8 +132,10 @@
[& asg-names]
(header asg-names)
(if-not (seq asg-names)
(println "[no-op] No target selected, please provide a list of regex for autoscaling groups to target.!")
(let [filters (where
(cons :or
(map (fn [g] [:AutoScalingGroupName :MATCHES? g]) asg-names)))]
(println "[no-op] No target selected, please provide a list of regex for autoscaling groups to target, or use --default-selection !")
(let [filters (if (= "--default-selection" (first asg-names))
(where (comp :chaos-testing :Tags) :is? "opt-in")
(where
(cons :or
(map (fn [g] [:AutoScalingGroupName :MATCHES? g]) asg-names))))]
(find-and-kill-one asg ec2 filters))))
29 changes: 29 additions & 0 deletions src/com/brunobonacci/command_line.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns com.brunobonacci.command-line
(:require [instaparse.core :as insta :refer [defparser]]
[clojure.java.io :as io]))


(defparser parser (io/resource "cli-grammar.ebnf"))



(defn parse-options
[cli]
(->> cli
(parser)
(insta/transform
{:help #(vector :help true)
:version #(vector :version true)
:dryrun #(vector :dryrun true)

:target-name #(array-map :target-name %)
:tag (fn [[_ k] [_ v]] {:tag {k v}})

:command (fn [& args] (into {} args))
})))


;;(parse-options "-h --version tag:chaos-testing='foo with space'")
;;(parse-options "-h --version unlucky*-test")
;;(parse-options "unlucky* --dryrun")
;;

0 comments on commit f74d985

Please sign in to comment.