Skip to content

Commit

Permalink
fix: serve respect bundle version (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
npaton authored Jan 21, 2024
1 parent 1503d53 commit fba5e09
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 198 deletions.
5 changes: 5 additions & 0 deletions .changeset/serve-respects-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@empirica/core": patch
---

`empirica serve` now respects the version in the bundle.
178 changes: 0 additions & 178 deletions build/download.sh

This file was deleted.

15 changes: 10 additions & 5 deletions cmds/empirica/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func addServeCommand(parent *cobra.Command) error {
return errors.New("missing project name")
}

conf := getConfig(true)

ctx := initContext()

clean, err := cmd.Flags().GetBool("clean")
Expand All @@ -47,7 +49,6 @@ func addServeCommand(parent *cobra.Command) error {

in := args[0]

conf := getConfig(true)
conf.Server.Addr = addr

return bundle.Serve(ctx, conf, in, clean, devMode)
Expand All @@ -63,10 +64,6 @@ func addServeCommand(parent *cobra.Command) error {
return nil
}

if err := empirica.ConfigFlags(cmd); err != nil {
return errors.Wrap(err, "define flags")
}

flag := "addr"
sval := ":3000"
cmd.Flags().String(flag, sval, "Address of the server")
Expand All @@ -77,10 +74,18 @@ func addServeCommand(parent *cobra.Command) error {
cmd.Flags().Bool(flag, bval, "Start in dev mode (no auth)")
viper.SetDefault(flag, bval)

if err := empirica.ConfigFlags(cmd); err != nil {
return errors.Wrap(err, "define flags")
}

if err := viper.BindPFlags(cmd.Flags()); err != nil {
return errors.Wrap(err, "bind serve flags")
}

if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return errors.Wrap(err, "bind persistent serve flags")
}

parent.AddCommand(cmd)

return nil
Expand Down
8 changes: 4 additions & 4 deletions cmds/proxy/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ func root(args []string) error {
var er *exec.ExitError
if errors.As(err, &er) {
os.Exit(er.ExitCode())
} else {
return errors.Wrap(err, "failed to start")
}

return errors.Wrap(err, "failed to start")
}

if err := c.Wait(); err != nil {
var er *exec.ExitError
if errors.As(err, &er) {
os.Exit(er.ExitCode())
} else {
return errors.Wrap(err, "failed to start")
}

return errors.Wrap(err, "failed to start")
}

return nil
Expand Down
55 changes: 50 additions & 5 deletions internal/bundle/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,55 @@ func Serve(ctx context.Context, config *empirica.Config, in string, clean, devMo
log.Fatal().Err(err).Msg("invalid config")
}

b, err := build.FindCurrentBinaryVersion()
if err != nil {
log.Fatal().Err(err).Msg("failed to find bundle version")
}

// If we are not running the correct version, restart with the correct
// version. The new version will automatically be picked up on restart since
// the version in the bundle will have already been exported to the
// .empirica/release file in the current dir.
if b.Version != build.Current().Version && os.Getenv(build.BuildSelectionEnvVar) == "" {
log.Info().
Str("from", build.Current().Version).
Str("to", b.Version).
Msg("serve: switching to empirica version in bundle")

if os.Getenv("EMPIRICA_SUBPROC") != "" {
log.Warn().Msg("serve: failed to run correct version of bundle, exiting...")

return nil
}

c := exec.CommandContext(ctx, "empirica", os.Args[1:]...)

c.Stderr = os.Stderr
c.Stdout = os.Stdout

c.Env = append(os.Environ(), "EMPIRICA_SUBPROC=1")

if err := c.Start(); err != nil {
var er *exec.ExitError
if errors.As(err, &er) {
os.Exit(er.ExitCode())
}

return errors.Wrap(err, "failed to start")
}

if err := c.Wait(); err != nil {
var er *exec.ExitError
if errors.As(err, &er) {
os.Exit(er.ExitCode())
}

return errors.Wrap(err, "failed to start")
}

return nil
}

go func(ctx context.Context) {
parts := strings.Split(conf.Callbacks.ServeCmd, " ")
if len(parts) == 0 {
Expand Down Expand Up @@ -90,12 +139,8 @@ func Serve(ctx context.Context, config *empirica.Config, in string, clean, devMo
return
}

log.Info().Msg("serve: server started")

if err := c.Wait(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
log.Debug().Msg("serve: restarting server")

return
}

Expand Down Expand Up @@ -143,7 +188,7 @@ func Serve(ctx context.Context, config *empirica.Config, in string, clean, devMo
log.Error().Err(err).Msg("serve: failed to get build version")
}

log.Info().
log.Debug().
RawJSON("build", bver).
Msg("tajriba: server started")

Expand Down
12 changes: 6 additions & 6 deletions internal/bundle/unbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func Unbundle(_ context.Context, config *empirica.Config, in string, clean, devM
dir := path.Join(os.TempDir(), hash)

if _, err := os.Stat(dir); err == nil {
log.Info().
log.Debug().
Str("target", dir).
Msg("unbundle: already installed")
Msg("serve: bundle already extracted")

if clean {
log.Info().
Str("target", dir).
Msg("unbundle: removing old installation")
Msg("serve: removing old bundle extraction")

if err := os.RemoveAll(dir); err != nil {
return "", nil, errors.Wrap(err, "remove previous installation")
Expand All @@ -145,9 +145,9 @@ func Unbundle(_ context.Context, config *empirica.Config, in string, clean, devM
}
}

log.Info().
log.Debug().
Str("target", dir).
Msg("unbundle: installing")
Msg("serve: extracting bundle")

if _, err := f.Seek(0, io.SeekStart); err != nil {
return "", nil, errors.Wrap(err, "rewind file")
Expand Down Expand Up @@ -220,7 +220,7 @@ func unbundleFile(tr *tar.Reader, header *tar.Header, dir string) error {
log.Trace().
Str("from", header.Name).
Str("to", target).
Msg("unbundle: copying file")
Msg("serve: copying file")

// Sanity check, we're still in our dir.
targetdir := path.Dir(target)
Expand Down

0 comments on commit fba5e09

Please sign in to comment.