-
Notifications
You must be signed in to change notification settings - Fork 2
Initial version #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
5cf9726
568fd2e
4dac4f4
c991c4e
9e6b131
5c6a555
89ee754
bc03b15
e9bf43f
96874a9
b611ec4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/bin | ||
/pkg |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# api-gateway-config-supervisor | ||
# | ||
# VERSION 1.9.3.1 | ||
# | ||
# From https://hub.docker.com/_/alpine/ | ||
# | ||
FROM alpine:latest | ||
|
||
ENV GOPATH /usr/lib/go/bin | ||
ENV GOBIN /usr/lib/go/bin | ||
ENV PATH $PATH:/usr/lib/go/bin | ||
|
||
|
||
RUN mkdir -p /tmp/go | ||
ADD . /tmp/go | ||
RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \ | ||
&& apk update \ | ||
&& apk add make git go \ | ||
|
||
&& echo " building local project ... " \ | ||
&& cd /tmp/go \ | ||
&& make setup \ | ||
&& mkdir -p /tmp/go/Godeps/_workspace \ | ||
&& ln -s /tmp/go/vendor /tmp/go/Godeps/_workspace/src \ | ||
&& mkdir -p /tmp/go-src/src/github.com/adobe-apiplatform \ | ||
&& ln -s /tmp/go /tmp/go-src/src/github.com/adobe-apiplatform/api-gateway-config-supervisor \ | ||
&& GOPATH=/tmp/go/vendor:/tmp/go-src CGO_ENABLED=0 GOOS=linux /usr/lib/go/bin/godep go build -ldflags "-s" -a -installsuffix cgo -o api-gateway-config-supervisor ./ \ | ||
&& cp /tmp/go/api-gateway-config-supervisor /usr/lib/go/bin \ | ||
|
||
&& echo "installing rclone sync ... " \ | ||
&& go get github.com/ncw/rclone \ | ||
|
||
&& echo " cleaning up ... " \ | ||
&& rm -rf /usr/lib/go/bin/src \ | ||
&& rm -rf /tmp/go \ | ||
&& rm -rf /tmp/go-src \ | ||
&& rm -rf /usr/lib/go/bin/pkg/ \ | ||
&& rm -rf /usr/lib/go/bin/godep \ | ||
&& apk del make git go \ | ||
&& rm -rf /var/cache/apk/* | ||
|
||
RUN echo " installing aws-cli ..." \ | ||
&& apk update \ | ||
&& apk add python \ | ||
&& apk add py-pip \ | ||
&& pip install --upgrade pip \ | ||
&& pip install awscli | ||
|
||
ENTRYPOINT ["api-gateway-config-supervisor"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
GOPATH ?= `pwd` | ||
GOBIN ?= `pwd`/bin | ||
GOOS ?= $(`uname -a | awk '{print tolower($1)}'`) | ||
|
||
setup: | ||
go get github.com/tools/godep | ||
|
||
install: | ||
@go version | ||
export GO15VENDOREXPERIMENT=1 | ||
# GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install -v ./... | ||
GOPATH=$(GOPATH) GOBIN=$(GOBIN) $(GOPATH)/bin/godep go install -v ./... | ||
|
||
format: | ||
gofmt -e -w ./ | ||
|
||
static: | ||
# CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o $(GOBIN)/api-gateway-config-supervisor-static ./ | ||
CGO_ENABLED=0 go build -ldflags "-s" -a -installsuffix cgo -o $(GOBIN)/api-gateway-config-supervisor-static ./ | ||
|
||
docker: | ||
docker build -t adobeapiplatform/api-gateway-config-supervisor . | ||
|
||
.PHONY: docker-ssh | ||
docker-ssh: | ||
docker run -ti --entrypoint='/bin/sh' adobeapiplatform/api-gateway-config-supervisor:latest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"runtime" | ||
"runtime/pprof" | ||
"time" | ||
// "net/http" | ||
"github.com/adobe-apiplatform/api-gateway-config-supervisor/sync" | ||
"github.com/adobe-apiplatform/api-gateway-config-supervisor/ws" | ||
_ "net/http/pprof" | ||
|
||
"github.com/carlescere/scheduler" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"github.com/koyachi/go-term-ansicolor/ansicolor" | ||
) | ||
|
||
var ( | ||
// Flags | ||
cpuprofile = pflag.StringP("cpuprofile", "", "", "Write cpu profile to file") | ||
version = pflag.BoolP("version", "V", false, "Print the version number") | ||
syncInterval = pflag.DurationP("sync-interval", "", time.Second*5, "Time interval for the next sync") | ||
syncCmd = pflag.StringP("sync-cmd", "", "echo sync-cmd not defined", "Command used to syncing") | ||
syncFolder = pflag.StringP("sync-folder", "", "~/tmp/api-gateway-config", "The folder to watch for changes.") | ||
reloadCmd = pflag.StringP("reload-cmd", "", "echo reload-cmd not defined", "Command used to reload the gateway") | ||
httpAddr = pflag.StringP("http-addr", "", "127.0.0.1:8888", "Http Address exposing a /health-check for the sync process") | ||
// when was the reload cmd executed last time | ||
lastReload = time.Now() | ||
// when did the last change occur | ||
lastChange = time.Now() | ||
) | ||
|
||
func syntaxError() { | ||
fmt.Fprintf(os.Stderr, `Execute a sync command and watch a folder for changes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to same line. Is this after go fmt ? |
||
`) | ||
} | ||
|
||
// ParseFlags parses the command line flags | ||
func ParseFlags() { | ||
pflag.Usage = syntaxError | ||
pflag.Parse() | ||
runtime.GOMAXPROCS(runtime.NumCPU()) | ||
|
||
// Setup profiling if desired | ||
if *cpuprofile != "" { | ||
log.Println(ansicolor.Red("Starting CPU Profiling")) | ||
f, err := os.Create(*cpuprofile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
|
||
err = pprof.StartCPUProfile(f) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer pprof.StopCPUProfile() | ||
} | ||
} | ||
|
||
func executeSyncCmd() { | ||
go sync.Execute(*syncCmd) | ||
} | ||
|
||
func executeReloadCmd() { | ||
log.Println(ansicolor.Red("Executing Reload Cmd")) | ||
go sync.Execute(*reloadCmd) | ||
lastReload = time.Now() | ||
} | ||
|
||
func checkForReload() { | ||
if time.Since(lastChange) < time.Since(lastReload) && time.Since(lastReload) > *syncInterval { | ||
lastReload = time.Now() | ||
executeReloadCmd() | ||
} | ||
} | ||
|
||
//watches for changes in the syncFolder, execute reloadCmd when there are changes | ||
func watchForFSChanges() { | ||
c := sync.WatchFolderRecursive(*syncFolder) | ||
for { | ||
select { | ||
case file := <-c: | ||
if file == "" { | ||
continue | ||
} | ||
lastChange = time.Now() | ||
if time.Since(lastReload) > *syncInterval { | ||
lastReload = time.Now() | ||
go func() { | ||
// wait a little in case there are more changes to sync | ||
for time.Since(lastChange) < time.Second*1 { | ||
time.Sleep(1 * time.Second) | ||
} | ||
executeReloadCmd() | ||
}() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
ParseFlags() | ||
if *version { | ||
fmt.Printf("config-supervisor %s\n", "0.1") | ||
os.Exit(0) | ||
} | ||
|
||
go ws.RunWS(*httpAddr) | ||
|
||
go watchForFSChanges() | ||
scheduler.Every(int(syncInterval.Seconds())).Seconds().Run(executeSyncCmd) | ||
scheduler.Every(int(syncInterval.Seconds())).Seconds().Run(checkForReload) | ||
|
||
// http.ListenAndServe(":8181", nil) | ||
// Waiting for terminating (i use a sighandler like in vitess) | ||
terminate := make(chan os.Signal) | ||
signal.Notify(terminate, os.Interrupt) | ||
<-terminate | ||
|
||
log.Printf("Stopped ... ") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove please