labkey/maas: Use hostnames= filter in maas.client.machines.list()
I've now learned how to search for machines by hostname[*]. Doing so speeds
up the API call, so let's do that in addition to the hostname->system_id
caching. This may speed things up enough that caching no longer provides a
noticable improvement, but it also shouldn't hurt anything.
labkey: Add ssh subcommand using MAAS to lookup the machine's IP
Since we currently don't have a good DNS setup, I always have to manually
look up the IP address for a machine in MAAS before I can ssh to it. We can
automate that by looking up the machine's IP via the MAAS API. The machine's
MAAS endpoint can be looked up in the lab's clouds.yaml, and we can prompt
the user for their API key.
This is the first labkey subcommand that uses MAAS. To support this, I've
added a LabMAAS class that wraps the maas.client API. This should be
re-usable for any future subcommands that need to access MAAS. Of course,
this adds a new runtime dependency on python-libmaas, which existing users
may not have installed. We import this library on-demand to avoid breaking
existing commands for them.
We also introduce a couple new files in ~/.config/labkey/ that cache data for
future runs:
- maas-credentials.yaml: Stores MAAS API keys, so the user should only need to enter them the first time.
- maas-host-cache.yaml: Looking up the MAAS ID for a machine by hostname requires a slow sequential search through all machines. Speed things up by caching that mapping. For the one test host I tried this reduced ssh time from 23s to 7s.
These files are managed by a new class called YAMLCache.