def test_website_redirect_all_requests(self): response = self.bucket.configure_website( redirect_all_requests_to=RedirectLocation('example.com')) config = self.bucket.get_website_configuration() self.assertEqual( config, { 'WebsiteConfiguration': { 'RedirectAllRequestsTo': { 'HostName': 'example.com' } } }) # Can configure the protocol as well. response = self.bucket.configure_website( redirect_all_requests_to=RedirectLocation('example.com', 'https')) config = self.bucket.get_website_configuration() self.assertEqual( config, { 'WebsiteConfiguration': { 'RedirectAllRequestsTo': { 'HostName': 'example.com', 'Protocol': 'https', } } })
def test_redirect_all_request_to_with_just_host(self): location = RedirectLocation(hostname='example.com') config = WebsiteConfiguration(redirect_all_requests_to=location) xml = config.to_xml() self.assertIn( ('<RedirectAllRequestsTo><HostName>' 'example.com</HostName></RedirectAllRequestsTo>'), xml)
def test_redirect_all_requests_with_protocol(self): location = RedirectLocation(hostname='example.com', protocol='https') config = WebsiteConfiguration(redirect_all_requests_to=location) xml = config.to_xml() self.assertIn(('<RedirectAllRequestsTo><HostName>' 'example.com</HostName><Protocol>https</Protocol>' '</RedirectAllRequestsTo>'), xml)
def enable_bucket_as_website(connection, module): bucket_name = module.params.get("name") suffix = module.params.get("suffix") error_key = module.params.get("error_key") if error_key == "None": error_key = None redirect_all_requests = module.params.get("redirect_all_requests") changed = False if redirect_all_requests is not None: redirect_location = RedirectLocation(hostname=redirect_all_requests) else: redirect_location = None try: bucket = connection.get_bucket(bucket_name) except S3ResponseError, e: print e if e.get('reason') == 'Moved permanently': print "bucket does not exit" if 'reason' in e: print e['reason'] else: print e.__dict__
def enable_bucket_as_website(connection, module): bucket_name = module.params.get("name") suffix = module.params.get("suffix") error_key = module.params.get("error_key") if error_key == "None": error_key = None redirect_all_requests = module.params.get("redirect_all_requests") changed = False if redirect_all_requests is not None: redirect_location = RedirectLocation(hostname=redirect_all_requests) else: redirect_location = None try: bucket = connection.get_bucket(bucket_name) if compare_bucket_as_website(bucket, module) is False: bucket.delete_website_configuration() bucket.configure_website(suffix, error_key, redirect_location) changed = True except BotoServerError as e: module.fail_json(msg=e.message) website_config = get_website_conf_plus(bucket) module.exit_json(changed=changed, config=website_config)
def migrate_zerigo_to_route53( domain_name, zerigo_api_user, zerigo_api_key, aws_access_key_id, aws_secret_access_key, comment='migrated from zerigo', use_s3_for_redirects=True, dry_run=False): # ZERIGO zerigo = zerigodns.NSZone(zerigo_api_user, zerigo_api_key) try: zone = zerigo.find_by_domain(domain_name) except zerigodns.ZerigoNotFound as ex: logging.exception(ex) logging.error('Domain not found in zerigo') return hosts = zone.hosts record_data = dict() for host in hosts: key = (host.hostname, host.host_type) if not record_data.has_key(key): record_data[key] = dict( name=host.hostname and (host.hostname + '.' + domain_name) or domain_name, type=host.host_type, ttl=host.ttl or 3600, values=[]) value = host.data if host.host_type == 'MX': value = (str(host.priority or 1) + ' ' + host.data) record_data[key]['values'].append(value) # ROUTE 53 route53 = boto.connect_route53(aws_access_key_id, aws_secret_access_key) hosted_zone_id = None if not dry_run: hosted_zone = route53.get_hosted_zone_by_name(domain_name) if hosted_zone: hosted_zone_id = hosted_zone['GetHostedZoneResponse']['HostedZone']['Id'].split("/")[2] if hosted_zone is None: hosted_zone = route53.create_hosted_zone( domain_name=domain_name, comment=comment) hosted_zone_id = hosted_zone['CreateHostedZoneResponse']['HostedZone']['Id'].split("/")[2] s3 = None # used for creating buckets for redirects for chunk in chunks(record_data.values(), 50): # errors if we do > 100 at a time record_set = ResourceRecordSets( connection=route53, hosted_zone_id=hosted_zone_id, comment=comment) for record in chunk: record_type = record['type'] # route53 doesnt support URL redirect record type, so we have to use S3 and alias # https://devcenter.heroku.com/articles/route-53#naked-root-domain if record_type == 'URL' and use_s3_for_redirects: if s3 is None: s3 = boto.connect_s3(aws_access_key_id, aws_secret_access_key) bucket_name = record['name'] if not dry_run: bucket = s3.lookup(bucket_name) or s3.create_bucket(bucket_name) redirect_to = record['values'][0].split("://")[1] # cant have http:// prefix http only logging.debug('redirect to: '+ redirect_to) bucket.configure_website(redirect_all_requests_to=RedirectLocation(redirect_to)) website_endpoint = bucket.get_website_endpoint() endpoint = website_endpoint.split(record['name']+".")[1] record_set.add_change( 'CREATE', name=record['name'], type='A', ttl=record['ttl'], alias_hosted_zone_id=ENDPOINT_HOSTED_ZONE_IDS[endpoint], alias_dns_name=endpoint) elif record_type in ('CNAME', 'A', 'MX', 'TXT'): logging.debug("adding record: %r" % record) if not dry_run: values = record.pop('values') change = record_set.add_change('CREATE', **record) for value in values: if record_type == 'TXT': value = '"'+value+'"' change.add_value(value) else: logging.info("Unsupported record type: %s" % record_type) if not dry_run: record_set.commit()
def configureRedirect(self, url): logging.info('Setup redirect %s -> %s', self.name, url) self.bucket().configure_website( redirect_all_requests_to=RedirectLocation(hostname=url))
def site_config(self, site): with hook('site config %s' % self.name, self, site): setup_aws_access_key(site) from boto import connect_s3 from boto.s3.bucket import Bucket from boto.s3.key import Key for bucket_config in self.settings['buckets']: # Connect and make sure the bucket exists print bold(u'Configuring bucket %s...' % bucket_config['name']) connection = connect_s3() try: bucket = connection.get_bucket(bucket_config['name']) except: bucket = connection.create_bucket(bucket_config['name']) # Set the bucket policy if bucket_config.has_key('policy'): bucket.set_policy(bucket_config['policy']) # Setup CORS, array of rules # http://boto.readthedocs.org/en/latest/ref/s3.html#boto.s3.cors.CORSConfiguration if bucket_config.has_key( 'cors') and bucket_config['cors'] is None: # If explicity set to None, then remove the cors policy bucket.delete_cors() else: if not bucket_config.has_key('cors'): # If not specified, use the default GET policy bucket_config['cors'] = (DEFAULT_CORS_RULE, ) from boto.s3.cors import CORSConfiguration cors_config = CORSConfiguration() for rule in bucket_config['cors']: cors_config.add_rule(**rule) bucket.set_cors(cors_config) # Setup the lifecycle, array of rules # http://boto.readthedocs.org/en/latest/ref/s3.html#boto.s3.lifecycle.Lifecycle if bucket_config.has_key('lifecycle'): from boto.s3.lifecycle import Lifecycle lifecycle_config = Lifecycle() for rule in bucket_config['lifecycle']: lifecycle_config.add_rule(**rule) bucket.configure_lifecycle(lifecycle_config) else: bucket.delete_lifecycle_configuration() # Setup the bucket website hosting {suffix, error_key, routing_rules, redirect_all_requests_to} # http://boto.readthedocs.org/en/latest/ref/s3.html # https://github.com/boto/boto/blob/develop/boto/s3/website.py if bucket_config.has_key('website'): # Expand the routing rules, array of {condition, redirect} if bucket_config['website'].has_key('routing_rules'): from boto.s3.website import RoutingRules, RoutingRule routing_rules = RoutingRules() for rule in bucket_config['website']['routing_rules']: routing_rules.add_rule(RoutingRule(**rule)) bucket_config['website'][ 'routing_rules'] = routing_rules # Expand the redirect, redirect_all_requests_to is {hostname, protocol} if bucket_config['website'].has_key( 'redirect_all_requests_to'): from boto.s3.website import RedirectLocation bucket_config['website'][ 'redirect_all_requests_to'] = RedirectLocation( **bucket_config['website'] ['redirect_all_requests_to']) bucket.configure_website(**bucket_config['website']) else: bucket.delete_website_configuration()