예제 #1
0
        public void VariablesInAdditionalVariablesPathAreContributed()
        {
            try
            {
                using (var varFile = new TemporaryFile(Path.GetTempFileName()))
                {
                    new CalamariVariables
                    {
                        {
                            "new.key", "new.value"
                        }
                    }.Save(varFile.FilePath);

                    Environment.SetEnvironmentVariable(VariablesFactory.AdditionalVariablesPathVariable, varFile.FilePath);

                    var variables = new VariablesFactory(CalamariPhysicalFileSystem.GetPhysicalFileSystem())
                                    .Create(new CommonOptions("test"));

                    variables.Get("new.key").Should().Be("new.value");
                }
            }
            finally
            {
                Environment.SetEnvironmentVariable(VariablesFactory.AdditionalVariablesPathVariable, null);
            }
        }
예제 #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(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 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();

            options = CommonOptions.Parse(new[]
            {
                "Test",
                "--variables",
                firstInsensitiveVariablesFileName,
                "--sensitiveVariables",
                firstSensitiveVariablesFileName,
                "--sensitiveVariables",
                secondSensitiveVariablesFileName,
                "--sensitiveVariablesPassword",
                encryptionPassword
            });
        }
예제 #4
0
        PowerShellBootstrapper GetPowerShellBootstrapper(IVariables variables)
        {
            if (CalamariEnvironment.IsRunningOnNix || CalamariEnvironment.IsRunningOnMac)
            {
                return(new UnixLikePowerShellCoreBootstrapper());
            }

            var specifiedEdition = variables[PowerShellVariables.Edition];

            if (string.IsNullOrEmpty(specifiedEdition))
            {
                return(new WindowsPowerShellBootstrapper());
            }

            if (specifiedEdition.Equals("Core", StringComparison.OrdinalIgnoreCase))
            {
                return(new WindowsPowerShellCoreBootstrapper(CalamariPhysicalFileSystem.GetPhysicalFileSystem()));
            }

            if (specifiedEdition.Equals("Desktop", StringComparison.OrdinalIgnoreCase))
            {
                return(new WindowsPowerShellBootstrapper());
            }

            throw new PowerShellEditionNotFoundException(specifiedEdition);
        }
        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 <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 <ICommand>()
            .Where(t => ((CommandAttribute)Attribute.GetCustomAttribute(t, typeof(CommandAttribute))).Name
                   .Equals(options.Command, StringComparison.OrdinalIgnoreCase))
            .Named <ICommand>(t => ((CommandAttribute)Attribute.GetCustomAttribute(t, typeof(CommandAttribute))).Name);
        }
예제 #6
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();
        }
예제 #7
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();
 }
예제 #8
0
        public void Execute(RunningDeployment deployment)
        {
            var variables = deployment.Variables;

            var(rootLocation, additionalLocations) = GetLocations(variables);
            if (rootLocation == null)
            {
                throw new NginxMissingRootLocationException();
            }

            var enabledBindings    = GetEnabledBindings(variables).ToList();
            var sslCertificates    = GetSslCertificates(enabledBindings, variables);
            var customNginxSslRoot = variables.Get(SpecialVariables.Action.Nginx.SslRoot);

            nginxServer
            .WithVirtualServerName(variables.Get(SpecialVariables.Package.NuGetPackageId))
            .WithHostName(variables.Get(SpecialVariables.Action.Nginx.Server.HostName))
            .WithServerBindings(enabledBindings, sslCertificates, customNginxSslRoot)
            .WithRootLocation(rootLocation)
            .WithAdditionalLocations(additionalLocations);

            Log.Verbose("Building nginx configuration");
            var customNginxConfRoot = variables.Get(SpecialVariables.Action.Nginx.ConfigRoot);

            nginxServer.BuildConfiguration(customNginxConfRoot);

            Log.Verbose("Saving nginx configuration");
            var tempDirectory = CalamariPhysicalFileSystem.GetPhysicalFileSystem().CreateTemporaryDirectory();

            variables.Set("OctopusNginxFeatureTempDirectory", tempDirectory);
            nginxServer.SaveConfiguration(tempDirectory);
        }
