Add port
and user
kwargs for ssh.cache_host_key()
deprecate the `for_user` kwarg. this also will now try a basic command over ssh before bothering with an update/cache for the host key
This commit is contained in:
parent
97ed8eeff0
commit
caf94ac65a
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2019 Lance Edgar
|
||||
# Copyright © 2010-2020 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -25,13 +25,34 @@ Fabric Library for SSH
|
|||
"""
|
||||
|
||||
|
||||
def cache_host_key(c, host, for_user='root'):
|
||||
def cache_host_key(c, host, port=None, user=None, **kwargs):
|
||||
"""
|
||||
Cache the SSH host key for the given host, for the given user.
|
||||
"""
|
||||
cmd = 'ssh -o StrictHostKeyChecking=no {} echo'.format(host)
|
||||
user = None if for_user == 'root' else for_user
|
||||
c.sudo(cmd, user=user, warn=True)
|
||||
if 'for_user' in kwargs:
|
||||
pass # TODO: deprecation warning
|
||||
|
||||
if not user and kwargs.get('for_user'):
|
||||
user = kwargs['for_user']
|
||||
|
||||
port = '-p {}'.format(port) if port else ''
|
||||
|
||||
# first try to run basic command over ssh; if it works then we don't
|
||||
# actually need to update/cache the host key
|
||||
cmd = 'ssh {} {} whoami'.format(port, host)
|
||||
if user:
|
||||
result = c.sudo(cmd, warn=True, user=None if user == 'root' else user)
|
||||
else:
|
||||
result = c.run(cmd, warn=True)
|
||||
if result.failed:
|
||||
|
||||
# basic command failed, which presumably means we *do* need to cache
|
||||
# the host key, so try that now
|
||||
cmd = 'ssh -o StrictHostKeyChecking=no {} {} echo'.format(port, host)
|
||||
if user:
|
||||
c.sudo(cmd, user=None if user == 'root' else user)
|
||||
else:
|
||||
c.run(cmd)
|
||||
|
||||
|
||||
def restart(c):
|
||||
|
|
Loading…
Reference in a new issue