From: Dmitry Borodaenko Date: Tue, 10 Sep 2013 18:43:00 +0000 (-0700) Subject: consistently remove domain from short hostname X-Git-Tag: v1.2.4~11^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d56c2f3879dd31abf7d175850108668b8abf7198;p=ceph-deploy.git consistently remove domain from short hostname Signed-off-by: Dmitry Borodaenko --- diff --git a/ceph_deploy/hosts/centos/mon/create.py b/ceph_deploy/hosts/centos/mon/create.py index e8dcf8d..51dfdcb 100644 --- a/ceph_deploy/hosts/centos/mon/create.py +++ b/ceph_deploy/hosts/centos/mon/create.py @@ -1,9 +1,10 @@ from ceph_deploy.util.wrappers import check_call from ceph_deploy.hosts import common +from ceph_deploy.misc import remote_shortname def create(distro, logger, args, monitor_keyring): - hostname = distro.sudo_conn.modules.socket.gethostname().split('.')[0] + hostname = remote_shortname(distro.sudo_conn.modules.socket) common.mon_create(distro, logger, args, monitor_keyring, hostname) service = common.which_service(distro.sudo_conn, logger) check_call( diff --git a/ceph_deploy/hosts/debian/mon/create.py b/ceph_deploy/hosts/debian/mon/create.py index 29b4836..4592c50 100644 --- a/ceph_deploy/hosts/debian/mon/create.py +++ b/ceph_deploy/hosts/debian/mon/create.py @@ -1,9 +1,10 @@ from ceph_deploy.util.wrappers import check_call from ceph_deploy.hosts import common +from ceph_deploy.misc import remote_shortname def create(distro, logger, args, monitor_keyring): - hostname = distro.sudo_conn.modules.socket.gethostname().split('.')[0] + hostname = remote_shortname(distro.sudo_conn.modules.socket) common.mon_create(distro, logger, args, monitor_keyring, hostname) if distro.init == 'upstart': # Ubuntu uses upstart diff --git a/ceph_deploy/hosts/fedora/mon/create.py b/ceph_deploy/hosts/fedora/mon/create.py index d7bae2d..80da3af 100644 --- a/ceph_deploy/hosts/fedora/mon/create.py +++ b/ceph_deploy/hosts/fedora/mon/create.py @@ -1,9 +1,10 @@ from ceph_deploy.util.wrappers import check_call from ceph_deploy.hosts import common +from ceph_deploy.misc import remote_shortname def create(distro, logger, args, monitor_keyring): - hostname = distro.sudo_conn.modules.socket.gethostname().split('.')[0] + hostname = remote_shortname(distro.sudo_conn.modules.socket) common.mon_create(distro, logger, args, monitor_keyring, hostname) service = common.which_service(distro.sudo_conn, logger) diff --git a/ceph_deploy/hosts/suse/mon/create.py b/ceph_deploy/hosts/suse/mon/create.py index d7bae2d..80da3af 100644 --- a/ceph_deploy/hosts/suse/mon/create.py +++ b/ceph_deploy/hosts/suse/mon/create.py @@ -1,9 +1,10 @@ from ceph_deploy.util.wrappers import check_call from ceph_deploy.hosts import common +from ceph_deploy.misc import remote_shortname def create(distro, logger, args, monitor_keyring): - hostname = distro.sudo_conn.modules.socket.gethostname().split('.')[0] + hostname = remote_shortname(distro.sudo_conn.modules.socket) common.mon_create(distro, logger, args, monitor_keyring, hostname) service = common.which_service(distro.sudo_conn, logger) diff --git a/ceph_deploy/misc.py b/ceph_deploy/misc.py index 09d14c3..08d7ce3 100644 --- a/ceph_deploy/misc.py +++ b/ceph_deploy/misc.py @@ -23,3 +23,10 @@ def mon_hosts(mons): name = name.split('.')[0] yield (name, host) +def remote_shortname(socket): + """ + Obtains remote hostname of the socket and cuts off the domain part + if it's FQDN. + """ + return socket.gethostname().split('.', 1)[0] + diff --git a/ceph_deploy/mon.py b/ceph_deploy/mon.py index de8fe81..372bc65 100644 --- a/ceph_deploy/mon.py +++ b/ceph_deploy/mon.py @@ -11,7 +11,7 @@ from .cliutil import priority from .sudo_pushy import get_transport from .util import paths, wrappers from . import hosts -from .misc import mon_hosts +from .misc import mon_hosts, remote_shortname LOG = logging.getLogger(__name__) @@ -113,7 +113,7 @@ def hostname_is_compatible(conn, logger, provided_hostname): `hostname` in the remote host, otherwise mons can fail not reaching quorum. """ logger.debug('determining if provided host has same hostname in remote') - remote_hostname = conn.modules.socket.gethostname() + remote_hostname = remote_shortname(conn.modules.socket) if remote_hostname == provided_hostname: return logger.warning('*'*80) @@ -133,7 +133,7 @@ def destroy_mon(cluster, paths, is_running): import time retries = 5 - hostname = socket.gethostname().split('.')[0] + hostname = remote_shortname(socket) path = paths.mon.path(cluster, hostname) if os.path.exists(path): diff --git a/ceph_deploy/sudo_pushy.py b/ceph_deploy/sudo_pushy.py index a44f438..ef57c98 100644 --- a/ceph_deploy/sudo_pushy.py +++ b/ceph_deploy/sudo_pushy.py @@ -5,6 +5,8 @@ import pushy.transport.ssh import pushy.transport.local import subprocess +from .misc import remote_shortname + logger = logging.getLogger(__name__) @@ -42,7 +44,7 @@ class LocalSudoTransport(object): def get_transport(hostname): use_sudo = needs_sudo() - myhostname = socket.gethostname().split('.')[0] + myhostname = remote_shortname(socket) if hostname == myhostname: if use_sudo: logger.debug('will use a local connection with sudo') diff --git a/ceph_deploy/tests/unit/test_mon.py b/ceph_deploy/tests/unit/test_mon.py index 5be3120..7f0c601 100644 --- a/ceph_deploy/tests/unit/test_mon.py +++ b/ceph_deploy/tests/unit/test_mon.py @@ -2,7 +2,7 @@ import sys from mock import Mock, MagicMock, patch, call from ceph_deploy import mon from ceph_deploy.hosts.common import mon_create -from ceph_deploy.misc import mon_hosts +from ceph_deploy.misc import mon_hosts, remote_shortname def path_exists(target_paths=None): @@ -131,6 +131,15 @@ class TestCreateMon(object): result = hosts.mock_calls assert result == expected + def test_remote_shortname(self): + socket = Mock() + socket.gethostname.return_value = 'host.f.q.d.n' + assert remote_shortname(socket) == 'host' + + socket = Mock() + socket.gethostname.return_value = 'host' + assert remote_shortname(socket) == 'host' + class TestIsRunning(object): def setup(self):