diff --git a/README.md b/README.md index 263b941..f4f3a2a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/badboy.png b/doc/badboy.png new file mode 100644 index 0000000..7eea51a Binary files /dev/null and b/doc/badboy.png differ diff --git a/project.clj b/project.clj index 183f45b..7b49492 100644 --- a/project.clj +++ b/project.clj @@ -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} diff --git a/resources/bad-boy.version b/resources/bad-boy.version index d917d3e..b1e80bb 100644 --- a/resources/bad-boy.version +++ b/resources/bad-boy.version @@ -1 +1 @@ -0.1.2 +0.1.3 diff --git a/resources/cli-grammar.ebnf b/resources/cli-grammar.ebnf new file mode 100644 index 0000000..139e44c --- /dev/null +++ b/resources/cli-grammar.ebnf @@ -0,0 +1,18 @@ +command = atom? ( atom)* + = global-options | target + + = 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_-]+' + + = #'[a-zA-Z][a-zA-Z0-9*?_-]+' + = #'\s+' diff --git a/src/com/brunobonacci/bad_boy.clj b/src/com/brunobonacci/bad_boy.clj index 879bb50..555f8b3 100644 --- a/src/com/brunobonacci/bad_boy.clj +++ b/src/com/brunobonacci/bad_boy.clj @@ -7,7 +7,7 @@ (:gen-class)) ;;(def creds (credentials/system-property-credentials-provider)) - +;;(reset! dry-run 1) (def ec2 (aws/client {:api :ec2})) @@ -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))) @@ -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) @@ -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)))) diff --git a/src/com/brunobonacci/command_line.clj b/src/com/brunobonacci/command_line.clj new file mode 100644 index 0000000..c0a34af --- /dev/null +++ b/src/com/brunobonacci/command_line.clj @@ -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") +;;