從 Python 使用服務管理

警告

這篇文章參考 CentOS,這是接近生命週期結束 (EOL) 狀態的 Linux 發行版本。 請據以考慮您的使用方式和規劃。 如需詳細資訊,請參閱 CentOS 生命週期結束指引

重要

針對新客戶目前已取代 Azure 雲端服務 (傳統),而針對所有客戶,該服務將從 2024 年 8 月 31 日起完全淘汰。 新部署應該使用 Azure Resource Manager 型的新部署模型 Azure 雲端服務 (延伸支援)

本指南說明如何以程序設計方式從 Python 執行一般服務管理工作。 Azure SDK for Python 中的 ServiceManagementService 類別支援以程式設計方式存取 Azure 入口網站提供的大部分服務管理功能。 您可以使用這項功能來建立、更新和刪除雲端服務、部署、數據管理服務和虛擬機。 這項功能對於建置需要以程序設計方式存取服務管理的應用程式很有用。

什麼是服務管理?

Azure 服務管理 API 可讓您以程式設計方式存取透過 Azure 入口網站 取得的大部分服務管理功能。 您可以使用適用於 Python 的 Azure SDK 來管理雲端服務和記憶體帳戶。

若要使用服務管理 API,您必須 建立 Azure 帳戶

概念

適用於 Python 的 Azure SDK 會 包裝服務管理 API,這是 REST API。 所有 API 作業都是透過 TLS 執行,並使用 X.509 v3 憑證相互驗證。 您可以從在 Azure 中執行的服務記憶體取管理服務。 它也可以直接從因特網存取任何可以傳送 HTTPS 要求並接收 HTTPS 回應的應用程式。

安裝

本文所述的所有功能都可在套件中使用 azure-servicemanagement-legacy ,您可以使用 pip 進行安裝。 如需安裝的詳細資訊(例如,如果您不熟悉 Python),請參閱 安裝 Python 和 Azure SDK

服務管理 連線

若要連線到服務管理端點,您需要 Azure 訂用帳戶標識碼和有效的管理憑證。 您可以透過 Azure 入口網站 取得訂用帳戶標識碼。

注意

您現在可以在 Windows 上執行時,使用以 OpenSSL 建立的憑證。 需要 Python 2.7.4 或更新版本。 建議您使用 OpenSSL 而非 .pfx,因為未來可能會移除 .pfx 憑證的支援。

Windows/Mac/Linux 上的管理憑證 (OpenSSL)

您可以使用 OpenSSL 來建立管理憑證。 您必須建立兩個憑證,一個用於伺服器(檔案 .cer ),另一個用於用戶端(檔案 .pem )。 若要建立 .pem 檔案,請執行:

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

若要建立 .cer 憑證,請執行:

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

如需 Azure 憑證的詳細資訊,請參閱 Azure 雲端服務 的憑證概觀。 如需 OpenSSL 參數的完整描述,請參閱 上的 https://www.openssl.org/docs/manmaster/man1/req.html檔。

建立這些檔案之後,請將 .cer 檔案上傳至 Azure。 在 [Azure 入口網站] 的 [設定] 索引卷標上,選取 [上傳]。 請注意,您儲存盤案的位置 .pem

取得訂用帳戶標識符之後,請建立憑證,並將檔案上傳 .cer 至 Azure,連線到 Azure 管理端點。 連線,方法是將訂用帳戶標識碼和檔案的路徑.pem傳遞至 ServiceManagementService

from azure import *
from azure.servicemanagement import *

subscription_id = '<your_subscription_id>'
certificate_path = '<path_to_.pem_certificate>'

sms = ServiceManagementService(subscription_id, certificate_path)

在上述範例中, smsServiceManagementService 物件。 ServiceManagementService 類別是用來管理 Azure 服務的主要類別。

Windows 上的管理憑證 (MakeCert)

您可以使用 在電腦上 makecert.exe建立自我簽署的管理憑證。 以系統管理員分開啟 Visual Studio 命令提示字元,並使用下列命令,以您想要使用的憑證名稱取代 AzureCertificate

makecert -sky exchange -r -n "CN=AzureCertificate" -pe -a sha1 -len 2048 -ss My "AzureCertificate.cer"

命令會建立檔案,.cer並將它安裝在個人證書存儲中。 如需詳細資訊,請參閱 Azure 雲端服務 的憑證概觀。

建立憑證之後,請將 .cer 檔案上傳至 Azure。 在 [Azure 入口網站] 的 [設定] 索引卷標上,選取 [上傳]。

取得訂用帳戶標識符之後,請建立憑證,並將檔案上傳 .cer 至 Azure,連線到 Azure 管理端點。 連線,方法是傳遞訂用帳戶標識碼和憑證在您中的位置將個人證書存儲取代為 ServiceManagementService(再次將 AzureCertificate 取代為您的憑證名稱)。

from azure import *
from azure.servicemanagement import *

subscription_id = '<your_subscription_id>'
certificate_path = 'CURRENT_USER\\my\\AzureCertificate'

sms = ServiceManagementService(subscription_id, certificate_path)

在上述範例中, smsServiceManagementService 物件。 ServiceManagementService 類別是用來管理 Azure 服務的主要類別。

列出可用的位置

若要列出裝載服務可用的位置,請使用 list_locations 方法。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

result = sms.list_locations()
for location in result:
    print(location.name)

當您建立雲端服務或記憶體服務時,必須提供有效的位置。 list_locations方法一律會傳回目前可用位置的最新清單。 截至本文所述,可用的位置如下:

  • 西歐
  • 北歐
  • 東南亞
  • 東亞
  • 美國中部
  • 美國中北部
  • 美國中南部
  • 美國西部
  • 美國東部
  • 日本東部
  • 日本西部
  • 巴西南部
  • 澳大利亞東部
  • 澳大利亞東南部

建立雲端服務

