當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ServiceConfigs.Keys方法代碼示例

本文整理匯總了Golang中github.com/docker/libcompose/config.ServiceConfigs.Keys方法的典型用法代碼示例。如果您正苦於以下問題:Golang ServiceConfigs.Keys方法的具體用法?Golang ServiceConfigs.Keys怎麽用?Golang ServiceConfigs.Keys使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/docker/libcompose/config.ServiceConfigs的用法示例。


在下文中一共展示了ServiceConfigs.Keys方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: RemoveOrphans

// RemoveOrphans implements project.RuntimeProject.RemoveOrphans.
// It will remove orphan containers that are part of the project but not to any services.
func (p *Project) RemoveOrphans(ctx context.Context, projectName string, serviceConfigs *config.ServiceConfigs) error {
	client := p.clientFactory.Create(nil)
	filter := filters.NewArgs()
	filter.Add("label", labels.PROJECT.EqString(projectName))
	containers, err := client.ContainerList(ctx, types.ContainerListOptions{
		Filter: filter,
	})
	if err != nil {
		return err
	}
	currentServices := map[string]struct{}{}
	for _, serviceName := range serviceConfigs.Keys() {
		currentServices[serviceName] = struct{}{}
	}
	for _, container := range containers {
		serviceLabel := container.Labels[labels.SERVICE.Str()]
		if _, ok := currentServices[serviceLabel]; !ok {
			if err := client.ContainerKill(ctx, container.ID, "SIGKILL"); err != nil {
				return err
			}
			if err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
				Force: true,
			}); err != nil {
				return err
			}
		}
	}
	return nil
}
開發者ID:vdemeester,項目名稱:libcompose,代碼行數:31,代碼來源:project.go

示例2: ConvertToTaskDefinition

// ConvertToTaskDefinition transforms the yaml configs to its ecs equivalent (task definition)
func ConvertToTaskDefinition(taskDefinitionName string, context *project.Context,
	serviceConfigs *config.ServiceConfigs) (*ecs.TaskDefinition, error) {

	if serviceConfigs.Len() == 0 {
		return nil, errors.New("cannot create a task definition with no containers; invalid service config")
	}

	logUnsupportedConfigFields(context.Project)

	containerDefinitions := []*ecs.ContainerDefinition{}
	volumes := make(map[string]string) // map with key:=hostSourcePath value:=VolumeName

	for _, name := range serviceConfigs.Keys() {
		serviceConfig, ok := serviceConfigs.Get(name)
		if !ok {
			return nil, fmt.Errorf("Couldn't get service with name=[%s]", name)
		}
		logUnsupportedServiceConfigFields(name, serviceConfig)
		containerDef := &ecs.ContainerDefinition{
			Name: aws.String(name),
		}
		if err := convertToContainerDef(context, serviceConfig, volumes, containerDef); err != nil {
			return nil, err
		}
		containerDefinitions = append(containerDefinitions, containerDef)
	}
	taskDefinition := &ecs.TaskDefinition{
		Family:               aws.String(taskDefinitionName),
		ContainerDefinitions: containerDefinitions,
		Volumes:              convertToECSVolumes(volumes),
	}
	return taskDefinition, nil
}
開發者ID:uttarasridhar,項目名稱:amazon-ecs-cli,代碼行數:34,代碼來源:convert_task_definition.go

示例3: NetworksFromServices

// NetworksFromServices creates a new Networks struct based on networks configurations and
// services configuration. If a network is defined but not used by any service, it will return
// an error along the Networks.
func NetworksFromServices(cli client.NetworkAPIClient, projectName string, networkConfigs map[string]*config.NetworkConfig, services *config.ServiceConfigs, networkEnabled bool) (*Networks, error) {
	var err error
	networks := make([]*Network, 0, len(networkConfigs))
	networkNames := map[string]*yaml.Network{}
	for _, serviceName := range services.Keys() {
		serviceConfig, _ := services.Get(serviceName)
		if serviceConfig.NetworkMode != "" || serviceConfig.Networks == nil || len(serviceConfig.Networks.Networks) == 0 {
			continue
		}
		for _, network := range serviceConfig.Networks.Networks {
			if network.Name != "default" {
				if _, ok := networkConfigs[network.Name]; !ok {
					return nil, fmt.Errorf(`Service "%s" uses an undefined network "%s"`, serviceName, network.Name)
				}
			}
			networkNames[network.Name] = network
		}
	}
	for name, config := range networkConfigs {
		network := NewNetwork(projectName, name, config, cli)
		networks = append(networks, network)
	}
	if len(networkNames) != len(networks) {
		unused := []string{}
		for name := range networkConfigs {
			if name == "default" {
				continue
			}
			if _, ok := networkNames[name]; !ok {
				unused = append(unused, name)
			}
		}
		if len(unused) != 0 {
			err = fmt.Errorf("Some networks were defined but are not used by any service: %v", strings.Join(unused, " "))
		}
	}
	return &Networks{
		networks:       networks,
		networkEnabled: networkEnabled,
	}, err
}
開發者ID:jainvipin,項目名稱:libcompose,代碼行數:44,代碼來源:network.go


注:本文中的github.com/docker/libcompose/config.ServiceConfigs.Keys方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。