func changeNodeRole(cmd *cobra.Command, args []string, role api.NodeRole) error { if len(args) == 0 { return errors.New("missing node ID") } c, err := common.Dial(cmd) if err != nil { return err } node, err := getNode(common.Context(cmd), c, args[0]) if err != nil { return err } spec := &node.Spec if spec.Role == role { return errNoChange } spec.Role = role _, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{ NodeID: node.ID, NodeVersion: &node.Meta.Version, Spec: spec, }) if err != nil { return err } return nil }
func updateNode(cmd *cobra.Command, args []string) error { if len(args) == 0 { return errors.New("node ID missing") } c, err := common.Dial(cmd) if err != nil { return err } node, err := getNode(common.Context(cmd), c, args[0]) if err != nil { return err } spec := node.Spec.Copy() flags := cmd.Flags() if flags.Changed(flagLabel) { labels, err := flags.GetStringSlice(flagLabel) if err != nil { return err } // overwrite existing labels spec.Annotations.Labels = map[string]string{} for _, l := range labels { parts := strings.SplitN(l, "=", 2) if len(parts) != 2 { return fmt.Errorf("malformed label for node %s", l) } spec.Annotations.Labels[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) } } if reflect.DeepEqual(spec, &node.Spec) { return errNoChange } _, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{ NodeID: node.ID, NodeVersion: &node.Meta.Version, Spec: spec, }) if err != nil { return err } return nil }
func changeNodeMembership(cmd *cobra.Command, args []string, membership api.NodeSpec_Membership) error { if len(args) == 0 { return errors.New("missing node ID") } if len(args) > 1 { return errors.New("command takes exactly 1 argument") } c, err := common.Dial(cmd) if err != nil { return err } node, err := getNode(common.Context(cmd), c, args[0]) if err != nil { return err } spec := &node.Spec if spec.Membership == membership { return errNoChange } spec.Membership = membership _, err = c.UpdateNode(common.Context(cmd), &api.UpdateNodeRequest{ NodeID: node.ID, NodeVersion: &node.Meta.Version, Spec: spec, }) if err != nil { return err } return nil }
"github.com/docker/swarmkit/api" "github.com/docker/swarmkit/cmd/swarmctl/common" "github.com/spf13/cobra" ) var removeCmd = &cobra.Command{ Use: "remove <secret ID or name>", Short: "Remove a secret", Aliases: []string{"rm"}, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { return errors.New("remove command takes a single secret ID or name") } client, err := common.Dial(cmd) if err != nil { return err } secret, err := getSecret(common.Context(cmd), client, args[0]) if err != nil { return err } _, err = client.RemoveSecret(common.Context(cmd), &api.RemoveSecretRequest{SecretID: secret.ID}) if err != nil { return err } fmt.Println(secret.ID) return nil
"github.com/spf13/cobra" ) var ( listCmd = &cobra.Command{ Use: "ls", Short: "List clusters", RunE: func(cmd *cobra.Command, args []string) error { flags := cmd.Flags() quiet, err := flags.GetBool("quiet") if err != nil { return err } c, err := common.Dial(cmd) if err != nil { return err } r, err := c.ListClusters(common.Context(cmd), &api.ListClustersRequest{}) if err != nil { return err } var output func(j *api.Cluster) if !quiet { w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) defer func() { // Ignore flushing errors - there's nothing we can do. _ = w.Flush()
// ParseAddSecret validates secrets passed on the command line func ParseAddSecret(cmd *cobra.Command, spec *api.ServiceSpec, flagName string) error { flags := cmd.Flags() if flags.Changed(flagName) { secrets, err := flags.GetStringSlice(flagName) if err != nil { return err } container := spec.Task.GetContainer() if container == nil { spec.Task.Runtime = &api.TaskSpec_Container{ Container: &api.ContainerSpec{}, } } lookupSecretNames := []string{} var needSecrets []*api.SecretReference for _, secret := range secrets { n, p, err := parseSecretString(secret) if err != nil { return err } // TODO(diogo): defaults to File targets, but in the future will take different types secretRef := &api.SecretReference{ SecretName: n, Target: &api.SecretReference_File{ File: &api.SecretReference_FileTarget{ Name: p, Mode: 0444, }, }, } lookupSecretNames = append(lookupSecretNames, n) needSecrets = append(needSecrets, secretRef) } client, err := common.Dial(cmd) if err != nil { return err } r, err := client.ListSecrets(common.Context(cmd), &api.ListSecretsRequest{Filters: &api.ListSecretsRequest_Filters{Names: lookupSecretNames}}) if err != nil { return err } foundSecrets := make(map[string]*api.Secret) for _, secret := range r.Secrets { foundSecrets[secret.Spec.Annotations.Name] = secret } for _, secretRef := range needSecrets { secret, ok := foundSecrets[secretRef.SecretName] if !ok { return fmt.Errorf("secret not found: %s", secretRef.SecretName) } secretRef.SecretID = secret.ID container.Secrets = append(container.Secrets, secretRef) } } return nil }