尝试通过 azure api for python 在 azure 上创建 spot 实例时遇到 RDP 访问问题

问题描述 投票:0回答:0

我正在尝试通过 python 代码从 azure API 在 azure 上创建一个 spot 实例。以下代码成功地在 Azure 上创建了 spot 实例机器,但是当我尝试通过 RDP 连接时,API 中提供的凭据不起作用。

import secrets
from datetime import datetime

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient

from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import (
    VirtualMachine,
    HardwareProfile,
    StorageProfile,
    InstanceViewTypes,
    OSDisk,
    ManagedDiskParameters,
    DiskCreateOptionTypes,
    OSProfile,
    NetworkProfile,
    NetworkInterfaceReference,
    VirtualMachinePriorityTypes,
    VirtualMachineEvictionPolicyTypes,
    BillingProfile
)
from azure.mgmt.network.models import PublicIPAddress, IPAllocationMethod, NetworkSecurityGroup, SecurityRule
from constance import config

from instance.client_utils.ssh_client import SSHClient
from instance.models import VMInstance
from syntric_cr.settings import (
    AZURE_CLIENT_ID,
    AZURE_CLIENT_SECRET,
    AZURE_TENANT,
    AZURE_SUBSCRIPTION_ID,
    AZURE_CAPTURE_IMAGE,
    AZURE_RESOURCE_GROUP,
    AZURE_RDP_USERNAME,
    AZURE_LOCATION, AZURE_EXISTING_NIC_NAME, AZURE_EXISTING_NSG_NAME,
)


def create_spot_instance(self):
        current_timestamp = round(datetime.timestamp(datetime.now()))

        vm_name = f"spot-{current_timestamp}"

        vm_username = AZURE_RDP_USERNAME
        vm_password = secrets.token_urlsafe(13)
        vm_location = AZURE_LOCATION
        vm_image_id = self.get_image_id(AZURE_RESOURCE_GROUP, AZURE_CAPTURE_IMAGE)
        new_nic = self.creating_nic_and_ip_from_existing_vm(vm_name, vm_location)

        vm_params = VirtualMachine(
            location=vm_location,
            hardware_profile=HardwareProfile(vm_size=config.AZURE_VIRTUAL_MACHINE_SIZE),
            storage_profile=StorageProfile(
                image_reference={'id': vm_image_id},
                os_disk=OSDisk(
                    name=vm_name,
                    create_option=DiskCreateOptionTypes.from_image,
                    managed_disk=ManagedDiskParameters(storage_account_type='Standard_LRS')
                )
            ),
            os_profile=OSProfile(
                computer_name=vm_name,
                admin_username='hello',
                admin_password='hello_123'
            ),
            network_profile=NetworkProfile(
                network_interfaces=[
                    NetworkInterfaceReference(id=new_nic.id)
                ]
            ),
            priority=VirtualMachinePriorityTypes.spot,
            eviction_policy=VirtualMachineEvictionPolicyTypes.deallocate,
            billing_profile=BillingProfile(max_price=config.AZURE_MAX_SPOT_INSTANCE_BUDGET)
        )

        try:
            self.service.virtual_machines.begin_create_or_update(AZURE_RESOURCE_GROUP, vm_name, vm_params)
            return VMInstance.objects.create(
                instance=vm_name, project=AZURE_RESOURCE_GROUP, provider='AZURE', status=False,
                script_run_on_start=False, username=vm_username, password=vm_password)
        except Exception as ex:
            return


创建 Spot 实例时,用户名“hello”和密码“hello_123”不适用于 RDP 访问。然后我必须手动重置密码才能使其工作

我尝试了上面的代码并从官方 Azure 文档中复制了这个

python azure azure-web-app-service azure-api-apps
© www.soinside.com 2019 - 2024. All rights reserved.