def upload(filepaths): """ For each path in file paths: * Determine if a key in S3 exists for the given template. If not, create it. * If the key does exist, check to see if we need to update the key. If not, do nothing. """ assert isinstance(filepaths, (list, tuple)) for path in filepaths: key = get_key(path) template_data = minify(path) if key is None: validate([path]) key = get_key(path, validate=False) key.set_contents_from_string(template_data) key.change_storage_class("REDUCED_REDUNDANCY") url = get_url_for_key(key) logger.info("Uploaded new template %s", url) else: md5 = hashlib.md5(template_data) if md5.hexdigest() != key.etag.replace('"', ""): validate([path]) url = get_url_for_key(key) logger.info("Uploading %s", url) key.set_contents_from_string(template_data) key.change_storage_class("REDUCED_REDUNDANCY")
def set_public_record(): parser = argparse.ArgumentParser(description="Updates DNS records") parser.add_argument( "--address", default=urllib2.urlopen( "http://169.254.169.254/latest/meta-data/public-ipv4", timeout=120 ).read() ) parser.add_argument( "hostname", help="The hostname to establish the DNS record for" ) args = parser.parse_args() if not args.hostname.endswith("."): parser.error("Expected record to end with '.'") zone_name = ".".join(list(filter(bool, args.hostname.split(".")))[-2:]) route53 = boto.connect_route53() zone = route53.get_zone(zone_name) record = zone.get_a(args.hostname) if record is None: logger.info("Creating A %s %s", args.hostname, args.address) zone.add_a(args.hostname, args.address, ttl=60) else: logger.info("Updating A %s %s", args.hostname, args.address) zone.update_record(record, args.address, new_ttl=60)
def delete_extra_templates(): """ Iterates over all cloudformation keys in S3 and deletes any that not under source control locally. """ bucket = s3.get_bucket(config.get("cloudformation", "bucket")) prefix = config.get("cloudformation", "template_s3_prefix") source_path = config.get("cloudformation", "template_source_path") for key in bucket.list(prefix=prefix): source_file = join(source_path, key.name.replace(prefix, "")) if not isfile(source_file): logger.info("Removing old template %s from S3", key.name) key.delete()
def validate(filepaths): assert isinstance(filepaths, (list, tuple)) for path in filepaths: logger.info("Validating %s", path) template_body = minify(path) cloud_formation.validate_template(template_body=template_body)