public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var rootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "", node);

            foreach (var item in rootNode.Children)
            {
                switch (item.Key.ToString())
                {
                case DockerComposeConstants.ExternalNetworkDriver:
                {
                    ParseExternalNetworkNode(
                        DockerComposeUtils.GenerateTraceContext(traceContext,
                                                                DockerComposeConstants.ExternalNetworkDriver),
                        DockerComposeUtils.ValidateAndGetMapping(
                            traceContext,
                            DockerComposeConstants.ExternalNetworkDriver,
                            item.Value),
                        ignoredKeys);

                    break;
                }

                default:
                {
                    // Parse custom network's when we add that support in our product
                    break;
                }
                }
            }
        }
示例#2
0
        private CodePackageType GetCodePackage(string namePrefix, ComposeServiceTypeDescription serviceTypeDescription)
        {
            var containerCodePackage = new CodePackageType()
            {
                Name    = DockerComposeUtils.GetCodePackageName(namePrefix),
                Version = serviceTypeDescription.TypeVersion,
                EnvironmentVariables = new EnvironmentVariableType[serviceTypeDescription.EnvironmentVariables.Count],
                EntryPoint           = new EntryPointDescriptionType()
                {
                    Item = this.GetContainerHostEntryPointType(serviceTypeDescription)
                }
            };

            var index = 0;

            foreach (var environmentVariable in serviceTypeDescription.EnvironmentVariables)
            {
                var codePackageEnv = new EnvironmentVariableType();

                codePackageEnv.Name  = environmentVariable.Key;
                codePackageEnv.Value = string.Empty;
                containerCodePackage.EnvironmentVariables[index] = codePackageEnv;
                ++index;
            }

            return(containerCodePackage);
        }
示例#3
0
        private ServiceManifestType CreateServiceManifest(
            string namePrefix,
            string version,
            ComposeServiceTypeDescription serviceTypeDescription,
            out IList <PortBindingType> portBindings)
        {
            var serviceManifestType = new ServiceManifestType()
            {
                Name    = DockerComposeUtils.GetServicePackageName(namePrefix),
                Version = version
            };

            // Service Type
            serviceManifestType.ServiceTypes    = new object[1];
            serviceManifestType.ServiceTypes[0] = this.GetContainerServiceType(namePrefix);

            // CodePackage
            serviceManifestType.CodePackage    = new CodePackageType[1];
            serviceManifestType.CodePackage[0] = this.GetCodePackage(namePrefix, serviceTypeDescription);

            var endpointResources = this.GetEndpointResources(namePrefix, serviceTypeDescription, out portBindings);

            if (endpointResources.Count() > 0)
            {
                serviceManifestType.Resources = new ResourcesType
                {
                    Endpoints = endpointResources.ToArray()
                };
            }

            return(serviceManifestType);
        }
示例#4
0
        private DefaultServicesTypeService GetDefaultServiceTypeEntry(
            string namePrefix,
            ComposeServiceTypeDescription serviceTypeDescription)
        {
            //
            // In order to avoid dns name collisions between services of 2 different applications, service dns names be subdomains
            // under the application name.
            //
            var defaultServiceType = new DefaultServicesTypeService
            {
                Name           = namePrefix,
                ServiceDnsName = this.generateDnsNames ? DockerComposeUtils.GetServiceDnsName(this.applicationName, namePrefix) : "",
                ServicePackageActivationMode = "ExclusiveProcess",
                Item = new StatelessServiceType()
                {
                    InstanceCount      = serviceTypeDescription.InstanceCount.ToString(),
                    ServiceTypeName    = DockerComposeUtils.GetServiceTypeName(namePrefix),
                    SingletonPartition = new ServiceTypeSingletonPartition(),
                },
            };

            if (serviceTypeDescription.PlacementConstraints.Count != 0)
            {
                defaultServiceType.Item.PlacementConstraints =
                    $"({this.MergePlacementConstraints(serviceTypeDescription.PlacementConstraints)})";
            }

            return(defaultServiceType);
        }
        public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var rootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "", node);

            foreach (var item in rootNode.Children)
            {
                switch (item.Key.ToString())
                {
                case DockerComposeConstants.DriverKey:
                {
                    this.DriverName = DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.DriverOptionsKey, item.Value).ToString();
                    break;
                }

                case DockerComposeConstants.DriverOptionsKey:
                {
                    var driverOptions = DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.DriverOptionsKey, item.Value);
                    foreach (var driverOption in driverOptions)
                    {
                        this.DriverOptions.Add(driverOption.Key.ToString(), driverOption.Value.ToString());
                    }
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
        }
        public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var portMapping = DockerComposeUtils.ValidateAndGetScalar(traceContext, "", node).ToString();
            var ports       = portMapping.Split(DockerComposeConstants.SemiColonDelimiter[0]);

            if (ports.Length > 2)
            {
                throw new FabricComposeException(string.Format("{0} - Unable to parse Port - {1}", traceContext, portMapping));
            }

            if (ports.Length == 1)
            {
                this.ExtractProtocol(traceContext, portMapping, ports[0]);
            }
            else
            {
                this.HostPort = ports[0];
                if (string.IsNullOrEmpty(this.HostPort))
                {
                    throw new FabricComposeException(string.Format("{0} - Port invalid format - specified {1}", traceContext, portMapping));
                }

                this.ExtractProtocol(traceContext, portMapping, ports[1]);
            }
        }
