def create_invalidation(domain): distro_id = get_distribution(domain) if distro_id is None: quit_on_error("Could not invalidate Cloudfront Distribution.") else: res = cloudfront.create_invalidation(DistributionId=distro_id, InvalidationBatch={ 'Paths': { 'Quantity': 1, 'Items': ['/*'] }, 'CallerReference': str(uuid4()) })
def sync_website(domain, rootdir, files): try: # Iterate through list to upload objects to S3 old_files = list_files(domain) uploaded_files = [] for filename in track(files): f = filename.replace(str(rootdir[2:]), "").strip(".").strip("/") print(f) uploaded_files.append(f) mt = mimetypes.guess_type(f)[0] mt = mt if mt is not None else "text/plain" client.upload_file( filename, domain, f, ExtraArgs={ "ACL": "public-read", "ContentType": mt }, ) for filename in old_files: if filename not in uploaded_files: client.delete_object(Bucket=domain, Key=filename) print(f"Deleting {filename}") except Exception as e: print(e) return quit_on_error("Failed to sync website.")
def get_cert_arn(domain, soft_fail=False): issued = list_certs("ISSUED")["CertificateSummaryList"] pending = list_certs("PENDING_VALIDATION")["CertificateSummaryList"] issued = [c["CertificateArn"] for c in issued if c["DomainName"] == domain] pending = [ c["CertificateArn"] for c in pending if c["DomainName"] == domain ] if len(issued) == 0 and soft_fail: return False elif len(issued) == 0: if len(pending) == 0: quit_on_error( (f"No Pending or Issued Certificates found for {domain}." " Try updating your name servers.")) else: quit_on_error( (f"Certificate for {domain} is pending." " Try updating your name servers and waiting anywhere from" " a few minutes to a few hours.")) else: return issued[0]
def set_bucket_website_config(domain, index_path="index.html", error_path="error.html"): try: # Set the website policy on the selected bucket return client.put_bucket_website( Bucket=domain, WebsiteConfiguration={ "ErrorDocument": { "Key": error_path }, "IndexDocument": { "Suffix": index_path }, }, ) except Exception as e: print(e) return quit_on_error("Failed to configure website.")
import cloudfront import route53 import acm import s3 import time import sys domain = sys.argv[1] if domain.startswith("www."): domain = domain[4:] rootdir = sys.argv[2] if "." not in domain: quit_on_error( "This is not a valid domain name. It does not contain a TLD. \ Please try again.") #Create Hosted Zone if it does not exist. zone_exists = route53.check_if_zone_exists_by_domain(domain) if not zone_exists: # Create Hosted Zone. res = route53.create_hosted_zone(domain) hosted_zone = res["HostedZone"] quit_on_error( "Please log in to AWS, find the DNS nameservers in the hosted zone for this domain, and \ point your domain's DNS at the nameservers.") # Create ACM Certificate and get DNS validation record. cert = acm.get_cert_arn(domain, True) hosted_zone = route53.get_hosted_zone(domain)
def create_bucket(domain): try: return client.create_bucket(ACL="public-read", Bucket=domain) except Exception as e: print(e) return quit_on_error("Failed to create website.")