예제 #9
0
        public CalamariVariableDictionary(string storageFilePath, List <string> sensitiveFilePaths, 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 (sensitiveFilePaths.Any())
            {
                foreach (var sensitiveFilePath in sensitiveFilePaths)
                {
                    if (string.IsNullOrEmpty(sensitiveFilePath))
                    {
                        continue;
                    }

                    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)
                {
                    throw new CommandException("Unable to parse output variables as valid JSON.");
                }
            }
        }
예제 #10
0
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);

            variables.EnrichWithEnvironmentVariables();
            variables.LogVariables();

            var filesystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            var semaphore  = SemaphoreFactory.Get();

            journal    = new DeploymentJournal(filesystem, semaphore, variables);
            deployment = new RunningDeployment(packageFile, (CalamariVariableDictionary)variables);

            ValidateArguments();

            var result = ExecuteScriptFromPackage() ??
                         ExecuteScriptFromVariables() ??
                         ExecuteScriptFromParameters();

            if (result.HasValue)
            {
                return(result.Value);
            }

            throw new CommandException("No script details provided.\r\n" +
                                       $"Pleave provide the script either via the `{SpecialVariables.Action.Script.ScriptBody}` variable, " +
                                       "through a package provided via the `--package` argument or directly via the `--script` argument.");
        }
예제 #11
0
        public override int Execute(string[] commandLineArguments)
        {
            Options.Parse(commandLineArguments);
            JavaRuntime.VerifyExists();

            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
            var variables  =
                new CalamariVariableDictionary(variablesFile, sensitiveVariableFiles, sensitiveVariablesPassword);

            var commandOutput =
                new SplitCommandOutput(new ConsoleCommandOutput(), new ServiceMessageCommandOutput(variables));
            var commandLineRunner = new CommandLineRunner(commandOutput);
            var embeddedResources = new AssemblyEmbeddedResources();

            var conventions = new List <IConvention>
            {
                new ContributeEnvironmentVariablesConvention(),
                new JavaStepConvention(actionType, new JavaRunner(commandLineRunner, variables)),
                new FeatureRollbackConvention(DeploymentStages.DeployFailed, fileSystem, scriptEngine,
                                              commandLineRunner, embeddedResources)
            };

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

            conventionRunner.RunConventions();

            return(0);
        }
예제 #12
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, new SystemSemaphore(), variables);
            var clock             = new SystemClock();

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

            retentionPolicy.ApplyRetentionPolicy(retentionPolicySet, days, releases);

            return(0);
        }
예제 #13
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);
        }
예제 #14
0
 public LockFileBasedSemaphore(string name, TimeSpan lockTimeout, ILog log)
     : this(name,
            lockTimeout,
            new LockIo(CalamariPhysicalFileSystem.GetPhysicalFileSystem()),
            new ProcessFinder(),
            log)
 {
 }
예제 #15
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);
        }
예제 #16
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");
        }
예제 #17
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);
        }
예제 #18
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);
        }
        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);
        }
        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);
        }
예제 #21
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 !);
        }
예제 #22
0
 private string GetRelativePath(CallInfo callInfo, CalamariPhysicalFileSystem realFileSystem)
 {
     //nsubstitute calls the first return when you setup a second one. odd.
     if (callInfo.ArgAt <string>(0) == null)
     {
         return(null);
     }
     return(realFileSystem.GetRelativePath(callInfo.ArgAt <string>(0), callInfo.ArgAt <string>(1)));
 }
예제 #23
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);
        }
예제 #24
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);
        }
        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));
        }
예제 #26
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));
        }
예제 #27
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);
        }
예제 #28
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");
        }