예제 #1
0
파일: common.go 프로젝트: IRCody/snap
// Convert from core.Plugin to Plugin protobuf message
func ToCorePluginMsg(pl core.Plugin) *Plugin {
	return &Plugin{
		TypeName: pl.TypeName(),
		Name:     pl.Name(),
		Version:  int64(pl.Version()),
	}
}
예제 #2
0
// UnloadPlugin unloads a plugin from the LoadedPlugins table
func (p *pluginManager) UnloadPlugin(pl core.Plugin) (*loadedPlugin, serror.SnapError) {

	plugin, err := p.loadedPlugins.get(fmt.Sprintf("%s:%s:%d", pl.TypeName(), pl.Name(), pl.Version()))
	if err != nil {
		se := serror.New(ErrPluginNotFound, map[string]interface{}{
			"plugin-name":    pl.Name(),
			"plugin-version": pl.Version(),
			"plugin-type":    pl.TypeName(),
		})
		return nil, se
	}

	if plugin.State != LoadedState {
		se := serror.New(ErrPluginNotInLoadedState, map[string]interface{}{
			"plugin-name":    plugin.Name(),
			"plugin-version": plugin.Version(),
			"plugin-type":    pl.TypeName(),
		})
		return nil, se
	}

	// If the plugin has been uploaded via REST API
	// aka, was not auto loaded from auto_discover_path
	// nor loaded from tests
	// then do clean up
	if !plugin.Details.IsAutoLoaded {
		pmLogger.WithFields(log.Fields{
			"plugin-type":    plugin.TypeName(),
			"plugin-name":    plugin.Name(),
			"plugin-version": plugin.Version(),
			"plugin-path":    plugin.Details.Path,
		}).Debugf("Removing plugin")
		if err := os.RemoveAll(filepath.Dir(plugin.Details.Path)); err != nil {
			pmLogger.WithFields(log.Fields{
				"plugin-type":    plugin.TypeName(),
				"plugin-name":    plugin.Name(),
				"plugin-version": plugin.Version(),
				"plugin-path":    plugin.Details.Path,
			}).Error(err)
			se := serror.New(err)
			se.SetFields(map[string]interface{}{
				"plugin-type":    plugin.TypeName(),
				"plugin-name":    plugin.Name(),
				"plugin-version": plugin.Version(),
				"plugin-path":    plugin.Details.Path,
			})
			return nil, se
		}
	}

	p.loadedPlugins.remove(plugin.Key())

	// Remove any metrics from the catalog if this was a collector
	if plugin.TypeName() == "collector" {
		p.metricCatalog.RmUnloadedPluginMetrics(plugin)
	}

	return plugin, nil
}
예제 #3
0
func (m MockManagesMetrics) Unload(plugin core.Plugin) (core.CatalogedPlugin, serror.SnapError) {
	for _, pl := range pluginCatalog {
		if plugin.Name() == pl.Name() &&
			plugin.Version() == pl.Version() &&
			plugin.TypeName() == pl.TypeName() {
			return pl, nil
		}
	}
	return nil, serror.New(errors.New("plugin not found"))
}
예제 #4
0
func (p *pluginControl) sendPluginUnsubscriptionEvent(taskId string, pl core.Plugin) serror.SnapError {
	pt, err := core.ToPluginType(pl.TypeName())
	if err != nil {
		return serror.New(err)
	}
	e := &control_event.PluginUnsubscriptionEvent{
		TaskId:        taskId,
		PluginType:    int(pt),
		PluginName:    pl.Name(),
		PluginVersion: pl.Version(),
	}
	if _, err := p.eventManager.Emit(e); err != nil {
		return serror.New(err)
	}
	return nil
}
예제 #5
0
파일: worker.go 프로젝트: Collinux/snap
func (w worker) unloadPlugin(plugin core.Plugin) error {
	logger := w.logger.WithFields(log.Fields{
		"plugin-name":    plugin.Name(),
		"plugin-version": plugin.Version(),
		"plugin-type":    plugin.TypeName(),
		"_block":         "unload-plugin",
	})
	if !w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
		return nil
	}
	if _, err := w.pluginManager.Unload(plugin); err != nil {
		logger.WithField("err", err).Info("failed to unload plugin")
		return err
	}
	return nil
}
예제 #6
0
파일: control.go 프로젝트: jcooklin/snap
func (p *pluginControl) sendPluginSubscriptionEvent(taskID string, pl core.Plugin) serror.SnapError {
	pt, err := core.ToPluginType(pl.TypeName())
	if err != nil {
		return serror.New(err)
	}
	e := &control_event.PluginSubscriptionEvent{
		TaskId:           taskID,
		PluginType:       int(pt),
		PluginName:       pl.Name(),
		PluginVersion:    pl.Version(),
		SubscriptionType: int(strategy.UnboundSubscriptionType),
	}
	if pl.Version() > 0 {
		e.SubscriptionType = int(strategy.BoundSubscriptionType)
	}
	if _, err := p.eventManager.Emit(e); err != nil {
		return serror.New(err)
	}
	return nil
}
예제 #7
0
파일: worker.go 프로젝트: Collinux/snap
func (w worker) loadPlugin(plugin core.Plugin) error {
	logger := w.logger.WithFields(log.Fields{
		"plugin-name":    plugin.Name(),
		"plugin-version": plugin.Version(),
		"plugin-type":    plugin.TypeName(),
		"_block":         "load-plugin",
	})
	if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
		return nil
	}
	members, err := w.memberManager.GetPluginAgreementMembers()
	if err != nil {
		logger.Error(err)
		return err
	}
	for _, member := range shuffle(members) {
		url := fmt.Sprintf("%s://%s:%s/v1/plugins/%s/%s/%d?download=true", member.GetRestProto(), member.GetAddr(), member.GetRestPort(), plugin.TypeName(), plugin.Name(), plugin.Version())
		c, err := client.New(url, "v1", member.GetRestInsecureSkipVerify(), client.Password(w.memberManager.GetRequestPassword()))
		if err != nil {
			logger.WithFields(log.Fields{
				"err": err,
				"url": url,
			}).Info("unable to create client")
			continue
		}
		resp, err := c.TribeRequest()
		if err != nil {
			logger.WithFields(log.Fields{
				"err": err,
				"url": url,
			}).Info("plugin not found")
			continue
		}
		if resp.StatusCode == 200 {
			if resp.Header.Get("Content-Type") != "application/x-gzip" {
				logger.WithField("content-type", resp.Header.Get("Content-Type")).Error("Expected application/x-gzip")
			}
			dir, err := ioutil.TempDir("", "")
			if err != nil {
				logger.Error(err)
				return err
			}
			f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s-%d", plugin.TypeName(), plugin.Name(), plugin.Version())))
			if err != nil {
				logger.Error(err)
				f.Close()
				return err
			}
			io.Copy(f, resp.Body)
			f.Close()
			err = os.Chmod(f.Name(), 0700)
			if err != nil {
				logger.Error(err)
				return err
			}
			rp, err := core.NewRequestedPlugin(f.Name())
			if err != nil {
				logger.Error(err)
				return err
			}
			_, err = w.pluginManager.Load(rp)
			if err != nil {
				logger.Error(err)
				return err
			}
			if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
				return nil
			}
			return errors.New("failed to load plugin")
		}
	}
	return errors.New("failed to find a member with the plugin")
}
예제 #8
0
파일: plugin.go 프로젝트: Collinux/snap
func pluginURI(host string, c core.Plugin) string {
	return fmt.Sprintf("%s://%s/v1/plugins/%s/%s/%d", protocolPrefix, host, c.TypeName(), c.Name(), c.Version())
}