示例#1
0
        public 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 VariableDictionary();
            Variables.EnrichWithEnvironmentVariables();
            Variables.Set(SpecialVariables.Tentacle.Agent.ApplicationDirectoryPath, StagingDirectory);

            //Chart Pckage
            Variables.Set(SpecialVariables.Package.NuGetPackageId, "mychart");
            Variables.Set(SpecialVariables.Package.NuGetPackageVersion, "0.3.7");
            Variables.Set(SpecialVariables.Packages.PackageId(""), $"#{{{SpecialVariables.Package.NuGetPackageId}}}");
            Variables.Set(SpecialVariables.Packages.PackageVersion(""), $"#{{{SpecialVariables.Package.NuGetPackageVersion}}}");


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

            //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);

            AddPostDeployMessageCheckAndCleanup();
        }
示例#2
0
        ICommand CreateInstance(Type type, IVariables variables, ILog log)
        {
            var fileSystem        = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            var commandLineRunner = new CommandLineRunner(ConsoleLog.Instance, variables);
            var substituteInFiles = new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables);
            var extractPackages   = new ExtractPackage(new CombinedPackageExtractor(log), fileSystem, variables, log);

            if (type == typeof(PlanCommand))
            {
                return(new PlanCommand(log, variables, fileSystem, commandLineRunner, substituteInFiles, extractPackages));
            }

            if (type == typeof(ApplyCommand))
            {
                return(new ApplyCommand(log, variables, fileSystem, commandLineRunner, substituteInFiles, extractPackages));
            }

            if (type == typeof(DestroyCommand))
            {
                return(new DestroyCommand(log, variables, fileSystem, commandLineRunner, substituteInFiles, extractPackages));
            }

            if (type == typeof(DestroyPlanCommand))
            {
                return(new DestroyPlanCommand(log, variables, fileSystem, commandLineRunner, substituteInFiles, extractPackages));
            }

            throw new ArgumentException();
        }
示例#3
0
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);

            Guard.NotNullOrWhiteSpace(retentionPolicySet, "No retention-policy-set was specified. Please pass --retentionPolicySet \"Environments-2/projects-161/Step-Package B/machines-65/<default>\"");

            if (days <= 0 && releases <= 0)
            {
                throw new CommandException("A value must be provided for either --days or --releases");
            }

            var variables = new CalamariVariableDictionary();

            variables.EnrichWithEnvironmentVariables();

            var fileSystem        = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            var deploymentJournal = new DeploymentJournal(fileSystem, SemaphoreFactory.Get(), variables);
            var clock             = new SystemClock();

            var retentionPolicy = new RetentionPolicy(fileSystem, deploymentJournal, clock);

            retentionPolicy.ApplyRetentionPolicy(retentionPolicySet, days, releases);

            return(0);
        }
 public void SetUp()
 {
     tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
     insensitiveVariablesFileName = Path.Combine(tempDirectory, "myVariables.json");
     sensitiveVariablesFileName   = Path.ChangeExtension(insensitiveVariablesFileName, "secret");
     fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
 }
示例#5
0
        protected string UploadEntireCompressedPackage(string packageFilePath, string packageId, string packageVersion, List <S3PackageOptions> propertiesList, VariableDictionary customVariables = null)
        {
            var bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";
            var variables       = new CalamariVariables();

            propertiesList.ForEach(properties =>
            {
                properties.BucketKeyPrefix = bucketKeyPrefix;
                variables.Set(AwsSpecialVariables.S3.PackageOptions, JsonConvert.SerializeObject(properties, GetEnrichedSerializerSettings()));
                variables.Set(PackageVariables.PackageId, packageId);
                variables.Set(PackageVariables.PackageVersion, packageVersion);
            });

            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);

            if (customVariables != null)
            {
                variables.Merge(customVariables);
            }

            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var log        = new InMemoryLog();
                var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

                var command = new UploadAwsS3Command(
                    log,
                    variables,
                    fileSystem,
                    new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables),
                    new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                    new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                {
                    new JsonFormatVariableReplacer(fileSystem, log),
                    new XmlFormatVariableReplacer(fileSystem, log),
                    new YamlFormatVariableReplacer(fileSystem, log),
                    new PropertiesFormatVariableReplacer(fileSystem, log),
                }, variables, fileSystem, log)
                    );

                var result = command.Execute(new[] {
                    "--package", $"{packageFilePath}",
                    "--variables", $"{variablesFile}",
                    "--bucket", bucketName,
                    "--targetMode", S3TargetMode.EntirePackage.ToString()
                });

                result.Should().Be(0);
            }

            return(bucketKeyPrefix);
        }
