forked from dmwm/cms-htcondor-es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffiliation_cache.py
95 lines (75 loc) · 3.35 KB
/
affiliation_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Authors: Christian Ariza <christian.ariza AT gmail [DOT] com>, Ceyhun Uzunoglu <cuzunogl AT gmail [DOT] com>
"""
This script uses the AffiliationManager class to create or update
the affiliation cache file (an indexed structure of username/login
and affiliation institution and country from cric data)
This will create a file in the users' home called .affiliation_dir.json or in output file,
if the file already exists it will overwrite it if and only if it's older
than one day or days parameter. You can specify the location and the recreation period using
the optional parameters:
This script can be setup as a daily cronjob and use the parameters to modify
how often the script is updated.
Attributes:
output_file (string): environment variable for affiliation json file output.
- Default is ``AFFILIATION_DIR_LOCATION``. It used in k8s deployment to define affiliation file location.
- ``AFFILIATION_DIR_LOCATION`` file is updated once a day by ``spider-cron-affiliation`` CronJob.
- It is stored in persistent volume called ``shared-spider``.
- Usage in k8s:
- https://github.com/dmwm/CMSKubernetes/blob/master/kubernetes/spider/cronjobs/spider-cron-affiliation.yaml
- https://github.com/dmwm/CMSKubernetes/blob/master/kubernetes/spider/cronjobs/spider-cron-queues.yaml
- https://github.com/dmwm/CMSKubernetes/blob/master/kubernetes/spider/deployments/spider-worker.yaml
- Usage in ``convert_to_json`` is to fetch affiliations and enrich ClassAd attributes with them.
Raises:
AffiliationManagerException: Generic exception of AffiliationManager
Notes:
- ``AFFILIATION_DIR_LOCATION`` env variable should be provided.
Examples:
``python affiliation_cache.py --output /htcondor_es/aff_dir.json --days 3``
# k8s_affiliation_cache.sh calls like this, which run in ``spider-cron-affiliation``
``python affiliation_cache.py```
"""
import argparse
import os
import traceback
from htcondor_es.AffiliationManager import (
AffiliationManager,
AffiliationManagerException,
)
def generate_affiliation_cache(output_file, days=1):
"""
Update the cache file if older than a given number of days.
Args:
output_file (string): Output file to write affiliations
days (int): recreate_older_days parameter for AffiliationManager
"""
try:
AffiliationManager(recreate_older_days=days, dir_file=output_file)
print("Affiliation creation successful.")
except AffiliationManagerException as _:
traceback.print_exc()
print("There was an error creating the affiliation manager")
if __name__ == "__main__":
output_file = os.getenv(
"AFFILIATION_DIR_LOCATION",
AffiliationManager._AffiliationManager__DEFAULT_DIR_PATH,
)
parser = argparse.ArgumentParser()
parser.add_argument(
"--output",
help="""location of the affiliation cache file
By default, it will use the AFFILIATION_DIR_LOCATION env variable if set
or the users' home as default.""",
default=None,
)
parser.add_argument(
"--days",
help="How often should the file be updated? (days)",
type=int,
default=1,
)
args = parser.parse_args()
if args.output:
output_file = args.output
generate_affiliation_cache(output_file, args.days)