Ejemplo n.º 1
0
        public override string ToString()
        {
            List <string> options = new List <string>();

            if (!string.IsNullOrEmpty(ContainerName))
            {
                options.Add($"--name \"{ContainerName}\"");
            }

            if (PortBindings.Any())
            {
                options.AddRange(PortBindings.Select(portBinding => $"-p {portBinding.HostPort}:{portBinding.ContainerPort}"));
            }

            if (EnvironmentVariables.Any())
            {
                options.AddRange(EnvironmentVariables.Select(environmentVariable =>
                {
                    if (string.IsNullOrEmpty(environmentVariable.Value))
                    {
                        return($"-e {environmentVariable.Key}");
                    }
                    return($"-e {environmentVariable.Key}={environmentVariable.Value}");
                }));
            }

            if (RestartAutomatically)
            {
                options.Add("--restart=unless-stopped");
            }

            if (!string.IsNullOrEmpty(AdditionalOptions))
            {
                options.Add(AdditionalOptions);
            }

            return(string.Join(" ", options));
        }
Ejemplo n.º 2
0
 private void AddOptions(IList <string> arguments)
 {
     arguments.AddConditionally(
         Detached != Default.Detached,
         "--detach");
     arguments.AddConditionally(
         WithExtendedPrivileges != Default.WithExtendedPrivileges,
         "--privileged");
     arguments.AddConditionally(
         !AsUser.Equals(Default.AsUser, StringComparison.OrdinalIgnoreCase),
         $"--user {AsUser}");
     arguments.AddConditionally(
         WithoutTty != Default.WithoutTty,
         "-T");
     arguments.AddConditionally(
         InServiceContainer > Default.InServiceContainer,
         $"--index {InServiceContainer}");
     arguments.AddConditionally(
         !WithinWorkingDirectory.Equals(Default.WithinWorkingDirectory),
         $"--workdir \"{WithinWorkingDirectory}\"");
     arguments.AddConditionally(
         EnvironmentVariables.Any(),
         $"{string.Join(" ", EnvironmentVariables.Select(pair => $"--env {pair.Key}=\"{pair.Value}\""))}");
 }
