A simple tutorial on using Python scripts to control Windows Azure _python

Source: Internet
Author: User
Tags install openssl openssl openssl x509 vars ssh python script ssh access

Inux developers often use Python to do small chunks of work because you can write scripts easily. It has become a popular way to accomplish small tasks such as configuration and deployment. Windows Azure, Microsoft's cloud, is no different. Python becomes a prime citizen of Windows Azure through the availability provided by the Python SDK. Let's see how we can use Python without the other and just a Windows Azure subscription you can programmatically deploy an image from Vmdepot to Windows Azure.
Establish a management certificate

Any interaction with Windows Azure requires two things:

Let's say you use Linux to run this script (if not, please contact me and I'll show you how to do the same thing with Windows). If OpenSSL is not installed, 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. 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.

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

Select Settings tab:

Select a management certificate from the menu:

The utility bar below contains an upload button:

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

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


To help explain this article, I've 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 fully automated deployments.

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 useful for what we are going to do next. As you can see, this requires a certificate and a subscription ID as an argument.
Construct a virtual machine

A virtual machine image is a template that we can use to build a virtual machine. In this case, we'll use a CentOS image, which is copied from a location called Vmdepot held by Microsoft's wholly owned subsidiary, MS Open Tech.


We can check whether we have previously 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 ' one listed, which should be the most for
  I-in stable Ist_os_images ():
   if name in I.name: return
    True
  

If not, we can go on with our work process.

The following shows a concise process for creating a storage account (requiring 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 all over the world and a bunch of them are being built. When the storage account is created, it allows the maximum of terabytes of BLOB data to be stored and is 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 that allows us to store the images we replicate.

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 replicate blobs from remote locations. This is done by using an API, called Copyblob, 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 replicated from a remote location. The great of this API is that you can use it to copy any HTTP endpoint from inside or outside Windows Azure and use it without any cost. The disadvantage is that it has no SLA (translator note: Service-level Agreement, Service level agreement, is a contract between network service provider and 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 a common endpoint for the virtual machine, then set a public endpoint to forward to the virtual machine's port so that you can access them via SSH. The script is written so that if you choose the same cloud service each time, it will increment from Port 22 to add another virtual machine that is ready to give SSH access to the open port as a cloud service.


We are copying the image from the vmdepot that contains the image. Through it, I am copying and registering the 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 associated blob in the storage account, each with a virtual hard disk (. vhd).

index =-1
blob_exists = True while
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 services.

All of these are 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

Depending on the instructions in README.MD to enable the script, you are ready to start.

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

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

The music!

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.