Skip to content

Commit

Permalink
Merge pull request #15 from pressly/only
Browse files Browse the repository at this point in the history
Implement `--only regexp` cmd option to filter hosts
  • Loading branch information
VojtechVitek committed Nov 25, 2015
2 parents 998b4b9 + 0348b31 commit 9c3cde0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] <network> <target/command>
$ sup [-f Supfile] [--only <regexp>] <network> <target/command>

- `<network>` - 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.
- `<command>` - A named command (or set of commands) to be run remotely.
- `<target>` - An alias to run multiple `<commands>`.
| command/option | description |
|-------------------|------------------------------------------------------------------------------------------------------|
| `<network>` | Group of hosts, eg. `production` that consists of 1-N hosts. |
| `<command>` | Name set of bash commands to be run remotely, eg `build` that triggers `docker build -t my/image .`. |
| `<target>` | An alias to run multiple `<commands>`, eg `deploy` that triggers `pull`, `build` and `run` commands. |
| | |
| `-f Supfile` | Custom deployment config file (YAML) for `sup`, see [example Supfile](./example/Supfile). |
| `--only <regexp>` | Filter `<target>` 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).
23 changes: 22 additions & 1 deletion cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"text/tabwriter"

Expand All @@ -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 <Supfile>] <network> <target/command>")
ErrCmd = errors.New("Usage: sup [-f <Supfile>] [--only host1] <network> <target/command>")
ErrUnknownNetwork = errors.New("Unknown network")
ErrNetworkNoHosts = errors.New("No hosts for a given network")
ErrTarget = errors.New("Unknown target")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9c3cde0

Please sign in to comment.