Skip to content

Commit

Permalink
add a script to delete users by email
Browse files Browse the repository at this point in the history
  • Loading branch information
bdzim committed Aug 19, 2020
1 parent 21d673a commit 1a80b36
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
96 changes: 96 additions & 0 deletions bin/purge_users_by_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import logging
from optparse import OptionParser, OptionGroup

from account_mgr.accounts_api import Api
from common import read_config_file, merge_config, set_config, validate_config, NetKesConfigError


def _initialize_logging(verbose=False):
handler = logging.StreamHandler()

formatter = logging.Formatter(
'%(asctime)s %(levelname)-8s %(name)-20s: %(message)s')
handler.setFormatter(formatter)
logging.root.addHandler(handler)
logging.root.setLevel(logging.INFO)


def parse_cmdline():
parser = OptionParser()

config_group = OptionGroup(parser, "General Configuration Options",
"These control the configuration of the overall SpiderOak Blue system.")
config_group.add_option("--config", dest="config_file",
help="The location of the JSON configuration file.",
metavar="FILE")
config_group.add_option("--emails", dest="email_file",
help="The location of the file containing the emails of users to purge.",
metavar="FILE")
config_group.add_option("--dry-run", dest="dry_run", action="store_true", default=False,
help="Only display actions to be taken - do not actually perform purging.")

parser.add_option_group(config_group)

options, _ = parser.parse_args()
if not options.config_file:
parser.error("Missing required argument --config")
if not options.email_file:
parser.error("Missing required argument --emails")

# Prune it up a bit and return it as a dict.
optdict = vars(options)
for key in optdict.keys():
if optdict[key] is None:
del optdict[key]

return optdict


def process_config():
cmdline_opts = parse_cmdline()

config = read_config_file(cmdline_opts.get('config_file', None))

try:
validate_config(config)
except NetKesConfigError, e:
raise e

with open(cmdline_opts['email_file']) as f:
emails = f.readlines()

return config, emails, cmdline_opts['dry_run']


def main():
config, emails, dry_run = process_config()
_initialize_logging()
log = logging.getLogger('purge_users_by_email')

api = Api.create(
config['api_root'],
config['api_user'],
config['api_password'],
)

for email in emails:
email = email.strip()
try:
user = api.get_user(email)
except Api.NotFound:
log.error('Unable to find user: "%s"', email)
continue
if user['purgehold_active']:
log.info('Skipping user "%s" because purgehold is active', email)
else:
log.info('Purging user "%s"', email)
if not dry_run:
try:
api.delete_user(email)
except:
log.error('Error purging user "%s"', email)
log.info('User "%s" has been purged', email)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion netkes/account_mgr/accounts_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)
except Error:
log = logging.getLogger('accounts_api')
log.exception('%s - %s - %s' % (func.__name__, args, kwargs))
log.debug('%s - %s - %s' % (func.__name__, args, kwargs))
raise
wrapper.__name__ = func.__name__
return wrapper
Expand Down

0 comments on commit 1a80b36

Please sign in to comment.