/// <summary> /// Extracts the contents of the JAR's manifest file /// </summary> public string ExtractManifest(string jarPath) { const string manifestJarPath = "META-INF/MANIFEST.MF"; var tempDirectory = fileSystem.CreateTemporaryDirectory(); var extractJarCommand = new CommandLineInvocation("java", $"-cp \"{toolsPath}\" sun.tools.jar.Main xf \"{jarPath}\" \"{manifestJarPath}\"", tempDirectory); try { Log.Verbose($"Invoking '{extractJarCommand}' to extract '{manifestJarPath}'"); var result = commandLineRunner.Execute(extractJarCommand); result.VerifySuccess(); // Ensure our slashes point in the correct direction var extractedManifestPathComponents = new List <string> { tempDirectory }; extractedManifestPathComponents.AddRange(manifestJarPath.Split('/')); return(File.ReadAllText(Path.Combine(extractedManifestPathComponents.ToArray()))); } catch (Exception ex) { throw new Exception($"Error invoking '{extractJarCommand}'", ex); } finally { fileSystem.DeleteDirectory(tempDirectory, FailureOptions.IgnoreFailure); } }
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]"); } }
public void Execute_UsesCorrectCommandLineInvocation_ForValidTargetDiscoveryCommands(string discoveryCommand) { // Arrange var instructions = BuildInstructions(discoveryCommand); var variables = BuildVariables(); var options = BuildOptions(); var commandLineRunner = Substitute.For <ICommandLineRunner>(); var sut = new NodeExecutor(options, variables, commandLineRunner, new InMemoryLog()); CommandLineInvocation capturedInvocation = null; var fakeResult = new CommandResult("fakeCommand", 0); commandLineRunner.Execute(Arg.Do <CommandLineInvocation>(arg => capturedInvocation = arg)).Returns(fakeResult); // Act sut.Execute(instructions); // Assert var arguments = capturedInvocation.Arguments.Split(' '); arguments.Should().HaveCount(7) .And.HaveElementAt(0, $"\"{Path.Combine("expectedBootstrapperPath","bootstrapper.js")}\"") .And.HaveElementAt(1, $"\"{discoveryCommand}\"") .And.HaveElementAt(2, "\"expectedTargetPath\"") .And.HaveElementAt(4, "\"encryptionPassword\"") .And.HaveElementAt(5, "\"Octopuss\"") .And.HaveElementAt(6, "\"discoveryContextKey\""); }
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)); } } }
protected override IEnumerable <ScriptExecution> PrepareExecution(Script script, IVariables variables, Dictionary <string, string> environmentVars = null) { var powerShellBootstrapper = GetPowerShellBootstrapper(variables); var(bootstrapFile, otherTemporaryFiles) = powerShellBootstrapper.PrepareBootstrapFile(script, variables); var debuggingBootstrapFile = powerShellBootstrapper.PrepareDebuggingBootstrapFile(script); var executable = powerShellBootstrapper.PathToPowerShellExecutable(variables); var arguments = powerShellBootstrapper.FormatCommandArguments(bootstrapFile, debuggingBootstrapFile, variables); var invocation = new CommandLineInvocation(executable, arguments) { EnvironmentVars = environmentVars, WorkingDirectory = Path.GetDirectoryName(script.File), UserName = powerShellBootstrapper.AllowImpersonation() ? variables.Get(PowerShellVariables.UserName) : null, Password = powerShellBootstrapper.AllowImpersonation() ? ToSecureString(variables.Get(PowerShellVariables.Password)) : null }; return(new[] { new ScriptExecution( invocation, otherTemporaryFiles.Concat(new[] { bootstrapFile, debuggingBootstrapFile }) ) }); }
/// <summary> /// Extracts a Java archive file (.jar, .war, .ear) to the target directory /// </summary> /// <returns>Count of files extracted</returns> public int ExtractJar(string jarPath, string targetDirectory) { try { /* * All extraction messages should be verbose */ commandOutput.WriteInfo("##octopus[stdout-verbose]"); /* * Start by verifying the archive is valid. */ commandLineRunner.Execute(new CommandLineInvocation( JavaRuntime.CmdPath, $"-cp \"{toolsPath}\" sun.tools.jar.Main tf \"{jarPath}\"", targetDirectory)).VerifySuccess(); /* * If it is valid, go ahead an extract it */ var extractJarCommand = new CommandLineInvocation( JavaRuntime.CmdPath, $"-cp \"{toolsPath}\" sun.tools.jar.Main xf \"{jarPath}\"", targetDirectory); Log.Verbose($"Invoking '{extractJarCommand}' to extract '{jarPath}'"); /* * All extraction messages should be verbose */ commandOutput.WriteInfo("##octopus[stdout-verbose]"); var result = commandLineRunner.Execute(extractJarCommand); result.VerifySuccess(); } catch (Exception ex) { commandOutput.WriteError($"Exception thrown while extracting a Java archive. {ex}"); throw ex; } finally { commandOutput.WriteInfo("##octopus[stdout-default]"); } var count = -1; try { count = Directory.EnumerateFiles(targetDirectory, "*", SearchOption.AllDirectories).Count(); } catch (Exception ex) { Log.Verbose( $"Unable to return extracted file count. Error while enumerating '{targetDirectory}':\n{ex.Message}"); } return(count); }
public CommandResult Execute(CommandLineInvocation invocation) { // We only want to output executable string. eg. ExecuteCommandAndReturnOutput("where", "kubectl.exe") if (new string[] { "kubectl", "az", "gcloud", "kubectl.exe", "az.cmd", "gcloud.cmd" }.Contains(invocation.Arguments)) { invocation.AdditionalInvocationOutputSink?.WriteInfo(Path.GetFileNameWithoutExtension(invocation.Arguments)); } return(new CommandResult(invocation.ToString(), 0)); }
/// <summary> /// Extracts a Java archive file (.jar, .war, .ear) to the target directory /// </summary> /// <returns>Count of files extracted</returns> public int ExtractJar(string jarPath, string targetDirectory) { try { /* * Start by verifying the archive is valid. */ var tfCommand = new CommandLineInvocation( JavaRuntime.CmdPath, $"-cp \"{toolsPath}\" sun.tools.jar.Main tf \"{jarPath}\"" ) { WorkingDirectory = targetDirectory, OutputAsVerbose = true }; commandLineRunner.Execute(tfCommand).VerifySuccess(); /* * If it is valid, go ahead an extract it */ var extractJarCommand = new CommandLineInvocation( JavaRuntime.CmdPath, $"-cp \"{toolsPath}\" sun.tools.jar.Main xf \"{jarPath}\"" ) { WorkingDirectory = targetDirectory, OutputAsVerbose = true }; log.Verbose($"Invoking '{extractJarCommand}' to extract '{jarPath}'"); var result = commandLineRunner.Execute(extractJarCommand); result.VerifySuccess(); } catch (Exception ex) { log.Error($"Exception thrown while extracting a Java archive. {ex}"); throw; } var count = -1; try { count = Directory.EnumerateFiles(targetDirectory, "*", SearchOption.AllDirectories).Count(); } catch (Exception ex) { log.Verbose( $"Unable to return extracted file count. Error while enumerating '{targetDirectory}':\n{ex.Message}"); } return(count); }
public static CommandLineInvocation NewCommandLineInvocation(string command, string arguments, CommandLineInvocationResult result) { var invocation = new CommandLineInvocation() { Command = command, Arguments = arguments, ReceiveOutputData = result.ReceiveOutputData, ReceiveErrorData = result.ReceiveErrorData, }; return(invocation); }
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)); } }
IEnumerable <string> ExecuteCommandAndReturnOutput(string exe, params string[] arguments) { var captureCommandOutput = new CaptureCommandOutput(); var invocation = new CommandLineInvocation(exe, arguments) { EnvironmentVars = environmentVars, WorkingDirectory = workingDirectory, OutputAsVerbose = false, OutputToLog = false, AdditionalInvocationOutputSink = captureCommandOutput }; var result = commandLineRunner.Execute(invocation); return(result.ExitCode == 0 ? captureCommandOutput.Messages.Where(m => m.Level == Level.Info).Select(m => m.Text).ToArray() : Enumerable.Empty <string>()); }
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(); }
CommandResult ExecuteCommand(CommandLineInvocation invocation) { invocation.EnvironmentVars = environmentVars; invocation.WorkingDirectory = workingDirectory; invocation.OutputAsVerbose = false; invocation.OutputToLog = false; var captureCommandOutput = new CaptureCommandOutput(); invocation.AdditionalInvocationOutputSink = captureCommandOutput; var commandString = invocation.ToString(); commandString = redactMap.Aggregate(commandString, (current, pair) => current.Replace(pair.Key, pair.Value)); log.Verbose(commandString); var result = commandLineRunner.Execute(invocation); foreach (var message in captureCommandOutput.Messages) { if (result.ExitCode == 0) { log.Verbose(message.Text); continue; } switch (message.Level) { case Level.Info: log.Verbose(message.Text); break; case Level.Error: log.Error(message.Text); break; } } return(result); }
/// <summary> /// This is a special case for when the invocation results in an error /// 1) but is to be expected as a valid scenario; and /// 2) we don't want to inform this at an error level when this happens. /// </summary> /// <param name="invocation"></param> /// <returns></returns> CommandResult ExecuteCommandWithVerboseLoggingOnly(CommandLineInvocation invocation) { invocation.EnvironmentVars = environmentVars; invocation.WorkingDirectory = workingDirectory; invocation.OutputAsVerbose = true; invocation.OutputToLog = false; var captureCommandOutput = new CaptureCommandOutput(); invocation.AdditionalInvocationOutputSink = captureCommandOutput; var commandString = invocation.ToString(); commandString = redactMap.Aggregate(commandString, (current, pair) => current.Replace(pair.Key, pair.Value)); log.Verbose(commandString); var result = commandLineRunner.Execute(invocation); captureCommandOutput.Messages.ForEach(message => log.Verbose(message.Text)); return(result); }
protected override IEnumerable <ScriptExecution> PrepareExecution(Script script, IVariables variables, Dictionary <string, string> environmentVars = null) { var workingDirectory = Path.GetDirectoryName(script.File); var configurationFile = BashScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables); var(bootstrapFile, otherTemporaryFiles) = BashScriptBootstrapper.PrepareBootstrapFile(script, configurationFile, workingDirectory, variables); var invocation = new CommandLineInvocation( BashScriptBootstrapper.FindBashExecutable(), BashScriptBootstrapper.FormatCommandArguments(Path.GetFileName(bootstrapFile)) ) { WorkingDirectory = workingDirectory, EnvironmentVars = environmentVars }; yield return(new ScriptExecution( invocation, otherTemporaryFiles.Concat(new[] { bootstrapFile, configurationFile }) )); }
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); } }
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); } }
CommandResult ExecuteCommandInternal(string arguments, out string result, ICommandOutput output = null) { var environmentVar = defaultEnvironmentVariables; if (environmentVariables != null) { environmentVar.MergeDictionaries(environmentVariables); } var commandLineInvocation = new CommandLineInvocation(TerraformExecutable, arguments, TemplateDirectory, environmentVar); var commandOutput = new CaptureOutput(output ?? new ConsoleCommandOutput()); var cmd = new CommandLineRunner(commandOutput); Log.Info(commandLineInvocation.ToString()); var commandResult = cmd.Execute(commandLineInvocation); result = String.Join("\n", commandOutput.Infos); return(commandResult); }
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]"); } }
public ScriptExecution(CommandLineInvocation commandLineInvocation, IEnumerable <string> temporaryFiles) { CommandLineInvocation = commandLineInvocation; TemporaryFiles = temporaryFiles; }
/// <summary> /// Extracts a Java archive file (.jar, .war, .ear) to the target directory /// </summary> /// <returns>Count of files extracted</returns> public int ExtractJar(string jarPath, string targetDirectory) { /* * 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) ?? ""; try { /* * All extraction messages should be verbose */ commandOutput.WriteInfo("##octopus[stdout-verbose]"); /* * Start by verifiying the archive is valid. */ commandLineRunner.Execute(new CommandLineInvocation( Path.Combine(javaBin, "java"), $"-cp \"{toolsPath}\" sun.tools.jar.Main tf \"{jarPath}\"", targetDirectory)).VerifySuccess(); /* * If it is valid, go ahead an extract it */ var extractJarCommand = new CommandLineInvocation( Path.Combine(javaBin, "java"), $"-cp \"{toolsPath}\" sun.tools.jar.Main xf \"{jarPath}\"", targetDirectory); Log.Verbose($"Invoking '{extractJarCommand}' to extract '{jarPath}'"); /* * All extraction messages should be verbose */ commandOutput.WriteInfo("##octopus[stdout-verbose]"); var result = commandLineRunner.Execute(extractJarCommand); result.VerifySuccess(); } catch (Exception ex) { commandOutput.WriteError($"Exception thrown while extracting a Java archive. {ex}"); throw ex; } finally { commandOutput.WriteInfo("##octopus[stdout-default]"); } var count = -1; try { count = Directory.EnumerateFiles(targetDirectory, "*", SearchOption.AllDirectories).Count(); } catch (Exception ex) { Log.Verbose( $"Unable to return extracted file count. Error while enumerating '{targetDirectory}':\n{ex.Message}"); } return(count); }
protected override List <ICommandInvocationOutputSink> GetCommandOutputs(CommandLineInvocation invocation) => new List <ICommandInvocationOutputSink>() { Output, new ServiceMessageCommandInvocationOutputSink(variables) };
public CommandLineInvocationResult Run(CommandLineInvocation invocation) { var result = CommandLineInvocationRunner.Run(invocation); return(result); }