def _ssh_setup(container_id, container_ip):
logger.info('Applying ssh configuration to manager container')
try:
known_hosts = path('~/.ssh/known_hosts').expanduser()
# Known hosts file may not exist
ssh_keygen('-R', container_ip)
fingerprint = None
while not fingerprint:
fingerprint = ssh_keyscan(
container_ip).stdout.split('\n')[0].strip()
time.sleep(0.01)
if fingerprint and known_hosts.exists():
current = known_hosts.text()
prefix = ''
if not current.endswith('\n'):
prefix = '\n'
known_hosts.write_text(
'{}{}\n'.format(prefix, fingerprint), append=True)
except sh.ErrorReturnCode:
pass
quiet_docker('exec', container_id, 'mkdir', '-p', '/root/.ssh')
ssh_public_key = ssh_keygen('-y', '-f', configuration.ssh_key_path).strip()
with tempfile.NamedTemporaryFile() as f:
f.write(ssh_public_key)
f.flush()
quiet_docker.cp(f.name, '{}:/root/.ssh/authorized_keys'.format(
container_id))
# due to a bug in docker 17.06, the file keeps ownership and is not
# chowned to the main container user automatically
quiet_docker('exec', container_id, 'chown', 'root:root',
'/root/.ssh/authorized_keys')
评论列表
文章目录