-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathhandler_update.go
More file actions
78 lines (70 loc) · 2.37 KB
/
handler_update.go
File metadata and controls
78 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package web
import (
"context"
"net/http"
"runtime"
"time"
"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/updater"
"github.com/labstack/echo/v4"
)
const updateApplyTimeout = 5 * time.Minute
type VersionInfo struct {
Version string `json:"version"`
GitBranch string `json:"git_branch"`
GitRevision string `json:"git_revision"`
BuildTime string `json:"build_time"`
StartTime time.Time `json:"start_time"`
GoOS string `json:"go_os"`
GoArch string `json:"go_arch"`
}
func (s *Server) Version(c echo.Context) error {
return c.JSON(http.StatusOK, VersionInfo{
Version: constant.Version,
GitBranch: constant.GitBranch,
GitRevision: constant.GitRevision,
BuildTime: constant.BuildTime,
StartTime: constant.StartTime,
GoOS: runtime.GOOS,
GoArch: runtime.GOARCH,
})
}
func (s *Server) UpdateCheck(c echo.Context) error {
channel := c.QueryParam("channel")
if channel == "" {
channel = updater.ChannelAuto
}
ctx, cancel := context.WithTimeout(c.Request().Context(), 30*time.Second)
defer cancel()
res, err := updater.Check(ctx, channel, constant.Version, constant.GitRevision)
if err != nil {
return echo.NewHTTPError(http.StatusBadGateway, err.Error())
}
return c.JSON(http.StatusOK, res)
}
// UpdateApply kicks off the update in a detached goroutine and returns
// immediately. The dashboard polls /version to detect completion (the
// running process restarts mid-flow, so any in-process state machine is
// inherently lossy). Failures are logged via s.l; check journalctl.
func (s *Server) UpdateApply(c echo.Context) error {
if runtime.GOOS != "linux" {
return echo.NewHTTPError(http.StatusBadRequest,
"self-update only supported on linux; current platform is "+runtime.GOOS)
}
var opts updater.ApplyOptions
if err := c.Bind(&opts); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if opts.Channel == "" {
opts.Channel = updater.ChannelAuto
}
s.l.Infof("update apply requested channel=%s force=%v restart=%v", opts.Channel, opts.Force, opts.Restart)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), updateApplyTimeout)
defer cancel()
if err := updater.Apply(ctx, opts, constant.Version, constant.GitRevision, s.l); err != nil {
s.l.Errorf("update failed: %v", err)
}
}()
return c.NoContent(http.StatusAccepted)
}