示例#6
0
        public void PackageWithInvalidUrl_Throws()
        {
            var    badUrl = new Uri($"https://octopusdeploy.jfrog.io/gobbelygook/{Guid.NewGuid().ToString("N")}");
            var    badEndpointDownloader = new HelmChartPackageDownloader(CalamariPhysicalFileSystem.GetPhysicalFileSystem());
            Action action = () => badEndpointDownloader.DownloadPackage("something", new SemanticVersion("99.9.7"), "gobbely", new Uri(badUrl, "something.99.9.7"), new NetworkCredential(FeedUsername, FeedPassword), true, 1, TimeSpan.FromSeconds(3));

            action.Should().Throw <Exception>().And.Message.Should().Contain("Unable to read Helm index file").And.Contain("404");
        }
示例#7
0
 public LockFileBasedSemaphore(string name, TimeSpan lockTimeout, ILog log)
     : this(name,
            lockTimeout,
            new LockIo(CalamariPhysicalFileSystem.GetPhysicalFileSystem()),
            new ProcessFinder(),
            log)
 {
 }
示例#8
0
        private void SubstituteVariablesInAdditionalFiles()
        {
            // Replace variables on any other files that may have been extracted with the package
            var substituter = new FileSubstituter(CalamariPhysicalFileSystem.GetPhysicalFileSystem());

            new SubstituteInFilesConvention(CalamariPhysicalFileSystem.GetPhysicalFileSystem(), substituter)
            .Install(deployment);
        }
示例#9
0
        private IVariables AddEnvironmentVariables()
        {
            var variables = new VariablesFactory(CalamariPhysicalFileSystem.GetPhysicalFileSystem()).Create(new CommonOptions("test"));

            Assert.That(variables.GetNames().Count, Is.GreaterThan(3));
            Assert.That(variables.GetRaw(TentacleVariables.Agent.InstanceName), Is.EqualTo("#{env:TentacleInstanceName}"));
            return(variables);
        }
