Skip to content

Commit

Permalink
support go install
Browse files Browse the repository at this point in the history
  • Loading branch information
Woellchen committed Feb 27, 2019
1 parent d14a0a6 commit 9468f24
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This essentially enables zero-downtime deployments of any TCP/UNIX socket-based

If you want to run it as a standalone application you can go install it:
```
$ go install github.com/innogames/tcpause/cmd/tcpause
$ go install github.com/innogames/tcpause/tcpause
```

To use the library for your own project simply go get it:
Expand Down
63 changes: 31 additions & 32 deletions cmd/tcpause/tcpause.go → tcpause/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tcpause
package main

import (
"github.com/sirupsen/logrus"
Expand All @@ -21,9 +21,9 @@ var (
| | \ \___| | / __ \| | /\___ \\ ___/
|____| \______ /____| (____ /____//____ >\___ >
\/ \/ \/ \/`
v = viper.New()
logger = logrus.New()
cmd = &cobra.Command{
v = viper.New()
logger = logrus.New()
tcpauseCmd = &cobra.Command{
Use: "tcpause",
Long: splash,
Short: "Zero-downtime proxy for any TCP backend",
Expand Down Expand Up @@ -56,37 +56,36 @@ func (l *logrusLogger) Info(msg string) {
// init initializes the CLI
func init() {
cobra.OnInitialize(loadConfig)
cmd.PersistentFlags().StringP("config", "c", "", "path to config file if any")
cmd.PersistentFlags().DurationP("grace-period", "g", 10*time.Second, "grace period for stopping the server")
cmd.PersistentFlags().String("proxy-addr", "localhost:3000", "proxy listen address")
cmd.PersistentFlags().Bool("proxy-reject-clients", false, "whether to accept the tls connection and reject with a 503 statuc code")
cmd.PersistentFlags().Duration("proxy-retry-after-interval", 3*time.Second, "time after which the client should retry when paused and rejected")
cmd.PersistentFlags().Duration("proxy-block-poll-interval", 100*time.Millisecond, "interval at which the state should be polled to continue blocked connections")
cmd.PersistentFlags().Duration("proxy-close-poll-interval", 100*time.Millisecond, "interval at which the proxy should poll whether to shutdown")
cmd.PersistentFlags().String("proxy-tls-ca-cert", "", "client ca cert if available")
cmd.PersistentFlags().String("proxy-tls-cert", "", "server cert if available")
cmd.PersistentFlags().String("proxy-tls-key", "", "server key if available")
cmd.PersistentFlags().String("control-addr", "localhost:3001", "control listen address")
cmd.PersistentFlags().String("upstream-addr", "localhost:3002", "upstream address")

_ = v.BindPFlag("grace-period", cmd.PersistentFlags().Lookup("grace-period"))
_ = v.BindPFlag("proxy.addr", cmd.PersistentFlags().Lookup("proxy-addr"))
_ = v.BindPFlag("proxy.reject-clients", cmd.PersistentFlags().Lookup("proxy-reject-clients"))
_ = v.BindPFlag("proxy.retry-after-interval", cmd.PersistentFlags().Lookup("proxy-retry-after-interval"))
_ = v.BindPFlag("proxy.block-poll-interval", cmd.PersistentFlags().Lookup("proxy-block-poll-interval"))
_ = v.BindPFlag("proxy.close-poll-interval", cmd.PersistentFlags().Lookup("proxy-close-poll-interval"))
_ = v.BindPFlag("proxy.tls.ca-cert", cmd.PersistentFlags().Lookup("proxy-tls-ca-cert"))
_ = v.BindPFlag("proxy.tls.cert", cmd.PersistentFlags().Lookup("proxy-tls-cert"))
_ = v.BindPFlag("proxy.tls.key", cmd.PersistentFlags().Lookup("proxy-tls-key"))
_ = v.BindPFlag("control.addr", cmd.PersistentFlags().Lookup("control-addr"))
_ = v.BindPFlag("upstream.addr", cmd.PersistentFlags().Lookup("upstream-addr"))
tcpauseCmd.PersistentFlags().StringP("config", "c", "", "path to config file if any")
tcpauseCmd.PersistentFlags().DurationP("grace-period", "g", 10*time.Second, "grace period for stopping the server")
tcpauseCmd.PersistentFlags().String("proxy-addr", "localhost:3000", "proxy listen address")
tcpauseCmd.PersistentFlags().Bool("proxy-reject-clients", false, "whether to accept the tls connection and reject with a 503 statuc code")
tcpauseCmd.PersistentFlags().Duration("proxy-retry-after-interval", 3*time.Second, "time after which the client should retry when paused and rejected")
tcpauseCmd.PersistentFlags().Duration("proxy-block-poll-interval", 100*time.Millisecond, "interval at which the state should be polled to continue blocked connections")
tcpauseCmd.PersistentFlags().Duration("proxy-close-poll-interval", 100*time.Millisecond, "interval at which the proxy should poll whether to shutdown")
tcpauseCmd.PersistentFlags().String("proxy-tls-ca-cert", "", "client ca cert if available")
tcpauseCmd.PersistentFlags().String("proxy-tls-cert", "", "server cert if available")
tcpauseCmd.PersistentFlags().String("proxy-tls-key", "", "server key if available")
tcpauseCmd.PersistentFlags().String("control-addr", "localhost:3001", "control listen address")
tcpauseCmd.PersistentFlags().String("upstream-addr", "localhost:3002", "upstream address")

_ = v.BindPFlag("grace-period", tcpauseCmd.PersistentFlags().Lookup("grace-period"))
_ = v.BindPFlag("proxy.addr", tcpauseCmd.PersistentFlags().Lookup("proxy-addr"))
_ = v.BindPFlag("proxy.reject-clients", tcpauseCmd.PersistentFlags().Lookup("proxy-reject-clients"))
_ = v.BindPFlag("proxy.retry-after-interval", tcpauseCmd.PersistentFlags().Lookup("proxy-retry-after-interval"))
_ = v.BindPFlag("proxy.block-poll-interval", tcpauseCmd.PersistentFlags().Lookup("proxy-block-poll-interval"))
_ = v.BindPFlag("proxy.close-poll-interval", tcpauseCmd.PersistentFlags().Lookup("proxy-close-poll-interval"))
_ = v.BindPFlag("proxy.tls.ca-cert", tcpauseCmd.PersistentFlags().Lookup("proxy-tls-ca-cert"))
_ = v.BindPFlag("proxy.tls.cert", tcpauseCmd.PersistentFlags().Lookup("proxy-tls-cert"))
_ = v.BindPFlag("proxy.tls.key", tcpauseCmd.PersistentFlags().Lookup("proxy-tls-key"))
_ = v.BindPFlag("control.addr", tcpauseCmd.PersistentFlags().Lookup("control-addr"))
_ = v.BindPFlag("upstream.addr", tcpauseCmd.PersistentFlags().Lookup("upstream-addr"))
}

// main entry point
func main() {
cmd.AddCommand(completionCmd)
tcpauseCmd.AddCommand(completionCmd)

if err := cmd.Execute(); err != nil {
if err := tcpauseCmd.Execute(); err != nil {
logger.WithError(err).Fatal("Failed to run the command")
}
}
Expand Down Expand Up @@ -122,7 +121,7 @@ func run(cmd *cobra.Command, args []string) {

// loadConfig loads and parses the config file
func loadConfig() {
path, err := cmd.PersistentFlags().GetString("config")
path, err := tcpauseCmd.PersistentFlags().GetString("config")
if err != nil {
logger.WithError(err).Fatal("Could not get config path")
}
Expand Down

0 comments on commit 9468f24

Please sign in to comment.