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
Ports --env-file
in minimalistic fashion
#695
Conversation
Thanks for the contribution!
|
Also, it's not clear what happens when a variable is specified both in a file and in the Arguably, docker-py should just provide a utility method to parse a dict of environment variables from a file, and leave the job of building the combined dict to the developer. |
Ok, let me take a look at that.
None really other than I figured that it was better to use a built-in feature instead of re-inventing the wheel (even if a simple
It works fine:
But you're right, the environment file from disk would override the manual one. |
Sorry, I meant: what happens when the same variable (e.g.
Fair enough - I'm just concerned that it could have all kinds of unexpected extra behaviour. For example, does it have special ways of dealing with quotes, or escaped delimiters? Even if those are useful things, we shouldn't deviate from the Docker client's behaviour. |
Yeah, in that case the environment file would override. Not sure if this makes sense or not, but it would be easy to add logic if that isn't the expected behavior.
Yeah, I was thinking about that too when I wrote it. I could not find any documentation for the escape character (and didn't find the code at first glance). The CSV library does have a feature for explicitly set this though so if you have the escape character, I can easily add that. |
It looks like the entire 'create_container_config' function lacks testing. I'm not sure if I'm well suited to write a test for this entire function. |
The problem is that the "expected behaviour" differs for different people. For example, with the Docker client, values specified with This is why I think that, as a first step, it'd be preferable to simply provide a utility function for parsing environment files and leave it to the user to construct an |
I see your point. Yeah, there are benefits with that, but for the use case I have in mind for this it would actually complicate things further, as you'd then have to re-implement the same stuff there (unless I'm missing something). |
You wouldn't be duplicating any logic - the logic for parsing files would live in docker-py, and the logic for building the final environment dict would live in your project. from docker.utils import parse_env_file
env = #... get env option
env_files = #... get env_file option
for filename in env_files:
env.update(parse_env_file(filename)) |
@aanand Let me know what you think. |
Looks much better, thanks.
|
Addressed the feedback, but still working on the tests. I'm having some issues there and have posted it on StackOverflow. |
|
@aanand Tests added. Let me know what you think. |
Thanks!
|
Done.
Sure. Fixed.
Yeah, that was an accidental commit. |
Also, I'm not sure if I should commit the whole |
environment[k] = v | ||
elif line[0] != '#': | ||
raise errors.DockerException( | ||
'Invalid env_file line:\n{0}'.format(line)) |
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.
Rather than checking twice for line[0] != '#'
, I think this could be simplified to:
if line[0] == '#':
continue
parse_line = line.strip().split('=')
if len(parse_line) == 2:
# ...
else:
# ...
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.
Nice! Yes, that's cleaner. Pushed.
Great stuff. Two last things:
|
There's already a test for that here or what do you mean?
Done. |
Great. Needs squashing to one commit, then LGTM. |
commit 4f053a06c1e9e3f63fd5afde60322f676acbdf45 Merge: 9177380 07a99ea Author: Viktor Petersson <vpetersson@wireload.net> Date: Thu Jul 30 14:37:16 2015 +0100 Merge branch 'master' into fixes commit 9177380 Author: Viktor Petersson <vpetersson@wireload.net> Date: Thu Jul 30 14:00:51 2015 +0100 Tweaks exception message. commit 6a5832e Author: Viktor Petersson <vpetersson@wireload.net> Date: Thu Jul 30 13:17:32 2015 +0100 Simplifies logic as per feedback. commit f750edd Author: Viktor Petersson <vpetersson@wireload.net> Date: Thu Jul 30 11:09:14 2015 +0100 Move return from list to dict. Adds exception handling. commit 8e50f57 Author: Viktor Petersson <vpetersson@wireload.net> Date: Thu Jul 30 10:15:58 2015 +0100 Reverts change to .gitignore. commit 5ba2c1b Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 21:15:21 2015 +0100 Fixes feedback. Adds three unittests. commit e1c719e Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 17:00:16 2015 +0100 WIP Adds test for parse_env_file commit 4448ae7 Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 16:42:49 2015 +0100 Excludes coverage files. commit 19a5d01 Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 16:42:42 2015 +0100 Switch fixes logic. commit a8094c6 Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 11:45:56 2015 +0100 Implements logic for envfile parsing from Docker-cli Ref: https://github.com/docker/docker/blob/master/opts/envfile.go#L19-L51 commit ea9bfd9 Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 11:41:23 2015 +0100 Replaces CSV module with manual splitting. commit a001d28 Author: Viktor Petersson <vpetersson@wireload.net> Date: Wed Jul 29 11:35:37 2015 +0100 Removes isinstance on filename. commit 419d596 Author: Viktor Petersson <vpetersson@wireload.net> Date: Tue Jul 28 22:39:33 2015 +0100 Reflects @aanand's feedback. commit e81e3c8 Author: Viktor Petersson <vpetersson@wireload.net> Date: Tue Jul 28 15:43:32 2015 +0100 Typo fix. commit 2898389 Author: Viktor Petersson <vpetersson@wireload.net> Date: Tue Jul 28 15:41:08 2015 +0100 Refs docker#565. Adds minimal implementation of env_file client-side support.
Done like dinner! |
@aanand can we land this? Would like to start working on the Ansible side of things :) |
It's got my LGTM, but another maintainer needs to approve and merge it. |
Merged - thanks! |
Refs #565. Adds support for providing environment variables from a file instead of manually providing them one-by-one.