Heim  >  Artikel  >  Backend-Entwicklung  >  使用Python脚本来控制Windows Azure的简单教程

使用Python脚本来控制Windows Azure的简单教程

WBOY
WBOYOriginal
2016-06-10 15:14:581329Durchsuche

inux开发人员经常使用 Python 完成小块的工作,因为你可以编写脚本的情况很容易。它已经成为完成配置和部署等小任务的一个流行方式。Windows Azure,微软的云,也没有什么不同。通过 Python SDK 所提供的可用性,Python 成为 Windows Azure 的头等公民。让我们看看我们如何能够使用Python无需其它而只需一个Windows Azure订阅就可以用编程方式从 vmdepot 部署一个映像到 Windows Azure上。
建立一个管理证书

任何与 Windows Azure 的交互都需要两个东西:

我们假设你使用 Linux 运行这个脚本(如果不是,请和我联系,我会告诉你如何使用 Windows 来做同样的事情)。 如果没有安装OpenSSL,请从root提示使用如下命令:

yum install openssl

以下将创建一个 .pem 文件,之后可被翻译成一个 .cer 文件,并导出和上传到Windows Azure。

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

用下面命令导出 .cer:

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

这样你就得到它了,一个可以上传到你的 Windows Azure 订阅的管理证书。当做完这个时,你应该已经能够以编程方式使用 Windows Azure 了。

用你的微软帐户或者 Windows Azure 活动目录凭据登录到 Windows Azure。管理门户位于https://manage.windowsazure.com 。

选择设置标签:

2015416150149846.png (800×600)

从菜单中选择管理证书:

2015416150220027.png (886×169)

下方的应用工具条包含一个上传按钮:

2015416150255515.png (1561×80)

选择这个按钮,上传前面导出的 .cer 文件:

2015416150332525.png (677×480)

在“结果”面板中你应该会看到类似这样的证书入口。

2015416150413342.png (1219×49)
为了有助于解释这篇文章,我已经写了一个Python脚本,可在这里下载:

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


你可以阅读安装说明获知如何使用脚本。本文的目的是带你领略 Windows Azure 的 Python API 的一些能够帮你开发完全自动化部署的关键功能。

要使用 Windows Azure 中的任何服务管理功能,我们需要一个服务管理对象:
 

self.sms = ServiceManagementService(vars.subscription_id, vars.certificate_path)

这对我们接下来要做的很有用。正如你能看到的,这需要一个证书和订阅ID作为参数。
构造一个虚拟机

虚拟机映像是一个模板,我们可以用它建立虚拟机。在本例中,我们将使用一个 CentOS 映像,它是从一个由微软的全资子公司 MS Open Tech 持有的称为 vmdepot 的位置拷贝过来的。


我们可以通过列出我们的订阅中所有命名的映像来检查我们是否之前已经复制过指定的映像和注册过现存的。
 

def _image_by_name(self, name):
  # return the first one listed, which should be the most stable
  for i in self.sms.list_os_images():
   if name in i.name:
    return True
  return False

如果没有,我们可以继续我们的工作流程了。

下面演示了一个创建一个存储帐户(需要一个名称和位置)的简洁过程。因为我在伦敦,所以我会使用“北欧”数据中心(位于都柏林),但在世界各地有超过10个数据中心而且还有一堆正在建设。当存储帐户创建完,它允许最多 200 TB 的blob数据被存储,并由2512位的AES保护,可以用它来访问帐户。存储数据的逻辑单元被称为是一个容器,所以我们需要创建这样的一个容器来让我们存储我们复制的映像。
 

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()

我们现在应该能够从远程位置复制blob。这是通过使用 Windows Azure 提供的一个被称为 copyblob 的 API 完成的。实现代码如下:
 

 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)


你可以看到,这是一个异步方法,允许从远程位置复制任何 blob。这个 API 的伟大是,你可以用它来从 Windows Azure 的内部或外部复制任何 HTTP 端点,并且使用它没有任何成本。缺点是,它没有 SLA(译者注:Service-Level Agreement的缩写,服务等级协议,是网络服务供应商和客户间的合同)。

然后 blob 就可以在你的 Windows Azure 订阅中注册为一个映像,你可以使用这个来创建多个虚拟机。

self.sms.add_os_image(label=Constants.image_name, media_link=storageimage_uri, name=Constants.image_name, os='Linux')

这个脚本将创建一个“云服务”的包含虚拟机的公共端点,然后设置一个公共端点转发到虚拟机的端口,这样你就可以通过 SSH 进入他们。脚本是这样写的,如果你每次选择相同的云服务,它将从端口22向上递增来添加另一个准备给SSH进入的开放端口来作为云服务的虚拟机。


我们正在从含有映像的 vmdepot 复制映像。通过它,我正在我的订阅中复制和注册 CentOS 迷你映像。

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

你可以从这个地址浏览 vmdepot:

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

最后,我们将使用一个非常简单的算法来确定虚拟机已经部署到云服务上,即通过查看存储账户中相关的blob,每个虚拟机都有一个虚拟硬盘(.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)

结果是,我们可以为我们的云服务添加多个虚拟机。

以上都是从 Setup.py 文件完成的。你可以在下面地址的文件中看到上面所有代码:

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

根据 readme.md 中的指示启用脚本,你就可以准备开始了。

你可以在下面地址克隆 Windows Azure 的 Python SDK: :

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

乐之!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn