Giter VIP home page Giter VIP logo

donkeyx / tcp-wait Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.11 MB

Go package to test and wait on the availability of a TCP host and port. This is mainly used as a pre start script for containers which depend on other services before starting. You can run this first with a defined interval and die if the host:port does not become available.

License: GNU General Public License v3.0

Dockerfile 6.32% Makefile 28.40% Go 65.28%

tcp-wait's Introduction

tcp-wait

Go package to test and wait on the availability of a TCP host and port. This is mainly used as a pre start script for containers which depend on other services before starting. You can run this first with a defined interval and die if the host:port does not become available.

Feel free to contribute, as I will allow most merges as long as they don't drastically break the usage.

Quickstart

Using docker:

# simply run the docker run command and you will be good to go immediately
$ docker run -it donkeyx/tcp-wait -version
Unable to find image 'donkeyx/tcp-wait:latest' locally
...
Version 'docker-build'
Git_Hash '6e567e4'

Using go get:

# get the package
go get github.com/donkeyx/tcp-wait

# add go bin path to your startup shell, bash in this case
echo 'export PATH="~/go/bin:$PATH"' >> ~/.bashrc

# quick run from there like this. If either fails it will return error code and messages to match
tcp-wait -hp localhost:8080,localhost:9090 -t 5

# for help just run with no flags
tcp-wait -h
Usage of /Users/dbinney/go/bin/tcp-wait:
  -hp value
    	<host:port> [host2:port,...] comma seperated list of host:ports
  -o string
    	output in format json/text (default "json")
  -t int
    	timeout (default 20)
  -version
    	version information
...

Usage

### simple (success)
$ ./bin/tcp-wait -hp github.com:80
{"level":"info","msg":"services are ready!","services":["github.com:80"],"time":"2020-03-12T17:18:30+10:30"}

### multiple hosts with timeout(1sec) and text as the output
$ ./bin/tcp-wait -hp github.com:443,google.com:443 -t 1 -o text
INFO[2020-03-12T17:20:15+10:30] services are ready!  services="[github.com:443 google.com:443]"

### multiple hosts with timeout(2sec) and fail condition on one host:port
$ ./bin/tcp-wait -hp github.com:443,localhost:10000 -t 2
{"level":"warning","msg":"tcp ping failed","tcp-host":"localhost:10000","time":"2020-03-12T17:26:16+10:30"}
{"level":"warning","msg":"tcp ping failed","tcp-host":"localhost:10000","time":"2020-03-12T17:26:17+10:30"}
{"level":"error","msg":"services did not respond","time":"2020-03-12T17:26:18+10:30"}

Building locally

You can just clone down the repo, then use the make file to build the packages. They will be placed in a local bin folder, with linux/mac and a local env build. You can execute them directly from that path.

# build and run using make all
$ make all
go get
go test -v ./...
=== RUN   TestSuccessSingle
--- PASS: TestSuccessSingle (0.01s)
=== RUN   TestFailureDouble
{"level":"warning","msg":"tcp ping failed","tcp-host":"nowhere:50","time":"2020-03-13T11:38:45+10:30"}
{"level":"warning","msg":"tcp ping failed","tcp-host":"nowhere:51","time":"2020-03-13T11:38:45+10:30"}
--- PASS: TestFailureDouble (0.50s)
PASS
ok      tcp-wait        (cached)
go build -o ./bin/tcp-wait -v
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/linux/tcp-wait -v
tcp-wait
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/mac/tcp-wait -v
tcp-wait

# structure created with binaries in your local bin directory.
$ tree
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── bin
│   ├── tcp-wait
│   ├── tcp-wait.darwin.amd64
│   ├── tcp-wait.linux.amd64
│   └── tcp-wait.windows.amd64
├── go.mod
├── go.sum
├── main.go
├── main_test.go
└── tmp
    └── test-coverage
        ├── coverage.html
        └── coverage.out.


# Binary should be imediately executable
$ ./bin/tcp-wait
Usage of ./bin/tcp-wait:
  -hp value
        <host:port> [host2:port,...] comma seperated list of services
  -o string
        output in format json/text (default "json")
  -t int
        timeout (default 20)

tcp-wait's People

Contributors

aliuygur avatar donkeyx avatar

Stargazers

 avatar  avatar

Watchers

 avatar

tcp-wait's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.