8000 fix: restore previous session on coder server --dev by f0ssel · Pull Request #1821 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: restore previous session on coder server --dev #1821

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

Merged
merged 3 commits into from
May 27, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
gracefully handle no session
  • Loading branch information
f0ssel committed May 27, 2022
commit 71109e0d894f7592f13f4ece3deb829d8dc9dd13
74 changes: 35 additions & 39 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ func server() *cobra.Command {
return xerrors.Errorf("generate random admin password for dev: %w", err)
}
}
cleanupSession, err := createFirstUser(logger, cmd, client, config, devUserEmail, devUserPassword)
restorePreviousSession, err := createFirstUser(logger, cmd, client, config, devUserEmail, devUserPassword)
if err != nil {
return xerrors.Errorf("create first user: %w", err)
}
defer cleanupSession()
defer restorePreviousSession()
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", devUserEmail)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", devUserPassword)
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
Expand Down Expand Up @@ -520,7 +520,7 @@ func server() *cobra.Command {
}

// createFirstUser creates the first user and sets a valid session.
// Caller must call cleanup on server exit.
// Caller must call restorePreviousSession on server exit.
func createFirstUser(logger slog.Logger, cmd *cobra.Command, client *codersdk.Client, cfg config.Root, email, password string) (func(), error) {
if email == "" {
return nil, xerrors.New("email is empty")
Expand All @@ -546,43 +546,39 @@ func createFirstUser(logger slog.Logger, cmd *cobra.Command, client *codersdk.Cl
}
client.SessionToken = token.SessionToken

oldURL, err := cfg.URL().Read()
if err != nil {
return nil, xerrors.Errorf("write local url: %w", err)
}
oldSession, err := cfg.Session().Read()
if err != nil {
return nil, xerrors.Errorf("write session token: %w", err)
}

// recover session data when server exits
cleanup := func() {
currentURL, err := cfg.URL().Read()
if err != nil {
logger.Error(cmd.Context(), "failed to read current session url", slog.Error(err))
return
}
currentSession, err := cfg.Session().Read()
if err != nil {
logger.Error(cmd.Context(), "failed to read current session token", slog.Error(err))
return
}
// capture the current session and if exists recover session on server exit
restorePreviousSession := func() {}
oldURL, _ := cfg.URL().Read()
oldSession, _ := cfg.Session().Read()
if oldURL != "" && oldSession != "" {
restorePreviousSession = func() {
currentURL, err := cfg.URL().Read()
if err != nil {
logger.Error(cmd.Context(), "failed to read current session url", slog.Error(err))
return
}
currentSession, err := cfg.Session().Read()
if err != nil {
logger.Error(cmd.Context(), "failed to read current session token", slog.Error(err))
return
}

// if it's changed since we wrote to it don't restore session
if currentURL != client.URL.String() ||
currentSession != token.SessionToken {
return
}
// if it's changed since we wrote to it don't restore session
if currentURL != client.URL.String() ||
currentSession != token.SessionToken {
return
}

err = cfg.URL().Write(oldURL)
if err != nil {
logger.Error(cmd.Context(), "failed to recover previous session url", slog.Error(err))
return
}
err = cfg.Session().Write(oldSession)
if err != nil {
logger.Error(cmd.Context(), "failed to recover previous session token", slog.Error(err))
return
err = cfg.URL().Write(oldURL)
if err != nil {
logger.Error(cmd.Context(), "failed to recover previous session url", slog.Error(err))
return
}
err = cfg.Session().Write(oldSession)
if err != nil {
logger.Error(cmd.Context(), "failed to recover previous session token", slog.Error(err))
return
}
}
}

Expand All @@ -595,7 +591,7 @@ func createFirstUser(logger slog.Logger, cmd *cobra.Command, client *codersdk.Cl
return nil, xerrors.Errorf("write session token: %w", err)
}

return cleanup, nil
return restorePreviousSession, nil
}

// nolint:revive
Expand Down
0