import errno
import logging
import os
from ceph_deploy import hosts, exc
from ceph_deploy.lib import remoto


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):
    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
        )
        LOG.info('assuming that a repository with Calamari packages is already configured.')
        LOG.info('Refer to the docs for examples (http://ceph.com/ceph-deploy/docs/conf.html)')

        rlogger = logging.getLogger(hostname)

        # Emplace minion config prior to installation so that it is present
        # when the minion first starts.
        minion_config_dir = os.path.join('/etc/salt/', 'minion.d')
        minion_config_file = os.path.join(minion_config_dir, 'calamari.conf')

        rlogger.debug('creating config dir: %s' % minion_config_dir)
        distro.conn.remote_module.makedir(minion_config_dir, [errno.EEXIST])

        rlogger.debug(
            'creating the calamari salt config: %s' % minion_config_file
        )
        distro.conn.remote_module.write_file(
            minion_config_file,
            'master: %s\n' % args.master
        )

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

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

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

        distro.conn.exit()


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


def make(parser):
    """
    Install and configure Calamari nodes. Assumes that a repository with
    Calamari packages is already configured. Refer to the docs for examples
    (http://ceph.com/ceph-deploy/docs/conf.html)
    """
    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(
        '--master',
        nargs='?',
        metavar='MASTER SERVER',
        help="The domain for the Calamari master server"
    )

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

    parser.set_defaults(
        func=calamari,
    )

@@ -2650,267 +2650,8 @@
  )%0A%0A
-    parser.add_argument(%0A        '--release',%0A        nargs='?',%0A        metavar='CODENAME',%0A        help=%22Use a given release from repositories%5C%0A                defined in ceph-deploy's configuration. Defaults to%5C%0A                'calamari-minion'%22,%0A%0A    )%0A%0A
     

remove --release flag as it is not longer needed for custom repos