Skip to content
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

Ability to specify Host Devices during container start #372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,29 @@ def search(self, term):
params={'term': term}),
True)

def _parse_devices(self, devices):
device_list = []
for device in devices:
device_mapping = device.split(",")
if device_mapping:
path_on_host = device_mapping[0]
if len(device_mapping) > 1:
path_in_container = device_mapping[1]
else:
path_in_container = path_on_host
if len(device_mapping) > 2:
permissions = device_mapping[2]
else:
permissions = 'rwm'
device_list.append({"PathOnHost": path_on_host,
"PathInContainer": path_in_container,
"CgroupPermissions": permissions})
return device_list

def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
publish_all_ports=False, links=None, privileged=False,
dns=None, dns_search=None, volumes_from=None, network_mode=None,
restart_policy=None, cap_add=None, cap_drop=None):
restart_policy=None, cap_add=None, cap_drop=None, devices=None):
if isinstance(container, dict):
container = container.get('Id')

Expand Down Expand Up @@ -895,6 +914,9 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
if cap_drop:
start_config['CapDrop'] = cap_drop

if devices:
start_config['Devices'] = self._parse_devices(devices)

url = self._url("/containers/{0}/start".format(container))
res = self._post_json(url, data=start_config)
self._raise_for_status(res)
Expand Down
35 changes: 35 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,41 @@ def test_start_container_with_dropped_capabilities(self):
docker.client.DEFAULT_TIMEOUT_SECONDS
)

def test_start_container_with_devices(self):
try:
self.client.start(fake_api.FAKE_CONTAINER_ID,
devices=['/dev/sda:/dev/xvda:rwm',
'/dev/sdb:/dev/xvdb',
'/dev/sdc'])
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
args = fake_request.call_args
self.assertEqual(
args[0][0],
url_prefix + 'containers/3cc2351ab11b/start'
)
self.assertEqual(
json.loads(args[1]['data']),
{"PublishAllPorts": False, "Privileged": False,
"Devices": [{'CgroupPermissions': 'rwm',
'PathInContainer': '/dev/sda:/dev/xvda:rwm',
'PathOnHost': '/dev/sda:/dev/xvda:rwm'},
{'CgroupPermissions': 'rwm',
'PathInContainer': '/dev/sdb:/dev/xvdb',
'PathOnHost': '/dev/sdb:/dev/xvdb'},
{'CgroupPermissions': 'rwm',
'PathInContainer': '/dev/sdc',
'PathOnHost': '/dev/sdc'}]}
)
self.assertEqual(
args[1]['headers'],
{'Content-Type': 'application/json'}
)
self.assertEqual(
args[1]['timeout'],
docker.client.DEFAULT_TIMEOUT_SECONDS
)

def test_resize_container(self):
try:
self.client.resize(
Expand Down