示例#1
0
    def modify_bucket_tags(self, name, bucket, tags):
        """Modify the tags on a bucket"""
        changes = {}
        new_tags = TagSet()
        current_tags = self.current_tags(bucket)

        for tag_name, tag_val in tags.items():
            if tag_name in current_tags:
                if current_tags[tag_name] != tag_val:
                    changes[tag_name] = ("modify", tag_name, current_tags[tag_name], tag_val)
            elif tag_name not in current_tags:
                changes[tag_name] = ("create", tag_name, None, tag_val)

            new_tags.add_tag(tag_name, tag_val)

        for tag_name in current_tags:
            if tag_name not in tags:
                changes[tag_name] = ("delete", tag_name, current_tags[tag_name], None)

        if changes:
            if not new_tags:
                for _ in self.change("D", "bucket_tags", bucket=name, changes=["Delete all tags"]):
                    bucket.delete_tags()
            else:
                one_letter = "M" if any(typ in ("modify", "delete") for typ, _, _, _ in changes.values()) else "C"
                for _ in self.change(one_letter, "bucket_tag", bucket=name, changes=["{0} {1} from {2} to {3}".format(*change) for change in changes.values()]):
                    t = Tags()
                    t.add_tag_set(new_tags)
                    bucket.set_tags(t)
示例#2
0
def setup_bucket(s3, dirname, bucket_name):
	"""Ensures the given bucket exists and prepares it for a duplicity run
	"""
	if not s3.lookup(bucket_name):
		s3.create_bucket(bucket_name)
		time.sleep(5)
	bucket = s3.get_bucket(bucket_name)
	
	# tag this bucket with the directory so we know what it 
	# is when we retrieve it after the terrible fire or burglary
	tags = Tags()
	tagset = TagSet()
	tagset.add_tag('path', dirname)
	tags.add_tag_set(tagset)
	bucket.set_tags(tags)

	# turn off any lifecycle rotations while we are in the middle of a backup
	to_glacier = Transition(days=1, storage_class='GLACIER')
	rule = Rule('movetoglacier', 'duplicity', 'Disabled', transition=to_glacier)
	lifecycle = Lifecycle()
	lifecycle.append(rule)
	bucket.configure_lifecycle(lifecycle)

	# rename the manifest files from their glacier-safe versions
	keys = bucket.list(prefix = '_duplicity')
	for key in keys:
		key.copy(bucket_name, key.name.replace("_duplicity", "duplicity"))
		key.delete()

	return bucket
示例#3
0
def setup_bucket(s3, dirname, bucket_name):
    """Ensures the given bucket exists and prepares it for a duplicity run
	"""
    if not s3.lookup(bucket_name):
        s3.create_bucket(bucket_name)
        time.sleep(5)
    bucket = s3.get_bucket(bucket_name)

    # tag this bucket with the directory so we know what it
    # is when we retrieve it after the terrible fire or burglary
    tags = Tags()
    tagset = TagSet()
    tagset.add_tag('path', dirname)
    tags.add_tag_set(tagset)
    bucket.set_tags(tags)

    # turn off any lifecycle rotations while we are in the middle of a backup
    to_glacier = Transition(days=1, storage_class='GLACIER')
    rule = Rule('movetoglacier',
                'duplicity',
                'Disabled',
                transition=to_glacier)
    lifecycle = Lifecycle()
    lifecycle.append(rule)
    bucket.configure_lifecycle(lifecycle)

    # rename the manifest files from their glacier-safe versions
    keys = bucket.list(prefix='_duplicity')
    for key in keys:
        key.copy(bucket_name, key.name.replace("_duplicity", "duplicity"))
        key.delete()

    return bucket
示例#4
0
def create_tags_container(tags):

    tag_set = TagSet()
    tags_obj = Tags()
    for key, val in tags.items():
        tag_set.add_tag(key, val)

    tags_obj.add_tag_set(tag_set)
    return tags_obj
示例#5
0
def create_tags_container(tags):

    tag_set = TagSet()
    tags_obj = Tags()
    for key, val in tags.items():
        tag_set.add_tag(key, val)

    tags_obj.add_tag_set(tag_set)
    return tags_obj
