Ability to specify mount path mapping from osx->vm->docker

The docker -v command takes the directory structure where the
docker itself is running. So when using with osx using boot2docker
or just a ubuntu with docker, we need to use say sshfs or virtual
box additions to first make the osx file system available on the
operating system where docker is running (vm in boot2docker). Then
we need to be able to specify how the osx directory maps to the
directory on the vm using the new --path-map parameter.

Change-Id: I8543c94accf5e809323f0f348e9729e32d942b80
This commit is contained in:
Davanum Srinivas 2014-10-13 16:39:33 -04:00
parent 43a68e3c66
commit 51bd5fede5
4 changed files with 55 additions and 3 deletions

View File

@ -123,6 +123,13 @@ When running dox you will need to specify the docker username from the boot2dock
dox --user-map=docker:1000:10
If you use VirtualBox guest additions to mount your osx onto the boot2docker vm,
example /Users/your_name/openstack/oslo-incubator as /home/your_name/openstack/oslo-incubator,
then you can add the following mapping::
dos --path-map=/Users/your_name:/home/your_name
Advanced
--------
It is possible to specify multiple images to be used in a dox run.

View File

@ -70,6 +70,11 @@ def parse_args():
help='User to run the container to '
'format is user:uid:gid, with boot2docker use '
'docker:1000:10 (default to your current user)')
parser.add_argument('--path-map', default=os.environ.get("DOX_PATH_MAP"),
help='with boot2docker, specify how osx path maps to '
'linux path. example --path-map /Users:/home, '
'means /Users is available as /home on docker '
'container')
parser.add_argument('-k', '--keep', dest='keep_image', default=False,
action='store_true',
help="Keep test container after command finishes")

View File

@ -37,6 +37,7 @@ class Runner(object):
self.base_image_name = 'dox_%s_base' % self.project
self.test_image_name = 'dox_%s_test' % self.project
self.user_map = self._get_user_mapping()
self.path_map = self._get_path_mapping()
def _get_user_mapping(self):
"""Get user mapping from command line or current user."""
@ -46,6 +47,13 @@ class Runner(object):
username, uid, gid = (os.getlogin(), os.getuid(), os.getgid())
return {'username': username, 'uid': int(uid), 'gid': int(gid)}
def _get_path_mapping(self):
"""Get path mapping from command line."""
if not self.args.path_map:
return None
local, remote = self.args.path_map.split(':', 1)
return {'local': local, 'remote': remote}
def is_docker_installed(self):
try:
self._docker_cmd("version")
@ -155,9 +163,13 @@ class Runner(object):
shutil.rmtree(tempd)
def run_commands(self, command):
path = os.path.abspath('.')
if self.path_map:
path = path.replace(self.path_map['local'],
self.path_map['remote'])
docker_args = ['--privileged=true',
'--user=%s' % self.user_map['username'],
'-v', "%s:/src" % os.path.abspath('.'),
'-v', "%s:/src" % path,
'-w', '/src']
if not self.args.keep_image:
docker_args.append('--rm')

View File

@ -26,7 +26,8 @@ class TestRunner(base.TestCase):
super(TestRunner, self).setUp()
def test_user_mapping(self):
dr = doxrunner.Runner(argparse.Namespace(user_map='foo:100:10'))
dr = doxrunner.Runner(argparse.Namespace(user_map='foo:100:10',
path_map=None))
self.assertEqual('foo', dr.user_map['username'])
self.assertEqual(100, dr.user_map['uid'])
self.assertEqual(10, dr.user_map['gid'])
@ -35,7 +36,34 @@ class TestRunner(base.TestCase):
@mock.patch('os.getgid', return_value=67890)
@mock.patch('os.getlogin', return_value='toto')
def test_user_mapping_default(self, os_uid, os_gid, os_username):
dr = doxrunner.Runner(argparse.Namespace(user_map=None))
dr = doxrunner.Runner(argparse.Namespace(user_map=None,
path_map=None))
self.assertEqual('toto', dr.user_map['username'])
self.assertEqual(12345, dr.user_map['uid'])
self.assertEqual(67890, dr.user_map['gid'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping(self, os_uid, os_gid, os_username):
dr = doxrunner.Runner(argparse.Namespace(path_map='/Users:/home',
user_map=None))
self.assertEqual('/Users', dr.path_map['local'])
self.assertEqual('/home', dr.path_map['remote'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping_extra_colon(self, os_uid, os_gid, os_username):
dr = doxrunner.Runner(argparse.Namespace(path_map='/Users:/home:foo',
user_map=None))
self.assertEqual('/Users', dr.path_map['local'])
self.assertEqual('/home:foo', dr.path_map['remote'])
@mock.patch('os.getuid')
@mock.patch('os.getgid')
@mock.patch('os.getlogin')
def test_path_mapping_default(self, os_uid, os_gid, os_username):
dr = doxrunner.Runner(argparse.Namespace(path_map=None,
user_map=None))
self.assertEqual(None, dr.path_map)