-
Notifications
You must be signed in to change notification settings - Fork 29
/
provision.go
86 lines (70 loc) · 2.17 KB
/
provision.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
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package provision
import (
"runtime"
"sync"
"time"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/plugin"
"github.com/choria-io/go-choria/providers/agent/mcorpc"
"github.com/choria-io/go-choria/server"
"github.com/choria-io/go-choria/server/agents"
"github.com/sirupsen/logrus"
)
// Reply is a generic reply used by most actions
type Reply struct {
Message string `json:"message"`
}
var mu = &sync.Mutex{}
var allowRestart = true
var ecdhPublic []byte
var ecdhPrivate []byte
var log *logrus.Entry
var metadata = &agents.Metadata{
Name: "choria_provision",
Description: "Choria Provisioner",
Author: "R.I.Pienaar <rip@devco.net>",
Version: build.Version,
License: build.License,
Timeout: 20,
URL: "https://choria.io",
}
var restartCb = restart
func init() {
switch {
case runtime.GOOS == "windows":
SetRestartAction(restartViaExit)
default:
SetRestartAction(restart)
}
}
// New creates a new instance of the agent
func New(mgr server.AgentManager) (agents.Agent, error) {
log = mgr.Logger()
agent := mcorpc.New("choria_provision", metadata, mgr.Choria(), log)
agent.SetActivationChecker(func() bool {
return mgr.Choria().SupportsProvisioning()
})
agent.MustRegisterAction("gencsr", csrAction)
agent.MustRegisterAction("gen25519", ed25519Action)
agent.MustRegisterAction("configure", configureAction)
agent.MustRegisterAction("restart", restartAction)
agent.MustRegisterAction("reprovision", reprovisionAction)
agent.MustRegisterAction("release_update", releaseUpdateAction)
agent.MustRegisterAction("jwt", jwtAction)
return agent, nil
}
// ChoriaPlugin creates the choria plugin hooks
func ChoriaPlugin() plugin.Pluggable {
return mcorpc.NewChoriaAgentPlugin(metadata, New)
}
// SetRestartAction sets a custom restart function to call than the default that
// causes a os.Exec() to be issued replacing the running instance with a new
// process on the old pid
func SetRestartAction(f func(splay time.Duration, si agents.ServerInfoSource, log *logrus.Entry)) {
mu.Lock()
restartCb = f
mu.Unlock()
}