]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-detect-init: ceph-disk helper to select the init system
authorLoic Dachary <ldachary@redhat.com>
Sat, 2 May 2015 14:25:22 +0000 (16:25 +0200)
committerLoic Dachary <ldachary@redhat.com>
Tue, 5 May 2015 23:54:32 +0000 (01:54 +0200)
On Ubuntu 14.04

$ ceph-detect-init --default=sysvinit
upstart

The ceph-detect-init helper is a copy/paste of the ceph-deploy init
system detection found at:

https://github.com/ceph/ceph-deploy/blob/4992ef0993c5ab41710c1f1e126886d43c78cc3a/ceph_deploy/hosts/__init__.py#L15

It is meant to be used by both ceph-disk and ceph-deploy to avoid
duplicating the logic. Some operating systems implement more than one
init system and ceph-detect-init will not return the default one, it
will return the init system suitable for ceph deployment.

ceph-detect-init is implemented as a standalone python module suitable
to be published at https://pypi.python.org/pypi/ceph-detect-init, with
unit tests and integration tests (based on docker) for the following
platforms:

centos-6
centos-7
debian-jessie
debian-sid
debian-squeeze
debian-wheezy
fedora-21
opensuse-13.1
opensuse-13.2
ubuntu-12.04
ubuntu-14.04
ubuntu-15.04

The tests can be run without network access with run-tox.sh, provided
pip wheel previously populated the wheelhouse directory.

