簡體   English   中英

OAuth:從Google App Engine中啟動Google Compute實例

[英]OAuth: Starting a Google Compute Instance from within Google App Engine

我有一個運行我網站大部分內容的Google App Engine網絡應用。 但是,對於某些功能,我需要一台linux機器。 我希望我的Google App Engine應用能夠在某些事件中自動啟動Google Compute Instance。

我了解您可以使用Compute Engine REST API添加Google Compute實例。 但是,要訪問Google Compute REST API,您需要使用OAuth2身份驗證過程獲取訪問令牌。

如何以編程方式從Google App Engine中獲取訪問令牌?

似乎所有身份驗證方法都需要顯示一個窗口,因此您可以輸入用戶名和密碼,這在Google App Engine中是不切實際的。

以下是使用服務帳戶和App Engine cron任務在運行一段時間后停止實例的完整示例:(與啟動實例相反,但授權代碼相同)

https://github.com/GoogleCloudPlatform/compute-appengine-timeout-python

AppAssertionCredentials 使用以下代碼處理訪問令牌:

# Obtain App Engine AppAssertion credentials and authorize HTTP connection.
# https://developers.google.com/appengine/docs/python/appidentity/overview
credentials = AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/compute')
HTTP = credentials.authorize(httplib2.Http(memcache))

# Build object for the 'v1beta15' version of the GCE API.
# https://developers.google.com/compute/docs/reference/v1beta13/
compute = build('compute', 'v1beta15', http=HTTP)

您應該能夠使用與項目關聯的服務帳戶對Compute Engine API進行身份驗證並啟動VM。

服務帳戶的文檔建議以下python代碼應該獲取服務帳戶令牌。

import httplib2

import discovery
from oauth2client.appengine import AppAssertionCredentials
...
credentials = AppAssertionCredentials(
    scope='https://www.googleapis.com/auth/compute')
auth_http = credentials.authorize(httplib2.Http())
compute_service = discovery.build('compute', 'v1beta15', http=auth_http)

我認為今年他們建立視頻共享網站的谷歌I / O演示將會可用,但我還沒有在GitHub上看到它。 許多演示使用AppEngine來控制GCE,但是大多數演示似乎都使用用戶的項目和憑據,而不是應用程序自己的憑據。

顯然,您可能不希望在直接用戶輸入上啟動虛擬機,除非您有一個非常大的預算或某種形式的速率限制,但是現在啟動虛擬機非常有幫助。我有很多計算要做。 (轉碼等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM