Execute .jar file in .net core on Openshift container platform - jar

net core application which internally calls a java (.jar) file using .net Process class. I have used cmd.exe to run .jar file along with parameters. I have deployed this application on Openshift Container Platform. But as openshift is running on Linux, so cmd.exe is not available.
Below is code in .net core to execute jar file.
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WorkingDirectory = Common.JarWorkingDir;
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/K java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"');
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
cmd.StandardInput.WriteLine("exit");
cmd.StandardInput.Flush();
cmd.WaitForExit();
So Jar file is unable to execute. Any alternative to execute this jar with .net on OpenShift. Please help.

OpenShift is essentially Kubernetes running Linux containers. In other words, your code should act as if it is running on Linux.
Instead of cmd.exe, use bash (or, sh, or really, whatever shell is pre-installed in your container):
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WorkingDirectory = Common.JarWorkingDir;
cmd.StartInfo.FileName = "bash";
cmd.StartInfo.Arguments = "-c 'java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"'');
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
cmd.StandardInput.WriteLine("exit");
cmd.StandardInput.Flush();
cmd.WaitForExit();
You can maybe even remove some of the lines. CreateNoWindow, for example, is not required because .NET Core does not create windows on Linux at all.
If you have no shell expressions, perhaps you can even get things to be simpler and simplify from this:
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = "bash";
cmd.StartInfo.Arguments = "-c 'java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"'');
to something like this:
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = "java";
cmd.StartInfo.Arguments = $"-jar \"{Common.JarFilePath}\" \"{sourceCodePath}\"";
Oh, and watch out for the quoting in your Arguments variable. If you wrap the entire command after -c with single quotes, you should be fine, but if you are doing something trickier - if Common.JarFilePath isn't a simple file name - it may not work so well. Definitely test and tweak that. Maybe consider EscapeAndConcatenate.

Related

Why i get error in dtexec command in asp.net?

I'm new in asp.net,and want to write code to run sql server dtexec for that purpose write this code:
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = #"c:\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = "/C dtexec / f E:/Package.dtsx / set /package.variables[FilePath].Value; e:\testFile.txt";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
si.Close();
but when i run that code,get this error:
Microsoft (R) SQL Server Execute Package Utility Version 12.0.2000.8 for 64-bit Copyright (C) Microsoft Corporation. All rights reserved. Option "/" is not valid.
How can i solve that problem?thanks all.
You should remove whitespaces from this line, like that:
si.StartInfo.Arguments = "/C dtexec /f E:\\Package.dtsx /set \\package.variables[FilePath].Value;e:\\testFile.txt"
You should also be able to run your command in cmd.exe:
dtexec /f e:\package.dtsx /set \package.variables[FilePath].Value;e:\testFile.txt
Unless it will work in cmd, it won't work also from your code.

PowerShell script not working on server