當您在 Azure 中建立應用程式並加以執行時,程式代碼和組態會一起稱為 Azure 雲端服務。 (在舊版 Azure 中,它被稱為 託管服務 。您可以使用 create_hosted_service 方法來建立新的託管服務。 藉由提供託管服務名稱(在 Azure 中必須是唯一的)、標籤(自動編碼為base64)、描述和位置來建立服務。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

name = 'myhostedservice'
label = 'myhostedservice'
desc = 'my hosted service'
location = 'West US'

sms.create_hosted_service(name, label, desc, location)

您可以使用 list_hosted_services 方法來列出訂用帳戶的所有託管服務。

result = sms.list_hosted_services()

for hosted_service in result:
    print('Service name: ' + hosted_service.service_name)
    print('Management URL: ' + hosted_service.url)
    print('Location: ' + hosted_service.hosted_service_properties.location)
    print('')

若要取得特定託管服務的相關信息,請將託管服務名稱傳遞至 get_hosted_service_properties 方法。

hosted_service = sms.get_hosted_service_properties('myhostedservice')

print('Service name: ' + hosted_service.service_name)
print('Management URL: ' + hosted_service.url)
print('Location: ' + hosted_service.hosted_service_properties.location)

建立雲端服務之後,請使用 create_deployment 方法將程式代碼部署至服務。

刪除雲端服務

您可以將服務名稱傳遞至 delete_hosted_service 方法,以刪除雲端服務。

sms.delete_hosted_service('myhostedservice')

您必須先刪除服務的所有部署,才能刪除服務。 如需詳細資訊,請參閱 刪除部署

刪除部署

若要刪除部署,請使用 delete_deployment 方法。 下列範例示範如何刪除名為 v1的部署:

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

sms.delete_deployment('myhostedservice', 'v1')

建立記憶體服務

記憶體服務可讓您存取 Azure Blob數據表佇列。 若要建立記憶體服務,您需要服務的名稱(介於 3 到 24 個小寫字元之間,且在 Azure 中是唯一的)。 您也需要描述、標籤(最多 100 個字元、自動編碼為base64),以及位置。 下列範例示範如何藉由指定位置來建立記憶體服務:

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

name = 'mystorageaccount'
label = 'mystorageaccount'
location = 'West US'
desc = 'My storage account description.'

result = sms.create_storage_account(name, desc, label, location=location)

operation_result = sms.get_operation_status(result.request_id)
print('Operation status: ' + operation_result.status)

在上述範例中,create_storage_account作業的狀態可以藉由將create_storage_account回的結果傳遞給get_operation_status方法來擷取。

您可以使用 list_storage_accounts 方法來列出記憶體帳戶及其屬性

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

result = sms.list_storage_accounts()
for account in result:
    print('Service name: ' + account.service_name)
    print('Location: ' + account.storage_service_properties.location)
    print('')

刪除記憶體服務

若要刪除記憶體服務,請將記憶體服務名稱傳遞至 delete_storage_account 方法。 刪除記憶體服務會刪除儲存在服務中的所有數據(Blob、數據表和佇列)。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

sms.delete_storage_account('mystorageaccount')

列出可用的作業系統

若要列出可用於裝載服務的操作系統,請使用 list_operating_systems 方法。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

result = sms.list_operating_systems()

for os in result:
    print('OS: ' + os.label)
    print('Family: ' + os.family_label)
    print('Active: ' + str(os.is_active))

或者,您可以使用 list_operating_system_families 方法,依系列將操作系統分組。

result = sms.list_operating_system_families()

for family in result:
    print('Family: ' + family.label)
    for os in family.operating_systems:
        if os.is_active:
            print('OS: ' + os.label)
            print('Version: ' + os.version)
    print('')

建立作業系統映像

若要將操作系統映像新增至映像存放庫,請使用 add_os_image 方法。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

name = 'mycentos'
label = 'mycentos'
os = 'Linux' # Linux or Windows
media_link = 'url_to_storage_blob_for_source_image_vhd'

result = sms.add_os_image(label, media_link, name, os)

operation_result = sms.get_operation_status(result.request_id)
print('Operation status: ' + operation_result.status)

若要列出可用的作業系統映像,請使用 list_os_images 方法。 其中包含所有平臺映像和使用者映像。

result = sms.list_os_images()

for image in result:
    print('Name: ' + image.name)
    print('Label: ' + image.label)
    print('OS: ' + image.os)
    print('Category: ' + image.category)
    print('Description: ' + image.description)
    print('Location: ' + image.location)
    print('Media link: ' + image.media_link)
    print('')

刪除作業系統映像

若要刪除使用者映像,請使用 delete_os_image 方法。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

result = sms.delete_os_image('mycentos')

operation_result = sms.get_operation_status(result.request_id)
print('Operation status: ' + operation_result.status)

建立虛擬機

若要建立虛擬機,您必須先建立 雲端服務。 然後使用 create_virtual_machine_deployment 方法來建立虛擬機部署

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

name = 'myvm'
location = 'West US'

#Set the location
sms.create_hosted_service(service_name=name,
    label=name,
    location=location)

# Name of an os image as returned by list_os_images
image_name = 'OpenLogic__OpenLogic-CentOS-62-20120531-en-us-30GB.vhd'

# Destination storage account container/blob where the VM disk
# will be created
media_link = 'url_to_target_storage_blob_for_vm_hd'

# Linux VM configuration, you can use WindowsConfigurationSet
# for a Windows VM instead
linux_config = LinuxConfigurationSet('myhostname', 'myuser', 'mypassword', True)

os_hd = OSVirtualHardDisk(image_name, media_link)

sms.create_virtual_machine_deployment(service_name=name,
    deployment_name=name,
    deployment_slot='production',
    label=name,
    role_name=name,
    system_config=linux_config,
    os_virtual_hard_disk=os_hd,
    role_size='Small')

刪除虛擬機

若要刪除虛擬機,您必須先使用 delete_deployment 方法來刪除部署。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

sms.delete_deployment(service_name='myvm',
    deployment_name='myvm')

接著可以使用 delete_hosted_service 方法來刪除雲端服務。

sms.delete_hosted_service(service_name='myvm')

從擷取的虛擬機映像建立虛擬機

若要擷取 VM 映像,您必須先呼叫 capture_vm_image 方法。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

# replace the below three parameters with actual values
hosted_service_name = 'hs1'
deployment_name = 'dep1'
vm_name = 'vm1'

image_name = vm_name + 'image'
image = CaptureRoleAsVMImage    ('Specialized',
    image_name,
    image_name + 'label',
    image_name + 'description',
    'english',
    'mygroup')

result = sms.capture_vm_image(
        hosted_service_name,
        deployment_name,
        vm_name,
        image
    )

若要確定您已成功擷取映像,請使用 list_vm_images API。 請確定您的影像顯示在結果中。

images = sms.list_vm_images()

若要最後使用擷取的映像來建立虛擬機,請使用 create_virtual_machine_deployment 方法,但這次會改為傳入vm_image_name。

from azure import *
from azure.servicemanagement import *

sms = ServiceManagementService(subscription_id, certificate_path)

name = 'myvm'
location = 'West US'

#Set the location
sms.create_hosted_service(service_name=name,
    label=name,
    location=location)

sms.create_virtual_machine_deployment(service_name=name,
    deployment_name=name,
    deployment_slot='production',
    label=name,
    role_name=name,
    system_config=linux_config,
    os_virtual_hard_disk=None,
    role_size='Small',
    vm_image_name = image_name)

若要深入瞭解如何在傳統部署模型中擷取Linux虛擬機,請參閱 擷取Linux虛擬機

若要深入瞭解如何在傳統部署模型中擷取 Windows 虛擬機,請參閱 擷取 Windows 虛擬機

後續步驟

既然您已瞭解服務管理的基本概念,您可以存取 Azure Python SDK 的完整 API 參考檔,並輕鬆地執行複雜的工作來管理 Python 應用程式。

如需詳細資訊,請參閱 Python 開發人員中心