-
Notifications
You must be signed in to change notification settings - Fork 57
/
standalone.go
350 lines (303 loc) · 9.23 KB
/
standalone.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package standalone
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/grafana/grafana-plugin-sdk-go/internal"
)
var (
standaloneEnabled = flag.Bool("standalone", false, "should this run standalone")
)
func NewServerSettings(address, dir string) ServerSettings {
return ServerSettings{
Address: address,
Dir: dir,
}
}
type ServerSettings struct {
Address string
Dir string
}
type ClientSettings struct {
TargetAddress string
TargetPID int
}
// ServerModeEnabled returns true if the plugin should run in standalone server mode.
func ServerModeEnabled(pluginID string) (ServerSettings, bool) {
flag.Parse() // Parse the flags so that we can check values for -standalone
if *standaloneEnabled {
s, err := serverSettings(pluginID)
if err != nil {
log.Printf("Error: %s", err.Error())
return ServerSettings{}, false
}
return s, true
}
return ServerSettings{}, false
}
// ClientModeEnabled returns standalone server connection settings if a standalone server is running and can be reached.
func ClientModeEnabled(pluginID string) (ClientSettings, bool) {
s, err := clientSettings(pluginID)
if err != nil {
log.Printf("Error: %s", err.Error())
return ClientSettings{}, false
}
return s, true
}
// RunDummyPluginLocator runs the standalone server locator that Grafana uses to connect to the standalone GRPC server.
func RunDummyPluginLocator(address string) {
fmt.Printf("1|2|tcp|%s|grpc\n", address)
t := time.NewTicker(time.Second * 10)
for ts := range t.C {
fmt.Printf("[%s] using address: %s\n", ts.Format("2006-01-02 15:04:05"), address)
}
}
func serverSettings(pluginID string) (ServerSettings, error) {
curProcPath, err := currentProcPath()
if err != nil {
return ServerSettings{}, err
}
dir := filepath.Dir(curProcPath) // Default to current directory
if pluginDir, found := findPluginRootDir(curProcPath); found {
dir = pluginDir
}
address, err := createStandaloneAddress(pluginID)
if err != nil {
return ServerSettings{}, err
}
return ServerSettings{
Address: address,
Dir: dir,
}, nil
}
// clientSettings will attempt to find a standalone server's address and PID.
func clientSettings(pluginID string) (ClientSettings, error) {
procPath, err := currentProcPath()
if err != nil {
return ClientSettings{}, err
}
dir := filepath.Dir(procPath)
// Determine running standalone address + PID
standaloneAddress, err := getStandaloneAddress(pluginID, dir)
if err != nil {
return ClientSettings{}, err
}
if standaloneAddress == "" {
return ClientSettings{}, errors.New("address is empty")
}
standalonePID, err := getStandalonePID(dir)
if err != nil {
return ClientSettings{}, err
}
return ClientSettings{
TargetAddress: standaloneAddress,
TargetPID: standalonePID,
}, nil
}
func currentProcPath() (string, error) {
curProcPath, err := os.Executable()
if err != nil {
return "", err
}
return curProcPath, nil
}
// findPluginRootDir will attempt to find a plugin directory based on the executing process's file-system path.
func findPluginRootDir(curProcPath string) (string, bool) {
cwd, _ := os.Getwd()
if filepath.Base(cwd) == "pkg" {
cwd = filepath.Join(cwd, "..")
}
check := []string{
filepath.Join(curProcPath, "plugin.json"),
filepath.Join(curProcPath, "dist", "plugin.json"),
filepath.Join(filepath.Dir(curProcPath), "plugin.json"),
filepath.Join(filepath.Dir(curProcPath), "dist", "plugin.json"),
filepath.Join(cwd, "dist", "plugin.json"),
filepath.Join(cwd, "plugin.json"),
}
for _, path := range check {
if _, err := os.Stat(path); err == nil {
return filepath.Dir(path), true
}
}
return "", false
}
func getStandaloneAddress(pluginID string, dir string) (string, error) {
if addressEnvVar, exists := standaloneAddressFromEnv(pluginID); exists {
return addressEnvVar, nil
}
// Check the local file for address
fb, err := os.ReadFile(standaloneAddressFilePath(dir))
addressFileContent := string(bytes.TrimSpace(fb))
switch {
case err != nil && !os.IsNotExist(err):
return "", fmt.Errorf("read standalone file: %w", err)
case os.IsNotExist(err) || len(addressFileContent) == 0:
// No standalone file, do not treat as standalone
return "", nil
}
return addressFileContent, nil
}
func createStandaloneAddress(pluginID string) (string, error) {
if addressEnvVar, exists := standaloneAddressFromEnv(pluginID); exists {
return addressEnvVar, nil
}
port, err := getFreePort()
if err != nil {
return "", fmt.Errorf("get free port: %w", err)
}
return fmt.Sprintf(":%d", port), nil
}
func standaloneAddressFromEnv(pluginID string) (string, bool) {
addressEnvVar := "GF_PLUGIN_GRPC_ADDRESS_" + strings.ReplaceAll(strings.ToUpper(pluginID), "-", "_")
if v, ok := os.LookupEnv(addressEnvVar); ok {
return v, true
}
return "", false
}
func getStandalonePID(dir string) (int, error) {
// Read PID (optional, as it was introduced later on)
fb, err := os.ReadFile(standalonePIDFilePath(dir))
pidFileContent := string(bytes.TrimSpace(fb))
switch {
case err != nil && !os.IsNotExist(err):
return 0, fmt.Errorf("read pid file: %w", err)
case os.IsNotExist(err) || len(pidFileContent) == 0:
// No PID, this is optional as it was introduced later, so it's fine.
// We lose hot switching between debug and non-debug without the pid file,
// but there's nothing better we can do.
return 0, nil
default:
pid, err := strconv.Atoi(pidFileContent)
if err != nil {
return 0, fmt.Errorf("could not parse pid: %w", err)
}
if !checkPIDIsRunning(pid) {
return 0, errors.New("standalone server is not running")
}
return pid, nil
}
}
// getFreePort returns a free port number on localhost.
func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer func() { _ = l.Close() }()
return l.Addr().(*net.TCPAddr).Port, nil
}
// FindAndKillCurrentPlugin kills the currently registered plugin, causing Grafana to restart it
// and connect to our new host.
func FindAndKillCurrentPlugin(dir string) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error finding processes", r)
}
}()
executablePath, err := internal.GetExecutableFromPluginJSON(dir)
if err != nil {
fmt.Printf("missing executable in plugin.json (standalone)")
return
}
out, err := exec.Command("pgrep", "-f", executablePath).Output()
if err != nil {
fmt.Printf("error running pgrep: %s (%s)", err.Error(), executablePath)
return
}
currentPID := os.Getpid()
for _, txt := range strings.Fields(string(out)) {
pid, err := strconv.Atoi(txt)
if err == nil {
// Do not kill the plugin process
if pid == currentPID {
continue
}
log.Printf("Killing process: %d", pid)
// err := syscall.Kill(pid, 9)
pidstr := fmt.Sprintf("%d", pid)
err = exec.Command("kill", "-9", pidstr).Run()
if err != nil {
log.Printf("Error: %s", err.Error())
}
}
}
}
// CreateStandaloneAddressFile creates the standalone.txt file containing the address of the GRPC server
func CreateStandaloneAddressFile(address, dir string) error {
return os.WriteFile(
standaloneAddressFilePath(dir),
[]byte(address),
0600,
)
}
// CreateStandalonePIDFile creates the pid.txt file containing the PID of the GRPC server process
func CreateStandalonePIDFile(pid int, dir string) error {
return os.WriteFile(
standalonePIDFilePath(dir),
[]byte(strconv.Itoa(pid)),
0600,
)
}
// CleanupStandaloneAddressFile attempts to delete standalone.txt from the specified folder.
// If the file does not exist, the function returns nil.
func CleanupStandaloneAddressFile(info ServerSettings) error {
return deleteFile(standaloneAddressFilePath(info.Dir))
}
// CleanupStandalonePIDFile attempts to delete pid.txt from the specified folder.
// If the file does not exist, the function returns nil.
func CleanupStandalonePIDFile(info ServerSettings) error {
return deleteFile(standalonePIDFilePath(info.Dir))
}
// deleteFile attempts to delete the specified file.
func deleteFile(fileName string) error {
err := os.Remove(fileName)
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// standaloneAddressFilePath returns the path to the standalone.txt file, which contains the standalone GRPC address
func standaloneAddressFilePath(dir string) string {
return filepath.Join(dir, "standalone.txt")
}
// standalonePIDFilePath returns the path to the pid.txt file, which contains the standalone GRPC's server PID
func standalonePIDFilePath(dir string) string {
return filepath.Join(dir, "pid.txt")
}
// checkPIDIsRunning returns true if there's a process with the specified PID
func checkPIDIsRunning(pid int) bool {
if pid == 0 {
return false
}
process, err := os.FindProcess(pid)
if err != nil {
return false
}
// FindProcess does not return an error if the process does not exist in UNIX.
//
// From man kill:
// If sig is 0, then no signal is sent, but error checking is still per‐
// formed; this can be used to check for the existence of a process ID or
// process group ID.
//
// So we send try to send a 0 signal to the process instead to test if it exists.
if err = process.Signal(syscall.Signal(0)); err != nil {
return false
}
return true
}