Esempio n. 1
0
        public void CreateJar(string contentsDirectory, string targetJarPath)
        {
            try
            {
                /*
                 *   The precondition script will set the OctopusEnvironment_Java_Bin environment variable based
                 *   on where it found the java executable based on the JAVA_HOME environment
                 *   variable. If OctopusEnvironment_Java_Bin is empty or null, it means that the precondition
                 *   found java on the path.
                 */
                var javaBin          = Environment.GetEnvironmentVariable(SpecialVariables.Action.Java.JavaBinEnvVar) ?? "";
                var createJarCommand = new CommandLineInvocation(
                    Path.Combine(javaBin, "java"),
                    $"-cp \"{toolsPath}\" sun.tools.jar.Main cvf \"{targetJarPath}\" -C \"{contentsDirectory}\" .",
                    contentsDirectory);

                Log.Verbose($"Invoking '{createJarCommand}' to create '{targetJarPath}'");

                /*
                 *   All extraction messages should be verbose
                 */
                commandOutput.WriteInfo("##octopus[stdout-verbose]");

                var result = commandLineRunner.Execute(createJarCommand);
                result.VerifySuccess();
            }
            finally
            {
                commandOutput.WriteInfo("##octopus[stdout-default]");
            }
        }
Esempio n. 2
0
        public CommandResult Execute(
            Script script,
            CalamariVariableDictionary variables,
            ICommandLineRunner commandLineRunner,
            Dictionary <string, string> environmentVars = null)
        {
            var executable       = PythonBootstrapper.FindPythonExecutable();
            var workingDirectory = Path.GetDirectoryName(script.File);

            var dependencyInstallerFile      = PythonBootstrapper.PrepareDependencyInstaller(workingDirectory);
            var dependencyInstallerArguments = PythonBootstrapper.FormatCommandArguments(dependencyInstallerFile, string.Empty);

            using (new TemporaryFile(dependencyInstallerFile))
            {
                var result = commandLineRunner.Execute(new CommandLineInvocation(executable, dependencyInstallerArguments,
                                                                                 workingDirectory,
                                                                                 environmentVars));

                if (result.ExitCode != 0)
                {
                    return(result);
                }
            }

            var configurationFile = PythonBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
            var bootstrapFile     = PythonBootstrapper.PrepareBootstrapFile(script, workingDirectory, configurationFile);
            var arguments         = PythonBootstrapper.FormatCommandArguments(bootstrapFile, script.Parameters);

            using (new TemporaryFile(configurationFile))
                using (new TemporaryFile(bootstrapFile))
                {
                    return(commandLineRunner.Execute(new CommandLineInvocation(executable, arguments, workingDirectory,
                                                                               environmentVars)));
                }
        }
Esempio n. 3
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 !);
        }
Esempio n. 4
0
        CommandResult ExecuteCommandInternal(string[] arguments, out string result, bool outputToCalamariConsole)
        {
            var environmentVar = defaultEnvironmentVariables;

            if (environmentVariables != null)
            {
                environmentVar.AddRange(environmentVariables);
            }

            var terraformExecutable = variables.Get(TerraformSpecialVariables.Action.Terraform.CustomTerraformExecutable) ??
                                      $"terraform{(CalamariEnvironment.IsRunningOnWindows ? ".exe" : String.Empty)}";
            var captureOutput         = new CaptureInvocationOutputSink();
            var commandLineInvocation = new CommandLineInvocation(terraformExecutable, arguments)
            {
                WorkingDirectory = templateDirectory,
                EnvironmentVars  = environmentVar,
                OutputToLog      = outputToCalamariConsole,
                AdditionalInvocationOutputSink = captureOutput
            };

            log.Info(commandLineInvocation.ToString());

            var commandResult = commandLineRunner.Execute(commandLineInvocation);

            result = String.Join("\n", captureOutput.Infos);

            return(commandResult);
        }
        public CommandResult Execute(
            Script script,
            CalamariVariableDictionary variables,
            ICommandLineRunner commandLineRunner,
            StringDictionary environmentVars = null)
        {
            var workingDirectory = Path.GetDirectoryName(script.File);

            var executable             = PowerShellBootstrapper.PathToPowerShellExecutable();
            var bootstrapFile          = PowerShellBootstrapper.PrepareBootstrapFile(script, variables);
            var debuggingBootstrapFile = PowerShellBootstrapper.PrepareDebuggingBootstrapFile(script);
            var arguments = PowerShellBootstrapper.FormatCommandArguments(bootstrapFile, debuggingBootstrapFile, variables);

            var userName = variables.Get(SpecialVariables.Action.PowerShell.UserName);
            var password = ToSecureString(variables.Get(SpecialVariables.Action.PowerShell.Password));

            using (new TemporaryFile(bootstrapFile))
            {
                using (new TemporaryFile(debuggingBootstrapFile))
                {
                    var invocation = new CommandLineInvocation(
                        executable,
                        arguments,
                        workingDirectory,
                        environmentVars,
                        userName,
                        password);
                    return(commandLineRunner.Execute(invocation));
                }
            }
        }
