Use node. js to access azure user role information

Source: Internet
Author: User

Return csdn!

When we develop software on Microsoft's Windows azure cloud computing platform, we may encounter this requirement, that is, to obtain information about the user's role environment. For example, to obtain the current instance name, deployment ID (deploymentid), or even know which roles are running? Which roles are not running? If we want to use nodejs to implement these tasks, we need some skills. Because the role environment class (roleenvironment) is hosted by. Net code.

In this article, we will discuss how to set the running azure role information so that this information can be easily accessed by the nodejs server. The trick is to use environment variables and start tasks, and use running powershell as a bridge between them.

I have created an example to illustrate this point. You can test it on your own. Basically, it obtains the startup task. It transfers the azure role environment information to the environment variables and outputs these variables using the server. js script file. In any case, you can see how it works from the following code.

We need to complete the following tasks:

1. Create a startup task and run a cmd script to access the azure role environment information;

2. Set the environment variable information so that the information can be accessed from nodejs;

3. Use the process. env object and server. js to obtain the value.

The following is a detailed description. The cmd script is as follows:

Script 1: setupenvironmentvariables. cmd

@ECHO off%~d0CD "%~dp0" IF EXIST %WINDIR%\SysWow64 (set powerShellDir=%WINDIR%\SysWow64\windowspowershell\v1.0) ELSE (set powerShellDir=%WINDIR%\system32\windowspowershell\v1.0) ECHO Setting the Environment variables..CALL %powerShellDir%\powershell.exe -Command Set-ExecutionPolicy unrestrictedCALL %powerShellDir%\powershell.exe -Command "& .\set_azure_role_information.ps1"ECHO Done! ECHO Restarting IIS..CALL iisresetECHO Done! ECHO Starting the W3SVC service..CALL NET START W3SVCECHO Done!

The above code is explained as follows:

1. To execute unsigned powershell scripts on the azure cloud, you must set the execution policy to unrestricted ). I used the set-executionpolicy command, but note that its value will not change unless you manually modify the value. In the powershell 2.0 Environment, you can also use the-executionpolicy command to set unlimited conditions in the current range. However, because the default webrole template uses the osfamily = "1" setting, it is valid on Windows Server 2008 SP2, so we will not use this command.

2. Important: After setting environment variables, We need to restart the IIS server and W3SVC to make the service take effect. The W3SVC of the azure cloud platform is set to manual mode, so that is why it will not be automatically started after the IIS server is reset.

Let's look at the next powershell script:

Script 2: setazurerole_information.ps1

[Reflection.Assembly]::LoadWithPartialName("Microsoft.WindowsAzure.ServiceRuntime")[Environment]::SetEnvironmentVariable("RoleName", [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::CurrentRoleInstance.Role.Name,"Machine")[Environment]::SetEnvironmentVariable("RoleInstanceID", [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::CurrentRoleInstance.Id,"Machine")[Environment]::SetEnvironmentVariable("RoleDeploymentID", [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::DeploymentId,"Machine")[Environment]::SetEnvironmentVariable("IsAvailable", [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::IsAvailable,"Machine")[Environment]::SetEnvironmentVariable("CustomVariable", "Some value","Machine")

Code Description: we use the roleenvironment attribute value to set some environment variables. You can also set custom environment variables.

Script 3: servicedefinition. csdef

<Task commandLine="setup_environment_variables.cmd"    executionContext="elevated" taskType="simple" />

Finally, we use nodejs to write the server. js code, which can output the response results.

Script 4: Server. js

var http = require('http');var port = process.env.port || 1337; http.createServer(function (req, res) {res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write("Role Name: " + process.env.RoleName + "\n");res.write("Role InstanceID: " + process.env.RoleInstanceID + "\n");res.write("Role DeploymentID: " + process.env.RoleDeploymentID + "\n");res.write("Is running?: " + process.env.IsAvailable + "\n");res.write("Custom variable: " + process.env.CustomVariable + "\n"); res.end();}).listen(port);

Run the preceding example on the simulator and the result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.