示例#1
0
func (s *systemtestSuite) TestVolCLIEmptyGlobal(c *C) {
	c.Assert(s.uploadGlobal("global-empty"), IsNil)

	out, err := s.volcli("global get")
	c.Assert(err, IsNil)

	target := &config.Global{}

	c.Assert(json.Unmarshal([]byte(out), target), IsNil, Commentf(out))
	c.Assert(config.NewGlobalConfig().Published(), DeepEquals, target, Commentf("%q %#v", out, target))
}
示例#2
0
func (s *dockerSuite) SetUpTest(c *C) {
	c.Assert(exec.Command("sh", "-c", "set -e; for i in $(rbd ls); do rbd snap purge $i; rbd rm $i; done").Run(), IsNil)
	exec.Command("/bin/sh", "-c", "etcdctl rm --recursive /volplugin").Run()
	client, err := config.NewClient("/volplugin", []string{"http://127.0.0.1:2379"})
	if err != nil {
		c.Fatal(err)
	}

	s.client = client
	global := config.NewGlobalConfig()
	s.api = api.NewAPI(NewVolplugin(), "mon0", client, &global)
	s.server = httptest.NewServer(s.api.Router(s.api))
}
示例#3
0
func (d *DaemonConfig) handleGlobalUpload(w http.ResponseWriter, r *http.Request) {
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		api.RESTHTTPError(w, errors.ReadBody.Combine(err))
		return
	}

	global := config.NewGlobalConfig()
	if err := json.Unmarshal(data, global); err != nil {
		api.RESTHTTPError(w, errors.UnmarshalGlobal.Combine(err))
		return
	}

	if err := d.Config.PublishGlobal(global); err != nil {
		api.RESTHTTPError(w, errors.PublishGlobal.Combine(err))
		return
	}
}
示例#4
0
// Daemon starts the volplugin service.
func (dc *DaemonConfig) Daemon() error {
	global, err := dc.Client.GetGlobal()
	if err != nil {
		logrus.Errorf("Error fetching global configuration: %v", err)
		logrus.Infof("No global configuration. Proceeding with defaults...")
		global = config.NewGlobalConfig()
	}

	dc.Global = global
	errored.AlwaysDebug = dc.Global.Debug
	errored.AlwaysTrace = dc.Global.Debug
	if dc.Global.Debug {
		logrus.SetLevel(logrus.DebugLevel)
	}

	go info.HandleDebugSignal()

	activity := make(chan *watch.Watch)
	dc.Client.WatchGlobal(activity)
	go func() {
		for {
			dc.Global = (<-activity).Config.(*config.Global)

			logrus.Debugf("Received global %#v", dc.Global)

			errored.AlwaysDebug = dc.Global.Debug
			errored.AlwaysTrace = dc.Global.Debug
			if dc.Global.Debug {
				logrus.SetLevel(logrus.DebugLevel)
			}
		}
	}()

	dc.API = api.NewAPI(docker.NewVolplugin(), dc.Hostname, dc.Client, &dc.Global)

	if err := dc.updateMounts(); err != nil {
		return err
	}

	go dc.pollRuntime()

	driverPath := path.Join(basePath, fmt.Sprintf("%s.sock", dc.PluginName))
	if err := os.Remove(driverPath); err != nil && !os.IsNotExist(err) {
		return err
	}
	if err := os.MkdirAll(basePath, 0700); err != nil {
		return err
	}

	l, err := net.ListenUnix("unix", &net.UnixAddr{Name: driverPath, Net: "unix"})
	if err != nil {
		return err
	}

	srv := http.Server{Handler: dc.API.Router(dc.API)}
	srv.SetKeepAlivesEnabled(false)
	if err := srv.Serve(l); err != nil {
		logrus.Fatalf("Fatal error serving volplugin: %v", err)
	}
	l.Close()
	return os.Remove(driverPath)
}
示例#5
0
// Daemon initializes the daemon for use.
func (d *DaemonConfig) Daemon(listen string) {
	global, err := d.Config.GetGlobal()
	if err != nil {
		logrus.Errorf("Error fetching global configuration: %v", err)
		logrus.Infof("No global configuration. Proceeding with defaults...")
		global = config.NewGlobalConfig()
	}

	d.Global = global
	if d.Global.Debug {
		logrus.SetLevel(logrus.DebugLevel)
	}
	errored.AlwaysDebug = d.Global.Debug
	errored.AlwaysTrace = d.Global.Debug

	go info.HandleDebugSignal()
	go info.HandleDumpTarballSignal(d.Config)

	activity := make(chan *watch.Watch)
	d.Config.WatchGlobal(activity)
	go func() {
		for {
			d.Global = (<-activity).Config.(*config.Global)

			errored.AlwaysDebug = d.Global.Debug
			errored.AlwaysTrace = d.Global.Debug
			if d.Global.Debug {
				logrus.SetLevel(logrus.DebugLevel)
			}
		}
	}()

	r := mux.NewRouter()

	postRouter := map[string]func(http.ResponseWriter, *http.Request){
		"/global":                           d.handleGlobalUpload,
		"/volumes/create":                   d.handleCreate,
		"/volumes/copy":                     d.handleCopy,
		"/volumes/request":                  d.handleRequest,
		"/policies/{policy}":                d.handlePolicyUpload,
		"/runtime/{policy}/{volume}":        d.handleRuntimeUpload,
		"/snapshots/take/{policy}/{volume}": d.handleSnapshotTake,
	}

	if err := addRoute(r, postRouter, "POST", d.Global.Debug); err != nil {
		logrus.Fatalf("Error starting apiserver: %v", err)
	}

	deleteRouter := map[string]func(http.ResponseWriter, *http.Request){
		"/volumes/remove":      d.handleRemove,
		"/volumes/removeforce": d.handleRemoveForce,
		"/policies/{policy}":   d.handlePolicyDelete,
	}

	if err := addRoute(r, deleteRouter, "DELETE", d.Global.Debug); err != nil {
		logrus.Fatalf("Error starting apiserver: %v", err)
	}

	getRouter := map[string]func(http.ResponseWriter, *http.Request){
		"/global":                              d.handleGlobal,
		"/policy-archives/{policy}":            d.handlePolicyListRevisions,
		"/policy-archives/{policy}/{revision}": d.handlePolicyGetRevision,
		"/policies":                            d.handlePolicyList,
		"/policies/{policy}":                   d.handlePolicy,
		"/uses/mounts/{policy}/{volume}":       d.handleUsesMountsVolume,
		"/uses/snapshots/{policy}/{volume}":    d.handleUsesMountsSnapshots,
		"/volumes":                             d.handleListAll,
		"/volumes/{policy}":                    d.handleList,
		"/volumes/{policy}/{volume}":           d.handleGet,
		"/runtime/{policy}/{volume}":           d.handleRuntime,
		"/snapshots/{policy}/{volume}":         d.handleSnapshotList,
	}

	if err := addRoute(r, getRouter, "GET", d.Global.Debug); err != nil {
		logrus.Fatalf("Error starting apiserver: %v", err)
	}

	if d.Global.Debug {
		r.HandleFunc("{action:.*}", d.handleDebug)
	}

	if err := http.ListenAndServe(listen, r); err != nil {
		logrus.Fatalf("Error starting apiserver: %v", err)
	}
}