Esempio n. 6
0
        public void CreateJar(string contentsDirectory, string targetJarPath)
        {
            var manifestPath = Path.Combine(contentsDirectory, "META-INF", "MANIFEST.MF");
            var args         = File.Exists(manifestPath)
                ? $"-cp \"{toolsPath}\" sun.tools.jar.Main cvmf \"{manifestPath}\" \"{targetJarPath}\" -C \"{contentsDirectory}\" ."
                : $"-cp \"{toolsPath}\" sun.tools.jar.Main cvf \"{targetJarPath}\" -C \"{contentsDirectory}\" .";

            var createJarCommand = new CommandLineInvocation(JavaRuntime.CmdPath, args)
            {
                WorkingDirectory = contentsDirectory,
                OutputAsVerbose  = true
            };

            log.Verbose($"Invoking '{createJarCommand}' to create '{targetJarPath}'");

            var result = commandLineRunner.Execute(createJarCommand);

            result.VerifySuccess();
        }
Esempio n. 7
0
        /// <summary>
        /// Execute java running the Octopus Deploy Java library
        /// </summary>
        public void Run(string mainClass, Dictionary <string, string> environmentVariables)
        {
            var javaLib = variables.Get(SpecialVariables.Action.Java.JavaLibraryEnvVar, "");
            var result  = commandLineRunner.Execute(new CommandLineInvocation(
                                                        JavaRuntime.CmdPath,
                                                        $"-Djdk.logger.finder.error=QUIET -cp calamari.jar {mainClass}",
                                                        Path.Combine(javaLib, "contentFiles", "any", "any"),
                                                        environmentVariables));

            result.VerifySuccess();
        }
Esempio n. 8
0
        public CommandResult Execute(string scriptFile, VariableDictionary variables, ICommandLineRunner commandLineRunner)
        {
            var workingDirectory = Path.GetDirectoryName(scriptFile);

            var configurationFile = ScriptCSBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
            var boostrapFile      = ScriptCSBootstrapper.PrepareBootstrapFile(scriptFile, configurationFile, workingDirectory);

            using (new TemporaryFile(configurationFile))
                using (new TemporaryFile(boostrapFile))
                {
                    return(commandLineRunner.Execute(new CommandLineInvocation(ScriptCSBootstrapper.FindScriptCSExecutable(), ScriptCSBootstrapper.FormatCommandArguments(boostrapFile), workingDirectory)));
                }
        }
Esempio n. 9
0
        public CommandResult Execute(string scriptFile, VariableDictionary variables, ICommandLineRunner commandLineRunner)
        {
            var workingDirectory = Path.GetDirectoryName(scriptFile);

            var configurationFile = ScriptCSBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
            var boostrapFile = ScriptCSBootstrapper.PrepareBootstrapFile(scriptFile, configurationFile, workingDirectory);

            using (new TemporaryFile(configurationFile))
            using (new TemporaryFile(boostrapFile))
            {
                return commandLineRunner.Execute(new CommandLineInvocation(ScriptCSBootstrapper.FindScriptCSExecutable(), ScriptCSBootstrapper.FormatCommandArguments(boostrapFile), workingDirectory));
            }
        }
