how to store .bat file in azure, execute it and connect to it - azure

I'm new in windows azure but I already know how to deploy my service in azure. I need to have sth like a folder in azure to store some text files, two .exe files and one .bat file. then I want my deployed service to use these files and execute .bat file and create some new text files on that folder.
My question is if there is sth like the folder that I need?how to execute it? and how is it possible to connect to that folder through my wcf service?
I really appreciate your help.

If you want that bat file to execute one time i.e. during role start up, you can use start up task.
If you want to execute bat file during WCF call, you can use .NET process class. However for executing this task, you may require higher privileges.

Related

How do I "shell out" to a binary in an Azure webjob?

I need to create an offline job processor which will call a console executable with a varying set of command line flags, based on a queue. I've created a new project with the "Azure WebJobs SDK: Queues" template in Visual Studio. I know that I can deploy this to Azure, and it will create a virtual machine and set it all up to work.
I already have a web "worker role" doing essentially the same sort of thing, but, in that case, I wrote the code that it calls, and have the source to a library my code needs. This time, I need to call a "canned" binary that someone else wrote, and to which I do not have access to the source.
I can create a subfolder in the project, and dump the executable (and all its folders and libraries) in this folder, but I wouldn't know how to call it from my webjob. What would be the path structure to the binary once deployed to the cloud service in Azure, if it's just sitting in a subfolder in the project?
Also, this binary really expects to live in C:\Software\<Directory>. Is is possible to specify this particular, external directory to be created on the virtual machine when this application is deployed to a cloud service in Azure? If I could do that, it would work better, and I'd know precisely how to call the executable.
You can use relative paths to locate your sub-executable. The current directory is always where your main WebJob exe is running from, making it easy to find satellite files in subfolders.
However, you will not be able to place your exe in c:\software\directory. You will need to make sure that the exe you need to spawn can run from an arbitrary folder.
As part of the worker role, you can create a subfolder, and put your binary in it. (You can't drag-and-drop a folder in Visual Studio when doing this, so, in my case, I had to create several subfolders, and drop files into them, in order to make my application work.)
When deployed, the worker role will then copy this folder of arbitrary files to the worker server in Azure, where it can then be called. When deployed, this arbitrary folder will be located in ...TASACDWS\TASACDWS\TASACDWS\csx\Debug\roles\TASACDWS_Worker\approot
The "root" or "base" folder of the worker role itself is the current directory of the process. So, if my application lives in a folder called "Arbitrary_Binary" under the worker role (in Visual Studio), I can reference it with a Process object like so:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WorkingDirectory = "Arbitrary_Binary";
p.StartInfo.FileName = "Arbitrary_Binary\\Arbitrary_Binary.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

OnStart vs Startup Script for batch file?

I have a Ruby on Rails application that needs to find a home in an Azure Worker Role.
I currently automate the deployment of the application with a batch file - a file that takes the apache and ruby installers, runs them, and then drops the RoR app in the appropriate directory. After the batch script finishes, Apache is serving to and from the application via port 80.
I'm new to Azure and trying to figure out how to do this.
From my understanding, I have two options here: OnStart with the installation files in Blob Storage, or a startup script. I'm not sure how to do the latter, but I have located the onStart method within the WorkerRole.vb file in the new Azure project I just created.
My question: Is it recommended to use OnStart to deploy the application (using the batch script)? If so, how would I go about integrating the script into the project? And - how do I get started with storing and referencing the files in blob storage?
I know these are super high-level questions. Any input or suggested reading would be super helpful. I have tried to google / search for relevant resources but haven't been able to find much. Thank you for your time!
When you are inside OnStart() function it is better to do role configuration things i.e. IP binding, etc however if you would want to install runtime, download application zip, configured role specific setting, it is best to use Startup task. Please visit my blog Windows Azure: Startup task or OnStart(), which to choose? to learn more about it.
Now in your case it is best to use Startup task. What you can do it as below:
Create your ROR package a zip and place it at Windows Azure Blob Storage
Create a Cmmmand batch file which will do:
2.1 Download the ZIP
2.2 Unzip to Zip content to a specific location
2.3 Update the status back to AZure Blob Storage (Optional)
In your OnStart() function you just need to configure the ROR
The code will look as below if you have TCP Endpoint name "RORWeb80" set to use port 80:
TcpListener RoRPortListener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["RORWeb80"].IPEndpoint);
RoRPortListener.Start();
I have written a sample app for Tomcat/Java based worker role which does exactly the same. So what you can do it just replace the Tomcat ZIP file with ROR ZIP and reuse the code exactly.
As long as you don't need admin-level access (e.g. modifying registry, installing msi's, etc.) you can do your setup from OnStart(), including launching your script. Just include the startup script with your project (don't forget to set Copy Local to true).
Same goes with startup script: you call your cmd file, which then executes the sequence for you. And if you give it elevated permissions, you can run installers, modify registry settings, install custom perf counters, whatever.
In either case: you can keep your apache zip, ruby installers, etc. in blob storage and, at startup, download them to local storage. This saves you from bundling everything within the deployment, which gives you a few advantages (being able to update ruby / apache without redeploy, reduced package size, etc.).
There's a sample app on codeplex that demonstrates the basics of setting up Tomcat via startup script. For one more example, you can look at the scripts installed via Eclipse Windows Azure plugin for Java. These scripts are quite similar. The key is to have some way of downloading files from blob storage and then unzipping them. the codeplex project I referred to points to a sample app that does simple blob downloading. The Eclipse packaging provides similar functionality in a .vbs app. Here's a snippet of one of my scripts from an Eclipse-based project:
SET SERVER_DIR_NAME=apache-tomcat-7.0.25
SET WAR_NAME=myapp.war
rd "\%ROLENAME%"
mklink /D "\%ROLENAME%" "%ROLEROOT%\approot"
cd /d "\%ROLENAME%"
cscript /NoLogo util\unzip.vbs jre7.zip "%CD%"
cscript /NoLogo util\unzip.vbs tomcat7.zip "%CD%"
copy %WAR_NAME% "%SERVER_DIR_NAME%\webapps\%WAR_NAME%"
cd "%SERVER_DIR_NAME%\bin"
set JAVA_HOME=\%ROLENAME%\jre7
set PATH=%PATH%;%JAVA_HOME%\bin
cmd /c startup.bat
The codeplex project has a similar-looking script.
Don't forget: you'll need to set up an Input Endpoint for your role (part of the role properties).
To get blobs into blob storage, there are both free tools (like Clumsy Leaf CloudXplorer and paid tools (such as Cerebrata's Cloud Storage Studio).
To download blobs to local storage, you can either write a few lines of .net code (from OnStart) or just use the utility pointed to in the codeplex project.

Can't delete files, IIS lock

I have a poweshell command which deletes the folder(i.e. Summer) from wwwroot directory and recreates the folder with the necessary files(images, css, dll etc) in it. The problem is every once in a while the IIS tends to lock some of the images or files in the directory so the powershell command fails to delete the file. I do recycle/stop the apppool before running powershell script which is used by site but still the problem persists. This issue is random i.e. the powershell script can delete the folder sometime while it can't other time. The weird thing is, if i start deleting the contents (subfolders, files) inside 'Summer', at the end, i am able to delete 'Summer' folder, but it is an manual process and which is tedious.
Is there any command which i can put in powershell or batch file to delete 'Summer' folder, even though when it is locked by IIS?
I agree with #Lynn Crumbling and recommend iisreset.
Sysinternals has two tools that provide other options:
The ProcExp tool allows you to find which processes have open handles to a given file, and allows you to close that handle. The downside of this tool is that it's not a command line tool.
The MoveFile tool allows you to schedule the file to be removed after reboot.
You can use the IIS powershell commandlets to start and stop app pools, web sites etc
Import-Module WebAdministration;
Stop-WebAppPool ${appPoolName}
Stop-WebSite ${webSiteName}
you can then start them again afterwards using the opposite commands
Start-WebAppPool ${appPoolName}
Start-WebSite ${webSiteName}
As put in comment, fully stopping IIS using iisreset stop would work.
Also, you may want to stop only the application from which you are trying to delete files from. Look at the Administration Guide.

How Can I Update My Web Site Automaticlly EveryDay?

The tasks I do manually for updating my web site:
Stop IIS 7
Copy source files from a folder to the virtual directory of my web site
Start IIS 7
There are many ways to approach this, but here is one way.
I am assuming you don't want every single file in your source repository to exist on your destination server. The best way to reliably extract what you need from your source on a regular basis is through a build file. Two options for achieving this are nant and msbuild.
Once you have the set of files you want to deploy, you now need a way to distribute them to your destination server & to stop and start IIS. Again, there are options, but I would personally recommend powershell (with the IIS snapin) for this.
If you want this to happen regularly, consider a batch file executed by some timer, such as a scheduled task, or even better, a CI solution such as TeamCity.
For a full rundown, there are examples within my PowerUp project that does this.
It depends where you are updating from, but you could have a your virtual directory pointing to a local read-only working copy of your source code and create a task that every day runs a batch file/powershell script/etc. that would update that working copy (via a svn update, git pull etc.)
That supposes that you have a branch that always contains the latest releasable code.
You have to create a batch file with the following content:
Stop WWW publishing service
Delete your old files
Copy the new files
Start WWW publishing service
You can start/stop services like this:
net stop "World Wide Web Publishing Service"
When you have your batch file you can create a task in the Task Scheduler that calls your batch in a regular time interval (e.g. each day).

Setting read / write / execute privilege on "cgi-bin" folder in Windows Azure webrole

We're talking about a simple webapp.
So I have a file called "modulev2.cgi" which is part of a trusted 3rd party online payment company. This file has to be put in a folder named "cgi-bin". For windows IIS environnement the file is renamed "modulev2.exe" and put in the same directory. This is what the documentation says.
Module is called as this :
FORM ACTION=../cgi-bin/modulev2.exe METHOD=post
with a bunch of parameters. It should not download when called of course but execute.
And indeed it does work in my dedicated server, provided the "cgi-bin" folder and the file in have "execute" setting level in IIS.
So to the point, would I be able to set the rights to execute to this file in Windows Azure ? If yes, how to script such a process ?
Any help greatly appreciated.
Thanks !
The best way to do this is to script it out locally against your IIS using appcmd.exe. You want to add your CGI handler programmatically. By default, IIS in Windows Azure is already running CGI/Fast-CGI, so you don't have to install it, it should be ready. I think you need to add it to the CGI restriction list and add your handler mappings.
http://technet.microsoft.com/en-us/library/cc732851(WS.10).aspx
Once you have a .cmd file that will correctly configure your local IIS settings, you can use that as the basis for a Startup task in Windows Azure to bootstrap the role.
http://channel9.msdn.com/Shows/Cloud+Cover/Cloud-Cover-Episode-31-Startup-Tasks-Elevated-Privileges-and-Classic-ASP
http://channel9.msdn.com/Shows/Cloud+Cover/Cloud-Cover-Episode-34-Advanced-Startup-Tasks-and-Video-Encoding

Resources