Esempio n. 1
0
File: ps.go Progetto: harche/docker
func runPS(dockerCli *command.DockerCli, opts psOptions) error {
	client := dockerCli.Client()
	ctx := context.Background()

	service, _, err := client.ServiceInspectWithRaw(ctx, opts.serviceID)
	if err != nil {
		return err
	}

	filter := opts.filter.Value()
	filter.Add("service", service.ID)
	if filter.Include("node") {
		nodeFilters := filter.Get("node")
		for _, nodeFilter := range nodeFilters {
			nodeReference, err := node.Reference(ctx, client, nodeFilter)
			if err != nil {
				return err
			}
			filter.Del("node", nodeFilter)
			filter.Add("node", nodeReference)
		}
	}

	tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
	if err != nil {
		return err
	}

	if opts.quiet {
		return task.PrintQuiet(dockerCli, tasks)
	}
	return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
}
Esempio n. 2
0
File: ps.go Progetto: docker/docker
func runPS(dockerCli *command.DockerCli, opts psOptions) error {
	client := dockerCli.Client()
	ctx := context.Background()

	filter := opts.filter.Value()

	serviceIDFilter := filters.NewArgs()
	serviceNameFilter := filters.NewArgs()
	for _, service := range opts.services {
		serviceIDFilter.Add("id", service)
		serviceNameFilter.Add("name", service)
	}
	serviceByIDList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceIDFilter})
	if err != nil {
		return err
	}
	serviceByNameList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceNameFilter})
	if err != nil {
		return err
	}

	for _, service := range opts.services {
		serviceCount := 0
		// Lookup by ID/Prefix
		for _, serviceEntry := range serviceByIDList {
			if strings.HasPrefix(serviceEntry.ID, service) {
				filter.Add("service", serviceEntry.ID)
				serviceCount++
			}
		}

		// Lookup by Name/Prefix
		for _, serviceEntry := range serviceByNameList {
			if strings.HasPrefix(serviceEntry.Spec.Annotations.Name, service) {
				filter.Add("service", serviceEntry.ID)
				serviceCount++
			}
		}
		// If nothing has been found, return immediately.
		if serviceCount == 0 {
			return fmt.Errorf("no such services: %s", service)
		}
	}

	if filter.Include("node") {
		nodeFilters := filter.Get("node")
		for _, nodeFilter := range nodeFilters {
			nodeReference, err := node.Reference(ctx, client, nodeFilter)
			if err != nil {
				return err
			}
			filter.Del("node", nodeFilter)
			filter.Add("node", nodeReference)
		}
	}

	tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
	if err != nil {
		return err
	}

	if opts.quiet {
		return task.PrintQuiet(dockerCli, tasks)
	}
	return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
}