-
Notifications
You must be signed in to change notification settings - Fork 10
Change the main to rely on the Sync class (HPC-7469) #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itkovian
wants to merge
6
commits into
hpcugent:master
Choose a base branch
from
itkovian:syncify-user-sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
edd4776
Refactor to use Sync.do
itkovian 2b61d96
Remove unused imports
itkovian 2cb4d7e
Changed parameters
itkovian 47e2ce3
Fix tests
itkovian dd10dda
Merge branch 'master' into syncify-user-sync
itkovian d8b6a39
Merge branch 'master' into syncify-user-sync
itkovian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,18 +29,12 @@ | |
""" | ||
|
||
import logging | ||
import sys | ||
|
||
from vsc.accountpage.client import AccountpageClient | ||
from vsc.accountpage.wrappers import mkVscUserSizeQuota | ||
from vsc.accountpage.sync import Sync | ||
from vsc.administration.user import process_users, process_users_quota | ||
from vsc.administration.vo import process_vos | ||
from vsc.config.base import GENT | ||
from vsc.utils import fancylogger | ||
from vsc.utils.missing import nub | ||
from vsc.utils.nagios import NAGIOS_EXIT_CRITICAL | ||
from vsc.utils.script_tools import ExtendedSimpleOption | ||
from vsc.utils.timestamp import convert_timestamp, write_timestamp, retrieve_timestamp_with_default | ||
|
||
NAGIOS_HEADER = "sync_vsc_users" | ||
NAGIOS_CHECK_INTERVAL_THRESHOLD = 15 * 60 # 15 minutes | ||
|
@@ -64,17 +58,9 @@ class UserGroupStatusUpdateError(Exception): | |
pass | ||
|
||
|
||
def main(): | ||
""" | ||
Main script. | ||
- build the filter | ||
- fetches the users | ||
- process the users | ||
- write the new timestamp if everything went OK | ||
- write the nagios check file | ||
""" | ||
class VscUserSync(Sync): | ||
|
||
options = { | ||
CLI_OPTIONS = { | ||
'nagios-check-interval-threshold': NAGIOS_CHECK_INTERVAL_THRESHOLD, | ||
'storage': ('storage systems on which to deploy users and vos', None, 'extend', []), | ||
'user': ('process users', None, 'store_true', False), | ||
|
@@ -85,97 +71,88 @@ def main(): | |
'start_timestamp': ('Timestamp to start the sync from', str, 'store', None), | ||
} | ||
|
||
opts = ExtendedSimpleOption(options) | ||
stats = {} | ||
def do(self, dry_run): | ||
""" | ||
Actual work | ||
- build the filter | ||
- fetches the users | ||
- process the users | ||
- write the new timestamp if everything went OK | ||
- write the nagios check file | ||
""" | ||
|
||
(last_timestamp, start_time) = retrieve_timestamp_with_default( | ||
SYNC_TIMESTAMP_FILENAME, | ||
start_timestamp=opts.options.start_timestamp) | ||
logging.info("Using timestamp %s", last_timestamp) | ||
logging.info("Using startime %s", start_time) | ||
|
||
try: | ||
client = AccountpageClient(token=opts.options.access_token, url=opts.options.account_page_url + "/api/") | ||
|
||
institute = opts.options.host_institute | ||
stats = {} | ||
institute = self.options.host_institute | ||
|
||
(users_ok, users_fail) = ([], []) | ||
(quota_ok, quota_fail) = ([], []) | ||
if opts.options.user: | ||
changed_accounts = client.account.institute[institute].modified[last_timestamp].get()[1] | ||
if self.options.user: | ||
changed_accounts, _ = self.apc.get_accounts() # we ignore inactive accounts | ||
|
||
logging.info("Found %d %s accounts that have changed in the accountpage since %s" % | ||
(len(changed_accounts), institute, last_timestamp)) | ||
|
||
accounts = nub([u['vsc_id'] for u in changed_accounts]) | ||
(len(changed_accounts), institute, self.start_timestamp)) | ||
|
||
for storage_name in opts.options.storage: | ||
for storage_name in self.options.storage: | ||
(users_ok, users_fail) = process_users( | ||
opts.options, | ||
accounts, | ||
changed_accounts, | ||
storage_name, | ||
client, | ||
institute) | ||
self.apc, | ||
institute, | ||
self.options.dry_run) | ||
stats["%s_users_sync" % (storage_name,)] = len(users_ok) | ||
stats["%s_users_sync_fail" % (storage_name,)] = len(users_fail) | ||
stats["%s_users_sync_fail_warning" % (storage_name,)] = STORAGE_USERS_LIMIT_WARNING | ||
stats["%s_users_sync_fail_critical" % (storage_name,)] = STORAGE_USERS_LIMIT_CRITICAL | ||
|
||
for storage_name in opts.options.storage: | ||
storage_changed_quota = [mkVscUserSizeQuota(q) for q in | ||
client.quota.user.storage[storage_name].modified[last_timestamp].get()[1]] | ||
storage_changed_quota = [q for q in storage_changed_quota if q.fileset.startswith('vsc')] | ||
logging.info("Found %d accounts that have changed quota on storage %s in the accountpage since %s", | ||
len(storage_changed_quota), storage_name, last_timestamp) | ||
for storage_name in self.options.storage: | ||
storage_changed_quota = [q for q in self.get_user_storage_quota(storage_name=storage_name) | ||
if q.fileset.startswith('vsc')] | ||
logging.info("Found %d quota changes on storage %s in the accountpage", | ||
len(storage_changed_quota), storage_name) | ||
(quota_ok, quota_fail) = process_users_quota( | ||
opts.options, | ||
storage_changed_quota, | ||
storage_name, | ||
client, | ||
institute) | ||
self.apc, | ||
institute, | ||
self.options.dry_run) | ||
stats["%s_quota_sync" % (storage_name,)] = len(quota_ok) | ||
stats["%s_quota_sync_fail" % (storage_name,)] = len(quota_fail) | ||
stats["%s_quota_sync_fail_warning" % (storage_name,)] = STORAGE_QUOTA_LIMIT_WARNING | ||
stats["%s_quota_sync_fail_critical" % (storage_name,)] = STORAGE_QUOTA_LIMIT_CRITICAL | ||
|
||
(vos_ok, vos_fail) = ([], []) | ||
if opts.options.vo: | ||
if self.options.vo: | ||
# FIXME: when api has changed, limit to modified per institute here | ||
changed_vos = client.vo.modified[last_timestamp].get()[1] | ||
changed_vo_quota = client.quota.vo.modified[last_timestamp].get()[1] | ||
changed_groups, _ = self.apc.get_groups() # we ignore inactive groups | ||
changed_vos = [g for g in changed_groups if g.vsc_id.startswith("gvo") and not g.vsc_id.startswith("gvos")] | ||
changed_vo_quota = [q for q in self.apc.get_vo_storage_quota(storage_name=storage_name) | ||
if q.fileset.startswith('gvo') and not q.fileset.startswith('gvos')] | ||
|
||
vos = sorted(set([v['vsc_id'] for v in changed_vos] + | ||
[v['virtual_organisation'] for v in changed_vo_quota])) | ||
vos = sorted(set([v.vsc_id for v in changed_vos] + [v.virtual_organisation for v in changed_vo_quota])) | ||
|
||
logging.info("Found %d %s VOs that have changed in the accountpage since %s" % | ||
(len(changed_vos), institute, last_timestamp)) | ||
(len(changed_vos), institute, self.start_timestamp)) | ||
logging.info("Found %d %s VOs that have changed quota in the accountpage since %s" % | ||
(len(changed_vo_quota), institute, last_timestamp)) | ||
(len(changed_vo_quota), institute, self.start_timestamp)) | ||
logging.debug("Found the following {institute} VOs: {vos}".format(institute=institute, vos=vos)) | ||
|
||
for storage_name in opts.options.storage: | ||
for storage_name in self.options.storage: | ||
(vos_ok, vos_fail) = process_vos( | ||
opts.options, | ||
vos, | ||
storage_name, | ||
client, | ||
last_timestamp, | ||
institute) | ||
self.apc, | ||
self.start_timestamp, | ||
institute, | ||
self.options.dry_run) | ||
stats["%s_vos_sync" % (storage_name,)] = len(vos_ok) | ||
stats["%s_vos_sync_fail" % (storage_name,)] = len(vos_fail) | ||
stats["%s_vos_sync_fail_warning" % (storage_name,)] = STORAGE_VO_LIMIT_WARNING | ||
stats["%s_vos_sync_fail_critical" % (storage_name,)] = STORAGE_VO_LIMIT_CRITICAL | ||
|
||
if not (users_fail or quota_fail or vos_fail) and not opts.options.dry_run: | ||
(_, ldap_timestamp) = convert_timestamp(start_time) | ||
write_timestamp(SYNC_TIMESTAMP_FILENAME, ldap_timestamp) | ||
except Exception as err: | ||
logger.exception("critical exception caught: %s" % (err)) | ||
opts.critical("Script failed in a horrible way") | ||
sys.exit(NAGIOS_EXIT_CRITICAL) | ||
|
||
opts.epilogue("%s users and VOs synchronised" % institute, stats) | ||
|
||
if users_fail or quota_fail or vos_fail: | ||
return users_fail + quota_fail + vos_fail | ||
else: | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nothing done with stats? set self.thresholds = stats ? |
||
|
||
if __name__ == '__main__': | ||
main() | ||
VscUserSync().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no institute nor timestamp passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this taken care of by the Sync class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be self.get_accounts, the timestamp is processed in sync class, not APclient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and found no clue where the site/institute should have been taken care of