From bc58b5f3c17dad6b9c750be992f7326ebcb0b7f4 Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Thu, 31 Jan 2019 13:45:46 +0100 Subject: [PATCH] ceph-volume: add --all flag to simple activate This is intended to behave similarly to the lvm activate --all argument. When passed, c-v will scan /etc/ceph/osd/ (or the location specified by CEPH_VOLUME_SIMPLE_JSON_DIR) for json files (glob *.json) and call activate for each file. This should greatly ease the take-over of ceph-disk OSDs with manual commands and deployment tools like DeepSea and ceph-ansible. Also adds a simple unit test and modifies the simple/centos7/filestore/activate functional test to use --all. Signed-off-by: Jan Fajerski (cherry picked from commit 05af94c6fca66083107e96e9aa1db87619e77e66) Conflicts: src/ceph-volume/ceph_volume/tests/functional/simple/centos7/filestore/activate/test.yml resolved by deleting everything after conflict marker --- .../ceph_volume/devices/simple/activate.py | 34 ++++++++++++++----- .../tests/devices/simple/test_activate.py | 20 +++++++++++ .../centos7/filestore/activate/test.yml | 4 +-- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/ceph-volume/ceph_volume/devices/simple/activate.py b/src/ceph-volume/ceph_volume/devices/simple/activate.py index 814c6fe3710df..3cf414fdc73a1 100644 --- a/src/ceph-volume/ceph_volume/devices/simple/activate.py +++ b/src/ceph-volume/ceph_volume/devices/simple/activate.py @@ -1,6 +1,7 @@ from __future__ import print_function import argparse import base64 +import glob import json import logging import os @@ -230,6 +231,12 @@ class Activate(object): nargs='?', help='The FSID of the OSD, similar to a SHA1' ) + parser.add_argument( + '--all', + help='Activate all OSDs with a OSD JSON config', + action='store_true', + default=False, + ) parser.add_argument( '--file', help='The path to a JSON file, from a scanned OSD' @@ -244,7 +251,7 @@ class Activate(object): print(sub_command_help) return args = parser.parse_args(self.argv) - if not args.file: + if not args.file and not args.all: if not args.osd_id and not args.osd_fsid: terminal.error('ID and FSID are required to find the right OSD to activate') terminal.error('from a scanned OSD location in /etc/ceph/osd/') @@ -253,13 +260,22 @@ class Activate(object): # implicitly indicate that it would be possible to activate a json file # at a non-default location which would not work at boot time if the # custom location is not exposed through an ENV var + self.skip_systemd = args.skip_systemd json_dir = os.environ.get('CEPH_VOLUME_SIMPLE_JSON_DIR', '/etc/ceph/osd/') - if args.file: - json_config = args.file + if args.all: + if args.file or args.osd_id: + mlogger.warn('--all was passed, ignoring --file and ID/FSID arguments') + json_configs = glob.glob('{}/*.json'.format(json_dir)) + for json_config in json_configs: + mlogger.info('activating OSD specified in {}'.format(json_config)) + args.json_config = json_config + self.activate(args) else: - json_config = os.path.join(json_dir, '%s-%s.json' % (args.osd_id, args.osd_fsid)) - if not os.path.exists(json_config): - raise RuntimeError('Expected JSON config path not found: %s' % json_config) - args.json_config = json_config - self.skip_systemd = args.skip_systemd - self.activate(args) + if args.file: + json_config = args.file + else: + json_config = os.path.join(json_dir, '%s-%s.json' % (args.osd_id, args.osd_fsid)) + if not os.path.exists(json_config): + raise RuntimeError('Expected JSON config path not found: %s' % json_config) + args.json_config = json_config + self.activate(args) diff --git a/src/ceph-volume/ceph_volume/tests/devices/simple/test_activate.py b/src/ceph-volume/ceph_volume/tests/devices/simple/test_activate.py index a275bdd00689c..885a6ec25437f 100644 --- a/src/ceph-volume/ceph_volume/tests/devices/simple/test_activate.py +++ b/src/ceph-volume/ceph_volume/tests/devices/simple/test_activate.py @@ -22,6 +22,26 @@ class TestActivate(object): stdout, stderr = capsys.readouterr() assert 'Activate OSDs by mounting devices previously configured' in stdout + def test_activate_all(self, is_root, monkeypatch): + ''' + make sure Activate calls activate for each file returned by glob + ''' + mocked_glob = [] + def mock_glob(glob): + path = os.path.dirname(glob) + mocked_glob.extend(['{}/{}.json'.format(path, file_) for file_ in + ['1', '2', '3']]) + return mocked_glob + activate_files = [] + def mock_activate(self, args): + activate_files.append(args.json_config) + monkeypatch.setattr('glob.glob', mock_glob) + monkeypatch.setattr(activate.Activate, 'activate', mock_activate) + activate.Activate(['--all']).main() + assert activate_files == mocked_glob + + + class TestEnableSystemdUnits(object): diff --git a/src/ceph-volume/ceph_volume/tests/functional/simple/centos7/filestore/activate/test.yml b/src/ceph-volume/ceph_volume/tests/functional/simple/centos7/filestore/activate/test.yml index 24e2c0353c942..0745f2571ff5f 100644 --- a/src/ceph-volume/ceph_volume/tests/functional/simple/centos7/filestore/activate/test.yml +++ b/src/ceph-volume/ceph_volume/tests/functional/simple/centos7/filestore/activate/test.yml @@ -24,8 +24,6 @@ register: osd_configs - name: activate all scanned OSDs - command: "ceph-volume --cluster={{ cluster }} simple activate --file {{ item.path }}" + command: "ceph-volume --cluster={{ cluster }} simple activate --all" environment: CEPH_VOLUME_DEBUG: 1 - with_items: - - "{{ osd_configs.files }}" -- 2.39.5