Azure CLI的版本問題

衡子發表於2016-05-27

Azure支援多種管理方法。命令列方法有:

  • PowerShell,PowerShell只能執行在Windows上
  • Azure CLI,而Azure CLI可以執行在Windows、MAC以及Linux上

如果能夠熟悉Azure CLI,在各種平臺的命令格式都相同。

在Windows上和MAC上安裝Azure CLI只要下載安裝軟體包就可以了,但在Linux上安裝,需要安裝NodeJS和NPM,然後用npm安裝Azure-cli。安裝過程和方法在前面都介紹過,請參考:

http://www.cnblogs.com/hengwei/p/5183493.html

由於Azure China已經支援ARM模式,但在用Azure CLI使用ARM的時候,會遇到多種報錯資訊。本文將介紹目前如何使用Azure CLI管理Azure China的ARM模式。

1. NodeJS版本

在Ubuntu的Linux下,安裝Azure CLI的方式是:apt-get install node; apt-get install npm; npm install -g azure-cli

其中apt-get的source list請參考:

http://wiki.ubuntu.org.cn/%E6%A8%A1%E6%9D%BF:14.04source

在CentOS的Linux下,安裝Azure CLI的方式是:yum install nodejs; apt-get install npm; npm install -g azure-cli

Epel的repo請參考:

http://www.cnblogs.com/hengwei/p/5183493.html

npm的國內源請參考:

http://www.cnblogs.com/hengwei/p/5183493.html

安裝完成後,就可以使用Azure的CLI了。

但在這種模式下,切換到ARM時,會有報錯:

[root@hwarmvm01 ~]# azure login -e AzureChinaCloud -u admin@xxxx.partner.onmschina.cn
info:    Executing command login
Password: ********
+ Authenticating...                                                            
error:   CERT_UNTRUSTED
info:    Error information has been recorded to /root/.azure/azure.err
error:   login command failed

[root@hwarmvm01 ~]# cat /root/.azure/azure.err 
2016-05-27T08:12:11.308Z:
{ [Error: CERT_UNTRUSTED]
  stack: [Getter/Setter],
  __frame: 
   { name: '__1',
     line: 49,
     file: '/usr/lib/node_modules/azure-cli/lib/commands/login.js',
     prev: undefined,
     calls: 1,
     active: false,
     offset: 64,
     col: 27 },
  rawStack: [Getter] }
Error: CERT_UNTRUSTED
  <<< async stack >>>
  at __1 (/usr/lib/node_modules/azure-cli/lib/commands/login.js:113:28)
  <<< raw stack >>>
    at SecurePair.<anonymous> (tls.js:1367:32)
    at SecurePair.emit (events.js:92:17)
    at SecurePair.maybeInitFinished (tls.js:979:10)
    at CleartextStream.read [as _read] (tls.js:471:13)
    at CleartextStream.Readable.read (_stream_readable.js:340:10)
    at EncryptedStream.write [as _write] (tls.js:368:25)
    at doWrite (_stream_writable.js:225:10)
    at writeOrBuffer (_stream_writable.js:215:5)
    at EncryptedStream.Writable.write (_stream_writable.js:182:11)
at write (_stream_readable.js:601:24)

這個報錯是由於nodejs的版本太低造成的:

root@hwubuntu02:~# node -v
v0.10.25

需要升級到4.x的版本。升級步驟如下文:

https://nodejs.org/en/download/package-manager/#installing-node-js-via-package-manager

我採用的是CentOS的機器,具體方法如下:

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum install -y nodejs

安裝完成後,檢視nodejs的版本:

[root@hwarmvm01 ~]# node -v
v4.4.5

再執行登陸的命令:

[root@hwarmvm01 ~]# azure login -e AzureChinaCloud -u admin@xxxx.partner.onmschina.cn
info:    Executing command login
Password: ********
|info:    Added subscription xxxx1                                          
info:    Added subscription xxxx2
info:    Setting subscription "xxxx1" as default
+
info:    login command OK
[root@hwarmvm01 ~]# azure account set xxxx1
info:    Executing command account set
info:    Setting subscription to "xxxx1" with id "xxxxxxxx".
info:    Changes saved
info:    account set command OK

2. Azure-cli的版本

登陸後,執行ARM的命令:

[root@hwarmvm01 ~]# azure vm list
info:    Executing command vm list
+ Getting virtual machines                                                     
error:   The resource type 'virtualMachines' could not be found in the namespace 'Microsoft.Compute' for api version '2016-03-30'. The supported api-versions are '2015-05-01-preview,2015-06-15'.
info:    Error information has been recorded to /root/.azure/azure.err
error:   vm list command failed

仍然有報錯,此時的報錯是說api的版本不匹配。這個原因是Azure China的ARM版本和Global的不同。用npm安裝的版本是0.10.0:

[root@hwarmvm01 ~]# azure
info: _ _____ _ ___ ___
info: /_\ |_ / | | | _ \ __|
info: _ ___/ _ \__/ /| |_| | / _|___ _ _
info: (___ /_/ \_\/___|\___/|_|_\___| _____)
info: (_______ _ _) _ ______ _)_ _
info: (______________ _ ) (___ _ _)
info:
info: Microsoft Azure: Microsoft's Cloud Platform
info:
info: Tool version 0.10.0

但目前Azure China要求的版本是0.9.x,比如0.9.18:

https://github.com/Azure/azure-xplat-cli/releases/tag/v0.9.18-hotfix

下載:

wget https://github.com/Azure/azure-xplat-cli/archive/v0.9.18-hotfix.tar.gz

先解除安裝原有Azure CLI:

cd /usr/local/lib
npm uninstall azure-cli

再安裝:

npm install -g /root/v0.9.18-hotfix.tar.gz

安裝完成後檢視Azure CLI的版本:

[root@hwarmvm01 ~]# azure
info: _ _____ _ ___ ___
info: /_\ |_ / | | | _ \ __|
info: _ ___/ _ \__/ /| |_| | / _|___ _ _
info: (___ /_/ \_\/___|\___/|_|_\___| _____)
info: (_______ _ _) _ ______ _)_ _
info: (______________ _ ) (___ _ _)
info:
info: Microsoft Azure: Microsoft's Cloud Platform
info:
info: Tool version 0.9.18

執行查詢VM的命令:

[root@hwarmvm01 ~]# azure vm list
info:    Executing command vm list
+ Getting virtual machines                                                     
data:    ResourceGroupName  Name       ProvisioningState  PowerState  Location   Size       
data:    -----------------  ---------  -----------------  ----------  ---------  -----------
data:    HWARM01            hwarmvm01  Succeeded          VM running  chinaeast  Standard_A1
data:    HWERTEST           hwarm01    Succeeded          VM running  chinaeast  Standard_A1
info:    vm list command OK

總結:

由於nodejs的版本和Azure-cli版本的問題,Azure China的ARM不能正常使用。正確的姿勢應該是:

1. 採用4.x的nodejs

2. 採用0.9.x的Azure-cli

為不走彎路,建議開始就安裝這兩個版本。

相關文章