2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-11-14 21:08:24 -05:00
|
|
|
"encoding/json"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceUpdate updates a Service.
|
2016-11-14 21:08:24 -05:00
|
|
|
func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
var (
|
|
|
|
query = url.Values{}
|
2017-04-05 18:43:17 -04:00
|
|
|
distErr error
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
2017-05-12 16:51:52 -04:00
|
|
|
headers := map[string][]string{
|
|
|
|
"version": {cli.version},
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
if options.EncodedRegistryAuth != "" {
|
2017-05-12 16:51:52 -04:00
|
|
|
headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:32:44 -04:00
|
|
|
if options.RegistryAuthFrom != "" {
|
|
|
|
query.Set("registryAuthFrom", options.RegistryAuthFrom)
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:27:01 -05:00
|
|
|
if options.Rollback != "" {
|
|
|
|
query.Set("rollback", options.Rollback)
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
query.Set("version", strconv.FormatUint(version.Index, 10))
|
|
|
|
|
2017-05-18 18:00:25 -04:00
|
|
|
// ensure that the image is tagged
|
|
|
|
if taggedImg := imageWithTagString(service.TaskTemplate.ContainerSpec.Image); taggedImg != "" {
|
|
|
|
service.TaskTemplate.ContainerSpec.Image = taggedImg
|
|
|
|
}
|
|
|
|
|
2017-04-05 18:43:17 -04:00
|
|
|
// Contact the registry to retrieve digest and platform information
|
|
|
|
// This happens only when the image has changed
|
|
|
|
if options.QueryRegistry {
|
|
|
|
distributionInspect, err := cli.DistributionInspect(ctx, service.TaskTemplate.ContainerSpec.Image, options.EncodedRegistryAuth)
|
|
|
|
distErr = err
|
|
|
|
if err == nil {
|
|
|
|
// now pin by digest if the image doesn't already contain a digest
|
2017-05-18 18:00:25 -04:00
|
|
|
if img := imageWithDigestString(service.TaskTemplate.ContainerSpec.Image, distributionInspect.Descriptor.Digest); img != "" {
|
2017-04-05 18:43:17 -04:00
|
|
|
service.TaskTemplate.ContainerSpec.Image = img
|
|
|
|
}
|
2017-05-16 19:09:53 -04:00
|
|
|
// add platforms that are compatible with the service
|
|
|
|
service.TaskTemplate.Placement = updateServicePlatforms(service.TaskTemplate.Placement, distributionInspect)
|
2017-04-05 18:43:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:08:24 -05:00
|
|
|
var response types.ServiceUpdateResponse
|
2016-09-06 14:46:37 -04:00
|
|
|
resp, err := cli.post(ctx, "/services/"+serviceID+"/update", query, service, headers)
|
2016-11-14 21:08:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
2017-04-05 18:43:17 -04:00
|
|
|
|
|
|
|
if distErr != nil {
|
|
|
|
response.Warnings = append(response.Warnings, digestWarning(service.TaskTemplate.ContainerSpec.Image))
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
ensureReaderClosed(resp)
|
2016-11-14 21:08:24 -05:00
|
|
|
return response, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|