2016-09-06 18:46:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-11-15 02:08:24 +00:00
|
|
|
"encoding/json"
|
2016-09-06 18:46:37 +00: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-15 02:08:24 +00: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 18:46:37 +00:00
|
|
|
var (
|
|
|
|
query = url.Values{}
|
2017-04-05 22:43:17 +00:00
|
|
|
distErr error
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
2017-05-12 20:51:52 +00:00
|
|
|
headers := map[string][]string{
|
|
|
|
"version": {cli.version},
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
if options.EncodedRegistryAuth != "" {
|
2017-05-12 20:51:52 +00:00
|
|
|
headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 23:32:44 +00:00
|
|
|
if options.RegistryAuthFrom != "" {
|
|
|
|
query.Set("registryAuthFrom", options.RegistryAuthFrom)
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:27:01 +00:00
|
|
|
if options.Rollback != "" {
|
|
|
|
query.Set("rollback", options.Rollback)
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
query.Set("version", strconv.FormatUint(version.Index, 10))
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
if err := validateServiceSpec(service); err != nil {
|
|
|
|
return types.ServiceUpdateResponse{}, err
|
2017-05-18 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
var imgPlatforms []swarm.Platform
|
|
|
|
// ensure that the image is tagged
|
|
|
|
if service.TaskTemplate.ContainerSpec != nil {
|
|
|
|
if taggedImg := imageWithTagString(service.TaskTemplate.ContainerSpec.Image); taggedImg != "" {
|
|
|
|
service.TaskTemplate.ContainerSpec.Image = taggedImg
|
|
|
|
}
|
|
|
|
if options.QueryRegistry {
|
|
|
|
var img string
|
|
|
|
img, imgPlatforms, distErr = imageDigestAndPlatforms(ctx, cli, service.TaskTemplate.ContainerSpec.Image, options.EncodedRegistryAuth)
|
|
|
|
if img != "" {
|
2017-04-05 22:43:17 +00:00
|
|
|
service.TaskTemplate.ContainerSpec.Image = img
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
// ensure that the image is tagged
|
|
|
|
if service.TaskTemplate.PluginSpec != nil {
|
|
|
|
if taggedImg := imageWithTagString(service.TaskTemplate.PluginSpec.Remote); taggedImg != "" {
|
|
|
|
service.TaskTemplate.PluginSpec.Remote = taggedImg
|
|
|
|
}
|
|
|
|
if options.QueryRegistry {
|
|
|
|
var img string
|
|
|
|
img, imgPlatforms, distErr = imageDigestAndPlatforms(ctx, cli, service.TaskTemplate.PluginSpec.Remote, options.EncodedRegistryAuth)
|
|
|
|
if img != "" {
|
|
|
|
service.TaskTemplate.PluginSpec.Remote = img
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.TaskTemplate.Placement == nil && len(imgPlatforms) > 0 {
|
|
|
|
service.TaskTemplate.Placement = &swarm.Placement{}
|
|
|
|
}
|
|
|
|
if len(imgPlatforms) > 0 {
|
|
|
|
service.TaskTemplate.Placement.Platforms = imgPlatforms
|
|
|
|
}
|
|
|
|
|
2016-11-15 02:08:24 +00:00
|
|
|
var response types.ServiceUpdateResponse
|
2016-09-06 18:46:37 +00:00
|
|
|
resp, err := cli.post(ctx, "/services/"+serviceID+"/update", query, service, headers)
|
2016-11-15 02:08:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
2017-04-05 22:43:17 +00:00
|
|
|
|
|
|
|
if distErr != nil {
|
|
|
|
response.Warnings = append(response.Warnings, digestWarning(service.TaskTemplate.ContainerSpec.Image))
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
ensureReaderClosed(resp)
|
2016-11-15 02:08:24 +00:00
|
|
|
return response, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|