Beispiel #1
0
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
}
Beispiel #2
0
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
}
Beispiel #3
0
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
}
Beispiel #4
0
func displayUnlockKey(cmd *cobra.Command) error {
	conn, err := common.DialConn(cmd)
	if err != nil {
		return err
	}
	defer conn.Close()

	resp, err := api.NewCAClient(conn).GetUnlockKey(common.Context(cmd), &api.GetUnlockKeyRequest{})
	if err != nil {
		return err
	}

	if len(resp.UnlockKey) == 0 {
		fmt.Printf("Managers not auto-locked")
	}
	fmt.Printf("Managers auto-locked.  Unlock key: %s\n", encryption.HumanReadableKey(resp.UnlockKey))
	return nil
}
Beispiel #5
0
func parseNetworks(cmd *cobra.Command, spec *api.ServiceSpec, c api.ControlClient) error {
	flags := cmd.Flags()

	if !flags.Changed("network") {
		return nil
	}
	input, err := flags.GetString("network")
	if err != nil {
		return err
	}

	n, err := network.GetNetwork(common.Context(cmd), c, input)
	if err != nil {
		return err
	}

	spec.Task.Networks = []*api.NetworkAttachmentConfig{
		{
			Target: n.ID,
		},
	}
	return nil
}
Beispiel #6
0
var (
	updateCmd = &cobra.Command{
		Use:   "update <cluster name>",
		Short: "Update a cluster",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("cluster name missing")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			cluster, err := getCluster(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			flags := cmd.Flags()
			spec := &cluster.Spec

			if flags.Changed("autoaccept") {
				autoaccept, err := flags.GetStringSlice("autoaccept")
				if err != nil {
					return err
				}

				// We are getting a whitelist, so make all of the autoaccepts false
				for _, policy := range spec.AcceptancePolicy.Policies {
Beispiel #7
0
// 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
}
Beispiel #8
0
)

var (
	removeCmd = &cobra.Command{
		Use:     "remove <task ID>",
		Short:   "Remove a task",
		Aliases: []string{"rm"},
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("task ID missing")
			}

			if len(args) > 1 {
				return errors.New("remove command takes exactly 1 argument")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			_, err = c.RemoveTask(common.Context(cmd), &api.RemoveTaskRequest{TaskID: args[0]})
			if err != nil {
				return err
			}
			fmt.Println(args[0])
			return nil
		},
	}
)
Beispiel #9
0
	listCmd = &cobra.Command{
		Use:   "ls",
		Short: "List services",
		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.ListServices(common.Context(cmd), &api.ListServicesRequest{})
			if err != nil {
				return err
			}

			var output func(j *api.Service)

			if !quiet {
				tr, err := c.ListTasks(common.Context(cmd), &api.ListTasksRequest{})
				if err != nil {
					return err
				}

				running := map[string]int{}
				for _, task := range tr.Tasks {
					if task.Status.State == api.TaskStateRunning {
Beispiel #10
0
			flags := cmd.Flags()

			all, err := flags.GetBool("all")
			if err != nil {
				return err
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			res := common.NewResolver(cmd, c)

			service, err := getService(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			r, err := c.ListTasks(common.Context(cmd),
				&api.ListTasksRequest{
					Filters: &api.ListTasksRequest_Filters{
						ServiceIDs: []string{service.ID},
					},
				})
			if err != nil {
				return err
			}
			var running int
			for _, t := range r.Tasks {
Beispiel #11
0
				return errors.New("node ID missing")
			}

			flags := cmd.Flags()

			all, err := flags.GetBool("all")
			if err != nil {
				return err
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			node, err := getNode(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			r, err := c.ListTasks(common.Context(cmd),
				&api.ListTasksRequest{
					Filters: &api.ListTasksRequest_Filters{
						NodeIDs: []string{node.ID},
					},
				})
			if err != nil {
				return err
			}

			printNodeSummary(node)
Beispiel #12
0
			if len(args) != 0 {
				return errors.New("ls command takes no arguments")
			}

			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.ListNetworks(common.Context(cmd), &api.ListNetworksRequest{})
			if err != nil {
				return err
			}

			var output func(*api.Network)

			if !quiet {
				w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
				defer func() {
					// Ignore flushing errors - there's nothing we can do.
					_ = w.Flush()
				}()
				common.PrintHeader(w, "ID", "Name", "Driver")
				output = func(n *api.Network) {
					spec := n.Spec
Beispiel #13
0
	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()
				}()
				common.PrintHeader(w, "ID", "Name")
				output = func(s *api.Cluster) {
					spec := s.Spec
Beispiel #14
0
			if len(args) != 0 {
				return errors.New("ls command takes no arguments")
			}

			flags := cmd.Flags()
			quiet, err := flags.GetBool("quiet")
			if err != nil {
				return err
			}

			client, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			resp, err := client.ListSecrets(common.Context(cmd), &api.ListSecretsRequest{})
			if err != nil {
				return err
			}

			var output func(*api.Secret)

			if !quiet {
				w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
				defer func() {
					// Ignore flushing errors - there's nothing we can do.
					_ = w.Flush()
				}()
				common.PrintHeader(w, "ID", "Name", "Created")
				output = func(s *api.Secret) {
					created, err := gogotypes.TimestampFromProto(s.Meta.CreatedAt)
Beispiel #15
0
				return err
			}

			spec := &api.NetworkSpec{
				Annotations: api.Annotations{
					Name: name,
				},
				DriverConfig: driver,
				IPAM:         ipamOpts,
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}
			r, err := c.CreateNetwork(common.Context(cmd), &api.CreateNetworkRequest{Spec: spec})
			if err != nil {
				return err
			}
			fmt.Println(r.Network.ID)
			return nil
		},
	}
)

func processIPAMOptions(cmd *cobra.Command) (*api.IPAMOptions, error) {
	flags := cmd.Flags()

	var ipamOpts *api.IPAMOptions
	if flags.Changed("ipam-driver") {
		driver, err := cmd.Flags().GetString("ipam-driver")
Beispiel #16
0
)

var (
	inspectCmd = &cobra.Command{
		Use:   "inspect <network ID>",
		Short: "Inspect a network",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("network ID missing")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}
			network, err := GetNetwork(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			printNetworkSummary(network)

			return nil
		},
	}
)

func printNetworkSummary(network *api.Network) {
	w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
	defer func() {
		// Ignore flushing errors - there's nothing we can do.
Beispiel #17
0
		Short: "Inspect a task",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("task ID missing")
			}

			if len(args) > 1 {
				return errors.New("inspect command takes exactly 1 argument")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			t, err := c.GetTask(common.Context(cmd), &api.GetTaskRequest{TaskID: args[0]})
			if err != nil {
				return err
			}
			task := t.Task

			r, err := c.ListTasks(common.Context(cmd),
				&api.ListTasksRequest{
					Filters: &api.ListTasksRequest_Filters{
						ServiceIDs: []string{task.ServiceID},
					},
				})
			if err != nil {
				return err
			}
			previous := []*api.Task{}
Beispiel #18
0
			if len(args) != 0 {
				return errors.New("ls command takes no arguments")
			}

			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.ListNodes(common.Context(cmd), &api.ListNodesRequest{})
			if err != nil {
				return err
			}

			var output func(n *api.Node)

			if !quiet {
				w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
				defer func() {
					// Ignore flushing errors - there's nothing we can do.
					_ = w.Flush()
				}()
				common.PrintHeader(w, "ID", "Name", "Membership", "Status", "Availability", "Manager Status")
				output = func(n *api.Node) {
					spec := &n.Spec
Beispiel #19
0
			}
		} else {
			secretData, err = ioutil.ReadAll(os.Stdin)
			if err != nil {
				return fmt.Errorf("Error reading content from STDIN: %s", err.Error())
			}
		}

		client, err := common.Dial(cmd)
		if err != nil {
			return err
		}

		spec := &api.SecretSpec{
			Annotations: api.Annotations{Name: args[0]},
			Data:        secretData,
		}

		resp, err := client.CreateSecret(common.Context(cmd), &api.CreateSecretRequest{Spec: spec})
		if err != nil {
			return err
		}
		fmt.Println(resp.Secret.ID)
		return nil
	},
}

func init() {
	createCmd.Flags().StringP("file", "f", "", "Rather than read the secret from STDIN, read from the given file")
}
Beispiel #20
0
		Use:     "remove <network ID>",
		Short:   "Remove a network",
		Aliases: []string{"rm"},
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("network ID missing")
			}

			if len(args) > 1 {
				return errors.New("remove command takes exactly 1 argument")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			network, err := GetNetwork(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}
			_, err = c.RemoveNetwork(common.Context(cmd), &api.RemoveNetworkRequest{NetworkID: network.ID})
			if err != nil {
				return err
			}
			fmt.Println(args[0])
			return nil
		},
	}
)
Beispiel #21
0
	"github.com/spf13/cobra"
)

var (
	removeCmd = &cobra.Command{
		Use:     "remove <node ID>",
		Short:   "Remove a node",
		Aliases: []string{"rm"},
		RunE: func(cmd *cobra.Command, args []string) 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
			}

			_, err = c.RemoveNode(common.Context(cmd), &api.RemoveNodeRequest{
				NodeID: node.ID,
			})

			return err
		},
	}
)
Beispiel #22
0
		Short: "Update a service",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("service ID missing")
			}

			if len(args) > 1 {
				return errors.New("update command takes exactly 1 argument")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			service, err := getService(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			spec := service.Spec.Copy()

			if err := flagparser.Merge(cmd, spec, c); err != nil {
				return err
			}

			if err := flagparser.ParseAddSecret(cmd, spec, "add-secret"); err != nil {
				return err
			}
			if err := flagparser.ParseRemoveSecret(cmd, spec, "rm-secret"); err != nil {
				return err
Beispiel #23
0
			fmt.Fprintf(w, "  Certificate Validity Duration: %s\n", clusterDuration.String())
		}
	}
}

var (
	inspectCmd = &cobra.Command{
		Use:   "inspect <cluster name>",
		Short: "Inspect a cluster",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("cluster name missing")
			}

			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			cluster, err := getCluster(common.Context(cmd), c, args[0])
			if err != nil {
				return err
			}

			printClusterSummary(cluster)

			return nil
		},
	}
)
Beispiel #24
0
				Task: api.TaskSpec{
					Runtime: &api.TaskSpec_Container{
						Container: &api.ContainerSpec{},
					},
				},
			}

			if err := flagparser.Merge(cmd, spec, c); err != nil {
				return err
			}

			if err := flagparser.ParseAddSecret(cmd, spec, "secret"); err != nil {
				return err
			}

			r, err := c.CreateService(common.Context(cmd), &api.CreateServiceRequest{Spec: spec})
			if err != nil {
				return err
			}
			fmt.Println(r.Service.ID)
			return nil
		},
	}
)

