diff --git a/inventory/host_vars/elections01.phx2.fedoraproject.org b/inventory/host_vars/elections01.phx2.fedoraproject.org index d226074601..99c8b5f66e 100644 --- a/inventory/host_vars/elections01.phx2.fedoraproject.org +++ b/inventory/host_vars/elections01.phx2.fedoraproject.org @@ -10,3 +10,8 @@ volgroup: /dev/vg_guests eth0_ip: 10.5.126.107 vmhost: virthost19.phx2.fedoraproject.org datacenter: phx2 + +# GDPR SAR variables - datanommer/datagrepper +sar_script: /usr/local/bin/elections_sar.py +sar_script_user: apache +sar_output_file: elections.json diff --git a/roles/elections/tasks/main.yml b/roles/elections/tasks/main.yml index 0d7f552fd7..f848eb291f 100644 --- a/roles/elections/tasks/main.yml +++ b/roles/elections/tasks/main.yml @@ -74,3 +74,15 @@ tags: - selinux - elections + +- name: Install the SAR script for elections + template: src={{ item.file }} + dest="{{ item.location }}/{{ item.file }}" + owner=apache group=apache mode=0700 + with_items: + - { file: elections_sar.py, location: /usr/local/bin/elections_sar.py } + tags: + - config + - elections + - SAR + - GDPR diff --git a/roles/elections/templates/elections_sar.py b/roles/elections/templates/elections_sar.py new file mode 100644 index 0000000000..e788ac7685 --- /dev/null +++ b/roles/elections/templates/elections_sar.py @@ -0,0 +1,64 @@ +#!/usr/bin/python + +from __future__ import unicode_literals, print_function + +import json +import os +import sys + +import sqlalchemy + + +if 'FEDORA_ELECTIONS_CONFIG' not in os.environ \ + and os.path.exists('/etc/fedora-elections/fedora-elections.cfg'): + os.environ['FEDORA_ELECTIONS_CONFIG'] = '/etc/fedora-elections/'\ + 'fedora-elections.cfg' + + +from fedora_elections import SESSION +from fedora_elections import models + + +def get_candidate_users(session, username): + ''' Return all fedora_elections.models.Candidate related to the username + provided + ''' + query = SESSION.query( + models.Candidate + ).filter( + sqlalchemy.or_( + models.Candidate.name.ilike('%{0}%'.format(username)), + models.Candidate.fas_name.ilike('%s%'.format(username)) + ) + ) + + return query.all() + + +def main(): + ''' Prints out all the election involving the username specified in the + SAR_USERNAME environment variable. + ''' + + username = os.getenv('SAR_USERNAME') + if not username: + print('An username is required to query datagrepper') + return 1 + + output = {} + output['candidates'] = [] + + for candidate in get_candidate_users(SESSION, username): + tmp = candidate.to_json() + tmp['election'] = candidate.election.to_json() + output['candidates'].append(tmp) + + SESSION.remove() + + print(json.dumps( + output, sort_keys=True, indent=4, separators=(',', ': ') + ).encode('utf-8')) + + +if __name__ == '__main__': + sys.exit(main())