Signed-off-by: Owen Synge <osynge@suse.com>
Signed-off-by: Loic Dachary <ldachary@redhat.com>
31 files changed:
src/ceph-detect-init/.gitignore [new file with mode: 0644]
src/ceph-detect-init/AUTHORS.rst [new file with mode: 0644]
src/ceph-detect-init/MANIFEST.in [new file with mode: 0644]
src/ceph-detect-init/README.rst [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/__init__.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/centos/__init__.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/debian/__init__.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/exc.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/fedora/__init__.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/main.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/rhel/__init__.py [new file with mode: 0644]
src/ceph-detect-init/ceph_detect_init/suse/__init__.py [new file with mode: 0644]
src/ceph-detect-init/integration/centos-6.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/centos-7.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/debian-jessie.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/debian-sid.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/debian-squeeze.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/debian-wheezy.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/fedora-21.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/opensuse-13.1.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/opensuse-13.2.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/test_main.py [new file with mode: 0644]
src/ceph-detect-init/integration/ubuntu-12.04.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/ubuntu-14.04.dockerfile [new file with mode: 0644]
src/ceph-detect-init/integration/ubuntu-15.04.dockerfile [new file with mode: 0644]
src/ceph-detect-init/requirements.txt [new file with mode: 0644]
src/ceph-detect-init/run-tox.sh [new file with mode: 0755]
src/ceph-detect-init/setup.py [new file with mode: 0644]
src/ceph-detect-init/test-requirements.txt [new file with mode: 0644]
src/ceph-detect-init/tests/test_all.py [new file with mode: 0644]
src/ceph-detect-init/tox.ini [new file with mode: 0644]

diff --git a/src/ceph-detect-init/.gitignore b/src/ceph-detect-init/.gitignore
new file mode 100644 (file)
index 0000000..bcfa281
--- /dev/null
@@ -0,0 +1,13 @@
+*~
+*.pyc
+*.pyo
+.coverage
+.tox
+*.egg-info
+*.egg
+dist
+build
+wheelhouse
+*.log
+*.trs
+
diff --git a/src/ceph-detect-init/AUTHORS.rst b/src/ceph-detect-init/AUTHORS.rst
new file mode 100644 (file)
index 0000000..3818d35
--- /dev/null
@@ -0,0 +1,2 @@
+- Owen Synge <osynge@suse.com>
+- Loic Dachary <loic@dachary.org>
diff --git a/src/ceph-detect-init/MANIFEST.in b/src/ceph-detect-init/MANIFEST.in
new file mode 100644 (file)
index 0000000..23abe0d
--- /dev/null
@@ -0,0 +1 @@
+include AUTHORS.rst
diff --git a/src/ceph-detect-init/README.rst b/src/ceph-detect-init/README.rst
new file mode 100644 (file)
index 0000000..e40f22f
--- /dev/null
@@ -0,0 +1,28 @@
+ceph-detect-init
+================
+
+ceph-detect-init is a command line tool that displays a normalized
+string describing the init system of the host on which it is running:
+
+Home page : https://pypi.python.org/pypi/ceph-detect-init
+
+Hacking
+=======
+
+* Get the code : git clone https://git.ceph.com/ceph.git
+* Run the unit tests : tox
+* Run the integration tests (requires docker) : tox -e integration
+* Check the documentation : rst2html < README.rst > /tmp/a.html
+* Prepare a new version
+
+ - version=1.0.0 ; perl -pi -e "s/^version.*/version='$version',/" setup.py ; do python setup.py sdist ; amend=$(git log -1 --oneline | grep --quiet "version $version" && echo --amend) ; git commit $amend -m "version $version" setup.py ; git tag -a -f -m "version $version" $version ; done
+
+* Publish a new version
+
+ - python setup.py sdist upload --sign
+ - git push ; git push --tags
+
+* pypi maintenance
+
+ - python setup.py register # if the project does not yet exist
+ - trim old versions at https://pypi.python.org/pypi/ceph-detect-init
diff --git a/src/ceph-detect-init/ceph_detect_init/__init__.py b/src/ceph-detect-init/ceph_detect_init/__init__.py
new file mode 100644 (file)
index 0000000..04f993b
--- /dev/null
@@ -0,0 +1,111 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Alfredo Deza <adeza@redhat.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Library Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library Public License for more details.
+#
+from ceph_detect_init import centos
+from ceph_detect_init import debian
+from ceph_detect_init import exc
+from ceph_detect_init import fedora
+from ceph_detect_init import rhel
+from ceph_detect_init import suse
+import logging
+import platform
+
+
+def get(use_rhceph=False):
+    distro_name, release, codename = platform_information()
+    if not codename or not _get_distro(distro_name):
+        raise exc.UnsupportedPlatform(
+            distro=distro_name,
+            codename=codename,
+            release=release)
+
+    module = _get_distro(distro_name, use_rhceph=use_rhceph)
+    module.name = distro_name
+    module.normalized_name = _normalized_distro_name(distro_name)
+    module.distro = module.normalized_name
+    module.is_el = module.normalized_name in ['redhat', 'centos',
+                                              'fedora', 'scientific']
+    module.release = release
+    module.codename = codename
+    module.init = module.choose_init()
+    return module
+
+
+def _get_distro(distro, use_rhceph=False):
+    if not distro:
+        return
+
+    distro = _normalized_distro_name(distro)
+    distributions = {
+        'debian': debian,
+        'ubuntu': debian,
+        'centos': centos,
+        'scientific': centos,
+        'redhat': centos,
+        'fedora': fedora,
+        'suse': suse,
+    }
+
+    if distro == 'redhat' and use_rhceph:
+        return rhel
+    else:
+        return distributions.get(distro)
+
+
+def _normalized_distro_name(distro):
+    distro = distro.lower()
+    if distro.startswith(('redhat', 'red hat')):
+        return 'redhat'
+    elif distro.startswith(('scientific', 'scientific linux')):
+        return 'scientific'
+    elif distro.startswith(('suse', 'opensuse')):
+        return 'suse'
+    elif distro.startswith('centos'):
+        return 'centos'
+    return distro
+
+
+def platform_information():
+    """detect platform information from remote host."""
+    logging.debug('platform_information: linux_distribution = ' +
+                  str(platform.linux_distribution()))
+    distro, release, codename = platform.linux_distribution()
+    # this could be an empty string in Debian
+    if not codename and 'debian' in distro.lower():
+        debian_codenames = {
+            '8': 'jessie',
+            '7': 'wheezy',
+            '6': 'squeeze',
+        }
+        major_version = release.split('.')[0]
+        codename = debian_codenames.get(major_version, '')
+
+        # In order to support newer jessie/sid or wheezy/sid strings
+        # we test this if sid is buried in the minor, we should use
+        # sid anyway.
+        if not codename and '/' in release:
+            major, minor = release.split('/')
+            if minor == 'sid':
+                codename = minor
+            else:
+                codename = major
+
+    return (
+        str(distro).rstrip(),
+        str(release).rstrip(),
+        str(codename).rstrip()
+    )
diff --git a/src/ceph-detect-init/ceph_detect_init/centos/__init__.py b/src/ceph-detect-init/ceph_detect_init/centos/__init__.py
new file mode 100644 (file)
index 0000000..f7bf85b
--- /dev/null
@@ -0,0 +1,11 @@
+distro = None
+release = None
+codename = None
+
+
+def choose_init():
+    """Select a init system
+
+    Returns the name of a init system (upstart, sysvinit ...).
+    """
+    return 'sysvinit'
diff --git a/src/ceph-detect-init/ceph_detect_init/debian/__init__.py b/src/ceph-detect-init/ceph_detect_init/debian/__init__.py
new file mode 100644 (file)
index 0000000..147bda1
--- /dev/null
@@ -0,0 +1,13 @@
+distro = None
+release = None
+codename = None
+
+
+def choose_init():
+    """Select a init system
+
+    Returns the name of a init system (upstart, sysvinit ...).
+    """
+    if distro.lower() == 'ubuntu':
+        return 'upstart'
+    return 'sysvinit'
diff --git a/src/ceph-detect-init/ceph_detect_init/exc.py b/src/ceph-detect-init/ceph_detect_init/exc.py
new file mode 100644 (file)
index 0000000..61d9752
--- /dev/null
@@ -0,0 +1,35 @@
+#
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Alfredo Deza <adeza@redhat.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see `<http://www.gnu.org/licenses/>`.
+#
+
+
+class UnsupportedPlatform(Exception):
+    """Platform is not supported."""
+    def __init__(self, distro, codename, release):
+        self.distro = distro
+        self.codename = codename
+        self.release = release
+
+    def __str__(self):
+        return '{doc}: {distro} {codename} {release}'.format(
+            doc=self.__doc__.strip(),
+            distro=self.distro,
+            codename=self.codename,
+            release=self.release,
+        )
diff --git a/src/ceph-detect-init/ceph_detect_init/fedora/__init__.py b/src/ceph-detect-init/ceph_detect_init/fedora/__init__.py
new file mode 100644 (file)
index 0000000..f7bf85b
--- /dev/null
@@ -0,0 +1,11 @@
+distro = None
+release = None
+codename = None
+
+
+def choose_init():
+    """Select a init system
+
+    Returns the name of a init system (upstart, sysvinit ...).
+    """
+    return 'sysvinit'
diff --git a/src/ceph-detect-init/ceph_detect_init/main.py b/src/ceph-detect-init/ceph_detect_init/main.py
new file mode 100644 (file)
index 0000000..320ae17
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 <contact@redhat.com>
+# Copyright (C) 2015 SUSE LINUX GmbH
+#
+# Author: Alfredo Deza <alfredo.deza@inktank.com>
+# Author: Owen Synge <osynge@suse.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Library Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library Public License for more details.
+#
+import argparse
+import logging
+
+import ceph_detect_init
+from ceph_detect_init import exc
+
+
+def parser():
+    parser = argparse.ArgumentParser(
+        'ceph-detect-init',
+    )
+    parser.add_argument(
+        "-v",
+        "--verbose",
+        action="store_true",
+        default=None,
+    )
+    parser.add_argument(
+        "--use-rhceph",
+        action="store_true",
+        default=False,
+    )
+    parser.add_argument(
+        "--default",
+        default=None,
+    )
+    return parser
+
+
+def run(argv=None, namespace=None):
+    args = parser().parse_args(argv, namespace)
+
+    if args.verbose:
+        logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
+                            level=logging.DEBUG)
+    try:
+        print(ceph_detect_init.get(args.use_rhceph).init)
+    except exc.UnsupportedPlatform:
+        if args.default:
+            print(args.default)
+        else:
+            raise
+
+    return 0
diff --git a/src/ceph-detect-init/ceph_detect_init/rhel/__init__.py b/src/ceph-detect-init/ceph_detect_init/rhel/__init__.py
new file mode 100644 (file)
index 0000000..f7bf85b
--- /dev/null
@@ -0,0 +1,11 @@
+distro = None
+release = None
+codename = None
+
+
+def choose_init():
+    """Select a init system
+
+    Returns the name of a init system (upstart, sysvinit ...).
+    """
+    return 'sysvinit'
diff --git a/src/ceph-detect-init/ceph_detect_init/suse/__init__.py b/src/ceph-detect-init/ceph_detect_init/suse/__init__.py
new file mode 100644 (file)
index 0000000..69bf7c4
--- /dev/null
@@ -0,0 +1,17 @@
+distro = None
+release = None
+codename = None
+
+
+def choose_init():
+    """Select a init system
+
+    Returns the name of a init system (upstart, sysvinit ...).
+    """
+    init_mapping = {
+        '11': 'sysvinit',   # SLE_11
+        '12': 'systemd',    # SLE_12
+        '13.1': 'systemd',  # openSUSE_13.1
+        '13.2': 'systemd',  # openSUSE_13.2
+    }
+    return init_mapping.get(release, 'sysvinit')
diff --git a/src/ceph-detect-init/integration/centos-6.dockerfile b/src/ceph-detect-init/integration/centos-6.dockerfile
new file mode 100644 (file)
index 0000000..7cb5095
--- /dev/null
@@ -0,0 +1,4 @@
+FROM centos:6
+
+RUN yum install -y yum-utils && yum-config-manager --add-repo https://dl.fedoraproject.org/pub/epel/6/x86_64/ && yum install --nogpgcheck -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 && rm /etc/yum.repos.d/dl.fedoraproject.org*
+RUN yum install -y python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/integration/centos-7.dockerfile b/src/ceph-detect-init/integration/centos-7.dockerfile
new file mode 100644 (file)
index 0000000..59a5748
--- /dev/null
@@ -0,0 +1,4 @@
+FROM centos:7
+
+RUN yum install -y yum-utils && yum-config-manager --add-repo https://dl.fedoraproject.org/pub/epel/7/x86_64/ && yum install --nogpgcheck -y epel-release && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 && rm /etc/yum.repos.d/dl.fedoraproject.org*
+RUN yum install -y python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/integration/debian-jessie.dockerfile b/src/ceph-detect-init/integration/debian-jessie.dockerfile
new file mode 100644 (file)
index 0000000..bca22d5
--- /dev/null
@@ -0,0 +1,6 @@
+FROM debian:jessie
+
+RUN apt-get update
+RUN apt-get install -y python-virtualenv python-pip git
+
+
diff --git a/src/ceph-detect-init/integration/debian-sid.dockerfile b/src/ceph-detect-init/integration/debian-sid.dockerfile
new file mode 100644 (file)
index 0000000..00e4472
--- /dev/null
@@ -0,0 +1,4 @@
+FROM debian:sid
+
+RUN apt-get update
+RUN apt-get install -y python-virtualenv python-pip git
diff --git a/src/ceph-detect-init/integration/debian-squeeze.dockerfile b/src/ceph-detect-init/integration/debian-squeeze.dockerfile
new file mode 100644 (file)
index 0000000..e5080f6
--- /dev/null
@@ -0,0 +1,4 @@
+FROM debian:squeeze
+
+RUN apt-get update
+RUN apt-get install -y python-virtualenv python-pip git
diff --git a/src/ceph-detect-init/integration/debian-wheezy.dockerfile b/src/ceph-detect-init/integration/debian-wheezy.dockerfile
new file mode 100644 (file)
index 0000000..e03e30e
--- /dev/null
@@ -0,0 +1,4 @@
+FROM debian:wheezy
+
+RUN apt-get update
+RUN apt-get install -y python-virtualenv python-pip git
diff --git a/src/ceph-detect-init/integration/fedora-21.dockerfile b/src/ceph-detect-init/integration/fedora-21.dockerfile
new file mode 100644 (file)
index 0000000..ee2ac93
--- /dev/null
@@ -0,0 +1,3 @@
+FROM fedora:21
+
+RUN yum install -y python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/integration/opensuse-13.1.dockerfile b/src/ceph-detect-init/integration/opensuse-13.1.dockerfile
new file mode 100644 (file)
index 0000000..00a5a28
--- /dev/null
@@ -0,0 +1,3 @@
+FROM opensuse:13.1
+
+RUN zypper --non-interactive --gpg-auto-import-keys install lsb python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/integration/opensuse-13.2.dockerfile b/src/ceph-detect-init/integration/opensuse-13.2.dockerfile
new file mode 100644 (file)
index 0000000..26f591b
--- /dev/null
@@ -0,0 +1,3 @@
+FROM opensuse:13.2
+
+RUN zypper --non-interactive --gpg-auto-import-keys install python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/integration/test_main.py b/src/ceph-detect-init/integration/test_main.py
new file mode 100644 (file)
index 0000000..e7a620e
--- /dev/null
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 SUSE LINUX GmbH
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Owen Synge <osynge@suse.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Library Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library Public License for more details.
+#
+import logging
+import shutil
+import subprocess
+import testtools
+
+from ceph_detect_init import main
+
+logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
+                    level=logging.DEBUG)
+
+
+def run(os):
+    name = 'ceph-detect-init-' + os
+    shutil.rmtree(name, ignore_errors=True)
+    script = """\
+docker build -t {name} --file integration/{os}.dockerfile .
+toplevel=$(git rev-parse --show-toplevel)
+mkdir {name}
+cat > {name}/try.sh <<EOF
+  virtualenv {name}
+  . {name}/bin/activate
+  pip install -r requirements.txt
+  python setup.py install
+  ceph-detect-init > {name}/init
+EOF
+
+docker run -v $toplevel:$toplevel -w $(pwd) --user $(id -u) {name} bash -x {name}/try.sh
+""".format(name=name,
+           os=os)
+    subprocess.check_call(script, shell=True)
+    init = open(name + '/init').read().strip()
+    shutil.rmtree(name)
+    return init
+    
+
+class TestCephDetectInit(testtools.TestCase):
+
+    def test_centos_6(self):
+        self.assertEqual('sysvinit', run('centos-6'))
+
+    def test_centos_7(self):
+        self.assertEqual('sysvinit', run('centos-7'))
+
+    def test_ubuntu_12_04(self):
+        self.assertEqual('upstart', run('ubuntu-12.04'))
+
+    def test_ubuntu_14_04(self):
+        self.assertEqual('upstart', run('ubuntu-14.04'))
+
+    def test_ubuntu_15_04(self):
+        self.assertEqual('upstart', run('ubuntu-15.04'))
+
+    def test_debian_squeeze(self):
+        self.assertEqual('sysvinit', run('debian-squeeze'))
+
+    def test_debian_wheezy(self):
+        self.assertEqual('sysvinit', run('debian-wheezy'))
+
+    def test_debian_jessie(self):
+        self.assertEqual('sysvinit', run('debian-jessie'))
+
+    def test_debian_sid(self):
+        self.assertEqual('sysvinit', run('debian-sid'))
+
+    def test_fedora_21(self):
+        self.assertEqual('sysvinit', run('fedora-21'))
+
+    def test_opensuse_13_1(self):
+        self.assertEqual('systemd', run('opensuse-13.1'))
+
+    def test_opensuse_13_2(self):
+        self.assertEqual('systemd', run('opensuse-13.2'))
+
+# Local Variables:
+# compile-command: "cd .. ; .tox/py27/bin/py.test integration/test_main.py"
+# End:
diff --git a/src/ceph-detect-init/integration/ubuntu-12.04.dockerfile b/src/ceph-detect-init/integration/ubuntu-12.04.dockerfile
new file mode 100644 (file)
index 0000000..dda1a62
--- /dev/null
@@ -0,0 +1,4 @@
+FROM ubuntu:12.04
+
+RUN apt-get update
+RUN apt-get install -y python-virtualenv python-pip git
diff --git a/src/ceph-detect-init/integration/ubuntu-14.04.dockerfile b/src/ceph-detect-init/integration/ubuntu-14.04.dockerfile
new file mode 100644 (file)
index 0000000..4f7a698
--- /dev/null
@@ -0,0 +1,6 @@
+FROM ubuntu:14.04
+
+RUN apt-get update
+# http://stackoverflow.com/questions/27341064/how-do-i-fix-importerror-cannot-import-name-incompleteread
+RUN apt-get install -y python-setuptools && easy_install -U pip
+RUN apt-get install -y python-virtualenv git
diff --git a/src/ceph-detect-init/integration/ubuntu-15.04.dockerfile b/src/ceph-detect-init/integration/ubuntu-15.04.dockerfile
new file mode 100644 (file)
index 0000000..29b5776
--- /dev/null
@@ -0,0 +1,4 @@
+FROM ubuntu:15.04
+
+RUN apt-get update
+RUN apt-get install -y python-pip python-virtualenv git
diff --git a/src/ceph-detect-init/requirements.txt b/src/ceph-detect-init/requirements.txt
new file mode 100644 (file)
index 0000000..1352d5e
--- /dev/null
@@ -0,0 +1 @@
+argparse
diff --git a/src/ceph-detect-init/run-tox.sh b/src/ceph-detect-init/run-tox.sh
new file mode 100755 (executable)
index 0000000..3d7216c
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/bash
+#
+# Copyright (C) 2015 SUSE LINUX GmbH
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Owen Synge <osynge@suse.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Library Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Library Public License for more details.
+#
+
+# run from the ceph-detect-init directory or from its parent
+test -d ceph-detect-init && cd ceph-detect-init
+trap "rm -fr make-check" EXIT
+virtualenv make-check
+. make-check/bin/activate
+# older versions of pip will not install wrap_console scripts
+# when using wheel packages
+pip install --upgrade 'pip >= 6.1'
+if test -d wheelhouse ; then
+    export NO_INDEX=--no-index
+fi
+pip install $NO_INDEX --use-wheel --find-links=file://$(pwd)/wheelhouse --upgrade distribute
+pip install $NO_INDEX --use-wheel --find-links=file://$(pwd)/wheelhouse 'tox >=1.9' 
+tox 2>&1 | grep -v InterpreterNotFound
diff --git a/src/ceph-detect-init/setup.py b/src/ceph-detect-init/setup.py
new file mode 100644 (file)
index 0000000..8554d09
--- /dev/null
@@ -0,0 +1,71 @@
+#
+# Copyright (C) 2015 SUSE LINUX GmbH
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Owen Synge <osynge@suse.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see `<http://www.gnu.org/licenses/>`.
+#
+import os
+from setuptools import setup
+from setuptools import find_packages
+
+def read(fname):
+    path = os.path.join(os.path.dirname(__file__), fname)
+    f = open(path)
+    return f.read()
+
+install_requires = read('requirements.txt').split()
+tests_require = read('test-requirements.txt').split()
+
+setup(
+    name='ceph-detect-init',
+    version='1.0.1',
+    packages=find_packages(),
+
+    author='Owen Synge, Loic Dachary',
+    author_email='osynge@suse.de, loic@dachary.org',
+    description='display the normalized name of the init system',
+    long_description=read('README.rst'),
+    license='LGPLv2+',
+    keywords='ceph',
+    url="https://git.ceph.com/?p=ceph.git;a=summary",
+
+    install_requires=[
+        'setuptools',
+        ] + install_requires,
+
+    tests_require=tests_require,
+
+    classifiers=[
+        'Environment :: Console',
+        'Intended Audience :: Information Technology',
+        'Intended Audience :: System Administrators',
+        'Operating System :: POSIX :: Linux',
+        'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
+        'Programming Language :: Python',
+        'Programming Language :: Python :: 2',
+        'Programming Language :: Python :: 3',
+        'Topic :: Utilities',
+    ],
+
+    entry_points={
+
+        'console_scripts': [
+            'ceph-detect-init = ceph_detect_init.main:run',
+            ],
+
+        },
+    )
diff --git a/src/ceph-detect-init/test-requirements.txt b/src/ceph-detect-init/test-requirements.txt
new file mode 100644 (file)
index 0000000..5a0761c
--- /dev/null
@@ -0,0 +1,10 @@
+coverage>=3.6
+discover
+fixtures>=0.3.14
+python-subunit
+testrepository>=0.0.17
+testtools>=0.9.32
+mock
+pytest
+tox
+flake8
diff --git a/src/ceph-detect-init/tests/test_all.py b/src/ceph-detect-init/tests/test_all.py
new file mode 100644 (file)
index 0000000..68189bf
--- /dev/null
@@ -0,0 +1,162 @@
+#
+# Copyright (C) 2015 SUSE LINUX GmbH
+# Copyright (C) 2015 <contact@redhat.com>
+#
+# Author: Owen Synge <osynge@suse.com>
+# Author: Loic Dachary <loic@dachary.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see `<http://www.gnu.org/licenses/>`.
+#
+import logging
+import mock
+import testtools
+
+import ceph_detect_init
+from ceph_detect_init import centos
+from ceph_detect_init import debian
+from ceph_detect_init import exc
+from ceph_detect_init import fedora
+from ceph_detect_init import main
+from ceph_detect_init import rhel
+from ceph_detect_init import suse
+
+logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
+                    level=logging.DEBUG)
+
+
+class TestCephDetectInit(testtools.TestCase):
+
+    def test_centos(self):
+        self.assertEqual('sysvinit', centos.choose_init())
+
+    def test_debian(self):
+        with mock.patch('ceph_detect_init.debian.distro',
+                        'debian'):
+            self.assertEqual('sysvinit', debian.choose_init())
+        with mock.patch('ceph_detect_init.debian.distro',
+                        'ubuntu'):
+            self.assertEqual('upstart', debian.choose_init())
+
+    def test_fedora(self):
+        self.assertEqual('sysvinit', fedora.choose_init())
+
+    def test_rhel(self):
+        self.assertEqual('sysvinit', rhel.choose_init())
+
+    def test_suse(self):
+        with mock.patch('ceph_detect_init.suse.release',
+                        '11'):
+            self.assertEqual('sysvinit', suse.choose_init())
+        with mock.patch('ceph_detect_init.suse.release',
+                        '12'):
+            self.assertEqual('systemd', suse.choose_init())
+        with mock.patch('ceph_detect_init.suse.release',
+                        '13.1'):
+            self.assertEqual('systemd', suse.choose_init())
+        with mock.patch('ceph_detect_init.suse.release',
+                        '13.2'):
+            self.assertEqual('systemd', suse.choose_init())
+
+    def test_get(self):
+        g = ceph_detect_init.get
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('unknown', '', ''))):
+            self.assertRaises(exc.UnsupportedPlatform, g)
+            try:
+                g()
+            except exc.UnsupportedPlatform as e:
+                self.assertIn('Platform is not supported', str(e))
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', '6.0', ''))):
+            distro = ceph_detect_init.get()
+            self.assertEqual(debian, distro)
+            self.assertEqual('debian', distro.name)
+            self.assertEqual('debian', distro.normalized_name)
+            self.assertEqual('debian', distro.distro)
+            self.assertEqual(False, distro.is_el)
+            self.assertEqual('6.0', distro.release)
+            self.assertEqual('squeeze', distro.codename)
+            self.assertEqual('sysvinit', distro.init)
+
+    def test_get_distro(self):
+        g = ceph_detect_init._get_distro
+        self.assertEqual(None, g(None))
+        self.assertEqual(debian, g('debian'))
+        self.assertEqual(debian, g('ubuntu'))
+        self.assertEqual(centos, g('centos'))
+        self.assertEqual(centos, g('scientific'))
+        self.assertEqual(fedora, g('fedora'))
+        self.assertEqual(suse, g('suse'))
+        self.assertEqual(rhel, g('redhat', use_rhceph=True))
+
+    def test_normalized_distro_name(self):
+        n = ceph_detect_init._normalized_distro_name
+        self.assertEqual('redhat', n('RedHat'))
+        self.assertEqual('redhat', n('redhat'))
+        self.assertEqual('redhat', n('Red Hat'))
+        self.assertEqual('redhat', n('red hat'))
+        self.assertEqual('scientific', n('scientific'))
+        self.assertEqual('scientific', n('Scientific'))
+        self.assertEqual('scientific', n('Scientific Linux'))
+        self.assertEqual('scientific', n('scientific linux'))
+        self.assertEqual('suse', n('SUSE'))
+        self.assertEqual('suse', n('suse'))
+        self.assertEqual('suse', n('openSUSE'))
+        self.assertEqual('suse', n('opensuse'))
+        self.assertEqual('centos', n('CentOS'))
+        self.assertEqual('centos', n('centos'))
+        self.assertEqual('debian', n('Debian'))
+        self.assertEqual('debian', n('debian'))
+        self.assertEqual('ubuntu', n('Ubuntu'))
+        self.assertEqual('ubuntu', n('ubuntu'))
+
+    def test_platform_information(self):
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', '6.0', ''))):
+            self.assertEqual(('debian', '6.0', 'squeeze'),
+                             ceph_detect_init.platform_information())
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', '7.0', ''))):
+            self.assertEqual(('debian', '7.0', 'wheezy'),
+                             ceph_detect_init.platform_information())
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', '8.0', ''))):
+            self.assertEqual(('debian', '8.0', 'jessie'),
+                             ceph_detect_init.platform_information())
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', 'jessie/sid', ''))):
+            self.assertEqual(('debian', 'jessie/sid', 'sid'),
+                             ceph_detect_init.platform_information())
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('debian', 'sid/jessie', ''))):
+            self.assertEqual(('debian', 'sid/jessie', 'sid'),
+                             ceph_detect_init.platform_information())
+
+    def test_run(self):
+        argv = ['--use-rhceph', '--verbose']
+        self.assertEqual(0, main.run(argv))
+
+        with mock.patch('platform.linux_distribution',
+                        lambda: (('unknown', '', ''))):
+            self.assertRaises(exc.UnsupportedPlatform, main.run, argv)
+            self.assertEqual(0, main.run(argv + ['--default=sysvinit']))
+
+# Local Variables:
+# compile-command: "cd .. ; .tox/py27/bin/py.test tests/test_all.py"
+# End:
diff --git a/src/ceph-detect-init/tox.ini b/src/ceph-detect-init/tox.ini
new file mode 100644 (file)
index 0000000..3da7065
--- /dev/null
@@ -0,0 +1,31 @@
+[tox]
+envlist = pep8,py27,py3
+skip_missing_interpreters = True
+
+[testenv]
+basepython =
+    py27: python2.7
+    py3: python3
+setenv = VIRTUAL_ENV={envdir}
+usedevelop = true
+deps =
+  {env:NO_INDEX:}
+  --use-wheel
+  --find-links=file://{toxinidir}/wheelhouse
+  -r{toxinidir}/requirements.txt
+  -r{toxinidir}/test-requirements.txt
+
+commands = coverage run --source=ceph_detect_init {envbindir}/py.test -v tests
+           coverage report --omit=*test*,*tox* --show-missing --fail-under=100
+
+[testenv:pep8]
+basepython = python2
+commands = flake8 ceph_detect_init tests
+
+[testenv:integration]
+basepython = python2
+setenv = VIRTUAL_ENV={envdir}
+deps = -r{toxinidir}/requirements.txt
+  -r{toxinidir}/test-requirements.txt
+
+commands = {envbindir}/py.test -v integration/test_main.py