示例#10
0
        public void DownloadMavenSourcePackage()
        {
            var downloader = new MavenPackageDownloader(CalamariPhysicalFileSystem.GetPhysicalFileSystem(), new FreeSpaceChecker(CalamariPhysicalFileSystem.GetPhysicalFileSystem(), new CalamariVariables()));
            var pkg        = downloader.DownloadPackage("com.google.guava:guava:jar:sources", VersionFactory.CreateMavenVersion("22.0"), "feed-maven",
                                                        new Uri("https://repo.maven.apache.org/maven2/"), new NetworkCredential("", ""), true, 3, TimeSpan.FromSeconds(3));

            Assert.AreEqual("com.google.guava:guava:jar:sources", pkg.PackageId);
        }
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);

            Guard.NotNullOrWhiteSpace(packageId, "No package ID was specified. Please pass --packageId YourPackage");
            Guard.NotNullOrWhiteSpace(packageVersion, "No package version was specified. Please pass --packageVersion 1.0.0.0");
            Guard.NotNullOrWhiteSpace(packageHash, "No package hash was specified. Please pass --packageHash YourPackageHash");

            var fileSystem        = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            var commandLineRunner = new CommandLineRunner(
                new SplitCommandOutput(
                    new ConsoleCommandOutput(),
                    new ServiceMessageCommandOutput(
                        new CalamariVariableDictionary())));

            NuGetVersion version;

            if (!NuGetVersion.TryParse(packageVersion, out version))
            {
                throw new CommandException(String.Format("Package version '{0}' is not a valid Semantic Version", packageVersion));
            }

            var packageStore = new PackageStore(
                new GenericPackageExtractorFactory().createJavaGenericPackageExtractor(fileSystem));
            var packageMetadata = new ExtendedPackageMetadata()
            {
                Id = packageId, Version = packageVersion, Hash = packageHash
            };
            var package = packageStore.GetPackage(packageMetadata);

            if (package == null)
            {
                Log.VerboseFormat("Package {0} version {1} hash {2} has not been uploaded.",
                                  packageMetadata.Id, packageMetadata.Version, packageMetadata.Hash);

                Log.VerboseFormat("Finding earlier packages that have been uploaded to this Tentacle.");
                var nearestPackages = packageStore.GetNearestPackages(packageId, version).ToList();
                if (!nearestPackages.Any())
                {
                    Log.VerboseFormat("No earlier packages for {0} has been uploaded", packageId);
                    return(0);
                }

                Log.VerboseFormat("Found {0} earlier {1} of {2} on this Tentacle",
                                  nearestPackages.Count, nearestPackages.Count == 1 ? "version" : "versions", packageId);
                foreach (var nearestPackage in nearestPackages)
                {
                    Log.VerboseFormat("  - {0}: {1}", nearestPackage.Metadata.Version, nearestPackage.FullPath);
                    Log.ServiceMessages.PackageFound(nearestPackage.Metadata.Id, nearestPackage.Metadata.Version, nearestPackage.Metadata.Hash, nearestPackage.Metadata.FileExtension, nearestPackage.FullPath);
                }

                return(0);
            }

            Log.VerboseFormat("Package {0} {1} hash {2} has already been uploaded", package.Metadata.Id, package.Metadata.Version, package.Metadata.Hash);
            Log.ServiceMessages.PackageFound(package.Metadata.Id, package.Metadata.Version, package.Metadata.Hash, package.Metadata.FileExtension, package.FullPath, true);
            return(0);
        }
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            var variables   = new CalamariVariableDictionary(variablesFile, sensitiveVariablesFile, sensitiveVariablesPassword);
            var packageFile = variables.GetEnvironmentExpandedPath(SpecialVariables.Tentacle.CurrentDeployment.PackageFilePath);

            if (string.IsNullOrEmpty(packageFile))
            {
                throw new CommandException($"No package file was specified. Please provide `{SpecialVariables.Tentacle.CurrentDeployment.PackageFilePath}` variable");
            }

            if (!fileSystem.FileExists(packageFile))
            {
                throw new CommandException("Could not find package file: " + packageFile);
            }

            fileSystem.FreeDiskSpaceOverrideInMegaBytes = variables.GetInt32(SpecialVariables.FreeDiskSpaceOverrideInMegaBytes);
            fileSystem.SkipFreeDiskSpaceCheck           = variables.GetFlag(SpecialVariables.SkipFreeDiskSpaceCheck);

            var commandLineRunner = new CommandLineRunner(new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables)));
            var journal           = new DeploymentJournal(fileSystem, SemaphoreFactory.Get(), variables);

            var conventions = new List <IConvention>
            {
                new ContributeEnvironmentVariablesConvention(),
                new ContributePreviousInstallationConvention(journal),
                new ContributePreviousSuccessfulInstallationConvention(journal),
                new LogVariablesConvention(),
                new AlreadyInstalledConvention(journal),
                new TransferPackageConvention(fileSystem),
                new RollbackScriptConvention(DeploymentStages.DeployFailed, fileSystem, new CombinedScriptEngine(), commandLineRunner),
            };

            var deployment       = new RunningDeployment(packageFile, variables);
            var conventionRunner = new ConventionProcessor(deployment, conventions);

            try
            {
                conventionRunner.RunConventions();
                if (!deployment.SkipJournal)
                {
                    journal.AddJournalEntry(new JournalEntry(deployment, true));
                }
            }
            catch (Exception)
            {
                if (!deployment.SkipJournal)
                {
                    journal.AddJournalEntry(new JournalEntry(deployment, false));
                }
                throw;
            }

            return(0);
        }
示例#13
0
        public CommandResult Execute(Script script,
                                     IVariables variables,
                                     ICommandLineRunner commandLineRunner,
                                     Dictionary <string, string>?environmentVars = null)
        {
            var environmentVariablesIncludingProxy = environmentVars ?? new Dictionary <string, string>();

            foreach (var proxyVariable in ProxyEnvironmentVariablesGenerator.GenerateProxyEnvironmentVariables())
            {
                environmentVariablesIncludingProxy[proxyVariable.Key] = proxyVariable.Value;
            }

            var prepared = PrepareExecution(script, variables, environmentVariablesIncludingProxy);

            CommandResult?result = null;

            foreach (var execution in prepared)
            {
                if (variables.IsSet(CopyWorkingDirectoryVariable))
                {
                    CopyWorkingDirectory(variables,
                                         execution.CommandLineInvocation.WorkingDirectory,
                                         execution.CommandLineInvocation.Arguments);
                }

                try
                {
                    if (execution.CommandLineInvocation.Isolate)
                    {
                        using (SemaphoreFactory.Get()
                               .Acquire("CalamariSynchronizeProcess",
                                        "Waiting for other process to finish executing script"))
                        {
                            result = commandLineRunner.Execute(execution.CommandLineInvocation);
                        }
                    }
                    else
                    {
                        result = commandLineRunner.Execute(execution.CommandLineInvocation);
                    }

                    if (result.ExitCode != 0)
                    {
                        return(result);
                    }
                }
                finally
                {
                    foreach (var temporaryFile in execution.TemporaryFiles)
                    {
                        var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
                        fileSystem.DeleteFile(temporaryFile, FailureOptions.IgnoreFailure);
                    }
                }
            }

            return(result !);
        }
示例#14
0
        public void PackageWithWrongCredentials_Fails()
        {
            var downloader = new HelmChartPackageDownloader(CalamariPhysicalFileSystem.GetPhysicalFileSystem());
            var exception  = Assert.Throws <CommandException>(() => downloader.DownloadPackage("mychart", new SemanticVersion("0.3.7"), "helm-feed", new Uri(AuthFeedUri), new NetworkCredential(FeedUsername, "FAKE"), true, 1,
                                                                                               TimeSpan.FromSeconds(3)));

            StringAssert.Contains("Helm failed to download the chart", exception.Message);
            //StringAssert.Contains("401 Unauthorized", exception.Message);
        }
示例#15
0
        public void PackageWithCredentials_Loads()
        {
            var downloader = new HelmChartPackageDownloader(CalamariPhysicalFileSystem.GetPhysicalFileSystem());
            var pkg        = downloader.DownloadPackage("mychart", new SemanticVersion("0.3.7"), "helm-feed", new Uri(AuthFeedUri), new NetworkCredential(FeedUsername, FeedPassword), true, 1,
                                                        TimeSpan.FromSeconds(3));

            Assert.AreEqual("mychart", pkg.PackageId);
            Assert.AreEqual(new SemanticVersion("0.3.7"), pkg.Version);
        }
示例#16
0
        public bool PurgeDirectoryWithFolderUsingGlobs(string folderName, string fileName, string glob)
        {
            var testFile = CreateFile(folderName, fileName);

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            fileSystem.PurgeDirectory(PurgeTestDirectory, FailureOptions.IgnoreFailure, glob);

            return(File.Exists(testFile));
        }
        public bool PurgeDirectoryWithFolderExclusionWillNotCheckSubFiles(string folderName, string fileName)
        {
            var testFile = CreateFile(folderName, fileName);

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            fileSystem.PurgeDirectory(PurgeTestDirectory, (fsi) => fsi.IsDirectory && fsi.Name.StartsWith("Important"), FailureOptions.IgnoreFailure);

            return(File.Exists(testFile));
        }
