cmd

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2020 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package cmd implements replicant commands

Index

Constants

This section is empty.

Variables

View Source
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")

		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)

		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()

	},
}

Executor command

View Source
var Root = &cobra.Command{
	Use:   "replicant",
	Short: "replicant command line interface",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		log.Init(cmdutil.GetFlagString(cmd, "log-level"))

	},
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Usage()
	},
}

Root command for replicant

View Source
var Run = &cobra.Command{
	Use:   "run",
	Short: "Run transactions for local development",
	Run: func(cmd *cobra.Command, args []string) {
		buf, err := ioutil.ReadFile(cmdutil.GetFlagString(cmd, "file"))
		if err != nil {
			log.Error("error reading transaction").String("file", cmdutil.GetFlagString(cmd, "file")).Error("error", err).Log()
			os.Exit(1)
		}

		tx := transaction.Config{}
		if err = yaml.Unmarshal(buf, &tx); err != nil {
			log.Error("error reading transaction").String("file", cmdutil.GetFlagString(cmd, "file")).Error("error", err).Log()
			os.Exit(1)
		}

		if tx.CallBack != nil {
			log.Error("callbacks not supported in local runs").Log()
			os.Exit(1)
		}

		tx, err = tmpl.Parse(tx)
		if err != nil {
			log.Error("error parsing transaction").String("file", cmdutil.GetFlagString(cmd, "file")).Error("error", err).Log()
			os.Exit(1)
		}

		config := executor.Config{}
		config.Web.ServerURL = cmdutil.GetFlagString(cmd, "chrome-remote-url")
		e, err := executor.New(config)
		if err != nil {
			log.Error("error creating local replicant-executor").Error("error", err).Log()
			os.Exit(1)
		}

		em := stdout.New(stdout.Config{Pretty: true})
		result, err := e.Run(ksuid.New().String(), tx)
		if err != nil {
			log.Error("error running transaction").Error("error", err).Log()
			os.Exit(1)
		}

		em.Emit(result)
		fmt.Print()
	},
}

Run command

View Source
var Server = &cobra.Command{
	Use:   "server",
	Short: "Start the replicant server",
	Run: func(cmd *cobra.Command, args []string) {

		router := httprouter.New()

		storeURI := cmdutil.GetFlagString(cmd, "store-uri")
		st, err := store.New(storeURI)
		if err != nil {
			log.Error("could not initialize store").String("error", err.Error()).Log()
			os.Exit(1)
		}

		executorURL := cmdutil.GetFlagString(cmd, "executor-url")
		m := manager.New(st, executorURL)

		emitStdout := cmdutil.GetFlagBool(cmd, "emit-stdout")
		if emitStdout {
			m.AddEmitter(stdout.New(stdout.Config{Pretty: cmdutil.GetFlagBool(cmd, "emit-stdout-pretty")}))
		}

		emitPrometheus := cmdutil.GetFlagBool(cmd, "emit-prometheus")
		if emitPrometheus {
			e, err := prometheus.New(prometheus.DefaultConfig, router)
			if err != nil {
				log.Error("failed to create server").String("error", err.Error()).Log()
				os.Exit(1)
			}
			m.AddEmitter(e)
		}

		webhookURL := cmdutil.GetFlagString(cmd, "webhook-advertise-url")
		webhookPrefix := cmdutil.GetFlagString(cmd, "webhook-path-prefix")

		err = callback.Register("webhook", webhook.New(
			webhook.Config{AdvertiseURL: webhookURL, PathPrefix: webhookPrefix}, router))
		if err != nil {
			log.Error("failed to create server").String("error", err.Error()).Log()
			os.Exit(1)
		}

		address := cmdutil.GetFlagString(cmd, "listen-address")
		timeout := cmdutil.GetFlagDuration(cmd, "max-runtime")

		srv, err := server.New(server.Config{
			ListenAddress:     address,
			ReadTimeout:       timeout,
			WriteTimeout:      timeout,
			ReadHeaderTimeout: timeout},
			m, router)

		if err != nil {
			log.Error("failed to create replicant server").String("error", err.Error()).Log()
			os.Exit(1)
		}

		if cmdutil.GetFlagBool(cmd, "debug") {
			log.Info("adding debug api routes for runtime profiling data").Log()
			api.AddDebugRoutes(srv)
		}

		api.AddAllRoutes("/api", srv)
		go srv.Start()

		signalCh := make(chan os.Signal, 1)
		signal.Notify(signalCh, os.Interrupt)

		log.Info("replicant server started").
			String("address", address).Log()
		<-signalCh

		if err = srv.Close(context.Background()); err != nil {
			log.Info("replicant server stopped").String("error", err.Error()).Log()
			os.Exit(1)
		}

		log.Info("replicant server stopped").Log()
	},
}

Server command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL