示例#1
0
文件: functions.go 项目: haj/kompose
// GetContainersByFilter looks up the hosts containers with the specified filters and
// returns a list of container matching it, or an error.
func GetContainersByFilter(ctx context.Context, clientInstance client.APIClient, containerFilters ...map[string][]string) ([]types.Container, error) {
	filterArgs := filters.NewArgs()

	// FIXME(vdemeester) I don't like 3 for loops >_<
	for _, filter := range containerFilters {
		for key, filterValue := range filter {
			for _, value := range filterValue {
				filterArgs.Add(key, value)
			}
		}
	}

	return clientInstance.ContainerList(ctx, types.ContainerListOptions{
		All:    true,
		Filter: filterArgs,
	})
}
示例#2
0
func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
	containerList, err := dockerClient.ContainerList(context.Background(), dockertypes.ContainerListOptions{})
	if err != nil {
		return []dockertypes.ContainerJSON{}, err
	}
	containersInspected := []dockertypes.ContainerJSON{}

	// get inspect containers
	for _, container := range containerList {
		containerInspected, err := dockerClient.ContainerInspect(context.Background(), container.ID)
		if err != nil {
			log.Warnf("Failed to inpsect container %s, error: %s", container.ID, err)
		}
		containersInspected = append(containersInspected, containerInspected)
	}
	return containersInspected, nil
}
示例#3
0
// GetContainerByID looks up the hosts containers with the specified Id and
// returns it, or an error.
func GetContainerByID(client client.APIClient, id string) (*types.Container, error) {
	filterArgs := filters.NewArgs()
	filterArgs.Add("id", id)

	containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
		All:    true,
		Filter: filterArgs,
	})
	if err != nil {
		return nil, err
	}

	if len(containers) == 0 {
		return nil, nil
	}

	return &containers[0], nil
}
示例#4
0
// GetContainerByName looks up the hosts containers with the specified name and
// returns it, or an error.
func GetContainerByName(client client.APIClient, name string) (*types.Container, error) {
	filterArgs := filters.NewArgs()
	filterArgs.Add("label", fmt.Sprintf("%s=%s", NAME, name))

	containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
		All:    true,
		Filter: filterArgs,
	})
	if err != nil {
		return nil, err
	}

	if len(containers) == 0 {
		return nil, nil
	}

	return &containers[0], nil
}
示例#5
0
文件: name.go 项目: haj/kompose
// NewNamer returns a namer that returns names based on the specified project and
// service name and an inner counter, e.g. project_service_1, project_service_2…
func NewNamer(ctx context.Context, client client.APIClient, project, service string, oneOff bool) (Namer, error) {
	namer := &defaultNamer{
		project: project,
		service: service,
		oneOff:  oneOff,
	}

	filter := filters.NewArgs()
	filter.Add("label", fmt.Sprintf("%s=%s", labels.PROJECT.Str(), project))
	filter.Add("label", fmt.Sprintf("%s=%s", labels.SERVICE.Str(), service))
	if oneOff {
		filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "True"))
	} else {
		filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "False"))
	}

	containers, err := client.ContainerList(ctx, types.ContainerListOptions{
		All:    true,
		Filter: filter,
	})
	if err != nil {
		return nil, err
	}

	maxNumber := 0
	for _, container := range containers {
		number, err := strconv.Atoi(container.Labels[labels.NUMBER.Str()])
		if err != nil {
			return nil, err
		}
		if number > maxNumber {
			maxNumber = number
		}
	}
	namer.currentNumber = maxNumber + 1

	return namer, nil
}