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

(SDI-2475) Configure the TempDir where the API loads plugins. #1512

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 0 additions & 3 deletions control/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,6 @@ func TestControlDefaultConfig(t *testing.T) {
Convey("ListenPort should be set to 8082", func() {
So(cfg.ListenPort, ShouldEqual, 8082)
})
Convey("KeyringPaths should be empty", func() {
So(cfg.KeyringPaths, ShouldEqual, "")
})
Convey("PluginTrust should equal 1", func() {
So(cfg.PluginTrust, ShouldEqual, 1)
})
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: fmt.Sprintf("Temporary path for loading plugins (default: %v)", defaultTempDirPath),
EnvVar: "SNAP_TEMP_DIR_PATH",
}

Flags = []cli.Flag{flNumberOfPLs, flPluginLoadTimeout, flAutoDiscover, flPluginTrust, flKeyringPaths, flCache, flControlRpcPort, flControlRpcAddr, flTempDirPath}
)
1 change: 1 addition & 0 deletions docs/SNAPTELD.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $ snapteld [global options] command [command options] [arguments...]
--cache-expiration value The time limit for which a metric cache entry is valid (default: 500ms) [$SNAP_CACHE_EXPIRATION]
--control-listen-port value Listen port for control RPC server (default: 8082) [$SNAP_CONTROL_LISTEN_PORT]
--control-listen-addr value Listen address for control RPC server [$SNAP_CONTROL_LISTEN_ADDR]
--temp_dir_path value Temporary path for loading plugins [$SNAP_TEMP_DIR_PATH]
--work-manager-queue-size value Size of the work manager queue (default: 25) [$WORK_MANAGER_QUEUE_SIZE]
--work-manager-pool-size value Size of the work manager pool (default: 4) [$WORK_MANAGER_POOL_SIZE]
--disable-api, -d Disable the agent REST API
Expand Down
3 changes: 3 additions & 0 deletions docs/SNAPTELD_CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ control:
# not be loaded. Valid values are 0 - Off, 1 - Enabled, 2 - Warning
plugin_trust_level: 1

# temp_dir_path sets the temporary directory which houses the temporary files
temp_dir_path: /tmp

# max_plugin_restarts controls how many times a plugin is allowed to be restarted
# before failing. Snap will not disable a plugin due to failures when this value is -1.
max_plugin_restarts: 10
Expand Down
1 change: 1 addition & 0 deletions examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"max_running_plugins":1,
"plugin_load_timeout":10,
"keyring_paths":"/etc/snap/keyrings",
"temp_dir_path":"/tmp",
"plugin_trust_level":0,
"plugins":{
"all":{
Expand Down
3 changes: 3 additions & 0 deletions examples/configs/snap-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ control:
# not be loaded. Valid values are 0 - Off, 1 - Enabled, 2 - Warning
plugin_trust_level: 0

# temp_dir_path sets the temporary directory which houses the temporary files
temp_dir_path: /tmp

# max_plugin_restarts controls how many times a plugin is allowed to be restarted
# before failing. Snap will not disable a plugin due to failures when this value is -1.
max_plugin_restarts: 10
Expand Down
39 changes: 39 additions & 0 deletions mgmt/rest/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

import (
"io/ioutil"
"os"
"path"
"runtime"

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

func WriteFile(filename string, b []byte) (string, error) {
// Create temporary directory
dir, err := ioutil.TempDir(control.GetDefaultConfig().TempDirPath, "snap-plugin-")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the trailing -?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing isn't required. Each time a plugin loads, a tmp file is being generated with a random number. snap-plugin- would be the prefix with which the file is generated. It might as well just be snap-plugin.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a prefix. The method will add a unique identifier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so the path for the plugin that's written will be something like:
<TempDirPath>/snap-plugin-<unique identifier>/snap-plugin-<plugin-name>? And the reason for separate directories per plugin is that multiple plugins might have the same binary name?

if err != nil {
return "", err
}

f, err := os.Create(path.Join(dir, filename))
if err != nil {
return "", err
}
// Close before load
defer f.Close()

n, err := f.Write(b)
log.Debugf("wrote %v to %v", n, f.Name())
if err != nil {
return "", err
}
if runtime.GOOS != "windows" {
err = f.Chmod(0700)
if err != nil {
return "", err
}
}
return f.Name(), nil
}
33 changes: 2 additions & 31 deletions mgmt/rest/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ import (
"mime/multipart"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/mgmt/rest/api"
"github.com/intelsdi-x/snap/mgmt/rest/common"
"github.com/intelsdi-x/snap/mgmt/rest/v1/rbody"
"github.com/julienschmidt/httprouter"
)
Expand Down Expand Up @@ -128,7 +126,7 @@ func (s *apiV1) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
rbody.Write(500, rbody.FromError(e), w)
return
}
if pluginPath, err = writeFile(p.FileName(), b); err != nil {
if pluginPath, err = common.WriteFile(p.FileName(), b); err != nil {
rbody.Write(500, rbody.FromError(err), w)
return
}
Expand Down Expand Up @@ -187,33 +185,6 @@ func (s *apiV1) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
}
}

func writeFile(filename string, b []byte) (string, error) {
// Create temporary directory
dir, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
f, err := os.Create(path.Join(dir, filename))
if err != nil {
return "", err
}
// Close before load
defer f.Close()

n, err := f.Write(b)
log.Debugf("wrote %v to %v", n, f.Name())
if err != nil {
return "", err
}
if runtime.GOOS != "windows" {
err = f.Chmod(0700)
if err != nil {
return "", err
}
}
return f.Name(), nil
}

func (s *apiV1) unloadPlugin(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
plName := p.ByName("name")
plType := p.ByName("type")
Expand Down
35 changes: 3 additions & 32 deletions mgmt/rest/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ import (
"strconv"
"strings"

"path"
"runtime"

log "github.com/Sirupsen/logrus"
"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/common"
"github.com/julienschmidt/httprouter"
)

Expand Down Expand Up @@ -95,6 +92,7 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
Write(415, FromError(err), w)
return
}

if strings.HasPrefix(mediaType, "multipart/") {
var pluginPath string
var signature []byte
Expand Down Expand Up @@ -146,7 +144,7 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
Write(400, FromError(e), w)
return
}
if pluginPath, err = writeFile(p.FileName(), b); err != nil {
if pluginPath, err = common.WriteFile(p.FileName(), b); err != nil {
Write(500, FromError(err), w)
return
}
Expand Down Expand Up @@ -204,33 +202,6 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
}
}

func writeFile(filename string, b []byte) (string, error) {
// Create temporary directory
dir, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
f, err := os.Create(path.Join(dir, filename))
if err != nil {
return "", err
}
// Close before load
defer f.Close()

n, err := f.Write(b)
log.Debugf("wrote %v to %v", n, f.Name())
if err != nil {
return "", err
}
if runtime.GOOS != "windows" {
err = f.Chmod(0700)
if err != nil {
return "", err
}
}
return f.Name(), nil
}

func pluginParameters(p httprouter.Params) (string, string, int, map[string]interface{}, serror.SnapError) {
plName := p.ByName("name")
plType := p.ByName("type")
Expand Down
2 changes: 2 additions & 0 deletions snapteld.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type managesTribe interface {

func main() {
// Add a check to see if gitversion is blank from the build process

if gitversion == "" {
gitversion = "unknown"
}
Expand Down Expand Up @@ -797,6 +798,7 @@ 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")
Expand Down