Switch to filter plugin instead of python script (#14)

pull/15/head
Cian Hatton 3 years ago committed by GitHub
parent 366bacb03c
commit 24d998f0b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,15 @@
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'keep_last': self._keep_last,
}
"""
_keep_last keeps the last n items of a list of lists
"""
def _keep_last(self, list_of_lists, num_to_keep):
lists_to_return = []
for list_items in list_of_lists:
lists_to_return.append(list_items[0:-num_to_keep])
return lists_to_return

@ -1,21 +0,0 @@
#!/usr/bin/python
import os
def main():
s3_result = eval(os.getenv("S3_RESULTS"))
num_backups_to_keep = int(os.getenv("NUM_BACKUPS_TO_KEEP"))
items_to_delete = []
for res in s3_result:
s3_keys = res["s3_keys"]
# fetch all of the backups before the desired number.
# these are the ones we want to delete.
items_to_delete.extend(s3_keys[0:-num_backups_to_keep])
for item in items_to_delete:
print(item)
if __name__ == "__main__":
main()

@ -1,21 +0,0 @@
#!/usr/bin/python
import os
def main():
s3_result = eval(os.getenv("S3_RESULTS"))
num_backups_to_keep = int(os.getenv("NUM_BACKUPS_TO_KEEP"))
items_to_delete = []
for res in s3_result:
s3_keys = res["s3_keys"]
# fetch all of the backups before the desired number.
# these are the ones we want to delete.
items_to_delete.extend(s3_keys[0:-num_backups_to_keep])
for item in items_to_delete:
print(item)
if __name__ == "__main__":
main()

@ -69,14 +69,9 @@
register: s3_list_outputs register: s3_list_outputs
with_items: "{{ volume_mounts }}" with_items: "{{ volume_mounts }}"
# TODO: do this in a more native way rather than a python script reading env vars. - name: Find keys to delete.
- name: Determine which backups should be deleted. ansible.builtin.set_fact:
ansible.builtin.script: determine-s3-keys-to-delete.py s3_keys_to_delete: "{{ s3_list_outputs.results | map(attribute='s3_keys') | keep_last(docker_backup_retain_count) | flatten }}"
environment:
S3_RESULTS: "{{ s3_list_outputs.results }}"
NUM_BACKUPS_TO_KEEP: "{{ docker_backup_retain_count }}"
register: python_output
changed_when: false
- name: Delete old backups. - name: Delete old backups.
amazon.aws.aws_s3: amazon.aws.aws_s3:
@ -87,4 +82,4 @@
s3_url: "{{ docker_backup_aws_s3_url }}" s3_url: "{{ docker_backup_aws_s3_url }}"
object: "{{ item }}" object: "{{ item }}"
mode: delobj mode: delobj
with_items: "{{ python_output.stdout_lines }}" with_items: "{{ s3_keys_to_delete }}"

@ -1,3 +1,4 @@
[defaults] [defaults]
roles_path = roles roles_path = roles
vault_password_file=tests/vault_key.sh vault_password_file=tests/vault_key.sh
filter_plugins = plugins/filter

Loading…
Cancel
Save