From 17f08daa173220dbba91b83bc1e7868b45c8a9ab Mon Sep 17 00:00:00 2001 From: Artur Albov Date: Fri, 19 Oct 2018 15:54:38 +0300 Subject: [PATCH] Add cyberdproxy process to docker container. Add status endpoint --- cosmos/poc/Dockerfile | 7 ++-- cosmos/poc/cyberdproxy/cyberdproxy.go | 7 ++-- cosmos/poc/cyberdproxy/proxy/account.go | 28 ---------------- cosmos/poc/cyberdproxy/proxy/get.go | 41 ++++++++++++++++++++++++ cosmos/poc/cyberdproxy/proxy/health.go | 21 ------------ cosmos/poc/cyberdproxy/proxy/search.go | 28 ---------------- cosmos/poc/docker_build_run.sh | 2 +- cosmos/poc/scripts/reset_earth_docker.sh | 2 +- cosmos/poc/start_script.sh | 36 +++++++++++++++++++++ 9 files changed, 88 insertions(+), 84 deletions(-) delete mode 100644 cosmos/poc/cyberdproxy/proxy/account.go create mode 100644 cosmos/poc/cyberdproxy/proxy/get.go delete mode 100644 cosmos/poc/cyberdproxy/proxy/health.go delete mode 100644 cosmos/poc/cyberdproxy/proxy/search.go create mode 100644 cosmos/poc/start_script.sh diff --git a/cosmos/poc/Dockerfile b/cosmos/poc/Dockerfile index 96019730..fb5bb1d0 100644 --- a/cosmos/poc/Dockerfile +++ b/cosmos/poc/Dockerfile @@ -18,13 +18,16 @@ COPY --from=builder /go/bin/cyberd /usr/bin/cyberd COPY --from=builder /go/bin/cyberdcli /usr/bin/cyberdcli COPY --from=builder /go/bin/cyberdproxy /usr/bin/cyberdproxy +COPY start_script.sh start_script.sh +RUN chmod +x start_script.sh + COPY genesis.json /genesis.json COPY config.toml /config.toml -EXPOSE 26656 26657 +EXPOSE 26656 26657 26660 COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] -CMD ["cyberd", "start"] +CMD ["./start_script.sh"] diff --git a/cosmos/poc/cyberdproxy/cyberdproxy.go b/cosmos/poc/cyberdproxy/cyberdproxy.go index b6afcc1b..b9753205 100644 --- a/cosmos/poc/cyberdproxy/cyberdproxy.go +++ b/cosmos/poc/cyberdproxy/cyberdproxy.go @@ -46,9 +46,10 @@ func StartCmd() *cobra.Command { mux := http.NewServeMux() mux.HandleFunc("/link", proxy.LinkHandlerFn(ctx)) - mux.HandleFunc("/search", proxy.SearchHandlerFn(ctx)) - mux.HandleFunc("/account", proxy.AccountHandlerFn(ctx)) - mux.HandleFunc("/health", proxy.HealthHandlerFn(ctx)) + mux.HandleFunc("/search", proxy.GetWithParamHandlerFn(ctx, "/search", "cid")) + mux.HandleFunc("/account", proxy.GetWithParamHandlerFn(ctx, "/account", "address")) + mux.HandleFunc("/health", proxy.GetHandlerFn(ctx, "/health")) + mux.HandleFunc("/status", proxy.GetHandlerFn(ctx, "/status")) c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, diff --git a/cosmos/poc/cyberdproxy/proxy/account.go b/cosmos/poc/cyberdproxy/proxy/account.go deleted file mode 100644 index e357bbb9..00000000 --- a/cosmos/poc/cyberdproxy/proxy/account.go +++ /dev/null @@ -1,28 +0,0 @@ -package proxy - -import ( - "net/http" -) - -func AccountHandlerFn(ctx ProxyContext) func(http.ResponseWriter, *http.Request) { - - return func(w http.ResponseWriter, r *http.Request) { - - w.Header().Set("Content-Type", "application/json") - - address, err := getSingleParamValue("address", r) - if err != nil { - HandleError(err, w) - return - } - - resp, err := ctx.Get("/account?address=\"" + address + "\"") - if err != nil { - HandleError(err, w) - return - } - - w.Write(resp) - - } -} diff --git a/cosmos/poc/cyberdproxy/proxy/get.go b/cosmos/poc/cyberdproxy/proxy/get.go new file mode 100644 index 00000000..83d70668 --- /dev/null +++ b/cosmos/poc/cyberdproxy/proxy/get.go @@ -0,0 +1,41 @@ +package proxy + +import "net/http" + +func GetHandlerFn(ctx ProxyContext, endpoint string) func(http.ResponseWriter, *http.Request) { + + return func(w http.ResponseWriter, r *http.Request) { + + w.Header().Set("Content-Type", "application/json") + + resp, err := ctx.Get(endpoint) + if err != nil { + HandleError(err, w) + return + } + + w.Write(resp) + } +} + +func GetWithParamHandlerFn(ctx ProxyContext, endpoint string, param string) func(http.ResponseWriter, *http.Request) { + + return func(w http.ResponseWriter, r *http.Request) { + + w.Header().Set("Content-Type", "application/json") + + paramValue, err := getSingleParamValue(param, r) + if err != nil { + HandleError(err, w) + return + } + + resp, err := ctx.Get(endpoint + "?" + param + "=\"" + paramValue + "\"") + if err != nil { + HandleError(err, w) + return + } + + w.Write(resp) + } +} diff --git a/cosmos/poc/cyberdproxy/proxy/health.go b/cosmos/poc/cyberdproxy/proxy/health.go deleted file mode 100644 index 0c8f70cc..00000000 --- a/cosmos/poc/cyberdproxy/proxy/health.go +++ /dev/null @@ -1,21 +0,0 @@ -package proxy - -import ( - "net/http" -) - -func HealthHandlerFn(ctx ProxyContext) func(http.ResponseWriter, *http.Request) { - - return func(w http.ResponseWriter, r *http.Request) { - - w.Header().Set("Content-Type", "application/json") - - resp, err := ctx.Get("/health") - if err != nil { - HandleError(err, w) - return - } - - w.Write(resp) - } -} diff --git a/cosmos/poc/cyberdproxy/proxy/search.go b/cosmos/poc/cyberdproxy/proxy/search.go deleted file mode 100644 index 2069d0ba..00000000 --- a/cosmos/poc/cyberdproxy/proxy/search.go +++ /dev/null @@ -1,28 +0,0 @@ -package proxy - -import ( - "net/http" -) - -func SearchHandlerFn(ctx ProxyContext) func(http.ResponseWriter, *http.Request) { - - return func(w http.ResponseWriter, r *http.Request) { - - w.Header().Set("Content-Type", "application/json") - - cid, err := getSingleParamValue("cid", r) - if err != nil { - HandleError(err, w) - return - } - - resp, err := ctx.Get("/search?cid=\"" + cid + "\"") - if err != nil { - HandleError(err, w) - return - } - - w.Write(resp) - - } -} diff --git a/cosmos/poc/docker_build_run.sh b/cosmos/poc/docker_build_run.sh index 5fb92280..ad3e4938 100644 --- a/cosmos/poc/docker_build_run.sh +++ b/cosmos/poc/docker_build_run.sh @@ -1,4 +1,4 @@ #!/bin/bash docker build -t cybercongress/cyberd . -docker run -d --restart always --name=cyberd -p 34656:26656 -p 34657:26657 -v /cyberdata/cyberd/data:/root/.cyberd/data -v /cyberdata/cyberd/config:/root/.cyberd/config cybernode/cyberd:master \ No newline at end of file +docker run -d --restart always --name=cyberd -p 34656:26656 -p 34657:26657 -p 34660:26660 -v /cyberdata/cyberd/data:/root/.cyberd/data -v /cyberdata/cyberd/config:/root/.cyberd/config cybernode/cyberd:master \ No newline at end of file diff --git a/cosmos/poc/scripts/reset_earth_docker.sh b/cosmos/poc/scripts/reset_earth_docker.sh index f79025fb..36a8dc01 100644 --- a/cosmos/poc/scripts/reset_earth_docker.sh +++ b/cosmos/poc/scripts/reset_earth_docker.sh @@ -3,4 +3,4 @@ docker stop cyberd docker rm cyberd docker run --rm -v /cyberdata/cyberd/data:/root/.cyberd/data -v /cyberdata/cyberd/config:/root/.cyberd/config cybernode/cyberd:master cyberd unsafe_reset_all docker pull cybernode/cyberd:master -docker run -d --restart always --name=cyberd -p 34656:26656 -p 34657:26657 -v /cyberdata/cyberd/data:/root/.cyberd/data -v /cyberdata/cyberd/config:/root/.cyberd/config cybernode/cyberd:master \ No newline at end of file +docker run -d --restart always --name=cyberd -p 34656:26656 -p 34657:26657 -p 34660:26660 -v /cyberdata/cyberd/data:/root/.cyberd/data -v /cyberdata/cyberd/config:/root/.cyberd/config cybernode/cyberd:master \ No newline at end of file diff --git a/cosmos/poc/start_script.sh b/cosmos/poc/start_script.sh new file mode 100644 index 00000000..f2b7abe0 --- /dev/null +++ b/cosmos/poc/start_script.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# Start the first process +cyberd start . & +status=$? +if [ $status -ne 0 ]; then + echo "Failed to start cyberd: $status" + exit $status +fi + +# Start the second process +cyberdproxy start . & +status=$? +if [ $status -ne 0 ]; then + echo "Failed to start cyberdproxy: $status" + exit $status +fi + +# Naive check runs checks once a minute to see if either of the processes exited. +# This illustrates part of the heavy lifting you need to do if you want to run +# more than one service in a container. The container exits with an error +# if it detects that either of the processes has exited. +# Otherwise it loops forever, waking up every 60 seconds +while sleep 60; do + ps aux |grep cyberd |grep -q -v grep + PROCESS_1_STATUS=$? + ps aux |grep cyberdproxy |grep -q -v grep + PROCESS_2_STATUS=$? + # If the greps above find anything, they exit with 0 status + # If they are not both 0, then something is wrong + if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then + echo "One of the processes has already exited." + exit 1 + fi +done +