Add healthcheck --wait and --quiet options

This commit is contained in:
Manfred Touron 2018-01-01 10:54:58 +01:00
parent 6494e69632
commit 57dd2c6c01
6 changed files with 52 additions and 10 deletions

View file

@ -9,7 +9,7 @@ Changes:
* Fix connection failure when sending too many environment variables (fix [#22](https://github.com/moul/sshportal/issues/22))
* Fix panic when entering empty command (fix [#13](https://github.com/moul/sshportal/issues/13))
* Add `config backup --ignore-events` option
* Add `sshportal healthcheck` cli command
* Add `sshportal healthcheck [--addr=] [--wait] [--quiet]` cli command
* Add [Docker Healthcheck](https://docs.docker.com/engine/reference/builder/#healthcheck) helper
## v1.6.0 (2017-12-12)

View file

@ -10,4 +10,4 @@ COPY --from=builder /go/bin/sshportal /bin/sshportal
ENTRYPOINT ["/bin/sshportal"]
CMD ["server"]
EXPOSE 2222
HEALTHCHECK --interval=10s --timeout=10s CMD /bin/sshportal healthcheck
HEALTHCHECK CMD /bin/sshportal healthcheck --wait

View file

@ -350,11 +350,22 @@ the `healtcheck` user can be changed using the `healthcheck-user` option.
Alternatively, you can run the built-in healthcheck helper (requiring no ssh client nor ssh key):
Usage: `sshportal healthcheck [--addr=host:port] [--wait] [--quiet]
```console
$ sshportal healthcheck --addr=localhost:2222; echo $?
$ 0
```
---
Wait for sshportal to be healthy, then connect
```console
$ sshportal healthcheck --wait && ssh sshportal -l admin
config>
```
## Scaling
`sshportal` is stateless but relies on a database to store configuration and logs.

View file

@ -1,4 +1,4 @@
FROM occitech/ssh-client
COPY . /integration
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["/integration/_client.sh"]
COPY . /integration

View file

@ -1,11 +1,7 @@
run:
docker-compose down
docker-compose up -d sshportal
@echo "Waiting for sshportal to be healthy"
@sleep 3
docker-compose build client
docker-compose exec sshportal /bin/sshportal healthcheck --wait --quiet
docker-compose run client /integration/_client.sh
docker-compose down

39
main.go
View file

@ -95,6 +95,15 @@ func main() {
cli.StringFlag{
Name: "addr, a",
Value: "localhost:2222",
Usage: "sshportal server address",
},
cli.BoolFlag{
Name: "wait, w",
Usage: "Loop indefinitely until sshportal is ready",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Do not print errors, if any",
},
},
},
@ -304,7 +313,31 @@ func healthcheck(c *cli.Context) error {
HostKeyCallback: func(hostname string, remote net.Addr, key gossh.PublicKey) error { return nil },
Auth: []gossh.AuthMethod{gossh.Password("healthcheck")},
}
client, err := gossh.Dial("tcp", c.String("addr"), &config)
if c.Bool("wait") {
for {
if err := healthcheckOnce(c.String("addr"), config, c.Bool("quiet")); err != nil {
if !c.Bool("quiet") {
log.Printf("error: %v", err)
}
time.Sleep(time.Second)
continue
}
return nil
}
}
if err := healthcheckOnce(c.String("addr"), config, c.Bool("quiet")); err != nil {
if c.Bool("quiet") {
return cli.NewExitError("", 1)
}
return err
}
return nil
}
func healthcheckOnce(addr string, config gossh.ClientConfig, quiet bool) error {
client, err := gossh.Dial("tcp", addr, &config)
if err != nil {
return err
}
@ -315,7 +348,9 @@ func healthcheck(c *cli.Context) error {
}
defer func() {
if err := session.Close(); err != nil {
panic(err)
if !quiet {
log.Printf("failed to close session: %v", err)
}
}
}()