Skip to content

Commit

Permalink
Merge branch 'hotfix/19.23.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
mfraezz committed Aug 20, 2019
2 parents 6c81f5c + 4fff006 commit a99d60d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 deletions.
2 changes: 1 addition & 1 deletion osf_tests/test_elastic_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def tearDownClass(cls):

def setUp(self):
super(TestSearchMigration, self).setUp()
populate_institutions('test')
populate_institutions(default_args=True)
self.es = search.search_engine.CLIENT
search.delete_index(settings.ELASTIC_INDEX)
search.create_index(settings.ELASTIC_INDEX)
Expand Down
105 changes: 76 additions & 29 deletions scripts/populate_institutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
"""Populate development database with Institution fixtures."""

import argparse
import logging
import sys
import urllib
Expand All @@ -18,10 +19,19 @@
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

ENVS = ['prod', 'stage', 'stage2', 'test']
ENVS = ['prod', 'stage', 'stage2', 'stage3', 'test', ]

# TODO: Store only the Entity IDs in OSF DB and move the URL building process to CAS
SHIBBOLETH_SP_LOGIN = '{}/Shibboleth.sso/Login?entityID={{}}'.format(settings.CAS_SERVER_URL)
SHIBBOLETH_SP_LOGOUT = '{}/Shibboleth.sso/Logout?return={{}}'.format(settings.CAS_SERVER_URL)

# Using optional args instead of positional ones to explicitly set them
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--env', help='select the server: prod, test, stage, stage2 or stage3')
group = parser.add_mutually_exclusive_group()
group.add_argument('-i', '--ids', nargs='+', help='select the institution(s) to add or update')
group.add_argument('-a', '--all', action='store_true', help='add or update all institutions')


def encode_uri_component(val):
return urllib.quote(val, safe='~()*!.\'')
Expand All @@ -44,11 +54,45 @@ def update_or_create(inst_data):
return inst, True


def main(env):
INSTITUTIONS = []
def main(default_args=False):

if default_args:
args = parser.parse_args(['--env', 'test', '--all'])
else:
args = parser.parse_args()

server_env = args.env
update_ids = args.ids
update_all = args.all

if not server_env or server_env not in ENVS:
logger.error('A valid environment must be specified: {}'.format(ENVS))
sys.exit(1)
institutions = INSTITUTIONS[server_env]

if not update_all and not update_ids:
logger.error('Nothing to update or create. Please either specify a list of institutions '
'using --ids or run for all with --all')
sys.exit(1)
elif update_all:
institutions_to_update = institutions
else:
institutions_to_update = [inst for inst in institutions if inst['_id'] in update_ids]
diff_list = list(set(update_ids) - set([inst['_id'] for inst in institutions_to_update]))
if diff_list:
logger.error('One or more institution ID(s) provided via -i or --ids do not match any '
'existing records: {}.'.format(diff_list))
sys.exit(1)

with transaction.atomic():
for inst_data in institutions_to_update:
update_or_create(inst_data)
for extra_inst in Institution.objects.exclude(_id__in=[x['_id'] for x in institutions]):
logger.warn('Extra Institution : {} - {}'.format(extra_inst._id, extra_inst.name))


if env == 'prod':
INSTITUTIONS = [
INSTITUTIONS = {
'prod': [
{
'_id': 'a2jlab',
'name': 'Access to Justice Lab',
Expand Down Expand Up @@ -529,7 +573,7 @@ def main(env):
{
'_id': 'ou',
'name': 'The University of Oklahoma',
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://www.ou.edu/research-norman">Office of the Vice President for Research</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
'banner_name': 'ou-banner.png',
'logo_name': 'ou-shield.png',
'login_url': SHIBBOLETH_SP_LOGIN.format(encode_uri_component('https://shib.ou.edu/idp/shibboleth')),
Expand Down Expand Up @@ -792,9 +836,8 @@ def main(env):
'email_domains': [],
'delegation_protocol': 'saml-shib',
},
]
if env == 'stage':
INSTITUTIONS = [
],
'stage': [
{
'_id': 'cos',
'name': 'Center For Open Science [Stage]',
Expand Down Expand Up @@ -842,9 +885,8 @@ def main(env):
'email_domains': ['yahoo.com'],
'delegation_protocol': '',
},
]
if env == 'stage2':
INSTITUTIONS = [
],
'stage2': [
{
'_id': 'cos',
'name': 'Center For Open Science [Stage2]',
Expand All @@ -857,9 +899,22 @@ def main(env):
'email_domains': ['cos.io'],
'delegation_protocol': '',
},
]
elif env == 'test':
INSTITUTIONS = [
],
'stage3': [
{
'_id': 'cos',
'name': 'Center For Open Science [Stage3]',
'description': 'Center for Open Science [Stage3]',
'banner_name': 'cos-banner.png',
'logo_name': 'cos-shield.png',
'login_url': None,
'logout_url': None,
'domains': ['staging3-osf.cos.io'],
'email_domains': ['cos.io'],
'delegation_protocol': '',
},
],
'test': [
{
'_id': 'a2jlab',
'name': 'Access to Justice Lab [Test]',
Expand Down Expand Up @@ -1340,7 +1395,7 @@ def main(env):
{
'_id': 'ou',
'name': 'The University of Oklahoma [Test]',
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://www.ou.edu/research-norman">Office of the Vice President for Research</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
'description': '<a href="https://www.ou.edu">The University of Oklahoma</a> | <a href="https://libraries.ou.edu">University Libraries</a>',
'banner_name': 'ou-banner.png',
'logo_name': 'ou-shield.png',
'login_url': SHIBBOLETH_SP_LOGIN.format(encode_uri_component('https://shib.ou.edu/idp/shibboleth')),
Expand Down Expand Up @@ -1603,19 +1658,11 @@ def main(env):
'email_domains': [],
'delegation_protocol': 'saml-shib',
},
]

init_app(routes=False)
with transaction.atomic():
for inst_data in INSTITUTIONS:
update_or_create(inst_data)
for extra_inst in Institution.objects.exclude(_id__in=[x['_id'] for x in INSTITUTIONS]):
logger.warn('Extra Institution : {} - {}'.format(extra_inst._id, extra_inst.name))
],
}


if __name__ == '__main__':
env = str(sys.argv[1]).lower() if len(sys.argv) == 2 else None
if env not in ENVS:
print('An environment must be specified : {}', ENVS)
sys.exit(1)
main(env)

init_app(routes=False)
main(default_args=False)

0 comments on commit a99d60d

Please sign in to comment.