示例#6
0
 def BotoTagsFromMessage(cls, message):
   label_dict = json.loads(cls.JsonFromMessage(message))
   tag_set = TagSet()
   for key, value in label_dict.iteritems():
     if value:  # Skip values which may be set to None.
       tag_set.add_tag(key, value)
   tags = Tags()
   tags.add_tag_set(tag_set)
   return tags
示例#7
0
 def BotoTagsFromMessage(cls, message):
     label_dict = json.loads(cls.JsonFromMessage(message))
     tag_set = TagSet()
     for key, value in label_dict.iteritems():
         if value:  # Skip values which may be set to None.
             tag_set.add_tag(key, value)
     tags = Tags()
     tags.add_tag_set(tag_set)
     return tags
示例#8
0
 def test_tagging_from_objects(self):
     """Create tags from python objects rather than raw xml."""
     t = Tags()
     tag_set = TagSet()
     tag_set.add_tag('akey', 'avalue')
     tag_set.add_tag('anotherkey', 'anothervalue')
     t.add_tag_set(tag_set)
     self.bucket.set_tags(t)
     response = self.bucket.get_tags()
     self.assertEqual(response[0][0].key, 'akey')
     self.assertEqual(response[0][0].value, 'avalue')
     self.assertEqual(response[0][1].key, 'anotherkey')
     self.assertEqual(response[0][1].value, 'anothervalue')
示例#9
0
    def modify_bucket_tags(self, name, bucket, tags):
        """Modify the tags on a bucket"""
        changes = {}
        new_tags = TagSet()
        current_tags = self.current_tags(bucket)

        for tag_name, tag_val in tags.items():
            if tag_name in current_tags:
                if current_tags[tag_name] != tag_val:
                    changes[tag_name] = ("modify", tag_name,
                                         current_tags[tag_name], tag_val)
            elif tag_name not in current_tags:
                changes[tag_name] = ("create", tag_name, None, tag_val)

            new_tags.add_tag(tag_name, tag_val)

        for tag_name in current_tags:
            if tag_name not in tags:
                changes[tag_name] = ("delete", tag_name,
                                     current_tags[tag_name], None)

        if changes:
            if not new_tags:
                for _ in self.change("D",
                                     "bucket_tags",
                                     bucket=name,
                                     changes=["Delete all tags"]):
                    bucket.delete_tags()
            else:
                one_letter = "M" if any(
                    typ in ("modify", "delete")
                    for typ, _, _, _ in changes.values()) else "C"
                for _ in self.change(
                        one_letter,
                        "bucket_tag",
                        bucket=name,
                        changes=[
                            "{0} {1} from {2} to {3}".format(*change)
                            for change in changes.values()
                        ]):
                    t = Tags()
                    t.add_tag_set(new_tags)
                    bucket.set_tags(t)
示例#10
0
 def test_tagging_from_objects(self):
     """Create tags from python objects rather than raw xml."""
     t = Tags()
     tag_set = TagSet()
     tag_set.add_tag('akey', 'avalue')
     tag_set.add_tag('anotherkey', 'anothervalue')
     t.add_tag_set(tag_set)
     self.bucket.set_tags(t)
     response = self.bucket.get_tags()
     self.assertEqual(response[0][0].key, 'akey')
     self.assertEqual(response[0][0].value, 'avalue')
     self.assertEqual(response[0][1].key, 'anotherkey')
     self.assertEqual(response[0][1].value, 'anothervalue')
示例#11
0
            changed = True
            current_policy = bucket.get_policy()
        except S3ResponseError, e:
            if e.error_code == "NoSuchBucketPolicy":
                current_policy = None
            else:
                module.fail_json(msg=e.message)

    ####
    ## Fix up json of policy so it's not escaped
    ####

    # Tags
    try:
        current_tags = bucket.get_tags()
        tag_set = TagSet()
    except S3ResponseError, e:
        if e.error_code == "NoSuchTagSet":
            current_tags = None
        else:
            module.fail_json(msg=e.message)

    if current_tags is not None or tags is not None:

        if current_tags is None:
            current_tags_dict = {}
        else:
            current_tags_dict = dict((t.key, t.value) for t in current_tags[0])

        if current_tags_dict != tags:
            try:
def _create_bucket(connection, module, location):

    policy = module.params.get("policy")
    name = module.params.get("name")
    requester_pays = module.params.get("requester_pays")
    tags = module.params.get("tags")
    versioning = module.params.get("versioning")
    changed = False

    try:
        bucket = connection.get_bucket(name)
    except S3ResponseError as e:
        try:
            bucket = connection.create_bucket(name, location=location)
            changed = True
        except S3CreateError as e:
            module.fail_json(msg=e.message)

    # Versioning
    versioning_status = bucket.get_versioning_status()
    if not versioning_status and versioning:
        try:
            bucket.configure_versioning(versioning)
            changed = True
            versioning_status = bucket.get_versioning_status()
        except S3ResponseError as e:
            module.fail_json(msg=e.message)
    elif not versioning_status and not versioning:
        # do nothing
        pass
    else:
        if versioning_status['Versioning'] == "Enabled" and not versioning:
            bucket.configure_versioning(versioning)
            changed = True
            versioning_status = bucket.get_versioning_status()
        elif ((versioning_status['Versioning'] == "Disabled" and versioning) or
              (versioning_status['Versioning'] == "Suspended" and versioning)):
            bucket.configure_versioning(versioning)
            changed = True
            versioning_status = bucket.get_versioning_status()

    # Requester pays
    requester_pays_status = get_request_payment_status(bucket)
    if requester_pays_status != requester_pays:
        if requester_pays:
            bucket.set_request_payment(payer='Requester')
            changed = True
            requester_pays_status = get_request_payment_status(bucket)
        else:
            bucket.set_request_payment(payer='BucketOwner')
            changed = True
            requester_pays_status = get_request_payment_status(bucket)

    # Policy
    try:
        current_policy = bucket.get_policy()
    except S3ResponseError as e:
        if e.error_code == "NoSuchBucketPolicy":
            current_policy = None
        else:
            module.fail_json(msg=e.message)

    if current_policy is not None and policy is not None:
        if policy is not None:
            policy = json.dumps(policy)

        if json.loads(current_policy) != json.loads(policy):
            try:
                bucket.set_policy(policy)
                changed = True
                current_policy = bucket.get_policy()
            except S3ResponseError as e:
                module.fail_json(msg=e.message)

    elif current_policy is None and policy is not None:
        policy = json.dumps(policy)

        try:
            bucket.set_policy(policy)
            changed = True
            current_policy = bucket.get_policy()
        except S3ResponseError as e:
            module.fail_json(msg=e.message)

    elif current_policy is not None and policy is None:
        try:
            bucket.delete_policy()
            changed = True
            current_policy = bucket.get_policy()
        except S3ResponseError as e:
            if e.error_code == "NoSuchBucketPolicy":
                current_policy = None
            else:
                module.fail_json(msg=e.message)

    ####
    ## Fix up json of policy so it's not escaped
    ####

    # Tags
    try:
        current_tags = bucket.get_tags()
        tag_set = TagSet()
    except S3ResponseError as e:
        if e.error_code == "NoSuchTagSet":
            current_tags = None
        else:
            module.fail_json(msg=e.message)

    if current_tags is not None or tags is not None:

        if current_tags is None:
            current_tags_dict = {}
        else:
            current_tags_dict = dict((t.key, t.value) for t in current_tags[0])

        if current_tags_dict != tags:
            try:
                if tags:
                    bucket.set_tags(create_tags_container(tags))
                else:
                    bucket.delete_tags()
                current_tags_dict = tags
                changed = True
            except S3ResponseError as e:
                module.fail_json(msg=e.message)

    module.exit_json(changed=changed,
                     name=bucket.name,
                     versioning=versioning_status,
                     requester_pays=requester_pays_status,
                     policy=current_policy,
                     tags=current_tags_dict)