2

I have some Virtual machine running with same configuration. On those VMs I have installed my application.

I want to set some custom unique identifier in my VM , so that I know that if particular (from my application) request is coming from which VM that request is coming(or that application is running in which VM). As same application is running inside all VM, so to differentiate each request , I want to setup some unique custom identifier at the time of creation of Virtual machine.

I want to setup some system properties/Environment variable at the time of creation of virtual machine.

I am using Windows Azure Rest API and Java to create and manage Virtual machine on Windows Azure Portal. I want to achieve this by using Java programing language.

Please let me know if you have nay information about same.

PSS
  • 51
  • 4

2 Answers2

2

Each instance should have a different SID because the machines are sysprepped with the generalize option (the same applies when you want to provide a custom image). You can get the SID using the code in this question, and use that as identifier:

        string domainName;
        Helper.SID_NAME_USE accountType;
        SecurityIdentifier machineSID = 
                Helper.LookupAccountName("", Environment.MachineName, out domainName, out accountType);

Note: even though it says domainName in the code, your machine doesn't need to be in a domain for this to work.

To set a "custom identifier" on your VM you can use environment variables. Since you're using Java the easiest way to set these is probably the setx.exe command line tool (download). You can call the tool like this:

setx.exe VmIdentifier 123 -m

Then you should be able to read it using System:

System.getenv("VmIdentifier")

It's not really clear to me if you're using Virtual Machines (IaaS) or Cloud Services (PaaS). If you're using Virtual Machines, it will be up to you to deploy the application, and while doing so, you can also execute setx.exe.

Now, if you're using Cloud Services, you actually upload a package which gets deployed on your instances. And you get support for startup tasks (which run before your application starts): How to Define Startup Tasks for a Role. You can use a startup tasks to run setx.exe, but make sure you run it with executionContext set to elevated .

Community
  • 1
  • 1
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • Thanks for reply, I want to set my own unique identifier and then retrieve it later on from application running inside that VM. – PSS Sep 18 '12 at 06:58
  • Thanks alot for answer. I want to set identifier at the time deploying that virtual machine on Azure cloud environment and programatically.So as soon as I start that machine after that I set that environment variable. – PSS Sep 18 '12 at 07:33
  • Are you deploying a WebRole/WorkerRole or a Virtual Machine? – Sandrino Di Mattia Sep 18 '12 at 07:34
  • I am deploying VirtualMachineRole. – PSS Sep 18 '12 at 07:44
  • Ow.. a VM-Role? These don't support startup tasks and are not persistent. I think your best bet would be to add the identifiers in the ServiceConfiguration.cscfg (maybe something like: Instance_1 = 12345939, Instance_2 = 29298483, ...). Then you can get the current instance through `com.microsoft.windowsazure.serviceruntime.RoleEnvironment.getCurrentRoleInstance` and find the matching setting. But I don't think there's a clean solution for this. – Sandrino Di Mattia Sep 18 '12 at 07:57
  • I think I can't use ServiceConfiguration.cscfg , as I have few configuration files and by using them I am creating various VM. Say I want to have 2-3 types of VM , so for that I have created those many config files and I can create as many VM out of those VM by using those same configuration(.cscfg) file. – PSS Sep 18 '12 at 08:23
  • also is there any way to get inside the VM, get information like "instance ip address", "instance dns name" etc – PSS Sep 18 '12 at 08:35
  • You could use remote powershell: http://social.msdn.microsoft.com/Forums/pl-PL/WAVirtualMachinesforWindows/thread/e477e6a1-eb8b-43f7-8089-6837ae3ee34c – Sandrino Di Mattia Sep 18 '12 at 08:46
0

You can connect to your VM with remote desktop services, so it should be trivial to connect and add a setting to your application's web.config file which identifies the deployment, or, if you want your web.config files to be identical, then you could upload a small text file.

Alternatively, your server must already have a unique name. I assume you're running a .Net application? If so then you should be able to use Server.MachineName (http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.machinename%28v=vs.71%29.aspx) to get the unique name of the box your code is running on.

Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
  • Thanks for reply. I am running a java application. I was planning if I can setup some environment variable unique to each VM for each VM , and then any application running on that VM can access that particular VM and send which each request. But how to setup env variable inside VM operating system. – PSS Sep 18 '12 at 06:57
  • You're welcome, although I don't think I've done much good! You might want to rephrase your question - adding Java and the need for Path vars will make it easier for someone to answer, and adding Java to the tags will get help from your flavour of developer. I think most people will assume an Azure question is about .NET unless you tag it explicitly. Good luck, and welcome to Stack Overflow. – Jude Fisher Sep 18 '12 at 07:00
  • Thanks for valuable suggestions. I updated question accordingly. – PSS Sep 18 '12 at 07:27