示例#7
0
 private ServiceTypeType GetContainerServiceType(string namePrefix)
 {
     return(new StatelessServiceTypeType()
     {
         ServiceTypeName = DockerComposeUtils.GetServiceTypeName(namePrefix),
         UseImplicitHost = true
     });
 }
        private void ParseVolumes(string context, YamlMappingNode volumes, HashSet <string> ignoredKeys)
        {
            foreach (var volume in volumes)
            {
                DockerComposeUtils.ValidateAndGetMapping(context, volume.Key.ToString(), volume.Value);

                var volumeDescription = new ComposeVolumeDescription();
                volumeDescription.Parse(context, volume.Value, ignoredKeys);
                this.VolumeDescriptions.Add(volume.Key.ToString(), volumeDescription);
            }
        }
 private void ParsePlacementConstraints(string traceContext, YamlMappingNode placementConstraints)
 {
     foreach (var placementConstraintItem in placementConstraints)
     {
         var constraints = DockerComposeUtils.ValidateAndGetSequence(traceContext, "", placementConstraintItem.Value);
         foreach (var constraint in constraints)
         {
             this.PlacementConstraints.Add(constraint.ToString());
         }
     }
 }
        private void ParseNetworks(string context, YamlMappingNode networks, HashSet <string> ignoredKeys)
        {
            foreach (var network in networks)
            {
                DockerComposeUtils.ValidateAndGetMapping(context, network.Key.ToString(), network.Value);

                var networkDescription = new ComposeNetworkDescription(network.Key.ToString());
                networkDescription.Parse(context, network.Value, ignoredKeys);
                this.NetworkDescriptions.Add(network.Key.ToString(), networkDescription);
            }
        }
        private void ParseServices(string context, YamlMappingNode servicesRootNode, HashSet <string> ignoredKeys)
        {
            foreach (var childItem in servicesRootNode.Children)
            {
                var name = childItem.Key.ToString();
                DockerComposeUtils.ValidateAndGetMapping(context, name, childItem.Value);

                var serviceTypeDesc = new ComposeServiceTypeDescription(name, this.ApplicationTypeVersion);
                serviceTypeDesc.Parse(
                    DockerComposeUtils.GenerateTraceContext(context, name),
                    childItem.Value,
                    ignoredKeys);

                this.ServiceTypeDescriptions.Add(name, serviceTypeDesc);
            }
        }
