Skip to content

Commit

Permalink
Merge pull request #41 from pressly/features_v0.3
Browse files Browse the repository at this point in the history
Features v0.3 (command: local, serial, once)
  • Loading branch information
VojtechVitek committed Dec 15, 2015
2 parents fc833ee + 13c4f6e commit d406a20
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 158 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Client interface {
Run(task *Task) error
Wait() error
Close() error
Prefix() string
Prefix() (string, int)
Write(p []byte) (n int, err error)
WriteClose() error
Stdin() io.WriteCloser
Expand Down
99 changes: 57 additions & 42 deletions example/Supfile
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
# Supfile for "Example" server
# Supfile for "Example" Docker service
---
version: 0.3

env: # Environment variables for the commands
env:
# Environment variables for all commands
NAME: example
REPO: github.com/pressly/stackup
REPO: github.com/pressly/sup
BRANCH: master
IMAGE: pressly/example
HOST_PORT: 8000
CONTAINER_PORT: 8000

networks: # Groups of hosts
networks:
# Groups of hosts
local:
env:
CONFIG: example.local.cfg
hosts:
- localhost

dev:
env:
CONFIG: example.dev.cfg
# Extra environment variable for dev hosts only
DOCKER_HOST: tcp://127.0.0.1:2375
hosts:
- docker@192.168.59.103

stg:
env:
CONFIG: example.stg.cfg
hosts:
- ubuntu@stg.example.com

prod:
inventory:
- for i in 4 5 6; do echo "$USER@prod$i.example.com\n"; done
env:
CONFIG: example.prod.cfg
hosts:
- ubuntu@prod1.example.com
- ubuntu@prod2.example.com
- ubuntu@prod3.example.com
inventory: for i in 1 2 3 4; do echo "ubuntu@prod$i.example.com"; done

commands: # Named set of commands to be run remotely
commands:
# Named set of commands to be run remotely
ping:
desc: Print uname and current date/time.
run: uname -a; date

upload:
desc: Upload this repository
pre-build:
desc: Initialize directory
run: mkdir -p /tmp/$IMAGE

build:
desc: Build Docker image from current directory, push to Docker Hub
# local: sup -f ./builder/Supfile $SUP_NETWORK build
upload:
- src: ./
dst: /tmp/$IMAGE

build:
desc: Build Docker image
script: ./scripts/docker-build.sh
once: true

image:
desc: List Docker image
run: sudo docker images | grep $IMAGE
pull:
desc: Pull latest Docker image
run: sudo docker pull $IMAGE

config:
desc: Upload/test config file.
upload:
- src: ./$CONFIG
- src: ./example.$SUP_NETWORK.cfg
dst: /tmp/
run: test -f /tmp/$CONFIG

# pull:
# desc: Pull git repository
# script: ./scripts/docker-pull.sh
run: test -f /tmp/example.$SUP_NETWORK.cfg

stop:
desc: Stop Docker container
Expand All @@ -82,12 +75,29 @@ commands: # Named set of commands to be run remotely

run:
desc: Run Docker container
script: ./scripts/docker-run.sh
run: >
sudo docker run -d \
-p $HOST_PORT:$CONTAINER_PORT \
-v /tmp/example.$SUP_NETWORK.cfg:/etc/example.cfg \
--restart=always \
--name $NAME $IMAGE

restart:
desc: Restart Docker container
run: sudo docker restart $NAME || exit 0

stop-rm-run:
desc: Stop & remove old Docker container, run new one
run: >
sudo docker stop $NAME || :; \
sudo docker rm $NAME || :; \
sudo docker run -d \
-p $HOST_PORT:$CONTAINER_PORT \
-v /tmp/example.$SUP_NETWORK.cfg:/etc/example.cfg \
--restart=always \
--name $NAME $IMAGE
serial: 1

ps:
desc: List running Docker containers
run: sudo docker ps | grep $NAME
Expand All @@ -104,6 +114,12 @@ commands: # Named set of commands to be run remotely
desc: Application health check
run: curl localhost:$HOST_PORT

slack-notify:
desc: Notify Slack about new deployment
local: >
curl -X POST --data-urlencode 'payload={"channel": "#_team_", "text": "['$SUP_NETWORK'] '$(whoami)' deployed '$NAME'"}' \
https://hooks.slack.com/services/X/Y/Z

shell:
desc: Interactive shell on all hosts
stdin: true
Expand All @@ -112,18 +128,17 @@ commands: # Named set of commands to be run remotely
exec:
desc: Interactive docker exec on all hosts
stdin: true
run: docker exec -i $NAME bash
run: sudo docker exec -i $NAME bash

targets: # Aliases to run multiple commands at once
targets:
# Aliases to run multiple commands at once
deploy:
#- pull
- upload
- pre-build
- build
- image
- pull
- config
- stop
- rm
- run
- stop-rm-run
- ps
- logs
- health
- health
- slack-notify
4 changes: 3 additions & 1 deletion example/scripts/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ sudo rm -rf bin
sudo docker run --rm \
-v $(pwd):/go/src/$REPO/$NAME \
-w /go/src/$REPO/$NAME \
golang:1.4 go build
golang:1.5.2 go build

# Bake bin/* into the resulting image.
sudo docker build --no-cache -t $IMAGE .

sudo docker push $IMAGE
30 changes: 0 additions & 30 deletions example/scripts/docker-pull.sh

This file was deleted.

8 changes: 0 additions & 8 deletions example/scripts/docker-run.sh

This file was deleted.

5 changes: 3 additions & 2 deletions localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ func (c *LocalhostClient) Stdout() io.Reader {
return c.stdout
}

func (c *LocalhostClient) Prefix() string {
return c.user + "@localhost"
func (c *LocalhostClient) Prefix() (string, int) {
host := c.user + "@localhost" + " | "
return ResetColor + host, len(host)
}

func (c *LocalhostClient) Write(p []byte) (n int, err error) {
Expand Down
6 changes: 4 additions & 2 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SSHClient struct {
sessOpened bool
running bool
env string //export FOO="bar"; export BAR="baz";
color string
}

type ErrConnect struct {
Expand Down Expand Up @@ -247,8 +248,9 @@ func (c *SSHClient) Stdout() io.Reader {
return c.remoteStdout
}

func (c *SSHClient) Prefix() string {
return c.user + "@" + c.host
func (c *SSHClient) Prefix() (string, int) {
host := c.user + "@" + c.host + " | "
return c.color + host + ResetColor, len(host)
}

func (c *SSHClient) Write(p []byte) (n int, err error) {
Expand Down
Loading

0 comments on commit d406a20

Please sign in to comment.