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

Lazy load the cli.Client machine #1190

Merged
merged 1 commit into from
Mar 28, 2019
Merged

Conversation

rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Mar 27, 2019

Problem:

Everytime we instantiate a new instance of cli.Client its __init__ method
code tries to make communication with the remote host to determine if it is
a local or ssh machine to be used.

The communication with machine should be done only when it is necessary,
which is when the run command is called.

To reproduce the problem

{
  "hosts": [
    {
      "hostname": "this-host-does-not-exists-or-server-is-down",
     ...
    }
  ]
}
$ export PULP_SMASH_CONFIG_FILE=/path/to/above/settings.json
$ python -c "from pulp_smash import cli, config; c = cli.Client(config.get_config()); print(c)"
Traceback (most recent call last):
plumbum.machines.session.SSHCommsError: No communication channel detected. Does the remote exist?

We have not called the run method, so the communication with server should not be done yet.

Solution:

Transform machine in a lazy @property then it is instantiated only
when it is first called.

Not calling run

$ python -c "from pulp_smash import cli, config; c = cli.Client(config.get_config()); print(c)"
<pulp_smash.cli.Client object at 0x7f5439275ef0>

When run is called

$ python -c "from pulp_smash import cli, config; c = cli.Client(config.get_config()); c.run(['ls'])"
Traceback (most recent call last):
plumbum.machines.session.SSHCommsError: No communication channel detected. Does the remote exist?

Problem:

Everytime we instantiate a new instance of `cli.Client` its `__init__` method
code tries to make communication with the remote host to determine if it is
a local or ssh machine to be used.

The communication with `machine` should be done only when it is necessary,
which is when the `run` command is called.

Solution:

Transform `machine` in a lazy `@property` then it is instantiated only
when it is first called.
@ragabala
Copy link
Contributor

@rochacbruno thanks for the documentation 💯

@bherrin3 bherrin3 merged commit c7a71b1 into pulp:master Mar 28, 2019
@rochacbruno rochacbruno deleted the lazy_cli branch March 28, 2019 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants