1
# This file is part of Online Services Jenkaas.
3
# Copyright 2017 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License version 3, as published by the
7
# Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# General Public License for more details.
14
# You should have received a copy of the GNU General Public License along with
15
# this program. If not, see <http://www.gnu.org/licenses/>.
16
from __future__ import unicode_literals
19
from olsvms import subprocesses
22
class SshAgent(object):
32
# C-shell output is easier to scan
33
_, out, _ = subprocesses.run(['ssh-agent', '-c'])
34
for line in out.splitlines():
35
if line.startswith('echo '):
37
if line.startswith('setenv '):
38
_, name, value = line.split()
39
value = value.rstrip(';')
40
if name == 'SSH_AUTH_SOCK':
41
self.auth_sock = value
42
elif name == 'SSH_AGENT_PID':
45
def add(self, key_path):
46
subprocesses.run(['env', 'SSH_AUTH_SOCK={}'.format(self.auth_sock),
52
subprocesses.run(['env', 'SSH_AGENT_PID={}'.format(self.pid),
57
"""Get the environment variables allowing agent use.
59
:return: A dict with SSH_AUTH_SOCK and SSH_AGENT_PID.
61
return dict(SSH_AUTH_SOCK=self.auth_sock, SSH_AGENT_PID=self.pid)