func init() {
	flags := createCmd.Flags()
	flagparser.AddServiceFlags(flags)
	flags.String("mode", "replicated", "one of replicated, global")
	flags.StringSlice("secret", nil, "add a secret from swarm")
}
Beispiel #25
0
var (
	removeCmd = &cobra.Command{
		Use:     "remove <service ID>",
		Short:   "Remove a service",
		Aliases: []string{"rm"},
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				return errors.New("service ID missing")
			}
			c, err := common.Dial(cmd)
			if err != nil {
				return err
			}

			for _, serviceName := range args {
				service, err := getService(common.Context(cmd), c, serviceName)
				if err != nil {
					return err
				}
				_, err = c.RemoveService(common.Context(cmd), &api.RemoveServiceRequest{ServiceID: service.ID})
				if err != nil {
					return err
				}
				fmt.Println(serviceName)
			}
			return nil
		},
	}
)
Beispiel #26
0
	"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
	},
}
Beispiel #27
0
		}
	}

	common.FprintfIfNotEmpty(w, "Created\t: %s\n", ptypes.TimestampString(secret.Meta.CreatedAt))
}

var (
	inspectCmd = &cobra.Command{
		Use:   "inspect <secret ID or name>",
		Short: "Inspect a secret",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("inspect 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
			}

			printSecretSummary(secret)
			return nil
		},
	}
)