This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Unbabel/brunotm.replicant-executor
Split replicant in server and test execution services, tag v0.2.0
- Loading branch information
Showing
95 changed files
with
10,620 additions
and
864 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM golang:alpine AS builder | ||
RUN apk --no-cache add git make | ||
COPY . /src/replicant | ||
WORKDIR /src/replicant | ||
|
||
RUN make build | ||
|
||
FROM chromedp/headless-shell:stable | ||
RUN apt update \ | ||
&& apt install -y ca-certificates \ | ||
&& apt clean; apt clean \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
COPY --from=builder /src/replicant/replicant /app/ | ||
ENTRYPOINT [] | ||
CMD ["/app/replicant"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package api | ||
|
||
/* | ||
Copyright 2019 Bruno Moura <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/Unbabel/replicant/server" | ||
"github.com/Unbabel/replicant/transaction" | ||
"github.com/Unbabel/replicant/transaction/callback" | ||
) | ||
|
||
// CallbackRequest services callback requests for transactions being run from executors | ||
func CallbackRequest(srv *server.Server) (handle server.Handler) { | ||
return func(w http.ResponseWriter, r *http.Request, p server.Params) { | ||
defer r.Body.Close() | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
|
||
var buf []byte | ||
var err error | ||
|
||
if buf, err = ioutil.ReadAll(r.Body); err != nil { | ||
httpError(w, fmt.Errorf("error reading request body: %w", err), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
config := transaction.Config{} | ||
if err = json.Unmarshal(buf, &config); err != nil { | ||
httpError(w, fmt.Errorf("error deserializing json request body: %w", err), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if config.CallBack == nil { | ||
httpError(w, fmt.Errorf("no callback config found"), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
listener, err := callback.GetListener(config.CallBack.Type) | ||
if err != nil { | ||
httpError(w, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
handler, err := listener.Listen(context.WithValue(r.Context(), "transaction_uuid", p.ByName("uuid"))) | ||
if err != nil { | ||
httpError(w, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
resp := <-handler.Response | ||
if resp.Error != nil { | ||
httpError(w, resp.Error, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(resp.Data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Unbabel/replicant/internal/cmdutil" | ||
"github.com/Unbabel/replicant/internal/executor" | ||
"github.com/Unbabel/replicant/log" | ||
"github.com/Unbabel/replicant/transaction" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
Executor.Flags().String("listen-address", "0.0.0.0:8081", "Address to for executor to listen on") | ||
Executor.Flags().String("server-url", "http://127.0.0.1:8080", "Replicant server url") | ||
Executor.Flags().Duration("max-runtime", time.Minute*5, "Maximum individual test runtime") | ||
Executor.Flags().Bool("debug", false, "Expose a debug profile endpoint at /debug/pprof") | ||
Executor.Flags().String("webhook-advertise-url", "http://localhost:8080", "URL to advertise when receiving webhook based async responses") | ||
Executor.Flags().String("chrome-remote-url", "http://127.0.0.1:9222", "Chrome remote debugging protocol server. For using remote chrome process instead of a local managed process") | ||
Executor.Flags().Bool("chrome-enable-local", true, "Enable running a local chrome worker process for web transactions") | ||
Executor.Flags().String("chrome-local-command", "/headless-shell/headless-shell --headless --no-zygote --no-sandbox --disable-gpu --disable-software-rasterizer --disable-dev-shm-usage --remote-debugging-address=127.0.0.1 --remote-debugging-port=9222 --incognito --disable-shared-workers --disable-remote-fonts --disable-background-networking --disable-crash-reporter --disable-default-apps --disable-domain-reliability --disable-extensions --disable-shared-workers --disable-setuid-sandbox", "Command for launching chrome with arguments included") | ||
Executor.Flags().Duration("chrome-recycle-interval", time.Minute*5, "Chrome recycle interval for locally managed chrome process") | ||
} | ||
|
||
// Executor command | ||
var Executor = &cobra.Command{ | ||
Use: "executor", | ||
Short: "Start the replicant transaction execution service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
config := executor.Config{} | ||
config.ServerURL = cmdutil.GetFlagString(cmd, "server-url") | ||
config.AdvertiseURL = cmdutil.GetFlagString(cmd, "webhook-advertise-url") | ||
|
||
// Setup chrome support for web applications | ||
config.Web.ServerURL = cmdutil.GetFlagString(cmd, "chrome-remote-url") | ||
|
||
if cmdutil.GetFlagBool(cmd, "chrome-enable-local") { | ||
arguments := strings.Split(cmdutil.GetFlagString(cmd, "chrome-local-command"), " ") | ||
config.Web.BinaryPath = arguments[:1][0] | ||
config.Web.BinaryArgs = arguments[1:] | ||
config.Web.RecycleInterval = cmdutil.GetFlagDuration(cmd, "chrome-recycle-interval") | ||
} | ||
|
||
server := &http.Server{} | ||
server.Addr = cmdutil.GetFlagString(cmd, "listen-address") | ||
server.ReadTimeout = cmdutil.GetFlagDuration(cmd, "max-runtime") | ||
server.WriteTimeout = cmdutil.GetFlagDuration(cmd, "max-runtime") | ||
server.ReadHeaderTimeout = cmdutil.GetFlagDuration(cmd, "max-runtime") | ||
router := httprouter.New() | ||
server.Handler = router | ||
|
||
e, err := executor.New(config) | ||
if err != nil { | ||
log.Error("error creating replicant-executor").Error("error", err).Log() | ||
os.Exit(1) | ||
} | ||
|
||
router.Handle(http.MethodPost, "/api/v1/run/:uuid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
defer r.Body.Close() | ||
|
||
uuid := p.ByName("uuid") | ||
var err error | ||
var buf []byte | ||
var config transaction.Config | ||
|
||
if buf, err = ioutil.ReadAll(r.Body); err != nil { | ||
httpError(w, uuid, config, fmt.Errorf("error reading request body: %w", err), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err = json.Unmarshal(buf, &config); err != nil { | ||
httpError(w, uuid, config, fmt.Errorf("error deserializing json request body: %w", err), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err = json.Unmarshal(buf, &config); err != nil { | ||
httpError(w, uuid, config, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
result, err := e.Run(uuid, config) | ||
if err != nil { | ||
httpError(w, uuid, config, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
buf, err = json.Marshal(&result) | ||
if err != nil { | ||
httpError(w, uuid, config, fmt.Errorf("error serializing results: %w", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(buf) | ||
}) | ||
|
||
signalCh := make(chan os.Signal, 1) | ||
signal.Notify(signalCh, os.Interrupt) | ||
|
||
// listen for stop signals | ||
go func() { | ||
<-signalCh | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
log.Error("error stopping replicant-executor").Error("error", err).Log() | ||
os.Exit(1) | ||
} | ||
}() | ||
|
||
log.Info("starting replicant-executor").Log() | ||
if err := server.ListenAndServe(); err != nil { | ||
log.Error("error running replicant-cdp").Error("error", err).Log() | ||
os.Exit(1) | ||
} | ||
|
||
log.Info("replicant-cdp stopped").Log() | ||
|
||
}, | ||
} | ||
|
||
// httpError wraps http status codes and error messages as json responses | ||
func httpError(w http.ResponseWriter, uuid string, config transaction.Config, err error, code int) { | ||
var result transaction.Result | ||
|
||
result.Name = config.Name | ||
result.Driver = config.Driver | ||
result.Metadata = config.Metadata | ||
result.Time = time.Now() | ||
result.DurationSeconds = 0 | ||
result.Failed = true | ||
result.Error = err | ||
|
||
res, _ := json.Marshal(&result) | ||
|
||
w.WriteHeader(code) | ||
w.Write(res) | ||
|
||
log.Error("handling web transaction request").Error("error", err).Log() | ||
} |
Oops, something went wrong.