2013-06-30 11 views
8

私のサイトの大部分を実行するGoogle App Engine Webアプリケーションがあります。しかし、特定の機能については、Linuxマシンが必要です。特定のイベントでGoogle App Engineアプリが自動的にGoogle Compute Instanceをスピンアップするようにしたいと思います。OAuth:Google App Engine内からGoogle Compute Instanceを起動する

Compute Engine REST APIを使用してGoogle Computeインスタンスを追加できるとのご理解をいただきました。ただし、Google Compute REST APIにアクセスするには、OAuth2認証プロセスを使用してアクセストークンを取得する必要があります。

Google App Engine内からプログラムトークンを取得するにはどうすればよいですか?

すべての認証方法には、ユーザー名とパスワードを入力できるようにするためのウィンドウが必要なようですが、これはGoogle App Engine内では実用的ではありません。

+0

:同じ)

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

AppAssertionCredentialsはusing this codeアクセストークンを処理します – voscausa

答えて

3

プロジェクトに関連付けられたサービスアカウントを使用して、Compute Engine APIを認証し、VMを起動することができます。

Documentation on service accountsは、次の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) 

私は、彼らがビデオ共有サイトを構築し、今年からGoogleのI/Oのデモが利用可能になる予定だったと思ったのだが、私はまだGitHubの上でそれを見ることはありません。 AppEngineを使用してGCEを制御するがありますが、そのほとんどはアプリの独自の資格情報ではなくユーザーのプロジェクトと資格情報を使用しているようです。

非常に大きな予算や何らかのレート制限がある場合を除いて、直接ユーザー入力でVMをスピンアップしたくないのは明らかですが、今すぐVMをスピンアップすると非常に便利ですあなたがするべき多くの計算があるとき。 (トランスコーディングなど)

4

サービスアカウントとApp Engineのcronタスクを使用して、しばらく実行したインスタンスを停止する完全な例は次のとおりです。開始インスタンスの反対ですが、認証コードは

サービスアカウントを使用することができます
# 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)