Hadron / carthage

Carthage is an Infrastructure as Code (IAC) framework
Other
8 stars 4 forks source link

Unclear error message for dependency_quote with async dependencies #22

Closed srak289 closed 2 years ago

srak289 commented 2 years ago

Given layout.py (see below) and accompanying stack-trace (below layout.py) this issue is specifically addressing the murkiness of the error message received by the user when dependency_quote is not specified where it is necessary.

The solution to indicate to the user is to change the line: add_provider(machine_implementation_key, AwsVm) to: add_provider(machine_implementation_key, dependency_quote(AwsVm)) Hopefully this can be made clearer.

layout.py:

class layout(CarthageLayout, AwsDnsManagement):

    domain = 'test.subatomicnetworks.com'

    add_provider(InjectionKey(AwsHostedZone), when_needed(AwsHostedZone, name="test.subatomicnetworks.com"))
    add_provider(machine_implementation_key, AwsVm)

    class default_network(NetworkModel):
        v4_config = V4Config(
            network = '192.168.100.0/24'
        )

    class default_netconfig(NetworkConfigModel):
        add('eth0', mac=None, net=InjectionKey('default_network'))

    class test_vm(MachineModel):
        name = 'test-vm'
        cloud_init = True
        key = 'srak'
        aws_ami = 'ami-0071048c60844169f'
        aws_instance_type = 't2.micro'

stack-trace resulting from carthage-runner --no-tmux --generate --config config.yml --start --keep:

Traceback (most recent call last):
  File "/home/srak/projects/carthage/bin/carthage-runner", line 195, in <module>
    loop.run_until_complete(run())
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/srak/projects/carthage/bin/carthage-runner", line 66, in run
    await layout.generate()
  File "/home/srak/projects/carthage/carthage/modeling/base.py", line 170, in generate
    models = await self.resolve_networking()
  File "/home/srak/projects/carthage/carthage/modeling/base.py", line 151, in resolve_networking
    if resolve_networking_futures: await asyncio.gather( *resolve_networking_futures)
  File "/home/srak/projects/carthage/carthage/modeling/base.py", line 385, in resolve_networking
    if issubclass(self.machine_type, LocalMachineMixin):
  File "/home/srak/projects/carthage/carthage/utils.py", line 41, in __get__
    res = self.fun(instance)
  File "/home/srak/projects/carthage/carthage/modeling/base.py", line 353, in machine_type
    implementation = self.injector.get_instance(machine_implementation_key)
  File "/home/srak/projects/carthage/carthage/dependency_injection.py", line 433, in get_instance
    result = satisfy_against._instantiate(
  File "/home/srak/projects/carthage/carthage/dependency_injection.py", line 549, in _instantiate
    injector.get_instance(d, placement = kwarg_place(k),
  File "/home/srak/projects/carthage/carthage/dependency_injection.py", line 433, in get_instance
    result = satisfy_against._instantiate(
  File "/home/srak/projects/carthage/carthage/dependency_injection.py", line 559, in _instantiate
    res = handle_result()
  File "/home/srak/projects/carthage/carthage/dependency_injection.py", line 489, in handle_result
    raise AsyncRequired("Asynchronous dependency injected into non-asynchronous context")
carthage.dependency_injection.AsyncRequired: Asynchronous dependency injected into non-asynchronous context
hartmans commented 2 years ago

I definitely think I can improve things by catching AsyncRequired in machine_type. Long term, we might be able to catching things at the add_provider call site, but that will take some sort of mechanism to add validators to check and see that a particular provider is sensible for a particular key.