Ejemplo n.º 3
0
        public void LogParameters(ILogger logger)
        {
            using var contentsScope = logger.BeginScope("Using create container parameters");

            logger.LogInformation($"Container name '{ContainerName}'");
            logger.LogInformation($"Container image: '{ImageId.FullName}");
            logger.LogInformation($"Network name: {ImageId.FullName}");

            using (logger.BeginScope("Labels"))
            {
                if (!Labels.Any())
                {
                    logger.LogError("No labels are configured for this container!");
                }

                foreach (var(name, value) in Labels)
                {
                    logger.LogInformation($"{name}: {value}");
                }
            }

            using (logger.BeginScope("Environment variables"))
            {
                if (!EnvironmentVariables.Any())
                {
                    logger.LogInformation("No environment variables are set");
                }

                foreach (var variable in EnvironmentVariables)
                {
                    logger.LogInformation(variable);
                }
            }

            using (logger.BeginScope("Volume mounts"))
            {
                if (!Mounts.Any())
                {
                    logger.LogWarning("There are no volume mounts specified for this container");
                }

                foreach (var mount in Mounts)
                {
                    logger.LogInformation($"Volume '{mount.Source}' mounted to '{mount.Target}'");
                }
            }

            using (logger.BeginScope("Port bindings"))
            {
                if (!PortBindings.Any())
                {
                    logger.LogWarning("There are no port bindings specified for this container");
                }

                foreach (var(containerPort, hostBindings) in PortBindings)
                {
                    foreach (var binding in hostBindings)
                    {
                        logger.LogInformation(
                            $"Container port {containerPort} is bound to {binding.HostIP}:{binding.HostPort}"
                            );
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override void WriteYaml(StreamWriter writer, AssetManager assetManager, ILoggerFactory loggerFactory, int indent = 0)
        {
            var spaces = "".PadLeft(indent);

            writer.Write($"{spaces}- name: {Name}\n");
            spaces = "".PadLeft(indent + 2);
            writer.Write($"{spaces}type: {ServiceType}\n");
            writer.Write($"{spaces}image:\n");
            spaces = "".PadLeft(indent + 4);
            writer.Write($"{spaces}name: {Image?.Name ?? Name}\n");
            writer.Write($"{spaces}tag: {Image?.Tag ?? "{{.Values.buildNumber}}"}\n");
            spaces = "".PadLeft(indent + 2);
            if (!string.IsNullOrEmpty(SolutionFile))
            {
                writer.Write($"{spaces}solutionFile: {SolutionFile.Replace("\\", "/")}\n");
            }

            if (string.IsNullOrEmpty(ProjectFile) && !string.IsNullOrEmpty(SolutionFile))
            {
                var projName = Name.GetProjectName();
                ProjectFile = Path.Combine(Path.GetDirectoryName(SolutionFile), projName, $"{projName}.csproj");
                if (string.IsNullOrEmpty(AssemblyName))
                {
                    AssemblyName = projName;
                }
            }
            if (!string.IsNullOrEmpty(ProjectFile))
            {
                writer.Write($"{spaces}projectFile: {ProjectFile.Replace("\\", "/")}\n");
            }

            if (!string.IsNullOrEmpty(AssemblyName))
            {
                writer.Write($"{spaces}assemblyName: {AssemblyName}\n");
            }

            if (!string.IsNullOrEmpty(PrivateNugetFeed))
            {
                writer.Write($"{spaces}privateNugetFeed: {PrivateNugetFeed}\n");
            }

            if (ServiceType == "web" || ServiceType == "api")
            {
                writer.Write($"{spaces}containerPort: {ContainerPort}\n");
                writer.Write($"{spaces}sshPort: {SshPort}\n");
                if (string.IsNullOrEmpty(SslCert))
                {
                    var dns = assetManager.Get(AssetType.Dns) as Dns;
                    SslCert = dns?.SslCert;
                }
                if (!string.IsNullOrEmpty(SslCert))
                {
                    writer.Write($"{spaces}sslCert: {SslCert}\n");
                }
                writer.Write($"{spaces}isFrontEnd: {IsFrontend}\n");

                if (!string.IsNullOrEmpty(LivenessCheck))
                {
                    writer.Write($"{spaces}livenessCheck: {LivenessCheck}\n");
                }
                if (!string.IsNullOrEmpty(ReadinessCheck))
                {
                    writer.Write($"{spaces}readinessCheck: {ReadinessCheck}\n");
                }
            }

            if (ServiceType == "job")
            {
                writer.Write($"{spaces}schedule: {Schedule ?? "*/1 * * * *"}\n");
                writer.Write($"{spaces}restartPolicy: {RestartPolicy ?? "Never"}\n");
                writer.Write($"{spaces}concurrencyPolicy: {ConcurrencyPolicy ?? "Forbid"}\n");
            }

            if (Volumes?.Any() == true)
            {
                writer.Write($"{spaces}volumes:\n");
                spaces = "".PadLeft(indent + 4);
                foreach (var volume in Volumes)
                {
                    writer.Write($"{spaces}- name: {volume}\n");
                }
            }
            spaces = "".PadLeft(indent + 4);

            if (EnvironmentVariables?.Any() == true)
            {
                writer.Write($"{spaces}env:\n");
                spaces = "".PadLeft(indent + 4);
                foreach (var envVar in EnvironmentVariables)
                {
                    writer.Write($"{spaces}- name: {envVar.Name}\n");
                    writer.Write($"{spaces}  value: {envVar.Value}\n");
                }
            }
        }
Ejemplo n.º 5
0
 private void AddEnvironmentVariables(IList <string> arguments)
 {
     arguments.AddConditionally(
         EnvironmentVariables.Any(),
         $"{string.Join(" ", EnvironmentVariables.Select(pair => $"-e {pair.Key}=\"{pair.Value}\""))}");
 }