|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# This file is part of node-cli |
| 4 | +# |
| 5 | +# Copyright (C) 2021 SKALE Labs |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Affero General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Affero General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Affero General Public License |
| 18 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +import glob |
| 21 | +import logging |
| 22 | +import os |
| 23 | +import shutil |
| 24 | +import tempfile |
| 25 | + |
| 26 | +from node_cli.utils.helper import run_cmd |
| 27 | +from node_cli.utils.git_utils import sync_repo |
| 28 | +from node_cli.configs import ( |
| 29 | + DOCKER_LVMPY_PATH, |
| 30 | + DOCKER_LVMPY_REPO_URL, |
| 31 | + FILESTORAGE_MAPPING, |
| 32 | + SCHAINS_MNT_DIR, |
| 33 | + SKALE_STATE_DIR |
| 34 | +) |
| 35 | + |
| 36 | +logger = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +class FilesystemExistsError(Exception): |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +def update_docker_lvmpy_env(env): |
| 44 | + env['PHYSICAL_VOLUME'] = env['DISK_MOUNTPOINT'] |
| 45 | + env['VOLUME_GROUP'] = 'schains' |
| 46 | + env['FILESTORAGE_MAPPING'] = FILESTORAGE_MAPPING |
| 47 | + env['SCHAINS_MNT_DIR'] = SCHAINS_MNT_DIR |
| 48 | + env['PATH'] = os.environ.get('PATH', None) |
| 49 | + return env |
| 50 | + |
| 51 | + |
| 52 | +def ensure_filestorage_mapping(mapping_dir=FILESTORAGE_MAPPING): |
| 53 | + if not os.path.isdir(FILESTORAGE_MAPPING): |
| 54 | + os.makedirs(FILESTORAGE_MAPPING) |
| 55 | + |
| 56 | + |
| 57 | +def sync_docker_lvmpy_repo(env): |
| 58 | + if os.path.isdir(DOCKER_LVMPY_PATH): |
| 59 | + shutil.rmtree(DOCKER_LVMPY_PATH) |
| 60 | + sync_repo( |
| 61 | + DOCKER_LVMPY_REPO_URL, |
| 62 | + DOCKER_LVMPY_PATH, |
| 63 | + env["DOCKER_LVMPY_STREAM"] |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def docker_lvmpy_update(env): |
| 68 | + sync_docker_lvmpy_repo(env) |
| 69 | + ensure_filestorage_mapping() |
| 70 | + logger.info('Running docker-lvmpy update script') |
| 71 | + update_docker_lvmpy_env(env) |
| 72 | + run_cmd( |
| 73 | + cmd=f'sudo -H -E {DOCKER_LVMPY_PATH}/scripts/update.sh'.split(), |
| 74 | + env=env |
| 75 | + ) |
| 76 | + logger.info('docker-lvmpy update done') |
| 77 | + |
| 78 | + |
| 79 | +def docker_lvmpy_install(env): |
| 80 | + sync_docker_lvmpy_repo(env) |
| 81 | + ensure_filestorage_mapping() |
| 82 | + update_docker_lvmpy_env(env) |
| 83 | + run_cmd( |
| 84 | + cmd=f'sudo -H -E {DOCKER_LVMPY_PATH}/scripts/install.sh'.split(), |
| 85 | + env=env |
| 86 | + ) |
| 87 | + logger.info('docker-lvmpy installed') |
| 88 | + |
| 89 | + |
| 90 | +def block_device_mounted(block_device): |
| 91 | + with open('/proc/mounts') as mounts: |
| 92 | + return any(block_device in mount for mount in mounts.readlines()) |
| 93 | + |
| 94 | + |
| 95 | +def ensure_not_mounted(block_device): |
| 96 | + logger.info('Making sure %s is not mounted', block_device) |
| 97 | + if block_device_mounted(block_device): |
| 98 | + run_cmd(['umount', block_device]) |
| 99 | + |
| 100 | + |
| 101 | +def cleanup_static_path(filestorage_mapping=FILESTORAGE_MAPPING): |
| 102 | + logger.info('Removing all links from filestorage mapping') |
| 103 | + for dir_link in glob.glob(os.path.join(filestorage_mapping, '*')): |
| 104 | + logger.debug('Unlinking %s', dir_link) |
| 105 | + if os.path.islink(dir_link): |
| 106 | + os.unlink(dir_link) |
| 107 | + |
| 108 | + |
| 109 | +def cleanup_volume_artifacts(block_device): |
| 110 | + ensure_not_mounted(block_device) |
| 111 | + cleanup_static_path() |
| 112 | + |
| 113 | + |
| 114 | +def get_block_device_filesystem(block_device): |
| 115 | + r = run_cmd(['blkid', '-o', 'udev', block_device]) |
| 116 | + output = r.stdout.decode('utf-8') |
| 117 | + logger.debug('blkid output %s', output) |
| 118 | + fs_line = next(filter(lambda s: s.startswith('ID_FS_TYPE'), output.split('\n'))) |
| 119 | + return fs_line.split('=')[1] |
| 120 | + |
| 121 | + |
| 122 | +def format_as_btrfs(block_device): |
| 123 | + logger.info('Formating %s as btrfs', block_device) |
| 124 | + run_cmd(['mkfs.btrfs', block_device, '-f']) |
| 125 | + |
| 126 | + |
| 127 | +def mount_device(block_device, mountpoint): |
| 128 | + os.makedirs(mountpoint, exist_ok=True) |
| 129 | + logger.info('Mounting %s device', block_device) |
| 130 | + run_cmd(['mount', block_device, mountpoint]) |
| 131 | + |
| 132 | + |
| 133 | +def prepare_block_device(block_device, force=False): |
| 134 | + filesystem = None |
| 135 | + try: |
| 136 | + filesystem = get_block_device_filesystem(block_device) |
| 137 | + except Exception as e: |
| 138 | + logger.info('Cannot get filesystem type %s', e) |
| 139 | + logger.debug('Cannot get filesystem type', exc_info=True) |
| 140 | + if not force: |
| 141 | + raise |
| 142 | + |
| 143 | + if filesystem == 'btrfs': |
| 144 | + logger.info('%s already formatted as btrfs', block_device) |
| 145 | + ensure_btrfs_for_all_space(block_device) |
| 146 | + else: |
| 147 | + logger.info('%s contains %s filesystem', block_device, filesystem) |
| 148 | + format_as_btrfs(block_device) |
| 149 | + mountpoint = os.path.join(SKALE_STATE_DIR, 'schains') |
| 150 | + mount_device(block_device, mountpoint) |
| 151 | + |
| 152 | + |
| 153 | +def max_resize_btrfs(path): |
| 154 | + run_cmd(['btrfs', 'filesystem', 'resize', 'max', path]) |
| 155 | + |
| 156 | + |
| 157 | +def ensure_btrfs_for_all_space(block_device): |
| 158 | + with tempfile.TemporaryDirectory(dir=SKALE_STATE_DIR) as mountpoint: |
| 159 | + try: |
| 160 | + mount_device(block_device, mountpoint) |
| 161 | + logger.info('Resizing btrfs filesystem for %s', block_device) |
| 162 | + max_resize_btrfs(mountpoint) |
| 163 | + finally: |
| 164 | + ensure_not_mounted(block_device) |
0 commit comments