示例#18
0
        string Upload(string packageName, List <S3FileSelectionProperties> fileSelections)
        {
            var bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";

            fileSelections.ForEach(properties =>
            {
                if (properties is S3MultiFileSelectionProperties multiFileSelectionProperties)
                {
                    multiFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                }

                if (properties is S3SingleFileSelectionProperties singleFileSelectionProperties)
                {
                    singleFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                }
            });

            var variablesFile = Path.GetTempFileName();
            var variables     = new CalamariVariables();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", Environment.GetEnvironmentVariable("AWS_Calamari_Access"));
            variables.Set("AWSAccount.SecretKey", Environment.GetEnvironmentVariable("AWS_Calamari_Secret"));
            variables.Set("Octopus.Action.Aws.Region", RegionEndpoint.APSoutheast1.SystemName);
            variables.Set(AwsSpecialVariables.S3.FileSelections,
                          JsonConvert.SerializeObject(fileSelections, GetEnrichedSerializerSettings()));
            variables.Save(variablesFile);

            var packageDirectory = TestEnvironment.GetTestPath("AWS", "S3", packageName);

            using (var package =
                       new TemporaryFile(PackageBuilder.BuildSimpleZip(packageName, "1.0.0", packageDirectory)))
                using (new TemporaryFile(variablesFile))
                {
                    var log        = new InMemoryLog();
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
                    var command    = new UploadAwsS3Command(
                        log,
                        variables,
                        fileSystem,
                        new SubstituteInFiles(fileSystem, new FileSubstituter(log, fileSystem), variables),
                        new ExtractPackage(new CombinedPackageExtractor(log), fileSystem, variables, log)
                        );
                    var result = command.Execute(new[] {
                        "--package", $"{package.FilePath}",
                        "--variables", $"{variablesFile}",
                        "--bucket", BucketName,
                        "--targetMode", S3TargetMode.FileSelections.ToString()
                    });

                    result.Should().Be(0);
                }

            return(bucketKeyPrefix);
        }
示例#19
0
        private void SubstituteVariablesInScript(string scriptFileName, CalamariVariableDictionary variables)
        {
            if (!File.Exists(scriptFileName))
            {
                throw new CommandException("Could not find script file: " + scriptFileName);
            }

            var substituter = new FileSubstituter(CalamariPhysicalFileSystem.GetPhysicalFileSystem());

            substituter.PerformSubstitution(scriptFileName, variables);
        }
        public void PurgeCanExcludeFile()
        {
            var importantFile = CreateFile("ImportantFile.txt");
            var purgableFile  = CreateFile("WhoCaresFile.txt");

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            fileSystem.PurgeDirectory(PurgeTestDirectory, (fsi) => fsi.Name.StartsWith("Important"), FailureOptions.IgnoreFailure);

            Assert.IsTrue(File.Exists(importantFile), $"Expected file `{importantFile}` to be preserved.");
            Assert.IsFalse(File.Exists(purgableFile), $"Expected file `{purgableFile}` to be removed.");
        }
        public void PurgeWithNoExcludeRemovesAll()
        {
            CreateFile("ImportantFile.txt");
            CreateFile("MyDirectory", "SubDirectory", "WhoCaresFile.txt");
            CollectionAssert.IsNotEmpty(Directory.EnumerateFileSystemEntries(PurgeTestDirectory).ToList(), "Expected all files to have been set up");

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            fileSystem.PurgeDirectory(PurgeTestDirectory, FailureOptions.IgnoreFailure);

            CollectionAssert.IsEmpty(Directory.EnumerateFileSystemEntries(PurgeTestDirectory).ToList(), "Expected all items to be removed");
        }
        public void SetUp()
        {
            if (Directory.Exists(PurgeTestDirectory))
            {
                Directory.Delete(PurgeTestDirectory, true);
            }

            fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            rootPath   = Path.GetTempFileName();
            File.Delete(rootPath);
            Directory.CreateDirectory(rootPath);
        }