示例#12
0
        private IEnumerable <EndpointType> GetEndpointResources(string namePrefix,
                                                                ComposeServiceTypeDescription serviceTypeDescription, out IList <PortBindingType> portBindings)
        {
            var endpoints = new List <EndpointType>();

            portBindings = new List <PortBindingType>();

            var i = 0;

            foreach (var portMapping in serviceTypeDescription.Ports)
            {
                var endpoint = new EndpointType()
                {
                    Name = DockerComposeUtils.GetEndpointName(namePrefix, i),
                };

                if (!string.IsNullOrEmpty(portMapping.HostPort))
                {
                    endpoint.Port          = Int32.Parse(portMapping.HostPort);
                    endpoint.PortSpecified = true;
                }

                endpoint.Protocol = EndpointTypeProtocol.tcp;
                if (portMapping.Protocol != null && portMapping.Protocol.StartsWith(DockerComposeConstants.HttpProtocol))
                {
                    endpoint.UriScheme = portMapping.Protocol;
                }

                if (!string.IsNullOrEmpty(portMapping.ContainerPort))
                {
                    var portBinding = new PortBindingType()
                    {
                        ContainerPort = Int32.Parse(portMapping.ContainerPort),
                        EndpointRef   = endpoint.Name
                    };
                    portBindings.Add(portBinding);
                }

                endpoints.Add(endpoint);
                ++i;
            }

            return(endpoints);
        }
        public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var resourcesRootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "", node);

            foreach (var childItem in resourcesRootNode.Children)
            {
                string key = childItem.Key.ToString();
                switch (childItem.Key.ToString())
                {
                case DockerComposeConstants.ResourceLimitsKey:
                {
                    this.ParseLimits(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.ResourceLimitsKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.ResourceLimitsKey, childItem.Value));

                    break;
                }

                case DockerComposeConstants.ResourceReservationsKey:
                {
                    this.ParseReservations(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.ResourceReservationsKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.ResourceReservationsKey, childItem.Value));

                    break;
                }

                case DockerComposeConstants.LabelsKey:
                {
                    // ignored
                    break;
                }

                default:
                {
                    ignoredKeys.Add(key);
                    break;
                }
                }
            }
        }
        private void ParseLimits(string traceContext, YamlMappingNode limitsNode)
        {
            foreach (var childItem in limitsNode.Children)
            {
                string key = childItem.Key.ToString();
                switch (childItem.Key.ToString())
                {
                case DockerComposeConstants.CpuSharesKey:
                {
                    this.LimitCpuShares = DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.CpuSharesKey, childItem.Value).ToString();
                    break;
                }

                case DockerComposeConstants.CpusKey:
                {
                    this.LimitCpus = DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.CpusKey, childItem.Value).ToString();
                    break;
                }

                case DockerComposeConstants.MemoryKey:
                {
                    this.LimitMemoryInMb = this.NormalizeToMb(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.MemoryKey),
                        DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.MemoryKey, childItem.Value).ToString());

                    break;
                }

                case DockerComposeConstants.MemorySwapKey:
                {
                    this.LimitMemorySwapInMb = this.NormalizeToMb(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.MemorySwapKey),
                        DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.MemorySwapKey, childItem.Value).ToString());
                    break;
                }
                }
            }
        }
        public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var serviceRootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "", node);

            foreach (var childItem in serviceRootNode.Children)
            {
                var key = childItem.Key.ToString();
                switch (childItem.Key.ToString())
                {
                case DockerComposeConstants.ImageKey:
                {
                    this.ImageName = DockerComposeUtils.ValidateAndGetScalar(
                        traceContext,
                        DockerComposeConstants.ImageKey,
                        childItem.Value).ToString();

                    this.ParseImageTag(this.ImageName);
                    break;
                }

                case DockerComposeConstants.PortsKey:
                {
                    this.ParsePorts(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.PortsKey),
                        DockerComposeUtils.ValidateAndGetSequence(traceContext, DockerComposeConstants.PortsKey, childItem.Value),
                        ignoredKeys);

                    break;
                }

                case DockerComposeConstants.DeployKey:
                {
                    this.ParseDeploymentParameters(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.DeployKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.DeployKey, childItem.Value),
                        ignoredKeys);

                    break;
                }

                case DockerComposeConstants.EntryPointKey:
                {
                    this.EntryPointOverride = DockerComposeUtils.ValidateAndGetScalar(
                        traceContext,
                        DockerComposeConstants.ImageKey,
                        childItem.Value).ToString();

                    break;
                }

                case DockerComposeConstants.CommandKey:
                {
                    this.Commands = DockerComposeUtils.ValidateAndGetScalar(
                        traceContext,
                        DockerComposeConstants.ImageKey,
                        childItem.Value).ToString();

                    break;
                }

                case DockerComposeConstants.LoggingKey:
                {
                    DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.LoggingKey, childItem.Value);

                    this.LoggingOptions.Parse(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.LoggingKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.LoggingKey, childItem.Value),
                        ignoredKeys);

                    break;
                }

                case DockerComposeConstants.VolumesKey:
                {
                    this.ParseVolumeMappings(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.VolumesKey),
                        DockerComposeUtils.ValidateAndGetSequence(traceContext, DockerComposeConstants.VolumesKey, childItem.Value),
                        ignoredKeys);

                    break;
                }

                case DockerComposeConstants.EnvironmentKey:
                {
                    if (childItem.Value.NodeType == YamlNodeType.Sequence)
                    {
                        this.ParseEnvironment(
                            DockerComposeUtils.GenerateTraceContext(traceContext,
                                                                    DockerComposeConstants.EnvironmentKey),
                            DockerComposeUtils.ValidateAndGetSequence(traceContext,
                                                                      DockerComposeConstants.EnvironmentKey, childItem.Value));
                    }
                    else if (childItem.Value.NodeType == YamlNodeType.Mapping)
                    {
                        this.ParseEnvironment(
                            DockerComposeUtils.GenerateTraceContext(traceContext,
                                                                    DockerComposeConstants.EnvironmentKey),
                            DockerComposeUtils.ValidateAndGetMapping(traceContext,
                                                                     DockerComposeConstants.EnvironmentKey, childItem.Value));
                    }
                    else
                    {
                        throw new FabricComposeException(
                                  string.Format("{0} - {1} expects a sequence or mapping element.", traceContext, DockerComposeConstants.EnvironmentKey));
                    }
                    break;
                }

                case DockerComposeConstants.IsolationKey:
                {
                    this.Isolation = DockerComposeUtils.ValidateAndGetScalar(
                        traceContext,
                        DockerComposeConstants.IsolationKey,
                        childItem.Value).ToString();

                    if (this.Isolation != DockerComposeConstants.IsolationValueDefault &&
                        this.Isolation != DockerComposeConstants.IsolationValueProcess &&
                        this.Isolation != DockerComposeConstants.IsolationValueHyperv)
                    {
                        throw new FabricComposeException(string.Format("{0} - Invalid value {1} specified for isolation", traceContext, this.Isolation));
                    }
                    break;
                }

                case DockerComposeConstants.LabelsKey:
                {
                    // ignored.
                    break;
                }

                default:
                {
                    // TODO: logging.
                    ignoredKeys.Add(key);
                    break;
                }
                }
            }
        }
        internal void ParseDeploymentParameters(string traceContext, YamlMappingNode rootDeployNode, HashSet <string> ignoredKeys)
        {
            foreach (var item in rootDeployNode)
            {
                var key = item.Key.ToString();
                switch (key)
                {
                case DockerComposeConstants.DeploymentModeKey:
                {
                    var value = item.Value.ToString();
                    if (value != DockerComposeConstants.DeploymentModeReplicatedValue)
                    {
                        throw new FabricComposeException(
                                  string.Format("{0} - Only 'replicated' deployment mode is supported. Specified {1}", traceContext, value));
                    }
                    break;
                }

                case DockerComposeConstants.ReplicasKey:
                {
                    var value = DockerComposeUtils.ValidateAndGetScalar(traceContext, DockerComposeConstants.ReplicasKey, item.Value).ToString();
                    try
                    {
                        this.InstanceCount = Int32.Parse(value);
                    }
                    catch (Exception e)
                    {
                        throw new FabricComposeException(string.Format("{0} - Parsing 'replicas' with value {1} failed", e, value));
                    }
                    break;
                }

                case DockerComposeConstants.PlacementKey:
                {
                    this.ParsePlacementConstraints(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.PlacementKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.PlacementKey, item.Value));
                    break;
                }

                case DockerComposeConstants.ResourcesKey:
                {
                    this.ParseResourceGovernance(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.ResourcesKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.ResourcesKey, item.Value),
                        ignoredKeys);
                    break;
                }

                case DockerComposeConstants.LabelsKey:
                {
                    break;
                }

                default:
                {
                    ignoredKeys.Add(key);
                    break;
                }
                }
            }
        }
        public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys)
        {
            var rootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "Root", node);

            foreach (var childItem in rootNode.Children)
            {
                var key = childItem.Key.ToString();
                switch (key)
                {
                case DockerComposeConstants.VersionKey:
                {
                    this.DockerComposeVersion = childItem.Value.ToString();

                    if (!this.DockerComposeVersion
                        .StartsWith(DockerComposeConstants.SupportedDockerComposeVersion3) &&
                        !this.DockerComposeVersion
                        .StartsWith(DockerComposeConstants.SupportedDockerComposeVersion2))
                    {
                        throw new FabricComposeException(
                                  string.Format(
                                      "Docker compose file version not supported. Supported version - '3' input file version - '{0}'",
                                      childItem.Value.ToString()));
                    }

                    break;
                }

                case DockerComposeConstants.ServicesKey:
                {
                    this.ParseServices(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.ServicesKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.ServicesKey, childItem.Value),
                        ignoredKeys);
                    break;
                }

                case DockerComposeConstants.VolumesKey:
                {
                    this.ParseVolumes(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.VolumesKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.VolumesKey, childItem.Value),
                        ignoredKeys);
                    break;
                }

                case DockerComposeConstants.LabelsKey:
                {
                    this.ParseLabels(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.LabelsKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.LabelsKey, childItem.Value));
                    break;
                }

                case DockerComposeConstants.NetworksKey:
                {
                    this.ParseNetworks(
                        DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.NetworksKey),
                        DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.NetworksKey, childItem.Value),
                        ignoredKeys);
                    break;
                }

                default:
                {
                    ignoredKeys.Add(key);
                    break;
                }
                }
            }
        }