import logging
from ceph_deploy import hosts, exc
from ceph_deploy.lib.remoto import process


LOG = logging.getLogger(__name__)


def distro_is_supported(distro_name):
    """
    An enforcer of supported distros that can differ from what ceph-deploy
    supports.
    """
    supported = ['centos', 'redhat', 'ubuntu', 'debian']
    if distro_name in supported:
        return True
    return False


def connect(args):
    cd_conf = getattr(args, 'cd_conf', None)
    if not cd_conf:
        raise RuntimeError(
            'a ceph-deploy configuration is required but was not found'
        )

    repo_name = args.release or 'calamari-minion'
    has_minion_repo = cd_conf.has_section(repo_name)

    if not has_minion_repo:
        raise RuntimeError('no calamari-minion repo found')

    # We rely on the default for repo installs that does not
    # install ceph unless specified otherwise
    options = dict(cd_conf.items(repo_name))

    for hostname in args.hosts:
        distro = hosts.get(hostname, username=args.username)
        if not distro_is_supported(distro.normalized_name):
            raise exc.UnsupportedPlatform(
                distro.distro_name,
                distro.codename,
                distro.release
            )

        LOG.info(
            'Distro info: %s %s %s',
            distro.name,
            distro.release,
            distro.codename
        )
        rlogger = logging.getLogger(hostname)
        rlogger.info('installing calamari-minion package on %s' % hostname)
        rlogger.info('adding custom repository file')
        try:
            distro.repo_install(
                distro,
                repo_name,
                options.pop('baseurl'),
                options.pop('gpgkey', ''),  # will probably not use a gpgkey
                **options
            )
        except KeyError as err:
            raise RuntimeError(
                'missing required key: %s in config section: %s' % (
                    err,
                    repo_name
                )
            )

        distro.pkg.install(distro, 'salt-minion')

        # redhat/centos need to get the service started
        if distro.normalized_name in ['redhat', 'centos']:
            process.run(
                distro.conn,
                ['chkconfig', 'salt-minion', 'on']
            )

            process.run(
                distro.conn,
                ['service', 'salt-minion', 'start']
            )

        distro.conn.exit()


def action(args):
    if args.subcommand == 'connect':
        connect(args)


def make(parser):
    """
    Install and configure Calamari nodes
    """
    parser.add_argument(
        'subcommand',
        choices=[
            'connect',
            ],
        )

    parser.add_argument(
        '--release',
        nargs='?',
        metavar='CODENAME',
        help="Use a given release from repositories\
                defined in ceph-deploy's configuration. Defaults to\
                'calamari-minion'",

    )

    parser.add_argument(
        'hosts',
        nargs='+',
    )

    parser.set_defaults(
        func=action,
    )

@@ -1,12 +1,25 @@
+import errno%0A
 import loggi
@@ -21,16 +21,26 @@
 logging%0A
+import os%0A
 from cep
@@ -2060,32 +2060,677 @@
 %0A            )%0A%0A
+        # Emplace minion config prior to installation so that it is present%0A        # when the minion first starts.%0A        minion_config_dir = os.path.join('/etc/salt/', 'minion.d')%0A        minion_config_file = os.path.join(minion_config_dir, 'calamari.conf')%0A%0A        rlogger.debug('creating config dir: %25s' %25 minion_config_dir)%0A        distro.conn.remote_module.makedir(minion_config_dir, %5Berrno.EEXIST%5D)%0A%0A        rlogger.debug(%0A            'creating the calamari salt config: %25s' %25 minion_config_file%0A        )%0A        distro.conn.remote_module.write_file(%0A            minion_config_file,%0A            'master: %25s%5Cn' %25 args.master%0A        )%0A%0A
         distro.p
@@ -3668,32 +3668,193 @@
 nion'%22,%0A%0A    )%0A%0A
+    parser.add_argument(%0A        '--master',%0A        nargs='?',%0A        metavar='MASTER SERVER',%0A        help=%22The domain for the Calamari master server%22%0A    )%0A%0A
     parser.add_a

add an argument to define master