Squashed commit of the following:

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 9177380ae9abf4ca01cf7a2a8b7de6640ed5f5d5
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Thu Jul 30 14:00:51 2015 +0100

    Tweaks exception message.

commit 6a5832e2f6655835a6b87897d7ec82d6ef5faa50
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Thu Jul 30 13:17:32 2015 +0100

    Simplifies logic as per feedback.

commit f750eddc34e5356dcfb53c16a7f98a99e817fa53
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 8e50f57cceb6370b7f7b41624f50d5a6835301a0
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Thu Jul 30 10:15:58 2015 +0100

    Reverts change to .gitignore.

commit 5ba2c1b29706ddb74bef9c24c2a6e64bb369db22
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 21:15:21 2015 +0100

    Fixes feedback. Adds three unittests.

commit e1c719e61993fdaec0f22ba0de706af80587dc16
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 17:00:16 2015 +0100

    WIP Adds test for parse_env_file

commit 4448ae72e55d889d9d194a2c0303d182cb157d4c
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 16:42:49 2015 +0100

    Excludes coverage files.

commit 19a5d01615dd9b7ee939f54f256e5bae89a94ee1
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 16:42:42 2015 +0100

    Switch fixes logic.

commit a8094c63a9f4351ae71393ea5603aa11c5c5bc94
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 ea9bfd95dfafe4023c58ab37f990158f73eb2e0d
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 11:41:23 2015 +0100

    Replaces CSV module with manual splitting.

commit a001d28ff48309d2b2338aaf27253fdfaa0f6c4b
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Wed Jul 29 11:35:37 2015 +0100

    Removes isinstance on filename.

commit 419d5961f6103df9166be3a9baa549276c12223d
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Tue Jul 28 22:39:33 2015 +0100

    Reflects @aanand's feedback.

commit e81e3c8ed797ff939843d2485bf15525e85e890d
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Tue Jul 28 15:43:32 2015 +0100

    Typo fix.

commit 2898389cada2bfca64bdfa71359aebeb3b5b6d1b
Author: Viktor Petersson <vpetersson@wireload.net>
Date:   Tue Jul 28 15:41:08 2015 +0100

    Refs #565. Adds minimal implementation of env_file client-side support.
This commit is contained in:
Viktor Petersson 2015-07-30 14:39:37 +01:00
parent 07a99ea5c7
commit d400717ff8
4 changed files with 84 additions and 2 deletions

View File

@ -2,7 +2,7 @@ from .utils import (
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

View File

@ -518,6 +518,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 = {}
with open(env_file, 'r') as f:
for line in f:
if line[0] == '#':
continue
parse_line = line.strip().split('=')
if len(parse_line) == 2:
k = parse_line[0]
v = parse_line[1]
environment[k] = v
else:
raise errors.DockerException(
'Invalid line in environment file {0}:\n{1}'.format(
env_file, line))
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,
@ -528,6 +554,7 @@ def create_container_config(
):
if isinstance(command, six.string_types):
command = shlex.split(str(command))
if isinstance(environment, dict):
environment = [
six.text_type('{0}={1}').format(k, v)

View File

@ -234,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

View File

@ -1,12 +1,13 @@
import os
import os.path
import unittest
import tempfile
from docker.client import Client
from docker.errors import DockerException
from docker.utils import (
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
create_host_config, Ulimit, LogConfig, parse_bytes
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file
)
from docker.utils.ports import build_port_bindings, split_port
from docker.auth import resolve_repository_name, resolve_authconfig
@ -17,6 +18,17 @@ import base
class UtilsTest(base.BaseTestCase):
longMessage = True
def generate_tempfile(self, file_content=None):
"""
Generates a temporary file for tests with the content
of 'file_content' and returns the filename.
Don't forget to unlink the file with os.unlink() after.
"""
local_tempfile = tempfile.NamedTemporaryFile(delete=False)
local_tempfile.write(file_content.encode('UTF-8'))
local_tempfile.close()
return local_tempfile.name
def setUp(self):
self.os_environ = os.environ.copy()
@ -95,6 +107,28 @@ class UtilsTest(base.BaseTestCase):
except TypeError as e:
self.fail(e)
def test_parse_env_file_proper(self):
env_file = self.generate_tempfile(
file_content='USER=jdoe\nPASS=secret')
get_parse_env_file = parse_env_file(env_file)
self.assertEqual(get_parse_env_file,
{'USER': 'jdoe', 'PASS': 'secret'})
os.unlink(env_file)
def test_parse_env_file_commented_line(self):
env_file = self.generate_tempfile(
file_content='USER=jdoe\n#PASS=secret')
get_parse_env_file = parse_env_file((env_file))
self.assertEqual(get_parse_env_file, {'USER': 'jdoe'})
os.unlink(env_file)
def test_parse_env_file_invalid_line(self):
env_file = self.generate_tempfile(
file_content='USER jdoe')
self.assertRaises(
DockerException, parse_env_file, env_file)
os.unlink(env_file)
def test_convert_filters(self):
tests = [
({'dangling': True}, '{"dangling": ["true"]}'),