简体   繁体   English

如何获取在 Google Cloud 上运行的计算引擎实例的数量

[英]How to get number of compute engine instances running on Google Cloud

In Google Cloud documentation, the command line to get the list and number of instances that are running on a project in Google Cloud is given as following:在 Google Cloud 文档中,获取在 Google Cloud 中的项目上运行的实例列表和数量的命令行如下所示:

gcloud compute instances list

or或者

GET https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances

How can I get the equivalent function in Python using Google Cloud?如何使用 Google Cloud 在 Python 中获得等效的 function?

There is documentation on this here: https://cloud.google.com/compute/docs/tutorials/python-guide这里有关于此的文档: https://cloud.google.com/compute/docs/tutorials/python-guide

In short:简而言之:

import googleapiclient.discovery

project="your project"
zone="your zone"

compute = googleapiclient.discovery.build('compute', 'v1')
instances = compute.instances().list(project=project, zone=zone).execute()

for instance in instances:
    print(' - ' + instance['name'])

Below is an example I wrote that uses the REST API and does not use one of the Google Cloud SDKs.下面是我编写的一个示例,它使用 REST API 并且不使用 Google Cloud SDK 之一。

This example will teach you the low level details of services accounts, authorization, access tokens and the Compute Engine REST API.此示例将教您服务帐户、授权、访问令牌和 Compute Engine REST API 的低级详细信息。

'''
This program lists lists the Google Compute Engine Instances in one zone
'''

import time
import json
import jwt
import requests
import httplib2

# Project ID for this request.
project = 'development-123456'

# The name of the zone for this request.
zone = 'us-west1-a'

# Service Account Credentials, Json format
json_filename = 'service-account.json'

# Permissions to request for Access Token
scopes = "https://www.googleapis.com/auth/cloud-platform"

# Set how long this token will be valid in seconds
expires_in = 3600   # Expires in 1 hour

def load_json_credentials(filename):
    ''' Load the Google Service Account Credentials from Json file '''

    with open(filename, 'r') as f:
        data = f.read()

    return json.loads(data)

def load_private_key(json_cred):
    ''' Return the private key from the json credentials '''

    return json_cred['private_key']

def create_signed_jwt(pkey, pkey_id, email, scope):
    '''
    Create a Signed JWT from a service account Json credentials file
    This Signed JWT will later be exchanged for an Access Token
    '''

    # Google Endpoint for creating OAuth 2.0 Access Tokens from Signed-JWT
    auth_url = "https://www.googleapis.com/oauth2/v4/token"

    issued = int(time.time())
    expires = issued + expires_in   # expires_in is in seconds

    # Note: this token expires and cannot be refreshed. The token must be recreated

    # JWT Headers
    additional_headers = {
            'kid': pkey_id,
            "alg": "RS256",
            "typ": "JWT"    # Google uses SHA256withRSA
    }

    # JWT Payload
    payload = {
        "iss": email,       # Issuer claim
        "sub": email,       # Issuer claim
        "aud": auth_url,    # Audience claim
        "iat": issued,      # Issued At claim
        "exp": expires,     # Expire time
        "scope": scope      # Permissions
    }

    # Encode the headers and payload and sign creating a Signed JWT (JWS)
    sig = jwt.encode(payload, pkey, algorithm="RS256", headers=additional_headers)

    return sig

def exchangeJwtForAccessToken(signed_jwt):
    '''
    This function takes a Signed JWT and exchanges it for a Google OAuth Access Token
    '''

    auth_url = "https://www.googleapis.com/oauth2/v4/token"

    params = {
        "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
        "assertion": signed_jwt
    }

    r = requests.post(auth_url, data=params)

    if r.ok:
        return(r.json()['access_token'], '')

    return None, r.text

def gce_list_instances(accessToken):
    '''
    This functions lists the Google Compute Engine Instances in one zone
    '''

    # Endpoint that we will call
    url = "https://www.googleapis.com/compute/v1/projects/" + project + "/zones/" + zone + "/instances"

    # One of the headers is "Authorization: Bearer $TOKEN"
    headers = {
        "Host": "www.googleapis.com",
        "Authorization": "Bearer " + accessToken,
        "Content-Type": "application/json"
    }

    h = httplib2.Http()

    resp, content = h.request(uri=url, method="GET", headers=headers)

    status = int(resp.status)

    if status < 200 or status >= 300:
        print('Error: HTTP Request failed')
        return

    j = json.loads(content.decode('utf-8').replace('\n', ''))

    print('Compute instances in zone', zone)
    print('------------------------------------------------------------')
    for item in j['items']:
        print(item['name'])

if __name__ == '__main__':
    cred = load_json_credentials(json_filename)

    private_key = load_private_key(cred)

    s_jwt = create_signed_jwt(
            private_key,
            cred['private_key_id'],
            cred['client_email'],
            scopes)

    token, err = exchangeJwtForAccessToken(s_jwt)

    if token is None:
        print('Error:', err)
        exit(1)

    gce_list_instances(token)

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

相关问题 在 Google Cloud Compute Engine 上运行 python 脚本 - Running a python script on Google Cloud Compute Engine 如何在Google Compute Engine中通过Python访问Google Cloud SQL - How to access Google Cloud SQL by Python in Google Compute Engine Google Cloud Compute Engine 15%限制 - Google Cloud Compute engine 15% limitation Google Cloud - 计算引擎VS机器学习 - Google Cloud - Compute Engine VS Machine Learning 列出谷歌云计算引擎活动实例 - List google cloud compute engine active instance Google Cloud Secret Manager 在计算引擎中运行时打印不必要的调试信息 - Google Cloud Secret Manager prints unnecessary debug information when running in compute engine 元数据中的启动脚本未运行(Python、Google Compute Engine、云存储触发器) - Startup Script in Metadata Not Running (Python, Google Compute Engine, Cloud Storage Trigger) 谷歌云引擎:输入实例不是JSON格式 - google cloud engine: Input instances are not in JSON format Google PubSub和自动扩展计算引擎实例(Python) - Google PubSub and auto-scaling Compute Engine instances (Python) 正在运行太多的Google App Engine实例? - Too many instances of Google App Engine running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM