Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Move plugin auto discovery into control module
Browse files Browse the repository at this point in the history
  • Loading branch information
obourdon committed Jun 1, 2016
1 parent 4d7d4ca commit cc4165b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 83 deletions.
89 changes: 89 additions & 0 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"

log "github.com/Sirupsen/logrus"

"github.com/intelsdi-x/gomit"

"github.com/intelsdi-x/snap/control/plugin"
Expand Down Expand Up @@ -234,6 +236,93 @@ func (p *pluginControl) Start() error {
controlLogger.WithFields(log.Fields{
"_block": "start",
}).Info("control started")
//Autodiscover
if p.Config.AutoDiscoverPath != "" {
controlLogger.WithFields(log.Fields{
"_block": "start",
}).Info("auto discover path is enabled")
paths := filepath.SplitList(p.Config.AutoDiscoverPath)
p.SetAutodiscoverPaths(paths)
for _, pa := range paths {
fullPath, err := filepath.Abs(pa)
if err != nil {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
}).Fatal(err)
}
controlLogger.WithFields(log.Fields{
"_block": "start",
}).Info("autoloading plugins from: ", fullPath)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
}).Fatal(err)
}
for _, file := range files {
if file.IsDir() {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
}).Warning("Ignoring subdirectory: ", file.Name())
continue
}
// Ignore tasks files (JSON and YAML)
fname := strings.ToLower(file.Name())
if strings.HasSuffix(fname, ".json") || strings.HasSuffix(fname, ".yaml") || strings.HasSuffix(fname, ".yml") {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
}).Warning("Ignoring JSON/Yaml file: ", file.Name())
continue
}
if strings.HasSuffix(file.Name(), ".aci") || !(strings.HasSuffix(file.Name(), ".asc")) {
rp, err := core.NewRequestedPlugin(path.Join(fullPath, file.Name()))
if err != nil {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
"plugin": file,
}).Error(err)
}
signatureFile := file.Name() + ".asc"
if _, err := os.Stat(path.Join(fullPath, signatureFile)); err == nil {
err = rp.ReadSignatureFile(path.Join(fullPath, signatureFile))
if err != nil {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": pa,
"plugin": file.Name() + ".asc",
}).Error(err)
}
}
pl, err := p.Load(rp)
if err != nil {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": fullPath,
"plugin": file,
}).Error(err)
} else {
controlLogger.WithFields(log.Fields{
"_block": "start",
"autodiscoverpath": fullPath,
"plugin-file-name": file.Name(),
"plugin-name": pl.Name(),
"plugin-version": pl.Version(),
"plugin-type": pl.TypeName(),
}).Info("Loading plugin")
}
}
}
}
} else {
controlLogger.WithFields(log.Fields{
"_block": "start",
}).Info("auto discover path is disabled")
}
return nil
}

Expand Down
15 changes: 11 additions & 4 deletions mgmt/rest/rest_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ var (
// Switching this turns on logging for all the REST API calls
LOG_LEVEL = log.WarnLevel

SNAP_PATH = os.Getenv("SNAP_PATH")
MOCK_PLUGIN_PATH1 = SNAP_PATH + "/plugin/snap-collector-mock1"
MOCK_PLUGIN_PATH2 = SNAP_PATH + "/plugin/snap-collector-mock2"
FILE_PLUGIN_PATH = SNAP_PATH + "/plugin/snap-publisher-file"
SNAP_PATH = os.Getenv("SNAP_PATH")
SNAP_AUTODISCOVER_PATH = os.Getenv("SNAP_AUTODISCOVER_PATH")
MOCK_PLUGIN_PATH1 = SNAP_PATH + "/plugin/snap-collector-mock1"
MOCK_PLUGIN_PATH2 = SNAP_PATH + "/plugin/snap-collector-mock2"
FILE_PLUGIN_PATH = SNAP_PATH + "/plugin/snap-publisher-file"

CompressedUpload = true
TotalUploadSize = 0
Expand Down Expand Up @@ -582,6 +583,12 @@ func TestPluginRestCalls(t *testing.T) {
cfg := getDefaultMockConfig()
err := cfgfile.Read("../../examples/configs/snap-config-sample.json", &cfg, MOCK_CONSTRAINTS)
So(err, ShouldBeNil)
if len(SNAP_AUTODISCOVER_PATH) == 0 {
if len(SNAP_PATH) != 0 {
SNAP_AUTODISCOVER_PATH = fmt.Sprintf("%s/plugin", SNAP_PATH)
}
}
cfg.Control.AutoDiscoverPath = SNAP_AUTODISCOVER_PATH
r := startAPI(cfg)
port := r.port
col := core.CollectorPluginType
Expand Down
79 changes: 0 additions & 79 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io/ioutil"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -40,7 +39,6 @@ import (
"github.com/vrischmann/jsonutil"

"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/tribe"
Expand Down Expand Up @@ -472,83 +470,6 @@ func action(ctx *cli.Context) {
}
}

//Autodiscover
if cfg.Control.AutoDiscoverPath != "" {
log.Info("auto discover path is enabled")
paths := filepath.SplitList(cfg.Control.AutoDiscoverPath)
c.SetAutodiscoverPaths(paths)
for _, p := range paths {
fullPath, err := filepath.Abs(p)
if err != nil {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": p,
}).Fatal(err)
}
log.Info("autoloading plugins from: ", fullPath)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
log.WithFields(
log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
}).Fatal(err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasSuffix(file.Name(), ".aci") || !(strings.HasSuffix(file.Name(), ".asc")) {
rp, err := core.NewRequestedPlugin(path.Join(fullPath, file.Name()))
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file,
}).Error(err)
}
signatureFile := file.Name() + ".asc"
if _, err := os.Stat(path.Join(fullPath, signatureFile)); err == nil {
err = rp.ReadSignatureFile(path.Join(fullPath, signatureFile))
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file.Name() + ".asc",
}).Error(err)
}
}
pl, err := c.Load(rp)
if err != nil {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin": file,
}).Error(err)
} else {
log.WithFields(log.Fields{
"_block": "main",
"_module": "snapd",
"autodiscoverpath": fullPath,
"plugin-file-name": file.Name(),
"plugin-name": pl.Name(),
"plugin-version": pl.Version(),
"plugin-type": pl.TypeName(),
}).Info("Loading plugin")
}
}
}
}
} else {
log.Info("auto discover path is disabled")
}

log.WithFields(
log.Fields{
"block": "main",
Expand Down

0 comments on commit cc4165b

Please sign in to comment.