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