110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package installguide
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
)
|
|
|
|
const progressFile = ".reims-go-install-guide.json"
|
|
|
|
type checkpoint struct {
|
|
Name string `json:"name"`
|
|
Kind Kind `json:"kind"`
|
|
Media string `json:"media,omitempty"`
|
|
Step page `json:"step"`
|
|
RestartFrom uint32 `json:"restart_from,omitempty"`
|
|
}
|
|
|
|
func (p page) postInstall() bool {
|
|
switch p {
|
|
case setup, network, account, localUser, preferences, desktop, restored, toolsTerminal, toolsVerify:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (p page) recoveryStep(k Kind) bool {
|
|
switch p {
|
|
case boot, waitInstall:
|
|
return true
|
|
case terminal, command, verifyCommand:
|
|
return k == Offline
|
|
case selectDisk, formatDisk:
|
|
return k == Online
|
|
case reinstall:
|
|
return k != Offline
|
|
case recoverMenu, mediaChoice:
|
|
return k == BuiltIn
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Resume attaches ordinary boots only when this VM has a pending post-install
|
|
// walkthrough. A restart request never replays an operation on startup.
|
|
func (g *Guide) Resume(dir, media string, recovery bool) bool {
|
|
g.dir, g.media, g.initial = dir, media, boot
|
|
if dir == "" {
|
|
return recovery
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(dir, progressFile))
|
|
var cp checkpoint
|
|
if err != nil || json.Unmarshal(raw, &cp) != nil || cp.Name != g.Name {
|
|
return recovery
|
|
}
|
|
if recovery {
|
|
// Older checkpoints may contain the abbreviated installer path from show.
|
|
sameMedia := strings.EqualFold(config.ExpandHomePath(cp.Media), media)
|
|
if cp.Kind == g.Kind && sameMedia && cp.Step.recoveryStep(g.Kind) {
|
|
g.initial = cp.Step
|
|
}
|
|
return true
|
|
}
|
|
if !cp.Step.postInstall() {
|
|
return false
|
|
}
|
|
g.Kind, g.initial = cp.Kind, cp.Step
|
|
if cp.RestartFrom != 0 && cp.RestartFrom == g.PID {
|
|
g.initial = restartPending
|
|
} else if g.initial == toolsTerminal && !g.ToolsAttached {
|
|
g.initial = desktop
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (g Guide) save(step page, restartFrom uint32) error {
|
|
if g.dir == "" {
|
|
return errors.New("missing VM directory for installation progress")
|
|
}
|
|
path := filepath.Join(g.dir, progressFile)
|
|
if step == done {
|
|
err := os.Remove(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
b, err := json.Marshal(checkpoint{g.Name, g.Kind, g.media, step, restartFrom})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f, err := os.CreateTemp(g.dir, ".install-guide-*.tmp")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(f.Name())
|
|
_, err = f.Write(b)
|
|
closeErr := f.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if closeErr != nil {
|
|
return closeErr
|
|
}
|
|
return os.Rename(f.Name(), path)
|
|
}
|