def get_galaxy_connection(history_id=None, obj=True):
history_id = history_id or os.environ['<STR_LIT>']<EOL>key = os.environ['<STR_LIT>']<EOL>galaxy_ip = _get_ip()<EOL>url = Template(os.environ['<STR_LIT>']).safe_substitute({'<STR_LIT>': galaxy_ip})<EOL>gi = _test_url(url, key, history_id, obj=obj)<EOL>if gi is not None:<EOL><INDENT>return gi<EOL><DEDENT>app_path = os.environ['<STR_LIT>'].rstrip('<STR_LIT:/>')<EOL>app_path = '<STR_LIT>'.join(app_path.split('<STR_LIT:/>')[<NUM_LIT:3>:])<EOL>if '<STR_LIT>' not in os.environ:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>galaxy_port = os.environ['<STR_LIT>']<EOL><DEDENT>built_galaxy_url = '<STR_LIT>' % (galaxy_ip.strip(), galaxy_port, app_path.strip())<EOL>url = built_galaxy_url.rstrip('<STR_LIT:/>')<EOL>gi = _test_url(url, key, history_id, obj=obj)<EOL>if gi is not None:<EOL><INDENT>return gi<EOL><DEDENT>msg = "<STR_LIT>"<EOL>raise Exception(msg)<EOL>
Given access to the configuration dict that galaxy passed us, we try and connect to galaxy's API.
First we try connecting to galaxy directly, using an IP address given
us by docker (since the galaxy host is the default gateway for docker).
Using additional information collected by galaxy like the port it is
running on and the application path, we build a galaxy URL and test our
connection by attempting to get a history listing. This is done to
avoid any nasty network configuration that a SysAdmin has placed
between galaxy and us inside docker, like disabling API queries.
If that fails, we failover to using the URL the user is accessing
through. This will succeed where the previous connection fails under
the conditions of REMOTE_USER and galaxy running under uWSGI.