ceph_volume: support overriding bind-mounts

This makes it possible to call `podman run` with custom bind-mounts.

cephadm-adopt.yml playbook needs it for a very specific use case:

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2027411

Signed-off-by: Guillaume Abrioux <gabrioux@redhat.com>
(cherry picked from commit b02d71c307)
pull/7010/head
Guillaume Abrioux 2021-11-30 09:52:59 +01:00
parent 1628347253
commit e3b2514e2b
2 changed files with 108 additions and 69 deletions

View File

@ -194,24 +194,40 @@ EXAMPLES = '''
''' '''
def container_exec(binary, container_image): def container_exec(binary, container_image, mounts=None):
''' '''
Build the docker CLI to run a command inside a container Build the docker CLI to run a command inside a container
''' '''
_mounts = {}
_mounts['/run/lock/lvm'] = '/run/lock/lvm:z'
_mounts['/var/run/udev'] = '/var/run/udev:z'
_mounts['/dev'] = '/dev'
_mounts['/etc/ceph'] = '/etc/ceph:z'
_mounts['/run/lvm'] = '/run/lvm'
_mounts['/var/lib/ceph'] = '/var/lib/ceph:z'
_mounts['/var/log/ceph'] = '/var/log/ceph:z'
if mounts is None:
mounts = _mounts
else:
_mounts.update(mounts)
volumes = sum(
[['-v', '{}:{}'.format(src_dir, dst_dir)]
for src_dir, dst_dir in _mounts.items()], [])
container_binary = os.getenv('CEPH_CONTAINER_BINARY') container_binary = os.getenv('CEPH_CONTAINER_BINARY')
command_exec = [container_binary, 'run', command_exec = [container_binary, 'run',
'--rm', '--privileged', '--net=host', '--ipc=host', '--rm',
'-v', '/run/lock/lvm:/run/lock/lvm:z', '--privileged',
'-v', '/var/run/udev/:/var/run/udev/:z', '--net=host',
'-v', '/dev:/dev', '-v', '/etc/ceph:/etc/ceph:z', '--ipc=host'] + volumes + \
'-v', '/run/lvm/:/run/lvm/', ['--entrypoint=' + binary, container_image]
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
'-v', '/var/log/ceph/:/var/log/ceph/:z',
'--entrypoint=' + binary, container_image]
return command_exec return command_exec
def build_cmd(action, container_image, cluster='ceph', binary='ceph-volume'): def build_cmd(action, container_image,
cluster='ceph',
binary='ceph-volume', mounts=None):
''' '''
Build the ceph-volume command Build the ceph-volume command
''' '''
@ -220,7 +236,7 @@ def build_cmd(action, container_image, cluster='ceph', binary='ceph-volume'):
if container_image: if container_image:
cmd = container_exec( cmd = container_exec(
binary, container_image) binary, container_image, mounts=mounts)
else: else:
binary = [binary] binary = [binary]
cmd = binary cmd = binary
@ -409,7 +425,10 @@ def list_osd(module, container_image):
# Build the CLI # Build the CLI
action = ['lvm', 'list'] action = ['lvm', 'list']
cmd = build_cmd(action, container_image, cluster) cmd = build_cmd(action,
container_image,
cluster,
mounts={'/var/lib/ceph': '/var/lib/ceph:ro'})
if data: if data:
cmd.append(data) cmd.append(data)
cmd.append('--format=json') cmd.append('--format=json')

View File

@ -17,15 +17,26 @@ except ImportError:
print('You need the mock library installed on python2.x to run tests') print('You need the mock library installed on python2.x to run tests')
container_cmd = ['docker', 'run', '--rm', '--privileged', def get_mounts(mounts=None):
'--net=host', '--ipc=host', volumes = {}
'-v', '/run/lock/lvm:/run/lock/lvm:z', volumes['/run/lock/lvm'] = '/run/lock/lvm:z'
'-v', '/var/run/udev/:/var/run/udev/:z', volumes['/var/run/udev'] = '/var/run/udev:z'
'-v', '/dev:/dev', '-v', '/etc/ceph:/etc/ceph:z', volumes['/dev'] = '/dev'
'-v', '/run/lvm/:/run/lvm/', volumes['/etc/ceph'] = '/etc/ceph:z'
'-v', '/var/lib/ceph/:/var/lib/ceph/:z', volumes['/run/lvm'] = '/run/lvm'
'-v', '/var/log/ceph/:/var/log/ceph/:z', volumes['/var/lib/ceph'] = '/var/lib/ceph:z'
'--entrypoint=ceph-volume'] volumes['/var/log/ceph'] = '/var/log/ceph:z'
if mounts is not None:
volumes.update(mounts)
return sum([['-v', '{}:{}'.format(src_dir, dst_dir)] for src_dir, dst_dir in volumes.items()], [])
def get_container_cmd(mounts=None):
return ['docker', 'run', '--rm', '--privileged',
'--net=host', '--ipc=host'] + \
get_mounts(mounts) + ['--entrypoint=ceph-volume']
@mock.patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': 'docker'}) @mock.patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': 'docker'})
@ -66,7 +77,7 @@ class TestCephVolumeModule(object):
def test_container_exec(self): def test_container_exec(self):
fake_binary = "ceph-volume" fake_binary = "ceph-volume"
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image] expected_command_list = get_container_cmd() + [fake_container_image]
result = ceph_volume.container_exec(fake_binary, fake_container_image) result = ceph_volume.container_exec(fake_binary, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -74,13 +85,14 @@ class TestCephVolumeModule(object):
fake_module = MagicMock() fake_module = MagicMock()
fake_module.params = {'data': '/dev/sda'} fake_module.params = {'data': '/dev/sda'}
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd() + \
'--cluster', [fake_container_image,
'ceph', '--cluster',
'lvm', 'ceph',
'zap', 'lvm',
'--destroy', 'zap',
'/dev/sda'] '--destroy',
'/dev/sda']
result = ceph_volume.zap_devices(fake_module, fake_container_image) result = ceph_volume.zap_devices(fake_module, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -156,13 +168,17 @@ class TestCephVolumeModule(object):
fake_module = MagicMock() fake_module = MagicMock()
fake_module.params = {'cluster': 'ceph', 'data': '/dev/sda'} fake_module.params = {'cluster': 'ceph', 'data': '/dev/sda'}
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd(
'--cluster', {
'ceph', '/var/lib/ceph': '/var/lib/ceph:ro'
'lvm', }) + \
'list', [fake_container_image,
'/dev/sda', '--cluster',
'--format=json'] 'ceph',
'lvm',
'list',
'/dev/sda',
'--format=json']
result = ceph_volume.list_osd(fake_module, fake_container_image) result = ceph_volume.list_osd(fake_module, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -181,11 +197,12 @@ class TestCephVolumeModule(object):
def test_list_storage_inventory_container(self): def test_list_storage_inventory_container(self):
fake_module = MagicMock() fake_module = MagicMock()
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd() + \
'--cluster', [fake_container_image,
'ceph', '--cluster',
'inventory', 'ceph',
'--format=json'] 'inventory',
'--format=json']
result = ceph_volume.list_storage_inventory(fake_module, fake_container_image) result = ceph_volume.list_storage_inventory(fake_module, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -198,14 +215,15 @@ class TestCephVolumeModule(object):
fake_action = "create" fake_action = "create"
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd() + \
'--cluster', [fake_container_image,
'ceph', '--cluster',
'lvm', 'ceph',
'create', 'lvm',
'--%s' % objectstore, 'create',
'--data', '--%s' % objectstore,
'/dev/sda'] '--data',
'/dev/sda']
result = ceph_volume.prepare_or_create_osd( result = ceph_volume.prepare_or_create_osd(
fake_module, fake_action, fake_container_image) fake_module, fake_action, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -240,14 +258,15 @@ class TestCephVolumeModule(object):
fake_action = "prepare" fake_action = "prepare"
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd() + \
'--cluster', [fake_container_image,
'ceph', '--cluster',
'lvm', 'ceph',
'prepare', 'lvm',
'--%s' % objectstore, 'prepare',
'--data', '--%s' % objectstore,
'/dev/sda'] '--data',
'/dev/sda']
result = ceph_volume.prepare_or_create_osd( result = ceph_volume.prepare_or_create_osd(
fake_module, fake_action, fake_container_image) fake_module, fake_action, fake_container_image)
assert result == expected_command_list assert result == expected_command_list
@ -284,18 +303,19 @@ class TestCephVolumeModule(object):
'batch_devices': ["/dev/sda", "/dev/sdb"]} 'batch_devices': ["/dev/sda", "/dev/sdb"]}
fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest" fake_container_image = "quay.ceph.io/ceph-ci/daemon:latest"
expected_command_list = container_cmd + [fake_container_image, expected_command_list = get_container_cmd() + \
'--cluster', [fake_container_image,
'ceph', '--cluster',
'lvm', 'ceph',
'batch', 'lvm',
'--%s' % objectstore, 'batch',
'--yes', '--%s' % objectstore,
'--prepare', '--yes',
'--journal-size' if objectstore == 'filestore' else '--block-db-size', # noqa E501 '--prepare',
'4096', '--journal-size' if objectstore == 'filestore' else '--block-db-size', # noqa E501
'/dev/sda', '4096',
'/dev/sdb'] '/dev/sda',
'/dev/sdb']
result = ceph_volume.batch( result = ceph_volume.batch(
fake_module, fake_container_image) fake_module, fake_container_image)
assert result == expected_command_list assert result == expected_command_list