示例#23
0
        public void SetUp()
        {
            tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            firstSensitiveVariablesFileName   = Path.Combine(tempDirectory, "firstVariableSet.secret");
            secondSensitiveVariablesFileName  = Path.Combine(tempDirectory, "secondVariableSet.secret");
            firstInsensitiveVariablesFileName = Path.ChangeExtension(firstSensitiveVariablesFileName, "json");
            fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            fileSystem.EnsureDirectoryExists(tempDirectory);

            CreateSensitiveVariableFile();
            CreateInSensitiveVariableFile();
        }
        protected virtual void ConfigureContainer(ContainerBuilder builder, CommonOptions options)
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            builder.RegisterInstance(fileSystem).As <ICalamariFileSystem>();
            builder.RegisterType <VariablesFactory>().AsSelf();
            builder.Register(c => c.Resolve <VariablesFactory>().Create(options)).As <IVariables>().SingleInstance();
            builder.RegisterType <ScriptEngine>().As <IScriptEngine>();
            builder.RegisterType <VariableLogger>().AsSelf();
            builder.RegisterInstance(log).As <ILog>().SingleInstance();
            builder.RegisterType <FreeSpaceChecker>().As <IFreeSpaceChecker>().SingleInstance();
            builder.RegisterType <CommandLineRunner>().As <ICommandLineRunner>().SingleInstance();
            builder.RegisterType <FileSubstituter>().As <IFileSubstituter>();
            builder.RegisterType <SubstituteInFiles>().As <ISubstituteInFiles>();
            builder.RegisterType <CombinedPackageExtractor>().As <ICombinedPackageExtractor>();
            builder.RegisterType <ExtractPackage>().As <IExtractPackage>();
            builder.RegisterType <AssemblyEmbeddedResources>().As <ICalamariEmbeddedResources>();
            builder.RegisterType <ConfigurationVariablesReplacer>().As <IConfigurationVariablesReplacer>();
            builder.RegisterType <TransformFileLocator>().As <ITransformFileLocator>();
            builder.RegisterType <JsonFormatVariableReplacer>().As <IFileFormatVariableReplacer>();
            builder.RegisterType <YamlFormatVariableReplacer>().As <IFileFormatVariableReplacer>();
            builder.RegisterType <StructuredConfigVariablesService>().As <IStructuredConfigVariablesService>();
            builder.Register(context => ConfigurationTransformer.FromVariables(context.Resolve <IVariables>(), context.Resolve <ILog>())).As <IConfigurationTransformer>();
            builder.RegisterType <DeploymentJournalWriter>().As <IDeploymentJournalWriter>().SingleInstance();
            builder.RegisterType <CodeGenFunctionsRegistry>().SingleInstance();

            var assemblies = GetAllAssembliesToRegister().ToArray();

            builder.RegisterAssemblyTypes(assemblies).AssignableTo <ICodeGenFunctions>().As <ICodeGenFunctions>().SingleInstance();

            builder.RegisterAssemblyTypes(assemblies)
            .AssignableTo <IScriptWrapper>()
            .Except <TerminalScriptWrapper>()
            .As <IScriptWrapper>()
            .SingleInstance();

            builder.RegisterAssemblyTypes(assemblies)
            .AssignableTo <IBehaviour>()
            .AsSelf()
            .InstancePerDependency();

            builder.RegisterAssemblyTypes(assemblies)
            .AssignableTo <ICommandAsync>()
            .Where(t => t.GetCustomAttribute <CommandAttribute>().Name
                   .Equals(options.Command, StringComparison.OrdinalIgnoreCase))
            .Named <ICommandAsync>(t => t.GetCustomAttribute <CommandAttribute>().Name);

            builder.RegisterAssemblyTypes(assemblies)
            .AssignableTo <PipelineCommand>()
            .Where(t => t.GetCustomAttribute <CommandAttribute>().Name
                   .Equals(options.Command, StringComparison.OrdinalIgnoreCase))
            .Named <PipelineCommand>(t => t.GetCustomAttribute <CommandAttribute>().Name);
        }
        public CalamariVariableDictionary(string storageFilePath, string sensitiveFilePath, string sensitiveFilePassword, string outputVariablesFilePath = null, string outputVariablesFilePassword = null)
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            if (!string.IsNullOrEmpty(storageFilePath))
            {
                if (!fileSystem.FileExists(storageFilePath))
                {
                    throw new CommandException("Could not find variables file: " + storageFilePath);
                }

                var nonSensitiveVariables = new VariableDictionary(storageFilePath);
                nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name)));
            }

            if (!string.IsNullOrEmpty(sensitiveFilePath))
            {
                var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword)
                    ? fileSystem.ReadFile(sensitiveFilePath)
                    : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword);

                try
                {
                    var sensitiveVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables);
                    foreach (var variable in sensitiveVariables)
                    {
                        SetSensitive(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException)
                {
                    throw new CommandException("Unable to parse sensitive-variables as valid JSON.");
                }
            }

            if (!string.IsNullOrEmpty(outputVariablesFilePath))
            {
                var rawVariables = DecryptWithMachineKey(fileSystem.ReadFile(outputVariablesFilePath), outputVariablesFilePassword);
                try
                {
                    var outputVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables);
                    foreach (var variable in outputVariables)
                    {
                        Set(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException e)
                {
                    throw new CommandException("Unable to parse output variables as valid JSON.");
                }
            }
        }
