]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ssh_keys.py: pull the keys out of targets entry
authorJoe Buck <jbbuck@gmail.com>
Thu, 13 Dec 2012 22:42:09 +0000 (14:42 -0800)
committerJoe Buck <jbbuck@gmail.com>
Thu, 3 Jan 2013 23:04:55 +0000 (15:04 -0800)
rather than the hosts known hosts file.

Signed-off-by: Joe Buck <jbbuck@gmail.com>
Reviewed-by: Sam Lang <sam.lang@inktank.com>
teuthology/task/ssh_keys.py

index 049883dfba0b040ad079d46b12c71c9d4ae6a223..bb7ce8493d4fa9c993a54e15a55669833171351f 100644 (file)
@@ -9,6 +9,7 @@ import re
 from cStringIO import StringIO
 from teuthology import contextutil
 from ..orchestra import run
+from ..orchestra.connection import create_key
 
 log = logging.getLogger(__name__)
 
@@ -23,12 +24,30 @@ def generate_keys():
 def cleanup_keys(ctx, public_key):
     client = paramiko.SSHClient()
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-    client.load_system_host_keys()
 
     for host in ctx.cluster.remotes.iterkeys():
         username, hostname = str(host).split('@')
         log.info('cleaning up keys on {host}'.format(host=hostname, user=username))
 
+        # try to extract a public key for the host from the ctx.config entries
+        host_key_found = False
+        for t, host_key in ctx.config['targets'].iteritems():
+
+            if str(t) == str(host):
+                keytype, key = host_key.split(' ',1)
+                client.get_host_keys().add(
+                    hostname=hostname,
+                    keytype=keytype,
+                    key=create_key(keytype,key)
+                    )
+                host_key_found = True
+                log.info('ssh key found in ctx')
+
+        # if we did not find a key, load the system keys
+        if False == host_key_found:
+            client.load_system_host_keys()
+            log.info('no key found in ctx, using system host keys')
+
         client.connect(hostname, username=username)
         client.exec_command('rm ~/.ssh/id_rsa')
         client.exec_command('rm ~/.ssh/id_rsa.pub')
@@ -93,11 +112,30 @@ def push_keys_to_host(ctx, config, public_key, private_key):
 
     client = paramiko.SSHClient()
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-    client.load_system_host_keys()
 
     for host in ctx.cluster.remotes.iterkeys():
         log.info('host: {host}'.format(host=host))
         username, hostname = str(host).split('@')
+   
+        # try to extract a public key for the host from the ctx.config entries
+        host_key_found = False
+        for t, host_key in ctx.config['targets'].iteritems():
+
+            if str(t) == str(host):
+                keytype, key = host_key.split(' ',1)
+                client.get_host_keys().add(
+                    hostname=hostname,
+                    keytype=keytype,
+                    key=create_key(keytype,key)
+                    )
+                host_key_found = True
+                log.info('ssh key found in ctx')
+
+        # if we did not find a key, load the system keys
+        if False == host_key_found:
+            client.load_system_host_keys()
+            log.info('no key found in ctx, using system host keys')
+
         log.info('pushing keys to {host} for {user}'.format(host=hostname, user=username))
 
         client.connect(hostname, username=username)