Esempio n. 10
0
        public CommandResult Execute(string scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner)
        {
            var workingDirectory = Path.GetDirectoryName(scriptFile);

            var executable = PowerShellBootstrapper.PathToPowerShellExecutable();
            var boostrapFile = PowerShellBootstrapper.PrepareBootstrapFile(scriptFile, variables);
            var arguments = PowerShellBootstrapper.FormatCommandArguments(boostrapFile);

            using (new TemporaryFile(boostrapFile))
            {
                var invocation = new CommandLineInvocation(executable, arguments, workingDirectory);
                return commandLineRunner.Execute(invocation);
            }
        }
Esempio n. 11
0
        public CommandResult Execute(string scriptFile, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner)
        {
            var workingDirectory = Path.GetDirectoryName(scriptFile);

            var executable   = PowerShellBootstrapper.PathToPowerShellExecutable();
            var boostrapFile = PowerShellBootstrapper.PrepareBootstrapFile(scriptFile, variables);
            var arguments    = PowerShellBootstrapper.FormatCommandArguments(boostrapFile);

            using (new TemporaryFile(boostrapFile))
            {
                var invocation = new CommandLineInvocation(executable, arguments, workingDirectory);
                return(commandLineRunner.Execute(invocation));
            }
        }
Esempio n. 12
0
        public void CreateJar(string contentsDirectory, string targetJarPath)
        {
            try
            {
                var manifestPath = Path.Combine(contentsDirectory, "META-INF", "MANIFEST.MF");
                var args         = File.Exists(manifestPath)
                    ? $"-cp \"{toolsPath}\" sun.tools.jar.Main cvmf \"{manifestPath}\" \"{targetJarPath}\" -C \"{contentsDirectory}\" ."
                    : $"-cp \"{toolsPath}\" sun.tools.jar.Main cvf \"{targetJarPath}\" -C \"{contentsDirectory}\" .";

                var createJarCommand = new CommandLineInvocation(JavaRuntime.CmdPath, args, contentsDirectory);
                Log.Verbose($"Invoking '{createJarCommand}' to create '{targetJarPath}'");

                /*
                 *   All extraction messages should be verbose
                 */
                commandOutput.WriteInfo("##octopus[stdout-verbose]");
                var result = commandLineRunner.Execute(createJarCommand);
                result.VerifySuccess();
            }
            finally
            {
                commandOutput.WriteInfo("##octopus[stdout-default]");
            }
        }
Esempio n. 13
0
        public CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner)
        {
            var workingDirectory = Path.GetDirectoryName(script.File);

            var executable        = FSharpBootstrapper.FindExecutable();
            var configurationFile = FSharpBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
            var boostrapFile      = FSharpBootstrapper.PrepareBootstrapFile(script.File, configurationFile, workingDirectory);
            var arguments         = FSharpBootstrapper.FormatCommandArguments(boostrapFile, script.Parameters);

            using (new TemporaryFile(configurationFile))
                using (new TemporaryFile(boostrapFile))
                {
                    return(commandLineRunner.Execute(new CommandLineInvocation(executable, arguments, workingDirectory)));
                }
        }
Esempio n. 14
0
        public CommandResult Execute(
            Script script,
            CalamariVariableDictionary variables,
            ICommandLineRunner commandLineRunner,
            StringDictionary environmentVars = null)
        {
            var workingDirectory  = Path.GetDirectoryName(script.File);
            var configurationFile = BashScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
            var boostrapFile      = BashScriptBootstrapper.PrepareBootstrapFile(script, configurationFile, workingDirectory);

            using (new TemporaryFile(configurationFile))
                using (new TemporaryFile(boostrapFile))
                {
                    return(commandLineRunner.Execute(new CommandLineInvocation(
                                                         BashScriptBootstrapper.FindBashExecutable(),
                                                         BashScriptBootstrapper.FormatCommandArguments(boostrapFile), workingDirectory, environmentVars)));
                }
        }
        /// <summary>
        /// Execute java running the Octopus Deploy Java library
        /// </summary>
        protected void runJava(string mainClass)
        {
            /*
             *  The precondition script will set the OctopusEnvironment_Java_Bin environment variable based
             *  on where it found the java executable based on the JAVA_HOME environment
             *  variable. If OctopusEnvironment_Java_Bin is empty or null, it means that the precondition
             *  found java on the path.
             */
            var javaBin = Environment.GetEnvironmentVariable(SpecialVariables.Action.Java.JavaBinEnvVar) ?? "";

            /*
             *  The precondition script will also set the location of the calamari.jar file
             */
            var javaLib = Environment.GetEnvironmentVariable(SpecialVariables.Action.Java.JavaLibraryEnvVar) ?? "";
            var result  = commandLineRunner.Execute(new CommandLineInvocation(
                                                        Path.Combine(javaBin, "java"),
                                                        $"-cp calamari.jar {mainClass}",
                                                        Path.Combine(javaLib, "contentFiles", "any", "any")));

            result.VerifySuccess();
        }
