pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.85k stars 374 forks source link

Documentation and example for "Using the API" throw exceptions when run #851

Closed phlummox closed 1 year ago

phlummox commented 2 years ago

Describe the bug

The sample code contained in the "Using the API" documentation page (https://docs.pyinfra.com/en/2.x/api/index.html) and the api_deploy.py example throw exceptions when run – specifically, line 122, which calls pyinfra.api.facts.get_facts:

https://github.com/Fizzadar/pyinfra/blob/2d4792eba3233c77a298d86e9638434688e3a1c5/examples/api_deploy.py#L122

To Reproduce

1. Create a Vagrantfile:

Vagrantfile contents ``` Vagrant.configure("2") do |config| config.vm.box = "generic/ubuntu2004" config.vm.provider "virtualbox" do |vb| vb.memory = "500" end end ```

2. Bring up a Vagrant instance:

$ vagrant up

3. Create motd file needed for the example:

$ mkdir -p files && echo "foo" > files/motd

4. Copy api_deploy.py file into current directory

5. Execute api_deploy.py:

$ python3 api_deploy.py

Expected behavior

The script should run without error.

Actual behavior

A TypeError is thrown:

$ python3 api_deploy.py 
Loading Vagrant config...
Connecting...
Host connected: @vagrant/default
Generating operations...
Start operation: 784a97bf1955d5f7a2b9dd6c1e371e17b73c42bc
End operation: 784a97bf1955d5f7a2b9dd6c1e371e17b73c42bc
Start operation: 9c1c01dc3ac1445a500251fc34a15d3e75a849df
End operation: 9c1c01dc3ac1445a500251fc34a15d3e75a849df
Start operation: f4f59e822581d785ba910fbf3f268eca79db8204
End operation: f4f59e822581d785ba910fbf3f268eca79db8204
Start operation: 08743582456b52abe1182f5a5a3e12b457ba28b8
End operation: 08743582456b52abe1182f5a5a3e12b457ba28b8
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 906, in gevent._gevent_cgreenlet.Greenlet.run
  File "/mnt/data/dev/vouch-demo/env/lib/python3.8/site-packages/pyinfra/api/facts.py", line 127, in get_fact
    if issubclass(cls, ShortFactBase):
TypeError: issubclass() arg 1 must be a class
2022-07-09T11:00:01Z <Greenlet at 0x7f3d863c8ae0: get_fact(<pyinfra.api.state.State object at 0x7f3d8631b070>, Host(@vagrant/default), 'os')> failed with TypeError

Traceback (most recent call last):
  File "api_deploy.py", line 122, in <module>
    facts = get_facts(state, "os")
  File "/mnt/data/dev/vouch-demo/env/lib/python3.8/site-packages/pyinfra/api/facts.py", line 110, in get_facts
    results[host] = greenlet.get()
  File "src/gevent/greenlet.py", line 803, in gevent._gevent_cgreenlet.Greenlet.get
  File "src/gevent/greenlet.py", line 371, in gevent._gevent_cgreenlet.Greenlet._raise_exception
  File "/mnt/data/dev/vouch-demo/env/lib/python3.8/site-packages/gevent/_compat.py", line 65, in reraise
    raise value.with_traceback(tb)
  File "src/gevent/greenlet.py", line 906, in gevent._gevent_cgreenlet.Greenlet.run
  File "/mnt/data/dev/vouch-demo/env/lib/python3.8/site-packages/pyinfra/api/facts.py", line 127, in get_fact
    if issubclass(cls, ShortFactBase):
TypeError: issubclass() arg 1 must be a class

Meta

output of pyinfra --support

--> Support information:

    If you are having issues with pyinfra or wish to make feature requests, please
    check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
    When adding an issue, be sure to include the following:

    System: Linux
      Platform: Linux-5.13.0-48-generic-x86_64-with-glibc2.29
      Release: 5.13.0-48-generic
      Machine: x86_64
    pyinfra: v2.3
    Executable: /mnt/data/dev/vouch-demo//env/bin/pyinfra
    Python: 3.8.10 (CPython, GCC 9.4.0)

How was pyinfra installed (source/pip)?

Using pip install pyinfra.

output with -vv and --debug

Not applicable

Fizzadar commented 1 year ago

This should finally be fixed in https://github.com/Fizzadar/pyinfra/commit/976359cb1c9672830595f999f9fce1195f01402b!