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

Commit

Permalink
Adding control flag for loading plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rashmigottipati committed Feb 9, 2017
1 parent cf83e09 commit b7a011d
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 65 deletions.
27 changes: 17 additions & 10 deletions control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"time"
Expand All @@ -36,16 +37,17 @@ import (
)

// default configuration values
const (
defaultListenAddr string = "127.0.0.1"
defaultListenPort int = 8082
defaultMaxRunningPlugins int = 3
defaultPluginLoadTimeout int = 3
defaultPluginTrust int = 1
defaultAutoDiscoverPath string = ""
defaultKeyringPaths string = ""
defaultCacheExpiration time.Duration = 500 * time.Millisecond
defaultPprof bool = false
var (
defaultListenAddr = "127.0.0.1"
defaultListenPort = 8082
defaultMaxRunningPlugins = 3
defaultPluginLoadTimeout = 3
defaultPluginTrust = 1
defaultAutoDiscoverPath = ""
defaultKeyringPaths = ""
defaultCacheExpiration = 500 * time.Millisecond
defaultPprof = false
defaultTempDirPath = os.TempDir()
)

type pluginConfig struct {
Expand Down Expand Up @@ -83,6 +85,7 @@ type Config struct {
ListenPort int `json:"listen_port,omitempty"yaml:"listen_port"`
Pprof bool `json:"pprof"yaml:"pprof"`
MaxPluginRestarts int `json:"max_plugin_restarts"yaml:"max_plugin_restarts"`
TempDirPath string `json:"temp_dir_path"yaml:"temp_dir_path"`
}

const (
Expand Down Expand Up @@ -132,6 +135,9 @@ const (
"pprof": {
"type": "boolean"
},
"temp_dir_path": {
"type": "string"
},
"max_plugin_restarts": {
"type": "integer"
}
Expand All @@ -156,6 +162,7 @@ func GetDefaultConfig() *Config {
Tags: newPluginTags(),
Pprof: defaultPprof,
MaxPluginRestarts: MaxPluginRestartCount,
TempDirPath: defaultTempDirPath,
}
}

Expand Down
8 changes: 7 additions & 1 deletion control/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@ var (
EnvVar: "SNAP_CONTROL_LISTEN_ADDR",
}

Flags = []cli.Flag{flNumberOfPLs, flPluginLoadTimeout, flAutoDiscover, flPluginTrust, flKeyringPaths, flCache, flControlRpcPort, flControlRpcAddr}
flTempDirPath = cli.StringFlag{
Name: "temp_dir_path",
Usage: "Listen address for control RPC server",
EnvVar: "TEMP_DIR_PATH",
}

Flags = []cli.Flag{flNumberOfPLs, flPluginLoadTimeout, flAutoDiscover, flPluginTrust, flKeyringPaths, flCache, flControlRpcPort, flControlRpcAddr, flTempDirPath}
)
3 changes: 1 addition & 2 deletions mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/mgmt/rest/v1"
"github.com/intelsdi-x/snap/plugin/helper"
"github.com/intelsdi-x/snap/scheduler"
Expand Down Expand Up @@ -79,7 +78,7 @@ func startAPI() string {
// Start a REST API to talk to
v1.StreamingBufferWindow = 0.01
log.SetLevel(LOG_LEVEL)
r, _ := rest.New(restcfg.GetDefaultConfig())
r, _ := rest.New(rest.GetDefaultConfig())
c := control.New(control.GetDefaultConfig())
c.Start()
s := scheduler.New(scheduler.GetDefaultConfig())
Expand Down
3 changes: 1 addition & 2 deletions mgmt/rest/client/client_tribe_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/rest/client"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/scheduler"
Expand Down Expand Up @@ -199,7 +198,7 @@ func startTribes(count int) []int {
t.SetPluginCatalog(c)
t.SetTaskManager(s)
t.Start()
r, _ := rest.New(restcfg.GetDefaultConfig())
r, _ := rest.New(rest.GetDefaultConfig())
r.BindMetricManager(c)
r.BindTaskManager(s)
r.BindTribeManager(t)
Expand Down
4 changes: 2 additions & 2 deletions mgmt/rest/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"runtime"

log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/control"
)

func WriteFile(filename string, b []byte) (string, error) {
// Create temporary directory
dir, err := ioutil.TempDir(restcfg.DefaultRestLoadPath, "snap-plugin-")
dir, err := ioutil.TempDir(control.GetDefaultConfig().TempDirPath, "snap-plugin-")
//restcfg.DefaultRestLoadPath
if err != nil {
return "", err
Expand Down
34 changes: 13 additions & 21 deletions mgmt/rest/restcfg/config.go → mgmt/rest/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package restcfg

import "os"
package rest

// default configuration values
var (
defaultEnable = true
DefaultPort = 8181
defaultAddress = ""
defaultHTTPS = false
defaultRestCertificate = ""
defaultRestKey = ""
DefaultRestLoadPath = os.TempDir()
defaultAuth = false
defaultAuthPassword = ""
defaultPortSetByConfig = false
defaultPprof = false
const (
defaultEnable bool = true
defaultPort int = 8181
defaultAddress string = ""
defaultHTTPS bool = false
defaultRestCertificate string = ""
defaultRestKey string = ""
defaultAuth bool = false
defaultAuthPassword string = ""
defaultPortSetByConfig bool = false
defaultPprof bool = false
)

// holds the configuration passed in through the SNAP config file
Expand All @@ -28,7 +25,6 @@ type Config struct {
HTTPS bool `json:"https"yaml:"https"`
RestCertificate string `json:"rest_certificate"yaml:"rest_certificate"`
RestKey string `json:"rest_key"yaml:"rest_key"`
RestLoadPath string `json:"rest_load_path"yaml:"rest_load_path"`
RestAuth bool `json:"rest_auth"yaml:"rest_auth"`
RestAuthPassword string `json:"rest_auth_password"yaml:"rest_auth_password"`
portSetByConfig bool ``
Expand Down Expand Up @@ -58,9 +54,6 @@ const (
"rest_key" : {
"type": "string"
},
"rest_load_path" : {
"type": "string"
},
"port" : {
"type": "integer",
"minimum": 1,
Expand All @@ -82,12 +75,11 @@ const (
func GetDefaultConfig() *Config {
return &Config{
Enable: defaultEnable,
Port: DefaultPort,
Port: defaultPort,
Address: defaultAddress,
HTTPS: defaultHTTPS,
RestCertificate: defaultRestCertificate,
RestKey: defaultRestKey,
RestLoadPath: DefaultRestLoadPath,
RestAuth: defaultAuth,
RestAuthPassword: defaultAuthPassword,
portSetByConfig: defaultPortSetByConfig,
Expand Down
9 changes: 2 additions & 7 deletions mgmt/rest/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package rest
import (
"fmt"

"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/urfave/cli"
)

Expand All @@ -38,7 +37,7 @@ var (
}
flAPIPort = cli.StringFlag{
Name: "api-port, p",
Usage: fmt.Sprintf("API port (default: %v)", restcfg.DefaultPort),
Usage: fmt.Sprintf("API port (default: %v)", defaultPort),
EnvVar: "SNAP_PORT",
}
flRestHTTPS = cli.BoolFlag{
Expand All @@ -53,10 +52,6 @@ var (
Name: "rest-key",
Usage: "A path to a key file to use for HTTPS deployment of Snap's REST API",
}
flRestLoadPath = cli.StringFlag{
Name: "rest-load-path",
Usage: "A path where API loads plugins from",
}
flRestAuth = cli.BoolFlag{
Name: "rest-auth",
Usage: "Enables Snap's REST API authentication",
Expand All @@ -67,5 +62,5 @@ var (
}

// Flags consumed by snapteld
Flags = []cli.Flag{flAPIDisabled, flAPIAddr, flAPIPort, flRestHTTPS, flRestCert, flRestKey, flRestLoadPath, flRestAuth, flPProf}
Flags = []cli.Flag{flAPIDisabled, flAPIAddr, flAPIPort, flRestHTTPS, flRestCert, flRestKey, flRestAuth, flPProf}
)
5 changes: 2 additions & 3 deletions mgmt/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/plugin/helper"
"github.com/intelsdi-x/snap/scheduler"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ type mockConfig struct {
LogPath string `json:"-"yaml:"-"`
Control *control.Config
Scheduler *scheduler.Config `json:"-",yaml:"-"`
RestAPI *restcfg.Config `json:"-",yaml:"-"`
RestAPI *Config `json:"-",yaml:"-"`
}

func getDefaultMockConfig() *mockConfig {
Expand All @@ -69,7 +68,7 @@ func getDefaultMockConfig() *mockConfig {
LogPath: "",
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: restcfg.GetDefaultConfig(),
RestAPI: GetDefaultConfig(),
}
}

Expand Down
3 changes: 1 addition & 2 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/urfave/negroni"

"github.com/intelsdi-x/snap/mgmt/rest/api"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/mgmt/rest/v1"
"github.com/intelsdi-x/snap/mgmt/rest/v2"
)
Expand Down Expand Up @@ -64,7 +63,7 @@ type Server struct {
}

// New creates a REST API server with a given config
func New(cfg *restcfg.Config) (*Server, error) {
func New(cfg *Config) (*Server, error) {
// pull a few parameters from the configuration passed in by snapteld
s := &Server{
err: make(chan error),
Expand Down
15 changes: 7 additions & 8 deletions mgmt/rest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package rest
import (
"testing"

"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/pkg/cfgfile"
. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -42,22 +41,22 @@ const (
},
"additionalProperties": true,
"definitions": { ` +
`"control": {}, "scheduler": {}, ` + restcfg.CONFIG_CONSTRAINTS + `, "tribe":{}` +
`"control": {}, "scheduler": {}, ` + CONFIG_CONSTRAINTS + `, "tribe":{}` +
`}` +
`}`
)

type mockRestAPIConfig struct {
RestAPI *restcfg.Config
RestAPI *Config
}

func TestRestAPIConfigJSON(t *testing.T) {
config := &mockRestAPIConfig{
RestAPI: restcfg.GetDefaultConfig(),
RestAPI: GetDefaultConfig(),
}
path := "../../examples/configs/snap-config-sample.json"
err := cfgfile.Read(path, &config, MOCK_CONSTRAINTS)
var cfg *restcfg.Config
var cfg *Config
if err == nil {
cfg = config.RestAPI
}
Expand Down Expand Up @@ -98,11 +97,11 @@ func TestRestAPIConfigJSON(t *testing.T) {

func TestRestAPIConfigYaml(t *testing.T) {
config := &mockRestAPIConfig{
RestAPI: restcfg.GetDefaultConfig(),
RestAPI: GetDefaultConfig(),
}
path := "../../examples/configs/snap-config-sample.yaml"
err := cfgfile.Read(path, &config, MOCK_CONSTRAINTS)
var cfg *restcfg.Config
var cfg *Config
if err == nil {
cfg = config.RestAPI
}
Expand Down Expand Up @@ -142,7 +141,7 @@ func TestRestAPIConfigYaml(t *testing.T) {
}

func TestRestAPIDefaultConfig(t *testing.T) {
cfg := restcfg.GetDefaultConfig()
cfg := GetDefaultConfig()
Convey("Provided a default RestAPI config", t, func() {
Convey("Enable should be true", func() {
So(cfg.Enable, ShouldEqual, true)
Expand Down
3 changes: 1 addition & 2 deletions mgmt/rest/tribe_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/tribe_event"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/scheduler"
Expand Down Expand Up @@ -763,7 +762,7 @@ func startTribes(count int, seed string) ([]int, int, *listenToSeedEvents) {
t.SetPluginCatalog(c)
t.SetTaskManager(s)
t.Start()
r, _ := New(restcfg.GetDefaultConfig())
r, _ := New(GetDefaultConfig())
r.BindMetricManager(c)
r.BindTaskManager(s)
r.BindTribeManager(t)
Expand Down
9 changes: 4 additions & 5 deletions snapteld.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/rest/restcfg"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/mgmt/tribe/agreement"
"github.com/intelsdi-x/snap/pkg/cfgfile"
Expand Down Expand Up @@ -125,7 +124,7 @@ type Config struct {
LogColors bool `json:"log_colors,omitempty"yaml:"log_colors,omitempty"`
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
RestAPI *restcfg.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
Tribe *tribe.Config `json:"tribe,omitempty"yaml:"tribe,omitempty"`
}

Expand Down Expand Up @@ -167,7 +166,7 @@ const (
"definitions": { ` +
control.CONFIG_CONSTRAINTS + `,` +
scheduler.CONFIG_CONSTRAINTS + `,` +
restcfg.CONFIG_CONSTRAINTS + `,` +
rest.CONFIG_CONSTRAINTS + `,` +
tribe.CONFIG_CONSTRAINTS +
`}` +
`}`
Expand Down Expand Up @@ -523,7 +522,7 @@ func getDefaultConfig() *Config {
LogColors: defaultLogColors,
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: restcfg.GetDefaultConfig(),
RestAPI: rest.GetDefaultConfig(),
Tribe: tribe.GetDefaultConfig(),
}
}
Expand Down Expand Up @@ -799,14 +798,14 @@ func applyCmdLineFlags(cfg *Config, ctx *cli.Context) {
cfg.Control.ListenAddr = setStringVal(cfg.Control.ListenAddr, ctx, "control-listen-addr")
cfg.Control.ListenPort = setIntVal(cfg.Control.ListenPort, ctx, "control-listen-port")
cfg.Control.Pprof = setBoolVal(cfg.Control.Pprof, ctx, "pprof")
cfg.Control.TempDirPath = setStringVal(cfg.Control.TempDirPath, ctx, "temp_dir_path")
// next for the RESTful server related flags
cfg.RestAPI.Enable = setBoolVal(cfg.RestAPI.Enable, ctx, "disable-api", invertBoolean)
cfg.RestAPI.Port = setIntVal(cfg.RestAPI.Port, ctx, "api-port")
cfg.RestAPI.Address = setStringVal(cfg.RestAPI.Address, ctx, "api-addr")
cfg.RestAPI.HTTPS = setBoolVal(cfg.RestAPI.HTTPS, ctx, "rest-https")
cfg.RestAPI.RestCertificate = setStringVal(cfg.RestAPI.RestCertificate, ctx, "rest-cert")
cfg.RestAPI.RestKey = setStringVal(cfg.RestAPI.RestKey, ctx, "rest-key")
cfg.RestAPI.RestLoadPath = setStringVal(cfg.RestAPI.RestLoadPath, ctx, "rest-load-path")
cfg.RestAPI.RestAuth = setBoolVal(cfg.RestAPI.RestAuth, ctx, "rest-auth")
cfg.RestAPI.RestAuthPassword = setStringVal(cfg.RestAPI.RestAuthPassword, ctx, "rest-auth-pwd")
cfg.RestAPI.Pprof = setBoolVal(cfg.RestAPI.Pprof, ctx, "pprof")
Expand Down

0 comments on commit b7a011d

Please sign in to comment.