Merge pull request #28963 from vieux/refactor_plugin_install

refactor plugin install
This commit is contained in:
Tibor Vass 2016-12-05 12:00:29 -08:00 committed by GitHub
commit cff6855751
2 changed files with 23 additions and 12 deletions

View File

@ -12,7 +12,7 @@ import (
// PluginInspectWithRaw inspects an existing plugin // PluginInspectWithRaw inspects an existing plugin
func (cli *Client) PluginInspectWithRaw(ctx context.Context, name string) (*types.Plugin, []byte, error) { func (cli *Client) PluginInspectWithRaw(ctx context.Context, name string) (*types.Plugin, []byte, error) {
resp, err := cli.get(ctx, "/plugins/"+name, nil, nil) resp, err := cli.get(ctx, "/plugins/"+name+"/json", nil, nil)
if err != nil { if err != nil {
if resp.statusCode == http.StatusNotFound { if resp.statusCode == http.StatusNotFound {
return nil, nil, pluginNotFoundError{name} return nil, nil, pluginNotFoundError{name}

View File

@ -14,27 +14,21 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types
// FIXME(vdemeester) name is a ref, we might want to parse/validate it here. // FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
query := url.Values{} query := url.Values{}
query.Set("name", name) query.Set("name", name)
resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth) resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil { if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
newAuthHeader, privilegeErr := options.PrivilegeFunc() newAuthHeader, privilegeErr := options.PrivilegeFunc()
if privilegeErr != nil { if privilegeErr != nil {
ensureReaderClosed(resp) ensureReaderClosed(resp)
return privilegeErr return privilegeErr
} }
resp, err = cli.tryPluginPull(ctx, query, newAuthHeader) options.RegistryAuth = newAuthHeader
resp, err = cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
} }
if err != nil { if err != nil {
ensureReaderClosed(resp) ensureReaderClosed(resp)
return err return err
} }
defer func() {
if err != nil {
delResp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
ensureReaderClosed(delResp)
}
}()
var privileges types.PluginPrivileges var privileges types.PluginPrivileges
if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil { if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
ensureReaderClosed(resp) ensureReaderClosed(resp)
@ -52,6 +46,18 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types
} }
} }
_, err = cli.tryPluginPull(ctx, query, privileges, options.RegistryAuth)
if err != nil {
return err
}
defer func() {
if err != nil {
delResp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
ensureReaderClosed(delResp)
}
}()
if len(options.Args) > 0 { if len(options.Args) > 0 {
if err := cli.PluginSet(ctx, name, options.Args); err != nil { if err := cli.PluginSet(ctx, name, options.Args); err != nil {
return err return err
@ -65,7 +71,12 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types
return cli.PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: 0}) return cli.PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: 0})
} }
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) { func (cli *Client) tryPluginPrivileges(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
headers := map[string][]string{"X-Registry-Auth": {registryAuth}} headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
return cli.post(ctx, "/plugins/pull", query, nil, headers) return cli.get(ctx, "/plugins/privileges", query, headers)
}
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileges types.PluginPrivileges, registryAuth string) (serverResponse, error) {
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
return cli.post(ctx, "/plugins/pull", query, privileges, headers)
} }