Skip to content

Commit

Permalink
Reflects @aanand's feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetersson committed Jul 28, 2015
1 parent e81e3c8 commit 419d596
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docker/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
compare_version, convert_port_bindings, convert_volume_binds,
mkbuildcontext, tar, parse_repository_tag, parse_host,
kwargs_from_env, convert_filters, create_host_config,
create_container_config, parse_bytes, ping_registry
create_container_config, parse_bytes, ping_registry, parse_env_file
) # flake8: noqa

from .types import Ulimit, LogConfig # flake8: noqa
Expand Down
39 changes: 20 additions & 19 deletions docker/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,32 @@ def create_host_config(
return host_config


def parse_env_file(env_file):
"""
Reads a line-separated environment file.
The format of each line should be "key=value".
"""
environment = []

if isinstance(env_file, str):
if os.path.isfile(env_file):
with open(env_file, 'r') as f:
for line in csv.reader(f, delimiter='='):
if len(line) == 2:
k = line[0].strip()
v = line[1].strip()
environment.append('{0}={1}'.format(k, v))

return environment


def create_container_config(
version, image, command, hostname=None, user=None, detach=False,
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
dns=None, volumes=None, volumes_from=None, network_disabled=False,
entrypoint=None, cpu_shares=None, working_dir=None, domainname=None,
memswap_limit=None, cpuset=None, host_config=None, mac_address=None,
labels=None, volume_driver=None, env_file=None
labels=None, volume_driver=None
):
if isinstance(command, six.string_types):
command = shlex.split(str(command))
Expand All @@ -536,24 +555,6 @@ def create_container_config(
for k, v in six.iteritems(environment)
]

if isinstance(env_file, str):
if os.path.isfile(env_file):

# Re-classify None to list if needed
if isinstance(environment, type(None)):
environment = []

# Re-classify string to list if needed
if isinstance(environment, str):
environment = environment.split(',')

with open(env_file, 'r') as f:
for line in csv.reader(f, delimiter='='):
if len(line) == 2:
k = line[0].strip()
v = line[1].strip()
environment.append('{0}={1}'.format(k, v))

if labels is not None and compare_version('1.18', version) < 0:
raise errors.InvalidVersion(
'labels were only introduced in API version 1.18'
Expand Down
26 changes: 21 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ where unit = b, k, m, or g)
* ports (list of ints): A list of port numbers
* environment (dict or list): A dictionary or a list of strings in the
following format `["PASSWORD=xxx"]` or `{"PASSWORD": "xxx"}`.
* env_file (file): A line-separated list of environment variables, such as:
```
USERNAME=jdoe
PASSWORD=secret
```
* dns (list): DNS name servers
* volumes (str or list):
* volumes_from (str or list): List of container names or Ids to get volumes
Expand Down Expand Up @@ -239,6 +234,27 @@ from. Optionally a single string joining container id's with commas
'Warnings': None}
```

### parse_env_file

A utility for parsing an environment file.

The expected format of the file is as follows:

```
USERNAME=jdoe
PASSWORD=secret
```

The utility can be used as follows:

```python
>> import docker.utils
>> my_envs = docker.utils.parse_env_file('/path/to/file')
>> docker.utils.create_container_config('1.18', '_mongodb', 'foobar', environment=my_envs)
```

You can now use this with 'environment' for `create_container`.

## diff

Inspect changes on a container's filesystem
Expand Down

0 comments on commit 419d596

Please sign in to comment.