From 0348b3163a67702113cd43616c59bc1e935d59d6 Mon Sep 17 00:00:00 2001 From: "Vojtech Vitek (V-Teq)" Date: Wed, 25 Nov 2015 17:43:30 -0500 Subject: [PATCH] Implement `--only regexp` cmd option to filter hosts --- README.md | 28 ++++++++++++++++++++-------- cmd/sup/main.go | 23 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index eb8ce58..374248b 100644 --- a/README.md +++ b/README.md @@ -3,25 +3,37 @@ Stack Up Stack Up is a simple deployment tool that performs given set of commands on multiple hosts in parallel. It reads Supfile, a YAML configuration file, which defines networks (groups of hosts), commands and targets. - $ sup [-f Supfile] + $ sup [-f Supfile] [--only ] -- `` - A group of hosts, eg. `dev`, `stg` and `prod`. In this case, `prod` can map to `prod1.example.com`, `prod2.example.com` and `prod3.example.com` hosts. -- `` - A named command (or set of commands) to be run remotely. -- `` - An alias to run multiple ``. +| command/option | description | +|-------------------|------------------------------------------------------------------------------------------------------| +| `` | Group of hosts, eg. `production` that consists of 1-N hosts. | +| `` | Name set of bash commands to be run remotely, eg `build` that triggers `docker build -t my/image .`. | +| `` | An alias to run multiple ``, eg `deploy` that triggers `pull`, `build` and `run` commands. | +| | | +| `-f Supfile` | Custom deployment config file (YAML) for `sup`, see [example Supfile](./example/Supfile). | +| `--only ` | Filter `` hosts by regexp string, eg `--only host1`. | -`sup` picks up `Supfile` config file (YAML) from the current directory (the same way as `make` picks up `Makefile`). See [example Supfile](./example/Supfile). +## Examples: -[![Sup](https://github.com/pressly/sup/blob/gif/asciinema.gif?raw=true)](https://asciinema.org/a/19742?autoplay=1) + $ sup prod deploy + $ sup --only api1 dev tail-logs + $ sup -f Supfile.db stg restart # Installation $ go get -u github.com/pressly/sup/cmd/sup +# Demo + +[![Sup](https://github.com/pressly/sup/blob/gif/asciinema.gif?raw=true)](https://asciinema.org/a/19742?autoplay=1) + # Development - $ clone it.. + fork it.. + $ make deps $ make build # License -Stack Up is licensed under the [MIT License](./LICENSE). +Licensed under the [MIT License](./LICENSE). diff --git a/cmd/sup/main.go b/cmd/sup/main.go index 3668c4f..7cd4d38 100644 --- a/cmd/sup/main.go +++ b/cmd/sup/main.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "regexp" "strings" "text/tabwriter" @@ -16,8 +17,9 @@ var ( supfile = flag.String("f", "./Supfile", "custom path to Supfile") showVersionShort = flag.Bool("v", false, "print version") showVersionLong = flag.Bool("version", false, "print version") + onlyHosts = flag.String("only", "", "filter hosts with regexp") - ErrCmd = errors.New("Usage: sup [-f ] ") + ErrCmd = errors.New("Usage: sup [-f ] [--only host1] ") ErrUnknownNetwork = errors.New("Unknown network") ErrNetworkNoHosts = errors.New("No hosts for a given network") ErrTarget = errors.New("Unknown target") @@ -143,6 +145,25 @@ func main() { log.Fatal(err) } + // --only option to filter hosts + if *onlyHosts != "" { + expr, err := regexp.CompilePOSIX(*onlyHosts) + if err != nil { + log.Fatal(err) + } + + var hosts []string + for _, host := range network.Hosts { + if expr.MatchString(host) { + hosts = append(hosts, host) + } + } + if len(hosts) == 0 { + log.Fatal(fmt.Errorf("no hosts match '%v' regexp", *onlyHosts)) + } + network.Hosts = hosts + } + // Create new Stackup app. app, err := sup.New(conf) if err != nil {