Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
0b1913af
Unverified
Commit
0b1913af
authored
Oct 11, 2019
by
Arve Knudsen
Committed by
GitHub
Oct 11, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pkg/plugins: Check errors (#19715)
* pkg/plugins: Check errors
parent
f0264ebe
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
40 deletions
+84
-40
pkg/plugins/dashboard_importer_test.go
+4
-3
pkg/plugins/dashboards_test.go
+3
-4
pkg/plugins/datasource_plugin.go
+22
-10
pkg/plugins/plugins.go
+49
-18
pkg/plugins/plugins_test.go
+6
-5
No files found.
pkg/plugins/dashboard_importer_test.go
View file @
0b1913af
...
...
@@ -89,12 +89,13 @@ func pluginScenario(desc string, t *testing.T, fn func()) {
Convey
(
"Given a plugin"
,
t
,
func
()
{
setting
.
Raw
=
ini
.
Empty
()
sec
,
_
:=
setting
.
Raw
.
NewSection
(
"plugin.test-app"
)
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
_
,
err
:=
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
So
(
err
,
ShouldBeNil
)
pm
:=
&
PluginManager
{}
err
:=
pm
.
Init
()
err
=
pm
.
Init
()
So
(
err
,
ShouldBeNil
)
Convey
(
desc
,
fn
)
})
}
pkg/plugins/dashboards_test.go
View file @
0b1913af
...
...
@@ -12,15 +12,14 @@ import (
)
func
TestPluginDashboards
(
t
*
testing
.
T
)
{
Convey
(
"When asking plugin dashboard info"
,
t
,
func
()
{
setting
.
Raw
=
ini
.
Empty
()
sec
,
_
:=
setting
.
Raw
.
NewSection
(
"plugin.test-app"
)
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
_
,
err
:=
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
So
(
err
,
ShouldBeNil
)
pm
:=
&
PluginManager
{}
err
:=
pm
.
Init
()
err
=
pm
.
Init
()
So
(
err
,
ShouldBeNil
)
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetDashboardQuery
)
error
{
...
...
pkg/plugins/datasource_plugin.go
View file @
0b1913af
...
...
@@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
"github.com/grafana/grafana/pkg/tsdb"
plugin
"github.com/hashicorp/go-plugin"
"golang.org/x/xerrors"
)
// DataSourcePlugin contains all metadata about a datasource plugin
...
...
@@ -60,12 +61,17 @@ var handshakeConfig = plugin.HandshakeConfig{
func
(
p
*
DataSourcePlugin
)
startBackendPlugin
(
ctx
context
.
Context
,
log
log
.
Logger
)
error
{
p
.
log
=
log
.
New
(
"plugin-id"
,
p
.
Id
)
err
:=
p
.
spawnSubProcess
()
if
err
==
nil
{
go
p
.
restartKilledProcess
(
ctx
)
if
err
:=
p
.
spawnSubProcess
();
err
!=
nil
{
return
err
}
return
err
go
func
()
{
if
err
:=
p
.
restartKilledProcess
(
ctx
);
err
!=
nil
{
p
.
log
.
Error
(
"Attempting to restart killed process failed"
,
"err"
,
err
)
}
}()
return
nil
}
func
(
p
*
DataSourcePlugin
)
spawnSubProcess
()
error
{
...
...
@@ -105,15 +111,21 @@ func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
for
{
select
{
case
<-
ctx
.
Done
()
:
return
ctx
.
Err
()
if
err
:=
ctx
.
Err
();
err
!=
nil
&&
!
xerrors
.
Is
(
err
,
context
.
Canceled
)
{
return
err
}
return
nil
case
<-
ticker
.
C
:
if
p
.
client
.
Exited
()
{
err
:=
p
.
spawnSubProcess
()
p
.
log
.
Debug
(
"Spawning new sub process"
,
"name"
,
p
.
Name
,
"id"
,
p
.
Id
)
if
err
!=
nil
{
p
.
log
.
Error
(
"Failed to spawn subprocess"
)
if
!
p
.
client
.
Exited
()
{
continue
}
if
err
:=
p
.
spawnSubProcess
();
err
!=
nil
{
p
.
log
.
Error
(
"Failed to restart plugin"
,
"err"
,
err
)
continue
}
p
.
log
.
Debug
(
"Plugin process restarted"
)
}
}
}
...
...
pkg/plugins/plugins.go
View file @
0b1913af
...
...
@@ -17,6 +17,8 @@ import (
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/errutil"
"golang.org/x/xerrors"
)
var
(
...
...
@@ -63,7 +65,10 @@ func (pm *PluginManager) Init() error {
}
pm
.
log
.
Info
(
"Starting plugin search"
)
scan
(
path
.
Join
(
setting
.
StaticRootPath
,
"app/plugins"
))
plugDir
:=
path
.
Join
(
setting
.
StaticRootPath
,
"app/plugins"
)
if
err
:=
pm
.
scan
(
plugDir
);
err
!=
nil
{
return
errutil
.
Wrapf
(
err
,
"Failed to scan main plugin directory '%s'"
,
plugDir
)
}
// check if plugins dir exists
if
_
,
err
:=
os
.
Stat
(
setting
.
PluginsPath
);
os
.
IsNotExist
(
err
)
{
...
...
@@ -71,14 +76,22 @@ func (pm *PluginManager) Init() error {
plog
.
Error
(
"Failed to create plugin dir"
,
"dir"
,
setting
.
PluginsPath
,
"error"
,
err
)
}
else
{
plog
.
Info
(
"Plugin dir created"
,
"dir"
,
setting
.
PluginsPath
)
scan
(
setting
.
PluginsPath
)
if
err
:=
pm
.
scan
(
setting
.
PluginsPath
);
err
!=
nil
{
return
errutil
.
Wrapf
(
err
,
"Failed to scan configured plugin directory '%s'"
,
setting
.
PluginsPath
)
}
}
}
else
{
scan
(
setting
.
PluginsPath
)
if
err
:=
pm
.
scan
(
setting
.
PluginsPath
);
err
!=
nil
{
return
errutil
.
Wrapf
(
err
,
"Failed to scan configured plugin directory '%s'"
,
setting
.
PluginsPath
)
}
}
// check plugin paths defined in config
checkPluginPaths
()
if
err
:=
pm
.
checkPluginPaths
();
err
!=
nil
{
return
err
}
for
_
,
panel
:=
range
Panels
{
panel
.
initFrontendPlugin
()
...
...
@@ -95,16 +108,16 @@ func (pm *PluginManager) Init() error {
return
nil
}
func
(
pm
*
PluginManager
)
startBackendPlugins
(
ctx
context
.
Context
)
error
{
func
(
pm
*
PluginManager
)
startBackendPlugins
(
ctx
context
.
Context
)
{
for
_
,
ds
:=
range
DataSources
{
if
ds
.
Backend
{
if
!
ds
.
Backend
{
continue
}
if
err
:=
ds
.
startBackendPlugin
(
ctx
,
plog
);
err
!=
nil
{
pm
.
log
.
Error
(
"Failed to init plugin."
,
"error"
,
err
,
"plugin"
,
ds
.
Id
)
}
}
}
return
nil
}
func
(
pm
*
PluginManager
)
Run
(
ctx
context
.
Context
)
error
{
...
...
@@ -132,32 +145,49 @@ func (pm *PluginManager) Run(ctx context.Context) error {
return
ctx
.
Err
()
}
func
checkPluginPaths
()
error
{
func
(
pm
*
PluginManager
)
checkPluginPaths
()
error
{
for
_
,
section
:=
range
setting
.
Raw
.
Sections
()
{
if
strings
.
HasPrefix
(
section
.
Name
(),
"plugin."
)
{
if
!
strings
.
HasPrefix
(
section
.
Name
(),
"plugin."
)
{
continue
}
path
:=
section
.
Key
(
"path"
)
.
String
()
if
path
!
=
""
{
scan
(
path
)
if
path
=
=
""
{
continue
}
if
err
:=
pm
.
scan
(
path
);
err
!=
nil
{
return
errutil
.
Wrapf
(
err
,
"Failed to scan directory configured for plugin '%s': '%s'"
,
section
.
Name
(),
path
)
}
}
return
nil
}
func
scan
(
pluginDir
string
)
error
{
// scan a directory for plugins.
func
(
pm
*
PluginManager
)
scan
(
pluginDir
string
)
error
{
scanner
:=
&
PluginScanner
{
pluginPath
:
pluginDir
,
}
if
err
:=
util
.
Walk
(
pluginDir
,
true
,
true
,
scanner
.
walker
);
err
!=
nil
{
if
xerrors
.
Is
(
err
,
os
.
ErrNotExist
)
{
pm
.
log
.
Debug
(
"Couldn't scan dir '%s' since it doesn't exist"
)
return
nil
}
if
xerrors
.
Is
(
err
,
os
.
ErrPermission
)
{
pm
.
log
.
Debug
(
"Couldn't scan dir '%s' due to lack of permissions"
)
return
nil
}
if
pluginDir
!=
"data/plugins"
{
log
.
Warn
(
"Could not scan dir
\"
%v
\"
error: %s"
,
pluginDir
,
err
)
pm
.
log
.
Warn
(
"Could not scan dir"
,
"pluginDir"
,
pluginDir
,
"err"
,
err
)
}
return
err
}
if
len
(
scanner
.
errors
)
>
0
{
return
err
ors
.
New
(
"Some plugins failed to load"
)
return
err
util
.
Wrapf
(
scanner
.
errors
[
0
],
"Some plugins failed to load"
)
}
return
nil
...
...
@@ -167,7 +197,6 @@ func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err erro
// We scan all the subfolders for plugin.json (with some exceptions) so that we also load embedded plugins, for
// example https://github.com/raintank/worldping-app/tree/master/dist/grafana-worldmap-panel worldmap panel plugin
// is embedded in worldping app.
if
err
!=
nil
{
return
err
}
...
...
@@ -227,7 +256,9 @@ func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
}
}
reader
.
Seek
(
0
,
0
)
if
_
,
err
:=
reader
.
Seek
(
0
,
0
);
err
!=
nil
{
return
err
}
return
loader
.
Load
(
jsonParser
,
currentDir
)
}
...
...
pkg/plugins/plugins_test.go
View file @
0b1913af
...
...
@@ -29,15 +29,16 @@ func TestPluginScans(t *testing.T) {
Convey
(
"When reading app plugin definition"
,
t
,
func
()
{
setting
.
Raw
=
ini
.
Empty
()
sec
,
_
:=
setting
.
Raw
.
NewSection
(
"plugin.nginx-app"
)
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
sec
,
err
:=
setting
.
Raw
.
NewSection
(
"plugin.nginx-app"
)
So
(
err
,
ShouldBeNil
)
_
,
err
=
sec
.
NewKey
(
"path"
,
"testdata/test-app"
)
So
(
err
,
ShouldBeNil
)
pm
:=
&
PluginManager
{}
err
:=
pm
.
Init
()
err
=
pm
.
Init
()
So
(
err
,
ShouldBeNil
)
So
(
len
(
Apps
),
ShouldBeGreaterThan
,
0
)
So
(
len
(
Apps
),
ShouldBeGreaterThan
,
0
)
So
(
Apps
[
"test-app"
]
.
Info
.
Logos
.
Large
,
ShouldEqual
,
"public/plugins/test-app/img/logo_large.png"
)
So
(
Apps
[
"test-app"
]
.
Info
.
Screenshots
[
1
]
.
Path
,
ShouldEqual
,
"public/plugins/test-app/img/screenshot2.png"
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment