Example #1
0
func (driver *Driver) Start(host string) error {

	if host == "" {
		host = "unix:///run/docker/plugins/rexray.sock"
	}

	protoAndAddr := strings.Split(host, "://")
	if len(protoAndAddr) != 2 {
		return ErrBadHostSpecified
	}

	mux := http.NewServeMux()

	var unixPath string
	if protoAndAddr[0] == "unix" {
		path := protoAndAddr[1]
		if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
			return err
		}
		_ = os.RemoveAll(path)
		unixPath = fmt.Sprintf("%s://%s", "unix", path)
	} else if protoAndAddr[0] == "tcp" {
	} else {
		return ErrBadProtocol
	}

	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, `{"Implements": ["VolumeDriver"]}`)
	})

	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
		var pr pluginRequest
		if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		err := volume.Create(pr.Name)
		if err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, `{}`)
	})

	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
		var pr pluginRequest
		if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		err := volume.Remove(pr.Name)
		if err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, `{}`)
	})

	mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
		var pr pluginRequest
		if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		mountPath, err := volume.Path(pr.Name, "")
		if err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", mountPath))
	})

	mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
		var pr pluginRequest
		if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		mountPath, err := volume.Mount(pr.Name, "", false, "")
		if err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, fmt.Sprintf("{\"Mountpoint\": \"%s\"}", mountPath))
	})

	mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
		var pr pluginRequest
		if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		err := volume.Unmount(pr.Name, "")
		if err != nil {
			http.Error(w, fmt.Sprintf("{\"Error\":\"%s\"}", err.Error()), 500)
			return
		}

		w.Header().Set("Content-Type", "appplication/vnd.docker.plugins.v1+json")
		fmt.Fprintln(w, `{}`)
	})

	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
		return err
	}

	var specPath string
	if protoAndAddr[0] == "unix" {
		listener, err := net.Listen("unix", protoAndAddr[1])
		if err != nil {
			return err
		}

		daemonConfig.unixListener = listener
		go http.Serve(daemonConfig.unixListener, mux)
		specPath = unixPath
	} else {
		host = strings.Replace(host, "tcp://", "", 1)
		daemonConfig.httpServer = &http.Server{
			Addr:    host,
			Handler: mux,
		}
		go daemonConfig.httpServer.ListenAndServe()

		specPath = daemonConfig.httpServer.Addr
	}

	if err := ioutil.WriteFile("/etc/docker/plugins/rexray.spec", []byte(specPath), 0644); err != nil {
		return err
	}

	fmt.Println(fmt.Sprintf("Listening for HTTP (%s)", specPath))
	select {}

}
Example #2
0
			log.Fatal(err)
		}
		fmt.Printf(string(yamlOutput))

	},
}

var unmountvolumeCmd = &cobra.Command{
	Use: "unmount-volume",
	Run: func(cmd *cobra.Command, args []string) {

		if volumeName == "" && volumeID == "" {
			log.Fatal("Missing --volumename or --volumeid")
		}

		err := volume.Unmount(volumeName, volumeID)
		if err != nil {
			log.Fatal(err)
		}

	},
}

var getvolumepathCmd = &cobra.Command{
	Use: "get-volumepath",
	Run: func(cmd *cobra.Command, args []string) {

		if volumeName == "" && volumeID == "" {
			log.Fatal("Missing --volumename or --volumeid")
		}