示例#26
0
        public void SetUp()
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

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


            Environment.SetEnvironmentVariable("TentacleJournal", Path.Combine(StagingDirectory, "DeploymentJournal.xml"));
        }
示例#27
0
        private void SubstituteVariablesInScript(CalamariVariableDictionary variables)
        {
            if (!substituteVariables)
            {
                return;
            }

            Log.Info("Substituting variables in: " + scriptFile);

            var validatedScriptFilePath = AssertScriptFileExists();
            var substituter             = new FileSubstituter(CalamariPhysicalFileSystem.GetPhysicalFileSystem());

            substituter.PerformSubstitution(validatedScriptFilePath, variables);
        }
示例#28
0
        public void SetUp()
        {
            fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            configurationTransformer = Substitute.For <IConfigurationTransformer>();
            transformFileLocator     = new TransformFileLocator(fileSystem);

            var deployDirectory = BuildConfigPath(null);

            variables = new CalamariVariables();
            variables.Set(SpecialVariables.Package.EnabledFeatures, SpecialVariables.Features.ConfigurationTransforms);
            variables.Set(KnownVariables.OriginalPackageDirectoryPath, deployDirectory);

            deployment = new RunningDeployment(deployDirectory, variables);
            logs       = new InMemoryLog();
        }
示例#29
0
        public void IgnoresInvalidFiles()
        {
            using (new TemporaryFile(CreatePackage("1.0.0.1")))
                using (new TemporaryFile(CreateEmptyFile("1.0.0.2")))
                {
                    var store = new PackageStore(
                        CreatePackageExtractor(),
                        CalamariPhysicalFileSystem.GetPhysicalFileSystem()
                        );

                    var packages = store.GetNearestPackages("Acme.Web", new SemanticVersion("1.1.1.1"));

                    CollectionAssert.AreEquivalent(new[] { "1.0.0.1" }, packages.Select(c => c.Version.ToString()));
                }
        }
示例#30
0
        public void OldFormatsAreIgnored()
        {
            using (new TemporaryFile(CreatePackage("0.5.0.1")))
                using (new TemporaryFile(CreatePackage("1.0.0.1", true)))
                {
                    var store = new PackageStore(
                        CreatePackageExtractor(),
                        CalamariPhysicalFileSystem.GetPhysicalFileSystem()
                        );

                    var packages = store.GetNearestPackages("Acme.Web", new SemanticVersion(1, 1, 1, 1));

                    CollectionAssert.AreEquivalent(new[] { "0.5.0.1" }, packages.Select(c => c.Version.ToString()));
                }
        }