Created
December 25, 2017 23:44
-
-
Save sowbug/ac5ba8d0dfed3afcbf522734b995af99 to your computer and use it in GitHub Desktop.
A script that you can wrap with helper scripts to make it easier for your kids to start up the Minecraft server on their own.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# sudo pip install --upgrade google-api-python-client | |
from apiclient.discovery import build | |
from oauth2client import client, GOOGLE_TOKEN_URI | |
import argparse | |
import httplib2 | |
import sys | |
CMD_URL_TEMPLATE = "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s/%s" | |
parser = argparse.ArgumentParser(description= | |
"Starts/stops a Google Compute Engine instance using an OAuth refresh token.\n\nExample: gce_server --client_id 12345-abcdefg.apps.googleusercontent.com --client_secret abcdefg-x --refresh_token 9/aBcDeFg --project my-project-999 --zone us-central1-a --instance game-server-1 start") | |
parser.add_argument('--client_id', | |
help='Google API Client ID (https://console.developers.google.com/apis/credentials)') | |
parser.add_argument('--client_secret', | |
help='Google API Client secret') | |
parser.add_argument('--refresh_token', | |
help='Refresh token for obtaining access token') | |
parser.add_argument("verb", default="start", | |
help="Either 'start' or 'stop'") | |
parser.add_argument("--project", | |
help='Name of the GCE project that contains the instance') | |
parser.add_argument("--zone", | |
help='Name of the GCE zone where the instance is') | |
parser.add_argument("--instance", | |
help='Name of the GCE instance') | |
args = parser.parse_args() | |
credentials = client.OAuth2Credentials( | |
access_token = None, | |
client_id = args.client_id, | |
client_secret = args.client_secret, | |
refresh_token = args.refresh_token, | |
token_expiry = None, | |
token_uri = GOOGLE_TOKEN_URI, | |
user_agent = None, | |
revoke_uri= None) | |
http = credentials.authorize(httplib2.Http()) | |
credentials.refresh(httplib2.Http()) | |
h = httplib2.Http() | |
headers = { "Authorization" : \ | |
"Bearer %s" % credentials.token_response['access_token'] } | |
url = CMD_URL_TEMPLATE % (args.project, args.zone, args.instance, args.verb) | |
(resp, content) = h.request(url, "POST", headers=headers) | |
if resp.status == 200: | |
print "Success. Action '%s' should be ready soon." % args.verb | |
else: | |
print "Something is wrong. Email this to Tech Support:\n\n" | |
print resp, content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment