简体   繁体   English

尝试通过Azure REST API创建VM时出现运行时错误

[英]Runtime error while trying to create VM through Azure REST API

I've been following these instructions to create a VM on Azure with python. 我一直按照以下说明使用python在Azure上创建VM。 However Azure is returning an error: 但是,Azure返回错误:

 <!DOCTYPE html> <html> <head> <title>Runtime Error</title> <meta name=viewport content=width=device-width /> <style> body {font-family:Verdana;font-weight:normal;font-size: .7em;color:black;} p {font-family:Verdana;font-weight:normal;color:black;margin- b {font-family:Verdana;font-weight:bold;color:black;margin- H1 { font-family:Verdana;font-weight:normal;font-size:18pt;color:red } H2 { font-family:Verdana;font-weight:normal;font-size:14pt;color:maroon } pre {font-family:Consolas,Lucida Console,Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } @media screen and (max-width: 639px) { pre { width: 440px; white-space: pre-wrap; word-wrap: break-word; } } @media screen and (max-width: 479px) { pre { width: 280px; } } </style> </head> <body bgcolor=white> <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1> <h2> <i>Runtime Error</i> </h2></span> <font face=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif > <b> Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. <br><br> <b>Details:</b> To enable the details of this specific error message to be viewable on remote machines, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.<br><br> <table width=100% bgcolor=#ffffcc> <tr> <td> <code><pre> &lt;!-- Web.Config Configuration File --&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;customErrors mode=&quot;Off&quot;/&gt; &lt;/system.web&gt; &lt;/configuration&gt;</pre></code> </td> </tr> </table> <br> <b>Notes:</b> The current error page you are seeing can be replaced by a custom error page by modifying the &quot;defaultRedirect&quot; attribute of the application&#39;s &lt;customErrors&gt; configuration tag to point to a custom error page URL.<br><br> <table width=100% bgcolor=#ffffcc> <tr> <td> <code><pre> &lt;!-- Web.Config Configuration File --&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;customErrors mode=&quot;RemoteOnly&quot; defaultRedirect=&quot;mycustompage.htm&quot;/&gt; &lt;/system.web&gt; &lt;/configuration&gt;</pre></code> </td> </tr> </table> <br> </body> </html> 

The part that's confusing me is I'm not interacting with the API through any sort of web application, I'm running this python script locally, and it (ideally) runs with no user interaction once it starts. 令我感到困惑的部分是,我没有通过任何Web应用程序与API交互,而是在本地运行此python脚本,并且它(理想情况下)在启动后无需用户干预即可运行。 So I'm not sure how I'd modify any web config file to get a more useful error unless it's referring to something on my Azure account, and in that case, I'm not sure what it's referring to. 因此,我不确定如何修改任何Web配置文件以获取更有用的错误,除非它引用我的Azure帐户上的某个东西,在这种情况下,我不确定它所指的是什么。

I have authenticated programmatically using OAUTH2 token authentication, and that has previously allowed me to create/update resource groups, virtual networks, etc using the same python script (I actually do all of that immediately proceeding the creation of the VM). 我已经使用OAUTH2令牌身份验证以编程方式进行了身份验证,并且以前允许我使用相同的python脚本来创建/更新资源组,虚拟网络等(实际上,我会立即进行所有操作来创建VM)。 Also, while using the REST API to create resource groups and virtual networks, it returned useful errors detailing what went wrong. 另外,在使用REST API创建资源组和虚拟网络时,它返回了有用的错误,详细说明了问题所在。 So it's entirely possible I have a typo in the part of the script that creates the request to create a VM, but without a more useful error, I'm not sure what it is. 因此,在脚本部分中完全有可能出现错字,该错字会创建创建VM的请求,但没有更有用的错误,我不确定这是什么。

If anyone has any suggestions on how to get a more useful error, or a better way to do this, I would be eternally grateful 如果有人对如何获得更有用的错误或更好的方法有任何建议,我将永远感激不已

The relevant parts of the python script are: python脚本的相关部分包括:

def get_token(self):
    url = self._auth_url[:(self._auth_url.find('/'))]
    uri = self._auth_url[(self._auth_url.find('/')):]

    conn = httplib.HTTPSConnection( url, 443 )
    conn.request( 'POST', uri, body = "grant_type=client_credentials&client_id=%s&client_secret=%s&resource=https://management.azure.com/" % (self._app_id, self._app_secret), headers = { 'Content-Type' : 'application/x-www-form-urlencoded' } )
    resp = conn.getresponse()

    if not resp.status == 200:
        logger.error( resp.read() )
        raise Error( "Getting azure token failed with givein parameters. Check the _auth_url, _app_id, or _app_secret")
    self._connection = httplib.HTTPSConnection( "management.azure.com", 443 )
    return json.loads( resp.read() )[ "access_token" ]

def create_virtual_machine( self, resource_group_name, vm_name, os_type, role_size, disk_size, nic):
    body = self.generate_linux_virtual_machine_json( vm_name, role_size, disk_size, os_type, resource_group_name, nic )
    token = self.get_token()
    uri = "https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s&api-version=2016-08-30" % (self._subscription_id, resource_group_name, vm_name)

    self._connection.request( 'PUT', uri, body = body, headers = { 'Content-Type' : 'application/json', 'Authorization' : 'Bearer %s' % token } )
    resp = self._connection.getresponse()
    if not resp.status == 200:
        logger.error("status: %s, error: %s" % (resp.status, resp.read() ) )

def generate_linux_virtual_machine_json( self, vm_name, role_size, disk_size, os_type, resource_group_name, nic ):
    (publisher_name, publisher_offer, sku) = self.get_image_info( os_type )
    create_linux_virtual_machine_json = '''
    {
        "name": "%s",
        "location": "%s", 
        "properties":{
            "hardwareProfile": {
                "vmSize": "%s"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "%s",
                    "offer": "%s",
                    "sku": "%s",
                    "version": "latest"
                },
                "osDisk": {
                    "name": "osdisk",
                    "osType": "Linux",
                    "createOption": "fromImage",
                    "diskSizeGB": "%s",
                    "caching": "ReadWrite"
                }
            },
            "osProfile": {
                "computerName": "%s",
                "adminUsername": "********",
                "adminPassword": "********",
                "customData": "",
                "linuxConfiguration": {
                    "disablePasswordAuthentication": false
                }
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "%s",
                        "properties": {
                            "primary": true
                        } 
                    }
                ]
            }
        }
    }
    '''%(vm_name, self._location, role_size, publisher_name, publisher_offer, sku, disk_size, vm_name, nic)

    return create_linux_virtual_machine_json

I believe I figured out the issue. 我相信我已经解决了这个问题。 On the documentation I was using, it specified the request URI as: 在我使用的文档中,它将请求URI指定为:

".../providers/Microsoft.Compute/virtualMachines/{vm}&api-version={apiVersion}" “... /提供者/ Microsoft.Compute / virtualMachines / {VM}&API-版本= {apiVersion}”

However, it should be: 但是,应为:

".../providers/Microsoft.Compute/virtualMachines/{vm} ? api-version={apiVersion}" with a question mark rather than an ampersand “ ... / providers / Microsoft.Compute / virtualMachines / {vm} api-version = {apiVersion}”,带问号而不是“&”号

I've updated that in my code and everything seems to be working correctly. 我已经在代码中对其进行了更新,并且一切似乎都可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Rest API用于VM还原的resourceRestoreRequest语法是什么 - Azure Rest API What resourceRestoreRequest syntax for VM Restore Azure REST API授权失败错误 - Azure rest API AuthorizationFailed error Django 尝试运行服务器时出现运行时错误 - Django Runtime Error while trying to run server 尝试基本 tensorflow 代码时出现运行时错误 - Runtime error while trying basic tensorflow code Python InvalidHeader error when trying to send http requests to Azure REST API - Python InvalidHeader error when trying to send http requests to Azure REST API 尝试使用 Django Rest Framework 创建简单的 API - Trying to create simple API with Django Rest Framework osticket,通过REST API创建票证 - osticket, create ticket through REST API 获取 HTTP 错误 403 - 尝试通过 Azure 数据块访问集群时访问令牌无效 - Getting HTTP error 403 - invalid access token while trying to access cluster through Azure databricks 在python中使用azure REST api创建Runbook作为草稿我面临错误......! - while creating the runbook as draft using azure REST api in python i am facing the error…! 尝试使用python中的请求访问REST API时出现错误请求(400)错误 - Bad Request (400) error while trying to access REST API using requests in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM