]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
detect sudo needs and switch connections for any use-case 65/head
authorAlfredo Deza <alfredo.deza@inktank.com>
Fri, 6 Sep 2013 14:11:10 +0000 (10:11 -0400)
committerAlfredo Deza <alfredo.deza@inktank.com>
Fri, 6 Sep 2013 14:16:41 +0000 (10:16 -0400)
Signed-off-by: Alfredo Deza <alfredo.deza@inktank.com>
ceph_deploy/hosts/__init__.py
ceph_deploy/sudo_pushy.py

index 5fc973097e0351864d51011693893469d64b55b1..f0742e56969e196264293908169a0ee5200e7497 100644 (file)
@@ -6,7 +6,6 @@ on the type of distribution/version we are dealing with.
 """
 import logging
 from ceph_deploy import exc, lsb
-from ceph_deploy.util import wrappers
 from ceph_deploy.sudo_pushy import get_transport
 from ceph_deploy.hosts import debian, centos, fedora, suse
 
@@ -35,7 +34,8 @@ def get(hostname, fallback=None):
     :param hostname: A hostname that is reachable/resolvable over the network
     :param fallback: Optional fallback to use if no supported distro is found
     """
-    sudo_conn = pushy.connect(get_transport(hostname))
+    transport = get_transport(hostname)
+    sudo_conn = pushy.connect(transport)
     (distro, release, codename) = lsb.get_lsb_release(sudo_conn)
 
     module = _get_distro(distro)
index 26cfcd3d1ba8b933de1b2690e8666025429589c9..a44f438ed7834c75614d3bc9726bde4e8194a74d 100644 (file)
@@ -1,17 +1,22 @@
+import getpass
+import logging
+import socket
 import pushy.transport.ssh
 import pushy.transport.local
-import subprocess 
+import subprocess
+
+logger = logging.getLogger(__name__)
 
 
 class Local_Popen(pushy.transport.local.Popen):
     def __init__(self, command, address, **kwargs):
         pushy.transport.BaseTransport.__init__(self, address)
-    
+
         self.__proc = subprocess.Popen(command, stdin=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,
                                        bufsize=65535)
-    
+
         self.stdout = self.__proc.stdout
         self.stderr = self.__proc.stderr
         self.stdin  = self.__proc.stdin
@@ -19,27 +24,44 @@ class Local_Popen(pushy.transport.local.Popen):
     def close(self):
         self.stdin.close()
         self.__proc.wait()
-    
+
+
 class SshSudoTransport(object):
     @staticmethod
     def Popen(command, *a, **kw):
         command = ['sudo'] + command
         return pushy.transport.ssh.Popen(command, *a, **kw)
 
+
 class LocalSudoTransport(object):
     @staticmethod
     def Popen(command, *a, **kw):
         command = ['sudo'] + command
         return Local_Popen(command, *a, **kw)
 
-def get_transport(hostname):
-    import socket
 
+def get_transport(hostname):
+    use_sudo = needs_sudo()
     myhostname = socket.gethostname().split('.')[0]
     if hostname == myhostname:
-        return 'local+sudo:'
+        if use_sudo:
+            logger.debug('will use a local connection with sudo')
+            return 'local+sudo:'
+        logger.debug('will use a local connection without sudo')
+        return 'local:'
     else:
-        return 'ssh+sudo:{hostname}'.format(hostname=hostname)
+        if use_sudo:
+            logger.debug('will use a remote connection with sudo')
+            return 'ssh+sudo:{hostname}'.format(hostname=hostname)
+        logger.debug('will use a remote connection without sudo')
+        return 'ssh:{hostname}'.format(hostname=hostname)
+
+
+def needs_sudo():
+    if getpass.getuser() == 'root':
+        return False
+    return True
+
 
 def patch():
     """