Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove one call to get_ssh_client() #710

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lago/plugins/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def ssh_reachable(self, tries=None, propagate_fail=True):
return False

try:
ssh.get_ssh_client(
self._ssh_client = ssh.get_ssh_client(
ip_addr=self.ip(),
host_name=self.name(),
ssh_tries=tries,
Expand Down Expand Up @@ -660,19 +660,23 @@ def _normalize_spec(cls, spec):

@contextlib.contextmanager
def _scp(self, propagate_fail=True):
client = ssh.get_ssh_client(
propagate_fail=propagate_fail,
ip_addr=self.ip(),
host_name=self.name(),
ssh_key=self.virt_env.prefix.paths.ssh_id_rsa(),
username=self._spec.get('ssh-user'),
password=self._spec.get('ssh-password'),
)
if self._ssh_client is not None:
client = self._ssh_client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the same client in multiple threads?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be better to add try/catch when trying to get the client in _scp.
It will prevent a case where:

  1. You check if the vm is ssh reachable
  2. Something makes the vm go offline
  3. You call the _scp context manager.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What am I supposed to do in this case? What do I do in the catch? Nothing I can do then if the VM is offline - we'll fail miserably anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You can fall back to use libguestfs.
  2. Print an explanation about the failure. It's better than to see the entire stack trace on screen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot use the client in different threads - it is actually closed after the SCP.

I thought libguestfs doesn't work - but I guess I could once you get your new exception in.

else:
client = ssh.get_ssh_client(
propagate_fail=propagate_fail,
ip_addr=self.ip(),
host_name=self.name(),
ssh_key=self.virt_env.prefix.paths.ssh_id_rsa(),
username=self._spec.get('ssh-user'),
password=self._spec.get('ssh-password'),
)
scp = SCPClient(client.get_transport())
try:
yield scp
finally:
client.close()
self._ssh_client = None

def _detect_service_provider(self):
LOGGER.debug('Detecting service provider for %s', self.name())
Expand Down