A simple tutorial to control Windows Azure with a Python script

Source: Internet
Author: User
Tags install openssl openssl x509 vars
Inux developers often use Python to do small pieces of work, because you can write scripts in a very easy situation. It has become a popular way to accomplish small tasks such as configuration and deployment. Windows Azure, Microsoft's cloud, is no different. With the availability provided by the Python SDK, Python becomes the first-class citizen of Windows Azure. Let's see how we can use Python to deploy an image from Vmdepot to Windows Azure programmatically, without the need for a single Windows Azure subscription.
Establish a management certificate

Any interaction with Windows Azure requires two things:

Let's say you run this script with Linux (if not, contact me and I'll show you how to use Windows to do the same thing). If you do not have OpenSSL installed, please use the following command from the root prompt:

Yum Install OpenSSL

The following will create a. pem file, which can then be translated into a. cer file and exported and uploaded to Windows Azure.

OpenSSL req-x509-nodes-days 365-newkey rsa:1024-keyout elasta.pem-out Elasta.pem

Export the. cer with the following command:

OpenSSL x509-inform pem-in elasta.pem-outform der-out elasta.cer

So you get it, a management certificate that can be uploaded to your Windows Azure subscription. When this is done, you should already be able to use Windows Azure programmatically.

Sign in to Windows Azure with your Microsoft account or Windows Azure Active Directory credentials. The management portal is located in https://manage.windowsazure.com.

Select the Settings tab:

Select Manage certificates from the menu:

The application toolbar below contains an upload button:

Select this button to upload the previously exported. cer file:

You should see a certificate entry like this in the results panel.


To help explain this article, I have written a Python script that can be downloaded here:

Https://github.com/elastacloud/python-vmdepot-deploy


You can read the installation instructions to learn how to use the script. The purpose of this article is to take you through some of the key features of the Windows Azure Python API that can help you develop a fully automated deployment.

To use any of the service management features in Windows Azure, we need a service management object:

Self.sms = Servicemanagementservice (vars.subscription_id, Vars.certificate_path)

This is very useful for what we are going to do next. As you can see, this requires a certificate and a subscription ID as the parameter.
Construct a virtual machine

A virtual machine image is a template that we can use to build virtual machines. In this example, we will use a CentOS image that is copied from a location called Vmdepot, held by Ms Open Tech, a wholly owned subsidiary of Microsoft.


We can check whether we have copied the specified image and registered the existing ones by listing all the named images in our subscription.

def _image_by_name (self, name):  # Return the first one listed, which should is the most stable for  I in SELF.SMS.L Ist_os_images ():   if name in I.name:    return True  return False

If not, we can continue our workflow.

The following shows a concise process for creating a storage account that requires a name and location. Because I am in London, I will use the "Nordic" data Center (located in Dublin), but there are more than 10 data centers around the world and there is a pile of construction underway. When the storage account is created, it allows up to five terabytes of BLOB data to be stored and protected by 2,512-bit AES, which can be used to access the account. The logical unit that stores the data is called a container, so we need to create a container to let us store the image we copied.

Self._create_storage_account_if_not_exists (Vars.storage_account_name, vars.deploy_location) Account_key = self._ Get_primary_account_key (Vars.storage_account_name) self._create_container_if_not_exists ()

We should now be able to copy BLOBs from remote locations. This is done by using an API called Copyblob that is provided by Windows Azure. The implementation code is as follows:

Self.blob_service.copy_blob (Container_name=constants.storage_container_name, Blob_name=constants.vhd_blob_name, X_ms_copy_source=constants.centos_minimal_image) self._wait_for_async_copy (Constants.storage_container_name, Constants.vhd_blob_name)


As you can see, this is an asynchronous method that allows any blob to be copied from a remote location. The great of this API is that you can use it to copy any HTTP endpoint from inside or outside of Windows Azure, and use it without any cost. The disadvantage is that it does not have an SLA (translator note: service-level agreement abbreviation, Service level agreement, is a contract between the network service provider and the customer).

The blob can then be registered as an image in your Windows Azure subscription, which you can use to create multiple virtual machines.

Self.sms.add_os_image (Label=constants.image_name, Media_link=storageimage_uri, Name=constants.image_name, os= ' Linux ')

This script will create a "cloud service" that contains the public endpoint of the virtual machine, and then set a common endpoint to forward the port to the virtual machine so that you can enter them via SSH. The script writes, if you choose the same cloud service every time, it will increment from Port 22 to add another open port ready for SSH to enter as a virtual machine for the cloud service.


We are replicating the image from the vmdepot that contains the image. Through it, I am copying and registering a CentOS mini image in my subscription.

Https://vmdepotneurope.blob.core.windows.net/linux-community-store/ Community-32167-508624a5-01d1-4d57-b109-df2af5b4b232-1.vhd

You can browse Vmdepot from this address:

Http://vmdepot.msopentech.com/List/Index

Finally, we will use a very simple algorithm to determine that the virtual machine has been deployed to the cloud service by looking at the relevant blobs in the storage account, each with a virtual hard disk (. vhd).

index = -1blob_exists = Truewhile Blob_exists:index + = 1   blob_exists = self._blob_exists (constants.storage_ Container_name, "ELASTAVM" + str (index) + ". vhd") Vm_media_link = Self._make_blob_url (Vars.storage_account_name, Constants.storage_container_name, "ELASTAVM" + str (index) + ". vhd") Self._create_vm_linux (Vars.storage_account_name, Vars.storage_account_name, "ELASTAVM" + str (index), Vm_media_link, Vars.deploy_location, index, Vars.username, Vars.password)

As a result, we can add multiple virtual machines to our cloud service.

All of the above are done from the setup.py file. You can see all of the above code in the file at the following address:

https://github.com/elastacloud/python-vmdepot-deploy/blob/master/elastacloud/pyvms/Setup.py

Based on the instructions in readme.md, you are ready to start.

You can clone the Windows Azure Python SDK at the following address:

Https://github.com/WindowsAzure/azure-sdk-for-python

The music!

  • Related Article

    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.