-
Notifications
You must be signed in to change notification settings - Fork 29
/
release_update_action.go
74 lines (58 loc) · 1.83 KB
/
release_update_action.go
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
package provision
import (
"context"
"fmt"
"time"
"github.com/choria-io/go-choria/inter"
"github.com/choria-io/go-choria/lifecycle"
"github.com/choria-io/go-choria/providers/agent/mcorpc"
"github.com/choria-io/go-updater"
)
type ReleaseUpdateRequest struct {
Token string `json:"token"`
Repository string `json:"repository"`
Version string `json:"version"`
}
var updaterf func(...updater.Option) error
func releaseUpdateAction(ctx context.Context, req *mcorpc.Request, reply *mcorpc.Reply, agent *mcorpc.Agent, conn inter.ConnectorInfo) {
if !agent.Choria.ProvisionMode() {
abort("Cannot reconfigure a server that is not in provisioning mode", reply)
return
}
mu.Lock()
defer mu.Unlock()
args := ReleaseUpdateRequest{}
if !mcorpc.ParseRequestData(&args, req, reply) {
return
}
if !checkToken(args.Token, reply) {
return
}
opts := []updater.Option{
updater.Version(args.Version),
updater.SourceRepo(args.Repository),
updater.Logger(agent.Log),
}
err := versionUpdater()(opts...)
if err != nil {
if err := updater.RollbackError(err); err != nil {
abort(fmt.Sprintf("Update to version %s failed, rollback also failed, server in broken state: %s", args.Version, err), reply)
return
}
abort(fmt.Sprintf("Update to version %s failed, release rolled back: %s", args.Version, err), reply)
return
}
err = agent.ServerInfoSource.NewEvent(lifecycle.Shutdown)
if err != nil {
agent.Log.Errorf("Could not publish shutdown event: %s", err)
}
reply.Data = Reply{"Restarting Choria Server after 2s"}
agent.Log.Warnf("Restarting server via request %s from %s (%s) with splay 2s", req.RequestID, req.CallerID, req.SenderID)
go restartCb(2*time.Second, agent.ServerInfoSource, agent.Log)
}
func versionUpdater() func(...updater.Option) error {
if updaterf != nil {
return updaterf
}
return updater.Apply
}