diff --git a/.gitignore b/.gitignore index 7dfaf93..febaabd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ beethoven devconf.json dockerconf.json temp +Dockerfile.temp diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f777ecb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,25 @@ +language: go +go: + - 1.7 +env: + - "PATH=$HOME/gopath/bin:$PATH" + +before_install: + - go get github.com/stretchr/testify + +install: + - go get -v ./... + +script: + - go test ./... + - go build + +after_success: + - go build -ldflags "-X main.version=0.1-$TRAVIS_BUILD_NUMBER -X 'main.built=$(date -u '+%Y-%m-%d %H:%M:%S')'" + - docker build -t containx/beethoven . + - docker push containx/beethoven + +# whitelist +branches: + only: + - master \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..176238e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:stable + +ENV DOCKER_ENV true + +RUN apt-get update \ + && apt-get install -y supervisor \ + && rm -rf /var/lib/apt/lists/* + +COPY beethoven /usr/sbin/beethoven +COPY scripts/run.sh /bin/run.sh + +RUN chmod a+x /bin/run.sh + +COPY scripts/supervisord.conf /etc/supervisord.tmpl + +ENTRYPOINT ["/bin/run.sh"] \ No newline at end of file diff --git a/Makefile b/Makefile index 6fa9f38..cd42ab7 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ deps: go get docker-dev: - GOOS=linux GOARCH=amd64 go build - docker build -t beethoven . + GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=0.1-DEV -X main.built=$(date -u '+%Y-%m-%d %H:%M:%S')" + docker build -t containx/beethoven . format: $(GO_FMT) diff --git a/config/config.go b/config/config.go index ce37dd9..4617464 100644 --- a/config/config.go +++ b/config/config.go @@ -65,7 +65,7 @@ var ( func AddFlags(cmd *cobra.Command) { cmd.Flags().StringP("config", "c", "", "Path and filename of local configuration file. ex: config.yml") cmd.Flags().BoolP("remote", "r", false, "Use remote configuraion server") - cmd.Flags().StringP("server", "s", "", "Remote: URI to remote config server. ex: http://server:8888") + cmd.Flags().StringP("server", "s", "", "Remote: URI to remote config server. ex: http://server:8888, env: CONFIG_SERVER") cmd.Flags().String("name", "beethoven", "Remote: The name of the app, env: CONFIG_NAME") cmd.Flags().String("label", "master", "Remote: The branch to fetch the config from, env: CONFIG_LABEL") cmd.Flags().String("profile", "default", "Remote: The profile to use, env: CONFIG_PROFILE") diff --git a/generator/nginx.go b/generator/nginx.go index c402f59..a9e8d0d 100644 --- a/generator/nginx.go +++ b/generator/nginx.go @@ -39,7 +39,7 @@ func (g *Generator) writeConfiguration() (bool, error) { } g.tracker.SetLastConfigRendered(time.Now()) - log.Info("wrote config: %s, contents: \n\n%s", tplFilename, result) + // log.Info("wrote config: %s, contents: \n\n%s", tplFilename, result) if g.cfg.DryRun() { log.Debug("Has Changed from Config : %v", g.templateAndConfMatch(tplFilename)) diff --git a/proxy/api.go b/proxy/api.go index d995f68..e39541b 100644 --- a/proxy/api.go +++ b/proxy/api.go @@ -1,4 +1,16 @@ package proxy -type Info struct { +import ( + "fmt" + "github.com/ContainX/depcon/pkg/encoding" + "net/http" +) + +func (p *Proxy) getStatus(w http.ResponseWriter, r *http.Request) { + json, err := encoding.DefaultJSONEncoder().MarshalIndent(p.tracker.GetStatus()) + if err != nil { + fmt.Fprintf(w, "Error: %s", err.Error()) + return + } + fmt.Fprint(w, json) } diff --git a/proxy/server.go b/proxy/server.go index 6df56db..a58b02e 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -22,6 +22,7 @@ type Proxy struct { httpServer *http.Server generator *generator.Generator tracker *tracker.Tracker + mux *mux.Router } func New(cfg *config.Config) *Proxy { @@ -30,13 +31,18 @@ func New(cfg *config.Config) *Proxy { } } +func (p *Proxy) initRoutes() { + p.mux.HandleFunc("/bt", p.getVersion) + p.mux.HandleFunc("/bt/status/", p.getStatus) +} + func (p *Proxy) Serve() { - mux := mux.NewRouter() - mux.HandleFunc("/", p.getVersion) + p.mux = mux.NewRouter() + p.initRoutes() p.httpServer = &http.Server{ Addr: fmt.Sprintf(":%d", p.cfg.HttpPort()), - Handler: mux, + Handler: p.mux, } p.tracker = tracker.New(p.cfg) diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..0c402a4 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +export BT_ARGS="$@" +export DOCKER_ENV=true +envsubst < /etc/supervisord.tmpl > /etc/supervisor/conf.d/supervisord.conf && /usr/bin/supervisord diff --git a/scripts/supervisord.conf b/scripts/supervisord.conf new file mode 100644 index 0000000..77f515d --- /dev/null +++ b/scripts/supervisord.conf @@ -0,0 +1,13 @@ +[supervisord] +nodaemon=true +loglevel=debug + + + +[program:nginx] +command=/usr/sbin/nginx -g 'daemon off;' +redirect_stderr=true + +[program:beethoven] +command=/usr/sbin/beethoven serve ${BT_ARGS} +redirect_stderr=true \ No newline at end of file diff --git a/tracker/tracker.go b/tracker/tracker.go index 32896bd..a8f0d0a 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -22,6 +22,10 @@ func New(cfg *config.Config) *Tracker { } } +func (tr *Tracker) GetStatus() Status { + return tr.status +} + func (tr *Tracker) SetError(err error) { tr.status.LastError = err } diff --git a/tracker/types.go b/tracker/types.go index a9773a1..b2950f5 100644 --- a/tracker/types.go +++ b/tracker/types.go @@ -5,19 +5,19 @@ import ( ) type Updates struct { - LastSync time.Time - LastConfigRendered time.Time - LastConfigValid time.Time - LastProxyReload time.Time + LastSync time.Time `json:"last_sync"` + LastConfigRendered time.Time `json:"last_config_rendered"` + LastConfigValid time.Time `json:"last_config_valid"` + LastProxyReload time.Time `json:"last_proxy_reload"` } type Status struct { - LastUpdated Updates - LastError error - ValidationError *ValidationError + LastUpdated Updates `json:"last_updated"` + LastError error `json:"last_error"` + ValidationError *ValidationError `json:"validation_error"` } type ValidationError struct { - Error error - FailedConfig string + Error error `json:"error"` + FailedConfig string `json:"failed_config"` }