|
4 | 4 | "context" |
5 | 5 | "crypto/tls" |
6 | 6 | "crypto/x509" |
| 7 | + "encoding/json" |
7 | 8 | "fmt" |
8 | 9 | "io" |
9 | 10 | "log" |
@@ -51,6 +52,13 @@ type Task = task.Task |
51 | 52 | type ComposeProject = compose.ComposeProject |
52 | 53 |
|
53 | 54 | // Cached container info for instant scoped queries (drill-down) |
| 55 | +type PluginInfo struct { |
| 56 | + ID string |
| 57 | + Name string |
| 58 | + Description string |
| 59 | + Enabled bool |
| 60 | +} |
| 61 | + |
54 | 62 | type containerInfoCache struct { |
55 | 63 | Mounts []mountInfoCache |
56 | 64 | NetIDs map[string]bool |
@@ -626,6 +634,88 @@ func (d *DockerClient) CreateContext(name, description, host string) error { |
626 | 634 | return nil |
627 | 635 | } |
628 | 636 |
|
| 637 | +func (d *DockerClient) ListPlugins() ([]PluginInfo, error) { |
| 638 | + cmd := exec.Command("docker", "plugin", "ls", "--format", "{{json .}}") |
| 639 | + output, err := cmd.CombinedOutput() |
| 640 | + if err != nil { |
| 641 | + return nil, fmt.Errorf("error listing plugins: %v, output: %s", err, string(output)) |
| 642 | + } |
| 643 | + |
| 644 | + var plugins []PluginInfo |
| 645 | + for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") { |
| 646 | + if line == "" { |
| 647 | + continue |
| 648 | + } |
| 649 | + var p struct { |
| 650 | + ID string `json:"ID"` |
| 651 | + Name string `json:"Name"` |
| 652 | + Description string `json:"Description"` |
| 653 | + Enabled bool `json:"Enabled"` |
| 654 | + PluginReference string `json:"PluginReference"` |
| 655 | + } |
| 656 | + if err := json.Unmarshal([]byte(line), &p); err != nil { |
| 657 | + continue |
| 658 | + } |
| 659 | + plugins = append(plugins, PluginInfo{ |
| 660 | + ID: p.ID, |
| 661 | + Name: p.Name, |
| 662 | + Description: p.Description, |
| 663 | + Enabled: p.Enabled, |
| 664 | + }) |
| 665 | + } |
| 666 | + return plugins, nil |
| 667 | +} |
| 668 | + |
| 669 | +func (d *DockerClient) InspectPlugin(name string) (string, error) { |
| 670 | + cmd := exec.Command("docker", "plugin", "inspect", name) |
| 671 | + output, err := cmd.CombinedOutput() |
| 672 | + if err != nil { |
| 673 | + return "", fmt.Errorf("error inspecting plugin: %v, output: %s", err, string(output)) |
| 674 | + } |
| 675 | + return string(output), nil |
| 676 | +} |
| 677 | + |
| 678 | +func (d *DockerClient) RemovePlugin(name string, force bool) error { |
| 679 | + args := []string{"plugin", "rm"} |
| 680 | + if force { |
| 681 | + args = append(args, "-f") |
| 682 | + } |
| 683 | + args = append(args, name) |
| 684 | + cmd := exec.Command("docker", args...) |
| 685 | + output, err := cmd.CombinedOutput() |
| 686 | + if err != nil { |
| 687 | + return fmt.Errorf("error removing plugin: %v, output: %s", err, string(output)) |
| 688 | + } |
| 689 | + return nil |
| 690 | +} |
| 691 | + |
| 692 | +func (d *DockerClient) EnablePlugin(name string) error { |
| 693 | + cmd := exec.Command("docker", "plugin", "enable", name) |
| 694 | + output, err := cmd.CombinedOutput() |
| 695 | + if err != nil { |
| 696 | + return fmt.Errorf("error enabling plugin: %v, output: %s", err, string(output)) |
| 697 | + } |
| 698 | + return nil |
| 699 | +} |
| 700 | + |
| 701 | +func (d *DockerClient) DisablePlugin(name string) error { |
| 702 | + cmd := exec.Command("docker", "plugin", "disable", name) |
| 703 | + output, err := cmd.CombinedOutput() |
| 704 | + if err != nil { |
| 705 | + return fmt.Errorf("error disabling plugin: %v, output: %s", err, string(output)) |
| 706 | + } |
| 707 | + return nil |
| 708 | +} |
| 709 | + |
| 710 | +func (d *DockerClient) InstallPlugin(name string) error { |
| 711 | + cmd := exec.Command("docker", "plugin", "install", "--grant-all-permissions", name) |
| 712 | + output, err := cmd.CombinedOutput() |
| 713 | + if err != nil { |
| 714 | + return fmt.Errorf("error installing plugin: %v, output: %s", err, string(output)) |
| 715 | + } |
| 716 | + return nil |
| 717 | +} |
| 718 | + |
629 | 719 | func (d *DockerClient) ListTasks() ([]common.Resource, error) { |
630 | 720 | return d.Task.List() |
631 | 721 | } |
|
0 commit comments