Run node module in guest executable - azure-service-fabric

I've got a global npm package installed, TileServer, that I run as service through command line. I'd like to convert this to a guest executable in service fabric, but I'm having trouble implementing.
Pre-Guest executable, I would invoke the following command in cmd:
tileserver-gl-light --port=8788 map.mbtiles
My configuration for my guest executable is:
<ExeHost>
<Program>tileserver-gl-light</Program>
<Arguments>--port=8788 c:\maptiles.mbtiles</Arguments>
<WorkingFolder>Work</WorkingFolder>
</ExeHost>
Unfortunately, the error I get when trying to run the service just says "There were deployment errors. Continue?"
Any ideas on how to get this working?
Thanks!

Can you check to see if that application package validates on your local machine by calling Test-ServiceFabricApplicationPackage?
Usually SF expects the file to be
Present in the application package and
Some sort of executable file that the OS understands (for windows .bat, .exe, etc.)
In this case I think what you're saying is that the tileserver bits are actually outside the package (wherever your node packages are), and you're actually trying to use node to start it (since the tileserver-gl packages aren't natively executable by Windows).
If that's the case your program should probably be something like a batch file that just says "tileserver-gl-light" and then your command line args in it.
Include that batch file in your code package and reference that as your program (and straighten out how you want to pass the args) and you should be good to go.

As #masnider pointed out, the files must be present in the application package, and we must call the executable as something the OS understands. This presents particular challenges for node modules as:
They must be wrapped in a .bat
They can't be called using the global command
They need parameters
They files, node_modules, often has issues with file path length issues
My end service had the following file structure:
- ApplicationPackageRoot
- MyService.ServicePkg
- Code
- 7za.exe
- node.exe
- npm.7z
- start.bat
My ServiceManifest.xml had these values in it:
<SetupEntryPoint>
<ExeHost>
<Program>7za.exe</Program>
<Arguments>x npm.7z<Arguments>
<WorkingFolder>CodeBase</WorkingFolder>
</ExeHost>
</SetupEntryPoint>
<EntryPoint>
<ExeHost>
<Program>start.bat</Program>
<Arguments><Arguments>
<WorkingFolder>CodeBase</WorkingFolder>
</ExeHost>
<EnvironmentVariables>
<EnvironmentVariable Name="Port" Value="8788"/>
<EnvironmentVariable Name="TilePath" Value="mytiles.mbtiles"/>
</EnvironmentVariables>
</EntryPoint>
And finally, my start.bat was comprised of the following line:
.\node.exe npm/node_modules/tileserver-gl-light/sc/main.js --port=%Port% %TilePath%
So how did this work? The npm.7z in the Code consisted of my node_modules, pre-zipped. The 7za was the portable version of 7zip which allows use to get around the file path length issues present on windows. 7za is called in the SetupEntryPoint.
Then the SF calls start.bat, which reads in the environmental variables of Port and TilePath. This has to called the exactly .js file called by the normal tileserver-gl-light command.
Doing this results in a properly working node application in a service fabric guest executable.

Related

VS Code - Using Code Runner Extension to Run Programs on Virtual Machine (VM)

I develop on Windows using a Linux VM via Vagrant and Virtual Box. I'm trying to figure out how to get the Code Runner extension to run my files on the VM. The biggest hurdle so far is, for a given file, I need to convert from the Windows host path to the Linux guest path.
Background:
The Code Runner extension allows one to map file types to shell commands to run those files. For example,
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt"
tells Code Runner that when I try to run a Java file, it should cd to the directory that contains the file, and compile the file, and then run the compiled file. The mapping from file types to commands is called the code-runner.executorMap which is contained in settings.json. By adding the option
"code-runner.runInTerminal": true
to my settings.json, I can tell Code Runner to run in the integrated terminal. So by simply SSHing into my VM from the integrated terminal via vagrant ssh, I have code runner targeting the virtual machine.
This is where the problem comes in - Code Runner is using my Windows style paths and my Windows file structures as command line arguments to my VM.
For example, suppose my Windows file structure looks like c:\a\b\c\d and my VM has its root in c so that c and d are shared folders. If I want to run a file in d, the command cd $dir will tell my VM to do cd c:\a\b\c\d.
I have thought of workarounds like adding the following to my settings to run python files
"python": "cd \"$(dirname \"$(locate -l1 $fileName)\")\"; python3 $fileName",
This command, which runs on the integrated terminal (the VM) locates and changes to the directory which contains the file which is to be run. It then tells the python3 interpreter to run that file. However, this doesn't always work (e.g. multiple files with the same name), and requires me to update the database that locate depends upon every time I add a file.
There has to be some way to translate my Windows file paths to the paths on the virtual machine (so e.g. c:\a\b\c\d -> /c/d). Perhaps through Vagrant? I would appreciate any help.
I developed a workaround. I would still be interested in a "cleaner" solution.
The workaround is as follows:
Firstly, I wrote a Python script to convert from Windows Paths to paths on my virtual machine. The script takes in the windows path to a file and the file name as arguments.
#pathconverter.py
import sys
windows_path=sys.argv[1]
file_name=sys.argv[2]
path_to_vagrantfile = r"C:\Users\Evan\Google Drive\Development\Vagrantfile"
slashes=path_to_vagrantfile.count("\\")
y=windows_path.split("\\")[slashes:]
linux_path="/vagrant/"+'/'.join(y) + "/" + file_name
print(linux_path)
Thus the following code converts from a windows file location to one on my virtual machine (assuming you saved pathconverter.py at the root of your shared directory, \vagrant :
python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName
Therefore, to run most files of various interpreted languages, I just supply the output of this command as an argument to the interpreter. For example, to run a Python script on my VM automatically, I just add the following line to the code-runner.executorMap:
"python": "python3 \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""
Or for Racket/scheme, I just do:
"scheme": "racket \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""

Wildlfy Swarm YAML properites file

i'm trying to run swarm with custom parameters, when I pass the properties options on command line, the server detects these options and runs properly, for example when I run this command :
java -jar test.jar -Dswarm.https.certificate.generate=true
it generates a certificate and starts https on a specific port.
Now according to swarm documentation it's better to use a yaml config file, so I have created one with the properites I wanted. However when I specifiy the path to my yaml file in the command line :
java -jar test.jar -S C:\Users\x17\test\modules\wsserver\project-default.yml
in this case, the file containing the properties is ignored, and the server starts with a default configuration.
Should the project-default file be in a specific folder in my project ? does anybody have an idea how to solve this issue ?
Thanks
There's a difference between -s and -S. The -S option, that you're trying to use, is used for selecting a project-*.yml file that's packaged together with the application, inside the uberjar. For example,
java -jar app-swarm.jar -S local
means that the file project-local.yml packaged inside the application is used.
If you want to set a full path to the YAML file, you need to use -s:
java -jar app-swarm.jar -s .../path/to/project-defaults.yml
See the documentation for more info: http://docs.wildfly-swarm.io/2018.1.0/#configuring-an-application-using-yaml
https://github.com/thorntail/thorntail-examples/blob/master/config-options/cdi-injection/src/main/java/org/wildfly/swarm/examples/jaxrs/cdi/Controller.java
Can help you to figure out the issue.
#Inject
#ConfigurationValue("some.string.property")
String stringProperty;
is valid CDI injection solution.

Startup Task not running on Azure Cloud Service role

I'm having difficulties trying to setup a startup task in an Azure role.
The ultimate goal is to disable RC4 cipher, along with other SSL configurations. In my (VS2012Express) project (solution partially achieved following another answer here in SO that led me to https://gist.github.com/sidshetye/29d6d48dfa0c2f5488a4 ) I created a Startup.cmd file like this:
# Execute powershell command to disable RC4 and imporve SSL security settings
ECHO Batch started >> "StartupLog.txt" 2>&1
PowerShell -ExecutionPolicy Unrestricted .\HardenSSL.ps1 >> log- HardenSSL.txt 2>&1
EXIT /B 0
HardenSSL.ps1 is the PowerShell script from the previous link. Both the .cmd and .ps1 scripts are placed in the application root directory, marked as "Content" with properties set to "CopyLocal=Always".
In my service definition, I put this:
<Startup>
<Task commandLine="Startup.cmd" executionContext="elevated" taskType="background"></Task>
</Startup>
Now, when I deploy the application to Azure, "nothing" happens. I configured the role instance to allow remote desktop, connected to the machine. I verified the scripts where published, and there were no log files, RC4 still enabled. I tried to manually run the .cmd and the machine runs the scripts to completion, disables RC4 and restarts. So the scripts are actually "correct".
The problem is that the scripts are not getting fired up at startup. I may be wrong, but I don't see anything related looking Windows events. Actually, the server now keeps all the configurations, but I have to be sure the scripts get executed in case I'll have to publish to new instances/cloud services.
I also tried to:
1. place the scripts on a child directory
2. create other 2 "simpler" .cmd that just create a log file with "script started" to exclude problems related to the .cmd calling the PowerShell script.
None of those scripts got executed.
Hope I've been sufficiently clear, any help would be greatly appreciated.
Thank you in advance,
Alberto
UPDATE 1
Reading through various discussions, I missed one very important thing: the script files are actually published in 2 distinct places, one being inside the /bin folder.
Ex: I placed my scripts in a /StartupScripts folder in my project, and when I connect via Remote Desktop to the Azure server I find the scripts both in "approot/StartupScripts" and in "approot/bin/StartupScripts".
The scripts the are actually executing are those placed inside the "bin" folder. the real problem is that I have probably a path problem inside the .cmd since I now found the execution logs with an error.
Now I will try to change it up and update the question here on SO.
Ok.
In the end it was indeed a problem with a path in my Startup.cmd file: .\HardenSSL.ps1 could not be found if the StartUp Task pointed to a subfolder.
Solution was to place both Startup.cmd and HardenSSL.ps1 files in the application root, remove the ".\" part when calling the PowerShell Script and all worked well.
Anyway, I would like to suggest anyone to pick this other solution I found in stack exchage:
https://security.stackexchange.com/a/79957
It links to a NuGet package that does the same thing as the script I found on the link to github in the original post, just "better"; mainly:
Better configuration of cipher suites, with support for ForwardSecrecy for all reference browsers on SSLLabs
Retain SSL support for Internet Explorer 8 on windows XP (unfortunately still a necessity for us)
Alberto.

lost PATH while trying to set custom winlogon shell in WindowsXP

I have changed the shell key in windows registry to gain custom shell (Kiosk usage):
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon]
I set shell key to a batch file which runs two applications as below:
start "myFirstAppTitle" "myAppPath\myApp1.exe"
start "mySecondAppTitle" "myAppPath\myApp2.exe"
Each application runs but the second application which needs some files to be excuted throws an error which says could not find dependency files. whereas the dependency files are adjoining to the exe file and the mentioned app works fine, when starts from startup.
Meanwhile when i run the batch file manually it rusn fine.
I added the PATH command to the batch file but it did't work too.
Change the batch file to this:
set PATH=%PATH%;C:\MyAppPath
start "myFirstAppTitle" "myApp1.exe"
start "mySecondAppTitle" "myApp2.exe"
If you start executables without an absolute path, the path is relative to the current working directory. Also, when you specify an executable with a relative path, %PATH% is not searched for a matching subfolder with a matching executable.
Since the script worked when you manually started it, your working directory probably was C:\. However, when run at logon as a replacement shell, the working directory is most likely "%SystemRoot%\system32".
The problem solved strangely, i removed the title parameter of start command and it worked. In fact i used start command this fashion:
set PATH=%PATH%;C:\MyAppPath
start myapp.exe
start myapp2.exe

Executing subprocess.Popen inside Python script in PyDev context is different than running in terminal

I'm executing this code:
p = subprocess.Popen(['/path/to/my/script.sh','--flag'] , stdin=subprocess.PIPE)
p.communicate(input='Y')
p.wait()
It works when executing it on the shell using "python scriptName.py",
BUT when executing using PyDev in Eclipse, it fails, the reason:
/path/to/my/script.sh: line 111: service: command not found
This bash script "script.sh" contains the following command which causes the error:
service mysqld restart
So "service" is not recognized when running the .sh script from the context of PyDev.
I guess it has to do with some ENV VAR configurations, couldn't find how to do it.
BTW - Using "shell=True" when calling subprocess.Popen didn't solve it.
service usually is located in /usr/sbin, and that this directory isn't on the PATH. As this usually contains administrative binaries and scripts which arn't designed to be run by everyone (only by admins/root), the sbin directories arn't always added to the PATH by default.
To check this, try to print PATH in your script (or add an env command).
To fix it, you could either
set the PATH in your python script using os.setenv
pass an env dict containing the correct PATH to Popen
set the PATH in your shellscript
use the full path in your shellscript
set the PATH in eclipse