【问题标题】:GAE Running Python script and GCE permissionsGAE 运行 Python 脚本和 GCE 权限
【发布时间】:2015-12-22 04:37:53
【问题描述】:
  1. GAE 是否需要某种 WSGI (https://cloud.google.com/appengine/docs/python/tools/webapp/running)?这像 HTTPD 的 CGI 配置吗? 即,在app.yaml 中,我必须拥有script.app 并将app 引用到一个wsgi/webapp 对象?

  2. 尝试使用 AppAssertionCredentials 从 GAE 向 GCE 进行身份验证。 我已经制作了这个 sn-p 工作的另一个脚本:

credentials = AppAssertionCredentials( scope='https://www.googleapis.com/auth/compute') auth_http = credentials.authorize(httplib2.Http()) compute = discovery.build('compute', 'v1', http=auth_http)

我现在要做的是使用 REST API 从 GAE 创建 GCE 快照。 我不明白如何为我的POST 引用compute 对象,以使身份验证正常工作(现在未授权)。

这是我的脚本(由于测试,imports 太多):

import requests
import urllib2
import logging
import sys
import argparse
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
from oauth2client.tools import run_flow
from oauth2client.appengine import AppAssertionCredentials
from google.appengine.api import memcache
import datetime
import httplib2
import json
import logging
from pprint import pformat
from apiclient import discovery
from google.appengine.api import memcache
from oauth2client.appengine import AppAssertionCredentials
import cgi
from google.appengine.api import users
import urllib
from google.appengine.api import users
from google.appengine.ext import ndb
import time

PROJECT = "testprojgce"
ZONE = "europe-west1-b"


### OAuth2

credentials = AppAssertionCredentials(
scope='https://www.googleapis.com/auth/compute')
auth_http = credentials.authorize(httplib2.Http())
compute = discovery.build('compute', 'v1', http=auth_http)


# Create snapshot
createsnapurl= "https://www.googleapis.com/compute/v1/projects/"+PROJECT+"/zones/"+ZONE+"/disks/testdisk1/createSnapshot"
req=requests.post(createsnapurl)  

【问题讨论】:

  • 请坚持“每个问题一个问题”——这是非常基本的 StackOverflow 礼仪。只关注问题 1,是的:GAE 的 Web 服务器的 2.7 Python 运行时通过您的 WSGI 应用程序连接到您的代码(app.yaml 可以分派到其中的一个或多个),就像任何其他 Web 服务器一样——任何任何您选择的 Python 框架很容易实现这些(我为您的用例推荐轻量级框架,例如 falcon、flask、bottle 或 webapp2,而不是像 django 或 web2py 这样丰富而重的成熟框架——但是,当然,这是您的选择)。
  • 对是否以“这是一个 2 部分问题”或类似内容打开有疑问。很抱歉违反了 SO 礼仪。

标签: python google-app-engine google-compute-engine


【解决方案1】:

1) 据我所知,WSGI 对象实际上是必需的,因为它是应用程序与服务器环境通信的方式。这个对象很容易使用 Django、Flask、webapp2 或其他框架获得,所以应该不会太难获得。看看:

https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine

用于大量 GAE 配置示例。

2) 现在建议使用应用程序默认凭据,而不是 AppAssertion 凭据。

https://developers.google.com/identity/protocols/application-default-credentials

使用起来要容易得多,并且可以在 GAE、GCE(假设您创建具有正确范围的实例)、MVM 上无缝运行。它也可以在您的本地环境中使用,或者使用您从 gcloud init 获得的默认“用户”帐户,或者您可以将 GOOGLE_APPLICATION_CREDENTIALS 环境变量指向 JSON 服务帐户凭据。在本地,我通常建议下载 JSON 服务帐户并将该环境变量指向它,因为并非每个 API 都支持该用户帐户。

credentials = GoogleCredentials.get_application_default()
compute_service = discovery.build(
    'compute', 'v1', credentials=credentials)

请注意,对于大多数 API,您不需要指定范围,因为它是自动注入的,但如果您有缺少范围的问题,请尝试使用 'credentials.created_scoped' 方法。

最后,您几乎不需要使用请求之类的东西直接与 REST api 交互,这是客户端库的重点。它确保您的 http 请求得到正确授权,并且您拥有语言级别的方法而不是 URL 字符串。相反,请执行以下操作:

jsonBody = {
  #see request body section here for how to fill this in     #https://cloud.google.com/compute/docs/reference/latest/disks/createSnapshot
} 
compute_service.disks().createSnapshot(project=project, zone=zone, disk=diskName, body=jsonBody)

这可能不是确切的语法,如果你不能让它工作,请留下评论,我会尝试修复它。

【讨论】:

  • 比尔,非常感谢您的彻底回复。显然,客户端库方法是首选的做事方式,但我认为在这种情况下,它们是相当有限的(列表、添加、删除、启动、停止 - cloud.google.com/compute/docs/tutorials/python-guide#nextsteps)。我没有看到任何 disks 方法。我假设需要“低级”API (cloud.google.com/compute/docs/reference/latest/disks/…)。如果我错了,请纠正我。还是您的意思是我确实需要直接使用该低级 POST 调用,但只是不同?
  • @PyGAE,我遇到了类似的问题,但我还不确定。这可能是错误的发现文档被拉下或错误的 API 版本的问题。如果您不能使用它们进行 API 调用,那绝对是客户端库的问题,您永远不必使用请求。我正在圣诞假期,但我回来后会再看看。
  • 我想我找到了。从 API Developers 文档来看并不是那么简单。这里是 createsnapshot 方法 (developers.google.com/resources/api-libraries/documentation/…),这里是整个 disks 方法和 GCE 的客户端库方法 - developers.google.com/resources/api-libraries/documentation/…。我会尝试并报告
  • 最终更新:有效,方法列表见之前的评论。比尔,谢谢你,这正是你说的。
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
相关资源
最近更新 更多