jrxFive / python-nomad

Client library Hashicorp Nomad
https://python-nomad.readthedocs.io/en/latest/
MIT License
139 stars 73 forks source link

Environment variable NOMAD_ADDR overrides host parameter #86

Closed ionosphere80 closed 3 years ago

ionosphere80 commented 5 years ago

I'm developing a task log access feature (via the client/fs/logs endpoint) in a custom utility that requires connecting to a Nomad server/cluster to determine the Nomad client containing a particular allocation, connecting to that Nomad client, and accessing the stdout/stderr logs for a task in the allocation. Clients using the custom utility often contain the NOMAD_ADDR environment variable referencing the Nomad server/cluster. During creation of a Nomad object, the NOMAD_ADDR environment variable seems to override the value of the host parameter which prevents the feature from creating an additional Nomad object to access the client containing the allocation. I think the library should use NOMAD_ADDR as a fallback instead of an override.

Assume the following basic code snippet:

n = nomad.Nomad(host='203.0.113.101')

Debug output from the n object without the NOMAD_ADDR environment variable:

[debug] n.address: None, n.host: 203.0.113.101, n.port: 4646, n.get_uri(): http://203.0.113.101

Debug output from the n object with the NOMAD_ADDR environment variable set to http://host.domain.com:

[debug] n.address: http://host.domain.com, n.host: 203.0.113.101, n.port: 4646, n.get_uri(): http://203.0.113.101
jonathanrcross commented 5 years ago

Hey @ionosphere80, this is definitely an un-desired behavior of the constructor. As a workaround to unblock you you can provider the address kwarg.

n = nomad.Nomad(address='http://203.0.113.101') This will replace https://github.com/jrxFive/python-nomad/blob/master/nomad/__init__.py#L11 and use that value instead of attempting an environment variable lookup of NOMAD_ADDR. With address set you do not need set the host value.

host is mainly left for legacy purposes, the idea behind NOMAD_ADDR lookup is as a fallback but, I do think those assumptions should be better exposed and try to come up with a better implementation.

ionosphere80 commented 5 years ago

I refactored my code to use address= and the NOMAD_ADDR environment variable still overrides it.

jrxFive commented 5 years ago

Hmmm, I'm not able to recreate that which version of python-nomad are you using? Sorry for all the trouble, hopefully we can figure out what going on.

/ # export NOMAD_ADDR="http://example.com:4646"
/ # python
Python 3.7.1 (default, Nov 16 2018, 06:29:01)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nomad
>>> n = nomad.Nomad()
>>> n.address
'http://example.com:4646'
>>> n = nomad.Nomad(address="http://localhost:4646")
>>> n.address
'http://localhost:4646'
ionosphere80 commented 5 years ago

Hmm, I can't seem to reproduce it now either. I'm using version 1.1.0.