2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-07-20 14:15:08 -04:00
|
|
|
opts := swarmOptions{}
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "update [OPTIONS]",
|
2016-07-20 14:15:08 -04:00
|
|
|
Short: "Update the swarm",
|
2016-06-13 22:56:23 -04:00
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-17 11:19:51 -04:00
|
|
|
return runUpdate(dockerCli, cmd.Flags(), opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-21 17:27:04 -04:00
|
|
|
addSwarmFlags(cmd.Flags(), &opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runUpdate(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts swarmOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
var updateFlags swarm.UpdateFlags
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
swarm, err := client.SwarmInspect(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mergeSwarm(&swarm, flags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-15 17:30:54 -04:00
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, updateFlags)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:02:56 -04:00
|
|
|
fmt.Fprintln(dockerCli.Out(), "Swarm updated.")
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeSwarm(swarm *swarm.Swarm, flags *pflag.FlagSet) error {
|
|
|
|
spec := &swarm.Spec
|
|
|
|
|
2016-06-17 11:19:51 -04:00
|
|
|
if flags.Changed(flagTaskHistoryLimit) {
|
|
|
|
spec.Orchestration.TaskHistoryRetentionLimit, _ = flags.GetInt64(flagTaskHistoryLimit)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-17 11:19:51 -04:00
|
|
|
if flags.Changed(flagDispatcherHeartbeat) {
|
|
|
|
if v, err := flags.GetDuration(flagDispatcherHeartbeat); err == nil {
|
2016-08-31 11:25:14 -04:00
|
|
|
spec.Dispatcher.HeartbeatPeriod = v
|
2016-06-14 20:36:37 -04:00
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-17 11:19:51 -04:00
|
|
|
if flags.Changed(flagCertExpiry) {
|
|
|
|
if v, err := flags.GetDuration(flagCertExpiry); err == nil {
|
2016-06-16 20:04:33 -04:00
|
|
|
spec.CAConfig.NodeCertExpiry = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:39:39 -04:00
|
|
|
if flags.Changed(flagExternalCA) {
|
|
|
|
value := flags.Lookup(flagExternalCA).Value.(*ExternalCAOption)
|
|
|
|
spec.CAConfig.ExternalCAs = value.Value()
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
return nil
|
|
|
|
}
|