예제 #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
파일: s3_bucket.py 프로젝트: ernstp/ansible
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 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')
예제 #10
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)
예제 #11
0
 def _LabelDictFromXmlString(self, xml_str):
     label_dict = {}
     tags_list = Tags()
     h = handler.XmlHandler(tags_list, None)
     try:
         xml.sax.parseString(xml_str, h)
     except SaxExceptions.SAXParseException, e:
         raise CommandException(
             'Requested labels/tagging config is invalid: %s at line %s, column '
             '%s' %
             (e.getMessage(), e.getLineNumber(), e.getColumnNumber()))