Last active
November 5, 2015 18:08
-
-
Save soundofjw/01b43c163b385243e168 to your computer and use it in GitHub Desktop.
Cleaning up "old" appengine versions - great for Managed VM Deployment
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 | |
# -*- coding: utf-8 -*- | |
"""gcloud Version Cleanup Script | |
""" | |
import argparse | |
import os | |
import subprocess | |
import logging | |
import re | |
GCLOUD_PATH = os.path.join( | |
os.environ.get("HOME"), | |
"google-cloud-sdk", | |
"bin", | |
"gcloud", | |
) | |
# Setup Regex for lines like: | |
# front 20151001t172936 * | |
# front 20151001t172936 - | |
VERSION_RE = re.compile(r'([\w\-]+)\s+([\d\w]+)\s+([-\*])') | |
if __name__ == '__main__': | |
# Setup logging | |
logger = logging.getLogger() | |
formatter = logging.Formatter("%(message)s") | |
logger.setLevel(logging.INFO) | |
logger.propagate = False | |
logger.handlers = [] | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.INFO) | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
TITLE = "gcloud version cleanup tool" | |
logging.info("") | |
logging.info(TITLE) | |
logging.info("-"*len(TITLE)+"\n") | |
# Setup args | |
parser = argparse.ArgumentParser(description='gcloud version cleanup tool') | |
parser.add_argument( | |
'module', | |
help='appengine module to cleanup old versions for' | |
) | |
args = parser.parse_args() | |
output = subprocess.check_output([GCLOUD_PATH, | |
"preview", "app", | |
"modules", "list", | |
args.module, | |
]) | |
lines = output.split("\n") | |
for line in lines: | |
m = VERSION_RE.match(line) | |
if m: | |
is_default = m.group(3) == '*' | |
version = m.group(2) | |
logging.info("version: %s (default: %s)", | |
version, str(is_default).lower()) | |
if not is_default: | |
logging.info("deleting...") | |
r = subprocess.check_output([GCLOUD_PATH, | |
"preview", "app", | |
"modules", "delete", | |
args.module, "--version", | |
version, "--quiet", | |
]) | |
logging.info("ok") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was about to write this on my own, thanks! In case you are interested, a Go implementation is here.