<commit_before>import argparse
import logging
import os
import sys
from textwrap import dedent

from cStringIO import StringIO

from . import conf
from . import exc
from . import lsb, hosts
from .cliutil import priority
from .sudo_pushy import get_transport
from .util.wrappers import check_call
from .util.context import remote


LOG = logging.getLogger(__name__)


def get_bootstrap_osd_key(cluster):
    """
    Read the bootstrap-osd key for `cluster`.
    """
    path = '{cluster}.bootstrap-osd.keyring'.format(cluster=cluster)
    try:
        with file(path, 'rb') as f:
            return f.read()
    except IOError:
        raise RuntimeError('bootstrap-osd keyring not found; run \'gatherkeys\'')


def create_osd(conn, logger, cluster, key):
    """
    Run on osd node, writes the bootstrap key if not there yet.

    Returns None on success, error message on error exceptions. pushy
    mangles exceptions to all be of type ExceptionProxy, so we can't
    tell between bug and correctly handled failure, so avoid using
    exceptions for non-exceptional runs.
    """
    path = '/var/lib/ceph/bootstrap-osd/{cluster}.keyring'.format(
        cluster=cluster,
        )
    if not conn.modules.os.path.exists(path):
        logger.info('keyring file does not exist, creating one at: %s' % path)
        tmp = '{path}.{pid}.tmp'.format(
            path=path,
            pid=conn.modules.os.getpid(),
            )

        def write_keyring(tmp, path, key):
            """ create mon keyring file """
            # file() doesn't let us control access mode from the
            # beginning, and thus would have a race where attacker can
            # open before we chmod the file, so play games with os.open
            import os
            fd = os.open(
                tmp,
                (os.O_WRONLY | os.O_CREAT | os.O_EXCL
                 | os.O_NOCTTY | os.O_NOFOLLOW),
                0600,
                )
            with os.fdopen(fd, 'wb') as f:
                f.write(key)
                f.flush()
                os.fsync(f)
            os.rename(tmp, path)

        with remote(conn, logger, write_keyring) as remote_func:
            remote_func(tmp, path, key)

    return check_call(
        conn,
        logger,
        args=[
            'udevadm',
            'trigger',
            '--subsystem-match=block',
            '--action=add',
            ],
        )


def prepare_disk(
        conn,
        logger,
        cluster,
        disk,
        journal,
        activate_prepared_disk,
        zap,
        fs_type,
        dmcrypt,
        dmcrypt_dir):
    """
    Run on osd node, prepares a data disk for use.
    """
    args = [
        'ceph-disk-prepare',
        ]
    if zap:
        args.append('--zap-disk')
    if fs_type:
        if fs_type not in ('btrfs', 'ext4', 'xfs'):
            raise argparse.ArgumentTypeError(
                    "FS_TYPE must be one of 'btrfs', 'ext4' of 'xfs'")
        args.extend(['--fs-type', fs_type])
    if dmcrypt:
        args.append('--dmcrypt')
        if dmcrypt_dir is not None:
            args.append('--dmcrypt-key-dir')
            args.append(dmcrypt_dir)
    args.extend([
        '--cluster',
        cluster,
        '--',
        disk,
    ])
    if journal is not None:
        args.append(journal)

    check_call(
        conn,
        logger,
        args
    )

    if activate_prepared_disk:
        return check_call(
            conn,
            logger,
            args=[
                'udevadm',
                'trigger',<commit_msg>Fix typo in exception message<commit_after>import argparse
import logging
import os
import sys
from textwrap import dedent

from cStringIO import StringIO

from . import conf
from . import exc
from . import lsb, hosts
from .cliutil import priority
from .sudo_pushy import get_transport
from .util.wrappers import check_call
from .util.context import remote


LOG = logging.getLogger(__name__)


def get_bootstrap_osd_key(cluster):
    """
    Read the bootstrap-osd key for `cluster`.
    """
    path = '{cluster}.bootstrap-osd.keyring'.format(cluster=cluster)
    try:
        with file(path, 'rb') as f:
            return f.read()
    except IOError:
        raise RuntimeError('bootstrap-osd keyring not found; run \'gatherkeys\'')


def create_osd(conn, logger, cluster, key):
    """
    Run on osd node, writes the bootstrap key if not there yet.

    Returns None on success, error message on error exceptions. pushy
    mangles exceptions to all be of type ExceptionProxy, so we can't
    tell between bug and correctly handled failure, so avoid using
    exceptions for non-exceptional runs.
    """
    path = '/var/lib/ceph/bootstrap-osd/{cluster}.keyring'.format(
        cluster=cluster,
        )
    if not conn.modules.os.path.exists(path):
        logger.info('keyring file does not exist, creating one at: %s' % path)
        tmp = '{path}.{pid}.tmp'.format(
            path=path,
            pid=conn.modules.os.getpid(),
            )

        def write_keyring(tmp, path, key):
            """ create mon keyring file """
            # file() doesn't let us control access mode from the
            # beginning, and thus would have a race where attacker can
            # open before we chmod the file, so play games with os.open
            import os
            fd = os.open(
                tmp,
                (os.O_WRONLY | os.O_CREAT | os.O_EXCL
                 | os.O_NOCTTY | os.O_NOFOLLOW),
                0600,
                )
            with os.fdopen(fd, 'wb') as f:
                f.write(key)
                f.flush()
                os.fsync(f)
            os.rename(tmp, path)

        with remote(conn, logger, write_keyring) as remote_func:
            remote_func(tmp, path, key)

    return check_call(
        conn,
        logger,
        args=[
            'udevadm',
            'trigger',
            '--subsystem-match=block',
            '--action=add',
            ],
        )


def prepare_disk(
        conn,
        logger,
        cluster,
        disk,
        journal,
        activate_prepared_disk,
        zap,
        fs_type,
        dmcrypt,
        dmcrypt_dir):
    """
    Run on osd node, prepares a data disk for use.
    """
    args = [
        'ceph-disk-prepare',
        ]
    if zap:
        args.append('--zap-disk')
    if fs_type:
        if fs_type not in ('btrfs', 'ext4', 'xfs'):
            raise argparse.ArgumentTypeError(
                    "FS_TYPE must be one of 'btrfs', 'ext4' or 'xfs'")
        args.extend(['--fs-type', fs_type])
    if dmcrypt:
        args.append('--dmcrypt')
        if dmcrypt_dir is not None:
            args.append('--dmcrypt-key-dir')
            args.append(dmcrypt_dir)
    args.extend([
        '--cluster',
        cluster,
        '--',
        disk,
    ])
    if journal is not None:
        args.append(journal)

    check_call(
        conn,
        logger,
        args
    )

    if activate_prepared_disk:
        return check_call(
            conn,
            logger,
            args=[
                'udevadm',
                'trigger',
{'fraction_non_alphanumeric': 0.10131542121466555, 'fraction_numerical': 0.0016792611251049538, 'mean_word_length': 1.6858323938369033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903033', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2093, 'n_words': 607}