plugin_install.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // +build experimental
  2. package client
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "github.com/docker/engine-api/types"
  8. "golang.org/x/net/context"
  9. )
  10. // PluginInstall installs a plugin
  11. func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error {
  12. // FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
  13. query := url.Values{}
  14. query.Set("name", name)
  15. resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth)
  16. if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
  17. newAuthHeader, privilegeErr := options.PrivilegeFunc()
  18. if privilegeErr != nil {
  19. ensureReaderClosed(resp)
  20. return privilegeErr
  21. }
  22. resp, err = cli.tryPluginPull(ctx, query, newAuthHeader)
  23. }
  24. if err != nil {
  25. ensureReaderClosed(resp)
  26. return err
  27. }
  28. var privileges types.PluginPrivileges
  29. if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
  30. return err
  31. }
  32. ensureReaderClosed(resp)
  33. if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
  34. accept, err := options.AcceptPermissionsFunc(privileges)
  35. if err != nil {
  36. return err
  37. }
  38. if !accept {
  39. resp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
  40. ensureReaderClosed(resp)
  41. return pluginPermissionDenied{name}
  42. }
  43. }
  44. if options.Disabled {
  45. return nil
  46. }
  47. return cli.PluginEnable(ctx, name)
  48. }
  49. func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) {
  50. headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
  51. return cli.post(ctx, "/plugins/pull", query, nil, headers)
  52. }