mirror of
https://github.com/ansible/awx.git
synced 2026-01-12 02:19:58 -03:30
add task in isolated heartbeat to delete rogue files
This commit is contained in:
parent
dd1a261bc3
commit
1d9f2be0e4
75
awx/lib/management_modules/tower_isolated_cleanup.py
Normal file
75
awx/lib/management_modules/tower_isolated_cleanup.py
Normal file
@ -0,0 +1,75 @@
|
||||
# Copyright (c) 2017 Ansible by Red Hat
|
||||
#
|
||||
# This file is part of Ansible Tower, but depends on code imported from Ansible.
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import datetime
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
cutoff_pk = dict(required=False, default=0, type='int'),
|
||||
)
|
||||
)
|
||||
cutoff_pk = module.params.get('cutoff_pk')
|
||||
changed = False
|
||||
jobs_removed = set([])
|
||||
|
||||
cutoff_time = datetime.datetime.now() - datetime.timedelta(days=7)
|
||||
|
||||
for search_pattern, extract_pattern in [
|
||||
('/tmp/ansible_tower/jobs/*', r'\/tmp\/ansible_tower\/jobs\/(?P<job_id>\d+)'),
|
||||
('/tmp/ansible_tower_*', r'\/tmp\/ansible_tower_(?P<job_id>\d+)_*'),
|
||||
('/tmp/ansible_tower_proot_*', None),
|
||||
]:
|
||||
for path in glob.iglob(search_pattern):
|
||||
st = os.stat(path)
|
||||
modtime = datetime.datetime.fromtimestamp(st.st_mtime)
|
||||
if modtime > cutoff_time:
|
||||
# If job's pk value is lower than threshold, we delete it
|
||||
try:
|
||||
if extract_pattern is None:
|
||||
continue
|
||||
re_match = re.match(extract_pattern, path)
|
||||
if re_match is None:
|
||||
continue
|
||||
job_id = int(re_match.group('job_id'))
|
||||
if job_id >= cutoff_pk:
|
||||
module.debug('Skipping job {}, which may still be running.'.format(job_id))
|
||||
continue
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
else:
|
||||
module.debug('Deleting path {} because modification date is too old.'.format(path))
|
||||
job_id = 'unknown'
|
||||
changed = True
|
||||
jobs_removed.add(job_id)
|
||||
if os.path.islink(path):
|
||||
os.remove(path)
|
||||
else:
|
||||
shutil.rmtree(path)
|
||||
|
||||
module.exit_json(changed=changed, jobs_removed=[j for j in jobs_removed])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@ -12,5 +12,9 @@
|
||||
tower_capacity:
|
||||
register: result
|
||||
|
||||
- name: Print capacity in escaped string to scrape out
|
||||
- name: Print capacity in escaped string to scrape
|
||||
debug: msg="{{ start_delimiter|default('') }}{{ result['capacity'] }}{{ end_delimiter|default('') }}"
|
||||
|
||||
- name: Remove any stale temporary files
|
||||
tower_isolated_cleanup:
|
||||
cutoff_pk: "{{ cutoff_pk | default(0) }}"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user