I have a simple PowerShell script in which I try to write some log data.
The script that's not working:
Add-Content -Path C:\temp\logg.txt -Value $file.FullName
This line I placed in the middle of the script and I never get any text in the logg.txt. The Script executes fine except this, it compiles some files to a zip file.
This is on a Windows server.
If I, however, run the same script on my local machine, Win7, it does work!?
So I think it must be something with the server but I can't figure it out. Am using -ExecutionPolicy Bypass when I run the script.
EDIT: more info.
The script is called from an ASP.NET webpage:
Process.Start("Powershell.exe", "-ExecutionPolicy Bypass -Command ""& {" & destinationFolderRoot.Replace("\\", "\") & "\scriptParameters.ps1}""")
BaseZipScript:
function New-Zip
{
param([string]$zipfilename)
Set-Content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
function Add-Zip
{
param([string]$zipfilename)
if(-not (Test-Path($zipfilename)))
{
Set-Content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
$shellApplication = New-Object -COM Shell.Application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
Add-Content -Path "C:\temp\logg.txt" -Value $file.FullName
Start-Sleep -Milliseconds 750
}
}
scriptParameters1:
. "C:\temp\multiDownload\baseZipScript.ps1"
New-Zip c:\temp\multiDownload\user379\2016-09-15_14.39\files_2016-09-15_14.39.zip
dir c:\temp\multiDownload\user379\2016-09-15_14.39\tempCopy\*.* -Recurse |
Add-Zip c:\temp\multiDownload\user379\2016-09-15_14.39\files_2016-09-15_14.39.zip
Start-sleep -milliseconds 250
exit
As I said earlier the script works on local machine and also on the server, e.g. the files are being zipped, except that on the server nothing is logged in logg.txt.
Though the logg.txt works and exists on the server because the webpage also log some info there and that is being written.
Update:
As the comments guessed it was as simple as write access to the file. Though, it is kind of wierd because all files are created from the webservice if they doesn't exists? Any how, it works now. Thx! : )

How can I download a basic authentication-protected file in a Chocolatey install script?

The Install-ChocolateyZipPackage commandlet does not seem to support basic authentication when it comes to downloading files, i.e. via an URL such as https://user:password#example.com/file.zip. How can I work around this in my Chocolatey install script, i.e. download the file in question (e.g. https://user:password#example.com/file.zip) before I install it via Install-ChocolateyZipPackage?
Thomas put up a good answer in the group forum - https://groups.google.com/forum/#!msg/chocolatey/e4lcPIrLhis/vfSUVe0SZcIJ
As far as I know, authentication is not supported. But you could
specify wget as dependency and use that to download the file.
I use it in one of my packages for authentication and it works fine:
https://chocolatey.org/packages/rukerneltool#files (look at chocolateyInstall.ps1)
On Linux, wget would be the first choice to handle such things in a
Bash script.
But if the software you want to make a package of is Open Source, you
could integrate it directly into the package. That makes it easier.
Code for this is (in case it is later changed:
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.Networkcredential($username, $password)
Write-Output $('Downloading' + $url + '…')
$webClient.DownloadFile($url, $zipFilePath)
I use a similar way without wget, to fetch artifacts from our company's buildserver
$packageName = 'mycompanypackage'
$installerType = 'exe'
$username = 'chocolatey'
$password = '************'
$url = 'http://bamboo.mycompany.com/browse/DP-RS/latestSuccessful/artifact/JOB1/Setup/setup.exe'
$downloadFile = $url.Substring($url.LastIndexOf("/") + 1)
$url = $url+'?os_authType=basic'
$url64 = $url
$silentArgs = '/VERYSILENT /NORESTART /SUPPRESSMSGBOXES'
if (Test-Path "$downloadFile") {Remove-Item "$downloadFile"}
$webclient = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($username, $password)
$credCache.Add($url, "Basic", $creds)
$webclient.Credentials = $credCache
$webclient.DownloadFile($url, $downloadFile)
Install-ChocolateyInstallPackage "$packageName" "$installerType" "$silentArgs" "$downloadFile"

Run SSIS package in asp.net using powershell

Can anyone tell me how to run SSIS package with powershell on the server in asp.net. Please note i need to specify config file path also.
var processStartInfo = new ProcessStartInfo
{
Arguments = "dtexec /F " + "\"" + pkgPath + packageName +"\"",
FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
};
Process process = Process.Start(processStartInfo);
How to specify the config file path in the above code.

Custom rubble in Aptana not activated automatically

I am new to Aptana, so I could be missing something obvious. My problem in that bundle I created does not get activated when Aptana is loaded. I have to update date of the rubble.rb file for it to work.
touch ~/Documents/Aptana Rubles/ioncubeEncode.ruble
After I do that, for the rest of that Aptana session things are peachy.
I do not think it is relevant, but here is bundle code, which works just fine (when it works):
require 'ruble'
#Ruble::Logger.log_level = :trace
bundle do |bundle|
bundle.author = "Sasha"
bundle.copyright = "None"
bundle.display_name = "Ioncube Encode"
bundle.description = "Encode just saved php file form base_local to base folder"
end
command "Ioncube Encode" do |cmd|
cmd.input = :document
cmd.output = :output_to_console
cmd.trigger = :execution_listener , "org.eclipse.ui.file.save"
cmd.invoke do |ctx|
source_path = ENV['TM_FILEPATH']
ext = File.extname(source_path)
if ext == '.php'
if ( source_path =~ /base_local/)
destination_path = source_path.sub('/base_local/','/base/')
#CONSOLE.puts "Ioncube Encoding: " + source_path + " to " + destination_path
exec="/usr/local/ioncube/ioncube_encoder5 -v --optimize more --without-loader-check " + source_path + " -o " +destination_path + " 2>&1"
IO.popen(exec, 'r+') do |io|
io.close_write
CONSOLE.puts io.read
end
end
end
end
end
This might not seem like a big deal but it bothers me - a lot.

Resources