用命令行管理云端

[原文发表地址]  Managing the Cloud from the Command Line

[原文发表时间] 2012-06-20 18:29

几个星期前,我发表了一篇关于Windows Azure 云服务的博客。我正在挖掘新的东西,并且在Mac,PC和Linux(我更喜欢Ubuntu)上做着不同的尝试。作为很长一段时间的PowerShell命令行的粉丝,我一直在寻找在“文本模式”中处理事务的方法,以及脚本站点的创建和部署。

原来有一大堆用命令行访问Azure的方法-比我想象的还多。有一个基于JSON的Web API,这会让那些工具终止对话。如果你想的话,你当然可以直接调用那些API,但是命令行工具确实非常有趣。

你可以在Mac上安装Mac Azure SDK 安装程序来获得这些工具以及更多,或者,如果你在Windows或Mac或Linux上安装node.js,你可以用Node Page Manager(npm)来像这样安装Azure:

npm install azure --global

你也可以用apt-get或者其他存储命令。在这之后,你可以运行“azure”,这能以非常直观的方式为你提供链接在一起的命令。“azure topic(名词)动词选项”或者“azure 站点列表”或者“azure 虚拟硬盘创建”,如下:

Azure command line format "topic verb options"

甚至有ASCII码 的艺术,谁能不喜欢它呢。

认真的说,虽然它是很老套的。这里有一个我刚刚做的交互的例子。我削减了一些无聊的东西,但是这是始于一个全新的没有安装工具的机器,并用我的Windows Azure 账户结束交互的。

 scott@hanselmac:~$ npm install azure
npm http GET https://registry.npmjs.org/azure
...bunch of GETS...
scott@hanselmac:~$ azure
info:            _    _____   _ ___ ___
info:           /_\  |_  / | | | _ \ __|
info:     _ ___/ _ \__/ /| |_| |   / _|___ _ _
info:   (___  /_/ \_\/___|\___/|_|_\___| _____)
info:      (_______ _ _)         _ ______ _)_ _ 
info:             (______________ _ )   (___ _ _)
info:   
info:   Windows Azure: Microsoft's Cloud Platform
info:   
info:   Tool version 0.6.0
...bunch of help stuff...

scott@hanselmac:~$ azure account download
info:   Executing command account download
info:   Launching browser to https://go.microsoft.com/fwlink/?LinkId=254432
help:   Save the downloaded file, then execute the command
help:     account import <file>
info:   account download command OK
scott@hanselmac:~$ cd ~
scott@hanselmac:~$ cd Downloads/
scott@hanselmac:~/Downloads$ ls
3-Month Free Trial.publishsettings
scott@hanselmac:~/Downloads$ azure account import 3-Month\ Free\ Trial.publishsettings 
info:   Executing command account import
info:   Setting service endpoint to: management.core.windows.net
info:   Setting service port to: 443
info:   Found subscription: 3-Month Free Trial
info:   Setting default subscription to: 3-Month Free Trial
warn:   Remember to delete it now that it has been imported.
info:   Account publish settings imported successfully
info:   account import command OK
scott@hanselmac:~/Downloads$ azure site list
info:   Executing command site list
+ Enumerating locations                                                        
+ Enumerating sites                                                            
data:   Name               State    Host names                                                   
data:   -----------------  -------  -------------------------------------------------------------
data:   superawesome       Running  superawesome.azurewebsites.net,www.acustomdomain.com
info:   site list command OK
scott@hanselmac:~/Downloads$ azure site list --json
[
  {
    "AdminEnabled": "true",
    "AvailabilityState": "Normal",
    "EnabledHostNames": [
      "superawesome.azurewebsites.net",
      "www.acustomdomain.com"
    ],
    "HostNames": [
      "ratchetandthegeek.azurewebsites.net",
      "www.ratchetandthegeek.com"
    "State": "Running",
    "UsageState": "Normal",
    "WebSpace": "eastuswebspace"
  }
]

这儿是我如何用命令行创建并启动一台虚拟机的。首先我会列出可用的镜像,我可以用那些镜像来开始,然后我就来创建它。我等待它准备就绪,然后就开始和准备远程连接(RDP,SSH,等等)。

 scott@hanselmac:~$ azure vm image list
info:   Executing command vm image list
+ Fetching VM images                                                           
data:   Name                                                                        Category   OS     
data:   --------------------------------------------------------------------------  ---------  -------
data:   CANONICAL__Canonical-Ubuntu-12-04-amd64-server-20120528.1.3-en-us-30GB.vhd  Canonical  Linux  
data:   MSFT__Windows-Server-2012-RC-June2012-en-us-30GB.vhd                        Microsoft  Windows
data:   MSFT__Sql-Server-11EVAL-11.0.2215.0-05152012-en-us-30GB.vhd                 Microsoft  Windows
data:   MSFT__Win2K8R2SP1-120514-1520-141205-01-en-us-30GB.vhd                      Microsoft  Windows
data:   OpenLogic__OpenLogic-CentOS-62-20120531-en-us-30GB.vhd                      OpenLogic  Linux  
data:   SUSE__openSUSE-12-1-20120603-en-us-30GB.vhd                                 SUSE       Linux  
data:   SUSE__SUSE-Linux-Enterprise-Server-11SP2-20120601-en-us-30GB.vhd            SUSE       Linux  
info:   vm image list command OK
scott@hanselmac:~$ azure vm create hanselvm MSFT__Windows-Server-2012-RC-June2012-en-us-30GB.vhd scott superpassword 
 --location "West US"
info:   Executing command vm create
+ Looking up image                                                             
+ Looking up cloud service                                                     
+ Creating cloud service                                                       
+ Retrieving storage accounts                                                  
+ Creating VM                                                                  
info:   vm create command OK
scott@hanselmac:~$ azure vm list
info:   Executing command vm list
+ Fetching VMs                                                                 
data:   DNS Name               VM Name   Status      
data:   ---------------------  --------  ------------
data:   hanselvm.cloudapp.net  hanselvm  Provisioning
info:   vm list command OK

 

这是给Mac,Linux和可选的Windows的命令行工具。(如果你安装node,并运行“npm install azure--global”)并且,还有Windows管理员使用的PowerShell命令。值得注意的是, 您可以签出所有代码,因为它们在github上的https://github.com/windowsazure 都是开源的。事实上,整个命令行应用程序是用JavaScript编写的。

正如管理工具的命令行版本有一个非常具体的和舒服的名词/动词/选项的风格, comdlets也非常有“PowerShelly”风格,并且会让使用PowerShell的人感到非常舒服。文档和工具是在预览的模式下,并且正在开发中,所以你在文档中会发现一些漏洞。

这些PowerShell命令可以一起工作,并且数据在它们之间传输。在这里创建了一个新的Azure 虚拟机配置,而它的机器名是从列表中选出的,然后config对象被传递到New-AzureVM中。

    1: C:\PS>New-AzureVMConfig -Name "MySUSEVM2" -InstanceSize ExtraSmall -ImageName (Get-AzureVMImage)[7].ImageName ` 
    2: | Add-AzureProvisioningConfig –Linux –LinuxUser $lxUser -Password $adminPassword `
    3: | New-AzureVM

接下来,我想找出如何用命令行启动一整个场网站,如何将一个应用程序部署到新的网络场,以及为场配置流量,然后频繁地载入测试,这些都使用命令行获得。这将会多么有趣!

赞助者:我要感谢这周在DevExpress赞助源的同事们。看看他们的DXperience工具,实在是太棒了。你能用ASP.NET和Web Forms创建一个基于web的iPad应用程序。我个人印象非常深刻。DevExpress 引入的DXperience 12.1-技术环境正在发生改变,并且新的平台正在兴起。新的DevExpress 工具在桌面上,在Web上或者跨越各种各样的触摸移动设备中提供下一代用户体验。