本文整理匯總了Golang中github.com/docker/swarmkit/cmd/swarmctl/common.Context函數的典型用法代碼示例。如果您正苦於以下問題:Golang Context函數的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Context函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: changeNodeRole
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
}
示例2: updateNode
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
}
示例3: changeNodeMembership
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
}
示例4: displayUnlockKey
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
}
示例5: parseNetworks
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
}
示例6:
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 {
示例7: ParseAddSecret
// 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
}
示例8:
)
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
},
}
)
示例9:
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 {
示例10:
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 {
示例11:
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)
示例12:
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
示例13:
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)
示例14: processIPAMOptions
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")
示例15: printNetworkSummary
)
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.