func (pm *Manager) enable(p *plugin) error { spec, err := pm.initSpec(p) if err != nil { return err } p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0) if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only return err } socket := p.P.Manifest.Interface.Socket p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil) if err != nil { return err } pm.Lock() // fixme: lock single record p.P.Active = true pm.save() pm.Unlock() for _, typ := range p.P.Manifest.Interface.Types { if handler := pm.handlers[typ.String()]; handler != nil { handler(p.Name(), p.Client()) } } return nil }
func (pm *Manager) enable(p *v2.Plugin, force bool) error { if p.IsEnabled() && !force { return fmt.Errorf("plugin %s is already enabled", p.Name()) } spec, err := p.InitSpec(oci.DefaultSpec(), pm.libRoot) if err != nil { return err } p.RestartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0) if err := pm.containerdClient.Create(p.GetID(), "", "", specs.Spec(*spec), libcontainerd.WithRestartManager(p.RestartManager)); err != nil { if err := p.RestartManager.Cancel(); err != nil { logrus.Errorf("enable: restartManager.Cancel failed due to %v", err) } return err } p.PClient, err = plugins.NewClient("unix://"+filepath.Join(p.RuntimeSourcePath, p.GetSocket()), nil) if err != nil { if err := p.RestartManager.Cancel(); err != nil { logrus.Errorf("enable: restartManager.Cancel failed due to %v", err) } return err } pm.pluginStore.SetState(p, true) pm.pluginStore.CallHandler(p) return nil }
func (pm *Manager) enable(p *v2.Plugin, force bool) error { if p.IsEnabled() && !force { return fmt.Errorf("plugin %s is already enabled", p.Name()) } spec, err := p.InitSpec(oci.DefaultSpec(), pm.libRoot) if err != nil { return err } p.Lock() p.Restart = true p.Unlock() if err := pm.containerdClient.Create(p.GetID(), "", "", specs.Spec(*spec)); err != nil { return err } p.PClient, err = plugins.NewClient("unix://"+filepath.Join(p.RuntimeSourcePath, p.GetSocket()), nil) if err != nil { p.Lock() p.Restart = false p.Unlock() return err } pm.pluginStore.SetState(p, true) pm.pluginStore.CallHandler(p) return nil }
func (exec *exampleExecutor) LaunchTask(driver exec.ExecutorDriver, taskInfo *mesos.TaskInfo) { fmt.Println("Launching task", taskInfo.GetName(), "with command", taskInfo.Command.GetValue()) runStatus := &mesos.TaskStatus{ TaskId: taskInfo.GetTaskId(), State: mesos.TaskState_TASK_RUNNING.Enum(), } _, err := driver.SendStatusUpdate(runStatus) if err != nil { fmt.Println("Got error", err) } exec.tasksLaunched++ fmt.Println("Total tasks launched ", exec.tasksLaunched) // // this is where one would perform the requested task // // rexray.UpdateLogLevel() // rexray.InitDriverManagers() // sdm := rexray.GetSdm() // // allVolumes, err := sdm.GetVolume("", "") // if err != nil { // log.Fatal(err) // } // // if len(allVolumes) > 0 { // yamlOutput, err := yaml.Marshal(&allVolumes) // if err != nil { // log.Fatal(err) // } // fmt.Printf(string(yamlOutput)) // } client, _ := plugins.NewClient("unix:///run/mesos/executor/rexray.sock", tlsconfig.Options{InsecureSkipVerify: true}) vd := volumeDriverProxy{client} err = vd.Create("test", nil) if err != nil { fmt.Println("Got error", err) } // finish task fmt.Println("Finishing task", taskInfo.GetName()) finStatus := &mesos.TaskStatus{ TaskId: taskInfo.GetTaskId(), State: mesos.TaskState_TASK_FINISHED.Enum(), } _, err = driver.SendStatusUpdate(finStatus) if err != nil { fmt.Println("Got error", err) } fmt.Println("Task finished", taskInfo.GetName()) }
// createTestPlugin creates a new sample authorization plugin func createTestPlugin(t *testing.T) *authorizationPlugin { pwd, err := os.Getwd() if err != nil { t.Fatal(err) } client, err := plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), &tlsconfig.Options{InsecureSkipVerify: true}) if err != nil { t.Fatalf("Failed to create client %v", err) } return &authorizationPlugin{name: "plugin", plugin: client} }
// createTestPlugin creates a new sample authorization plugin func createTestPlugin(t *testing.T) *authorizationPlugin { plugin := &plugins.Plugin{Name: "authz"} var err error pwd, err := os.Getwd() if err != nil { fmt.Println(err) os.Exit(1) } if err != nil { log.Fatal(err) } plugin.Client, err = plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), tlsconfig.Options{InsecureSkipVerify: true}) if err != nil { t.Fatalf("Failed to create client %v", err) } return &authorizationPlugin{name: "plugin", plugin: plugin} }
func (pm *Manager) enable(p *plugin, force bool) error { if p.PluginObj.Enabled && !force { return fmt.Errorf("plugin %s is already enabled", p.Name()) } spec, err := pm.initSpec(p) if err != nil { return err } p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0) if err := pm.containerdClient.Create(p.PluginObj.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only if err := p.restartManager.Cancel(); err != nil { logrus.Errorf("enable: restartManager.Cancel failed due to %v", err) } return err } socket := p.PluginObj.Manifest.Interface.Socket p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil) if err != nil { if err := p.restartManager.Cancel(); err != nil { logrus.Errorf("enable: restartManager.Cancel failed due to %v", err) } return err } pm.Lock() // fixme: lock single record p.PluginObj.Enabled = true pm.save() pm.Unlock() for _, typ := range p.PluginObj.Manifest.Interface.Types { if handler := pm.handlers[typ.String()]; handler != nil { handler(p.Name(), p.Client()) } } return nil }
func TestVolumeRequestError(t *testing.T) { mux := http.NewServeMux() server := httptest.NewServer(mux) defer server.Close() mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot create volume"}`) }) mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`) }) mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`) }) mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`) }) mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Unknown volume"}`) }) mux.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot list volumes"}`) }) mux.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot get volume"}`) }) mux.HandleFunc("/VolumeDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") http.Error(w, "error", 500) }) u, _ := url.Parse(server.URL) client, err := plugins.NewClient("tcp://"+u.Host, tlsconfig.Options{InsecureSkipVerify: true}) if err != nil { t.Fatal(err) } driver := volumeDriverProxy{client} if err = driver.Create("volume", nil); err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot create volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Mount("volume", "123") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot mount volume") { t.Fatalf("Unexpected error: %v\n", err) } err = driver.Unmount("volume", "123") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot unmount volume") { t.Fatalf("Unexpected error: %v\n", err) } err = driver.Remove("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot remove volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Path("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Unknown volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.List() if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot list volumes") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Get("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot get volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Capabilities() if err == nil { t.Fatal(err) } }
func TestVolumeRequestError(t *testing.T) { mux := http.NewServeMux() server := httptest.NewServer(mux) defer server.Close() mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot create volume"}`) }) mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`) }) mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`) }) mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`) }) mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Err": "Unknown volume"}`) }) u, _ := url.Parse(server.URL) client := plugins.NewClient("tcp://" + u.Host) driver := volumeDriverProxy{client} err := driver.Create("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot create volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Mount("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot mount volume") { t.Fatalf("Unexpected error: %v\n", err) } err = driver.Unmount("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot unmount volume") { t.Fatalf("Unexpected error: %v\n", err) } err = driver.Remove("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Cannot remove volume") { t.Fatalf("Unexpected error: %v\n", err) } _, err = driver.Path("volume") if err == nil { t.Fatal("Expected error, was nil") } if !strings.Contains(err.Error(), "Unknown volume") { t.Fatalf("Unexpected error: %v\n", err) } }