Esempio n. 16
0
        protected override int ExecuteInternal(NodeInstructions instructions)
        {
            using (var variableFile = new TemporaryFile(Path.GetTempFileName()))
            {
                var jsonInputs = variables.GetRaw(instructions.InputsVariable) ?? string.Empty;
                variables.Set(instructions.InputsVariable, InputSubstitution.SubstituteAndEscapeAllVariablesInJson(jsonInputs, variables, log));
                var variablesAsJson = variables.CloneAndEvaluate().SaveAsString();
                File.WriteAllBytes(variableFile.FilePath, new AesEncryption(options.InputVariables.SensitiveVariablesPassword).Encrypt(variablesAsJson));
                var pathToNode            = variables.Get(instructions.NodePathVariable);
                var nodeExecutablePath    = BuildNodePath(pathToNode);
                var parameters            = BuildParams(instructions, variableFile.FilePath);
                var runningDeployment     = new RunningDeployment(variables);
                var commandLineInvocation = new CommandLineInvocation(nodeExecutablePath, parameters)
                {
                    WorkingDirectory = runningDeployment.CurrentDirectory,
                    OutputToLog      = true,
                    EnvironmentVars  = ProxyEnvironmentVariablesGenerator.GenerateProxyEnvironmentVariables().ToDictionary(e => e.Key, e => e.Value)
                };

                var commandResult = commandLineRunner.Execute(commandLineInvocation);
                return(commandResult.ExitCode);
            }
        }
Esempio n. 17
0
        protected override int ExecuteInternal(NodeInstructions instructions, params string[] args)
        {
            var pathToNode         = variables.Get(instructions.NodePathVariable);
            var pathToStepPackage  = variables.Get(instructions.TargetPathVariable);
            var pathToBootstrapper = variables.Get(instructions.BootstrapperPathVariable);
            var runningDeployment  = new RunningDeployment(variables);

            using (var variableFile = new TemporaryFile(Path.GetTempFileName()))
            {
                var variablesAsJson = variables.CloneAndEvaluate().SaveAsString();
                File.WriteAllBytes(variableFile.FilePath, new AesEncryption(options.InputVariables.SensitiveVariablesPassword).Encrypt(variablesAsJson));
                var commandLineInvocation = new CommandLineInvocation(BuildNodePath(pathToNode),
                                                                      BuildArgs(Path.Combine(pathToBootstrapper, "bootstrapper.js"), Path.Combine(pathToStepPackage, instructions.TargetEntryPoint), variableFile.FilePath, options.InputVariables.SensitiveVariablesPassword))
                {
                    WorkingDirectory = runningDeployment.CurrentDirectory,
                    OutputToLog      = true,
                };

                var commandResult = commandLineRunner.Execute(commandLineInvocation);

                return(commandResult.ExitCode);
            }
        }
Esempio n. 18
0
        public CommandResult Execute(Script script, CalamariVariableDictionary variables, ICommandLineRunner commandLineRunner,
                                     StringDictionary environmentVars = null)
        {
            var prepared = PrepareExecution(script, variables, environmentVars);

            if (variables.IsSet(SpecialVariables.CopyWorkingDirectoryIncludingKeyTo))
            {
                CopyWorkingDirectory(variables, prepared.CommandLineInvocation.WorkingDirectory,
                                     prepared.CommandLineInvocation.Arguments);
            }

            try
            {
                return(commandLineRunner.Execute(prepared.CommandLineInvocation));
            }
            finally
            {
                foreach (var temporaryFile in prepared.TemporaryFiles)
                {
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
                    fileSystem.DeleteFile(temporaryFile, FailureOptions.IgnoreFailure);
                }
            }
        }
Esempio n. 19
0
        public CommandResult Execute()
        {
            var result = commandLineRunner.Execute(OctoDiff.Build());

            return(result);
        }