Skip to content

Instantly share code, notes, and snippets.

@dcwangmit01
Last active July 9, 2017 07:00
Show Gist options
  • Save dcwangmit01/8a6e089c74735f816fe4 to your computer and use it in GitHub Desktop.
Save dcwangmit01/8a6e089c74735f816fe4 to your computer and use it in GitHub Desktop.
Python script for resizing a gcloud instance template
"""Resize a gcloud instanceTemplate by creating a new one
Given an existing instanceTemplate name, copy it to a new name and
optionally set a new size and machineType.
This script can be used to resize google cloud container kubernetes
node instance templates. It is necessary because the gcloud container
instanceTemplate UI does not allow the resizing of disks for
kubernetes nodes, and the curl and REST examples don't seem to work as
of 03/28/2016.
License:
http://www.apache.org/licenses/LICENSE-2.0
Location:
https://gist.github.com/dcwangmit01/8a6e089c74735f816fe4
Prerequisites:
# Fetch login credentials for running local scripts that access google cloud
gcloud beta auth application-default login
# Install dependencies
pip install --upgrade oauth2client google-api-python-client docopt
# Set environmental gcloud default configs
gcloud config set core/project <project_id>
# Run this script
python instanceTemplateresize "old_IT_name" "new_IT_name"
Usage:
gcloudInstanceTemplateResize.py SRC_NAME DST_NAME [options]
gcloudInstanceTemplateResize.py (-h | --help)
Arguments:
SRC_NAME Name of the instanceTemplate to copy from
DST_NAME Name of the instanceTemplate to create
Options:
--size=<SIZE> The disk size in gigabityes [default: 10]
--type=<TYPE> The machine type [default: n1-standard-1]
-h --help Show this screen.
"""
from docopt import docopt
import subprocess
import copy
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
from pprint import pprint as pp
# Docs on gcloud instanceTemplates API call
# https://cloud.google.com/compute/docs/reference/latest/instanceTemplates/insert
credentials = GoogleCredentials.get_application_default()
service = build('compute', 'v1', credentials=credentials)
PROJECT = subprocess.check_output(["bash", "-c",
"gcloud config list core/project 2>/dev/null | grep project | awk '{print $3}'"]).strip()
def main(args):
pp(args)
# gather input args
SRC_NAME = args['SRC_NAME']
DST_NAME = args['DST_NAME']
DST_SIZE = args['--size']
DST_TYPE = args['--type']
# fetch existing templates
request = service.instanceTemplates().list(project=PROJECT)
response = request.execute()
# created dict of template name to template
templates = { i['name']:i for i in response['items'] }
# do some checks
assert SRC_NAME in templates, "unable to find SRC_NAME in existing templates"
assert DST_NAME not in templates, "DST_NAME should not already exist in templates"
assert int(DST_SIZE) >= 10, "DST_SIZE must be >= 10GB"
# create the new template, by copying the old and setting values
s = templates[SRC_NAME]
d = {}
d['name'] = DST_NAME
d['description'] = copy.deepcopy(s['description'])
d['properties'] = copy.deepcopy(s['properties'])
d['properties']['machineType'] = DST_TYPE
d['properties']['disks'][0]['initializeParams']['diskSizeGb'] = DST_SIZE
d['properties']['disks'][0]['deviceName'] = DST_NAME
# create the template
request = service.instanceTemplates().insert(project=PROJECT, body=d)
response = request.execute()
# print the results
pp(response)
if __name__ == '__main__':
args = docopt(__doc__)
main(args)
@tobych
Copy link

tobych commented Jul 9, 2017

Thanks for this, David. Good to see I'm not the only one dropping down to running gcloud from Python to get stuff done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment