Beispiel #1
0
        protected void TestCustomHelmExeInPackage_RelativePath(string version)
        {
            var fileName = Path.Combine(Path.GetTempPath(), $"helm-v{version}-{HelmOsPlatform}.tgz");

            using (new TemporaryFile(fileName))
            {
                DownloadHelmPackage(version, fileName);

                var customHelmExePackageId = Kubernetes.SpecialVariables.Helm.Packages.CustomHelmExePackageKey;
                Variables.Set(PackageVariables.IndexedOriginalPath(customHelmExePackageId), fileName);
                Variables.Set(PackageVariables.IndexedExtract(customHelmExePackageId), "True");
                Variables.Set(PackageVariables.IndexedPackageId(customHelmExePackageId), "helmexe");
                Variables.Set(PackageVariables.IndexedPackageVersion(customHelmExePackageId), version);

                // If package is provided then it should be treated as a relative path
                var customLocation = HelmOsPlatform + Path.DirectorySeparatorChar + "helm";
                Variables.Set(Kubernetes.SpecialVariables.Helm.CustomHelmExecutable, customLocation);

                AddPostDeployMessageCheckAndCleanup();

                var result = DeployPackage();
                result.AssertSuccess();
                result.AssertOutput($"Using custom helm executable at {HelmOsPlatform}\\helm from inside package. Full path at");
            }
        }
Beispiel #2
0
        public static IEnumerable <DeployedPackage> GetDeployedPackages(RunningDeployment deployment)
        {
            var variables = deployment.Variables;

            if (!string.IsNullOrWhiteSpace(variables.Get(PackageVariables.PackageId)))
            {
                yield return(new DeployedPackage(
                                 variables.Get(PackageVariables.PackageId),
                                 variables.Get(PackageVariables.PackageVersion),
                                 deployment.PackageFilePath
                                 ));
            }

            foreach (var packageReferenceName in variables.GetIndexes(PackageVariables.PackageCollection))
            {
                if (string.IsNullOrEmpty(packageReferenceName) && variables.IsSet(PackageVariables.PackageId))
                {
                    continue;
                }

                yield return(new DeployedPackage(
                                 variables.Get(PackageVariables.IndexedPackageId(packageReferenceName)),
                                 variables.Get(PackageVariables.IndexedPackageVersion(packageReferenceName)),
                                 variables.Get(PackageVariables.IndexedOriginalPath(packageReferenceName))
                                 ));
            }
        }
Beispiel #3
0
        public string GetNormalizedPackageFilename(RunningDeployment deployment)
        {
            var id        = deployment.Variables.Get(PackageVariables.IndexedPackageId(null));
            var version   = deployment.Variables.Get(PackageVariables.IndexedPackageVersion(null));
            var extension = Path.GetExtension(deployment.Variables.Get(PackageVariables.IndexedOriginalPath(null)));

            return($"{id}.{version}{extension}");
        }
Beispiel #4
0
        string PackageDirectory(string packageReferenceName)
        {
            var packageRoot = packageReferenceName;

            if (string.IsNullOrEmpty(packageReferenceName))
            {
                packageRoot = variables.Get(PackageVariables.IndexedPackageId(packageReferenceName ?? ""));
            }
            return(fileSystem.RemoveInvalidFileNameChars(packageRoot ?? string.Empty));
        }
Beispiel #5
0
        public static IEnumerable <DeployedPackage> GetDeployedPackages(RunningDeployment deployment)
        {
            var variables = deployment.Variables;

            foreach (var packageReferenceName in variables.GetIndexes(PackageVariables.PackageCollection))
            {
                yield return(new DeployedPackage(
                                 variables.Get(PackageVariables.IndexedPackageId(packageReferenceName)),
                                 variables.Get(PackageVariables.IndexedPackageVersion(packageReferenceName)),
                                 variables.Get(PackageVariables.IndexedOriginalPath(packageReferenceName))
                                 ));
            }
        }
Beispiel #6
0
        void StagePackageReferences(RunningDeployment deployment)
        {
            var variables = deployment.Variables;

            // No need to check for "default" package since it gets extracted in the current directory in previous step.
            var packageReferenceNames = variables.GetIndexes(PackageVariables.PackageCollection)
                                        .Where(i => !string.IsNullOrEmpty(i));

            foreach (var packageReferenceName in packageReferenceNames)
            {
                Log.Verbose($"Considering '{packageReferenceName}' for extraction");
                var sanitizedPackageReferenceName = fileSystem.RemoveInvalidFileNameChars(packageReferenceName);

                var packageOriginalPath = variables.Get(PackageVariables.IndexedOriginalPath(packageReferenceName));

                if (string.IsNullOrWhiteSpace(packageOriginalPath))
                {
                    Log.Info($"Package '{packageReferenceName}' was not acquired or does not require staging");
                    continue;
                }

                packageOriginalPath = Path.GetFullPath(variables.Get(PackageVariables.IndexedOriginalPath(packageReferenceName)));

                // In the case of container images, the original path is not a file-path.  We won't try and extract or move it.
                if (!fileSystem.FileExists(packageOriginalPath))
                {
                    Log.Verbose($"Package '{packageReferenceName}' was not found at '{packageOriginalPath}', skipping extraction");
                    continue;
                }

                var shouldExtract = variables.GetFlag(PackageVariables.IndexedExtract(packageReferenceName));

                if (forceExtract || shouldExtract)
                {
                    var extractionPath = Path.Combine(deployment.CurrentDirectory, sanitizedPackageReferenceName);
                    ExtractPackage(packageOriginalPath, extractionPath);
                    Log.SetOutputVariable(SpecialVariables.Packages.ExtractedPath(packageReferenceName), extractionPath, variables);
                }
                else
                {
                    var localPackageFileName   = sanitizedPackageReferenceName + Path.GetExtension(packageOriginalPath);
                    var destinationPackagePath = Path.Combine(deployment.CurrentDirectory, localPackageFileName);
                    Log.Info($"Copying package: '{packageOriginalPath}' -> '{destinationPackagePath}'");
                    fileSystem.CopyFile(packageOriginalPath, destinationPackagePath);
                    Log.SetOutputVariable(SpecialVariables.Packages.PackageFilePath(packageReferenceName), destinationPackagePath, variables);
                    Log.SetOutputVariable(SpecialVariables.Packages.PackageFileName(packageReferenceName), localPackageFileName, variables);
                }
            }
        }
Beispiel #7
0
        public void ValuesFromPackage_NewValuesUsed()
        {
            //Additional Package
            Variables.Set(PackageVariables.IndexedPackageId("Pack-1"), "CustomValues");
            Variables.Set(PackageVariables.IndexedPackageVersion("Pack-1"), "2.0.0");
            Variables.Set(PackageVariables.IndexedOriginalPath("Pack-1"), GetFixtureResouce("Charts", "CustomValues.2.0.0.zip"));
            Variables.Set(Kubernetes.SpecialVariables.Helm.Packages.ValuesFilePath("Pack-1"), "values.yaml");

            //Variable that will replace packaged value in package
            Variables.Set("MySecretMessage", "Variable Replaced In Package");

            var result = DeployPackage();

            result.AssertSuccess();
            Assert.AreEqual("Hello Variable Replaced In Package", result.CapturedOutput.OutputVariables["Message"]);
        }
Beispiel #8
0
        public void ValuesFromPackageAndExplicit_ExplicitTakesPrecedence()
        {
            //Helm Config (lets make sure Explicit values take precedence
            Variables.Set(Kubernetes.SpecialVariables.Helm.KeyValues, "{\"SpecialMessage\": \"FooBar\"}");

            //Additional Package
            Variables.Set(PackageVariables.IndexedPackageId("Pack-1"), "CustomValues");
            Variables.Set(PackageVariables.IndexedPackageVersion("Pack-1"), "2.0.0");
            Variables.Set(PackageVariables.IndexedOriginalPath("Pack-1"),
                          GetFixtureResouce("Charts", "CustomValues.2.0.0.zip"));
            Variables.Set(Kubernetes.SpecialVariables.Helm.Packages.ValuesFilePath("Pack-1"), "values.yaml");

            //Variable that will replace packaged value in package
            Variables.Set("MySecretMessage", "From A Variable Replaced In Package");

            var result = DeployPackage();

            result.AssertSuccess();
            Assert.AreEqual("Hello FooBar", result.CapturedOutput.OutputVariables["Message"]);
        }
        IEnumerable <string> AdditionalValuesFiles(RunningDeployment deployment)
        {
            var variables             = deployment.Variables;
            var packageReferenceNames = variables.GetIndexes(PackageVariables.PackageCollection);

            foreach (var packageReferenceName in packageReferenceNames)
            {
                var sanitizedPackageReferenceName = fileSystem.RemoveInvalidFileNameChars(packageReferenceName);
                var paths = variables.GetPaths(SpecialVariables.Helm.Packages.ValuesFilePath(packageReferenceName));

                foreach (var providedPath in paths)
                {
                    var packageId    = variables.Get(PackageVariables.IndexedPackageId(packageReferenceName));
                    var version      = variables.Get(PackageVariables.IndexedPackageVersion(packageReferenceName));
                    var relativePath = Path.Combine(sanitizedPackageReferenceName, providedPath);
                    var files        = fileSystem.EnumerateFilesWithGlob(deployment.CurrentDirectory, relativePath).ToList();

                    if (!files.Any() && string.IsNullOrEmpty(packageReferenceName)) // Chart archives have chart name root directory
                    {
                        log.Verbose($"Unable to find values files at path `{providedPath}`. " +
                                    $"Chart package contains root directory with chart name, so looking for values in there.");
                        var chartRelativePath = Path.Combine(fileSystem.RemoveInvalidFileNameChars(packageId), relativePath);
                        files = fileSystem.EnumerateFilesWithGlob(deployment.CurrentDirectory, chartRelativePath).ToList();
                    }

                    if (!files.Any())
                    {
                        throw new CommandException($"Unable to find file `{providedPath}` for package {packageId} v{version}");
                    }

                    foreach (var file in files)
                    {
                        var relative = file.Substring(Path.Combine(deployment.CurrentDirectory, sanitizedPackageReferenceName).Length);
                        log.Info($"Including values file `{relative}` from package {packageId} v{version}");
                        yield return(Path.GetFullPath(file));
                    }
                }
            }
        }
Beispiel #10
0
        public virtual void SetUp()
        {
            FileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            // Ensure staging directory exists and is empty
            StagingDirectory = Path.Combine(Path.GetTempPath(), "CalamariTestStaging");
            FileSystem.EnsureDirectoryExists(StagingDirectory);
            FileSystem.PurgeDirectory(StagingDirectory, FailureOptions.ThrowOnFailure);

            Environment.SetEnvironmentVariable("TentacleJournal",
                                               Path.Combine(StagingDirectory, "DeploymentJournal.xml"));

            Variables = new VariablesFactory(FileSystem).Create(new CommonOptions("test"));
            Variables.Set(TentacleVariables.Agent.ApplicationDirectoryPath, StagingDirectory);

            //Chart Pckage
            Variables.Set(PackageVariables.PackageId, "mychart");
            Variables.Set(PackageVariables.PackageVersion, "0.3.7");
            Variables.Set(PackageVariables.IndexedPackageId(""), $"#{{{PackageVariables.PackageId}}}");
            Variables.Set(PackageVariables.IndexedPackageVersion(""), $"#{{{PackageVariables.PackageVersion}}}");

            //Helm Options
            Variables.Set(Kubernetes.SpecialVariables.Helm.ReleaseName, ReleaseName);
            Variables.Set(Kubernetes.SpecialVariables.Helm.ClientVersion, helmVersion.ToString());

            //K8S Auth
            Variables.Set(Kubernetes.SpecialVariables.ClusterUrl, ServerUrl);
            Variables.Set(Kubernetes.SpecialVariables.SkipTlsVerification, "True");
            Variables.Set(Kubernetes.SpecialVariables.Namespace, Namespace);
            Variables.Set(SpecialVariables.Account.AccountType, "Token");
            Variables.Set(SpecialVariables.Account.Token, ClusterToken);

            if (ExplicitExeVersion != null)
            {
                Variables.Set(Kubernetes.SpecialVariables.Helm.CustomHelmExecutable, HelmExePath);
            }

            AddPostDeployMessageCheckAndCleanup();
        }
Beispiel #11
0
        IEnumerable <string> FileTargetFactory()
        {
            var packageReferenceNames = variables.GetIndexes(PackageVariables.PackageCollection);

            foreach (var packageReferenceName in packageReferenceNames)
            {
                var packageRoot = packageReferenceName;
                if (string.IsNullOrEmpty(packageReferenceName))
                {
                    packageRoot = variables.Get(PackageVariables.IndexedPackageId(packageReferenceName));
                }
                var sanitizedPackageReferenceName = fileSystem.RemoveInvalidFileNameChars(packageRoot ?? String.Empty);

                yield return(Path.Combine(sanitizedPackageReferenceName, "values.yaml"));

                var paths = variables.GetPaths(SpecialVariables.Helm.Packages.ValuesFilePath(packageReferenceName));

                foreach (var path in paths)
                {
                    yield return(Path.Combine(sanitizedPackageReferenceName, path));
                }
            }
        }
        string GetChartLocation(RunningDeployment deployment)
        {
            var installDir = deployment.Variables.Get(PackageVariables.Output.InstallationDirectoryPath);

            var packageId = deployment.Variables.Get(PackageVariables.IndexedPackageId(string.Empty));

            // Try the root directory
            if (fileSystem.FileExists(Path.Combine(installDir, "Chart.yaml")))
            {
                return(Path.Combine(installDir, "Chart.yaml"));
            }

            // Try the directory that matches the package id
            var packageIdPath = Path.Combine(installDir, packageId);

            if (fileSystem.DirectoryExists(packageIdPath) && fileSystem.FileExists(Path.Combine(packageIdPath, "Chart.yaml")))
            {
                return(packageIdPath);
            }

            /*
             * Although conventions suggests that the directory inside the helm archive matches the package ID, this
             * can not be assumed. If the standard locations above failed to locate the Chart.yaml file, loop over
             * all subdirectories to try and find the file.
             */
            foreach (var dir in fileSystem.EnumerateDirectories(installDir))
            {
                if (fileSystem.FileExists(Path.Combine(dir, "Chart.yaml")))
                {
                    return(dir);
                }
            }

            // Nothing worked
            throw new CommandException($"Unexpected error. Chart.yaml was not found in {packageIdPath}");
        }