繁体   English   中英

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

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

我一直按照以下说明使用python在Azure上创建VM。 但是,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> 

令我感到困惑的部分是,我没有通过任何Web应用程序与API交互,而是在本地运行此python脚本,并且它(理想情况下)在启动后无需用户干预即可运行。 因此,我不确定如何修改任何Web配置文件以获取更有用的错误,除非它引用我的Azure帐户上的某个东西,在这种情况下,我不确定它所指的是什么。

我已经使用OAUTH2令牌身份验证以编程方式进行了身份验证,并且以前允许我使用相同的python脚本来创建/更新资源组,虚拟网络等(实际上,我会立即进行所有操作来创建VM)。 另外,在使用REST API创建资源组和虚拟网络时,它返回了有用的错误,详细说明了问题所在。 因此,在脚本部分中完全有可能出现错字,该错字会创建创建VM的请求,但没有更有用的错误,我不确定这是什么。

如果有人对如何获得更有用的错误或更好的方法有任何建议,我将永远感激不已

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

我相信我已经解决了这个问题。 在我使用的文档中,它将请求URI指定为:

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

但是,应为:

“ ... / providers / Microsoft.Compute / virtualMachines / {vm} api-version = {apiVersion}”,带问号而不是“&”号

我已经在代码中对其进行了更新,并且一切似乎都可以正常运行。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM