cloudstack-cloudmonkey icon indicating copy to clipboard operation
cloudstack-cloudmonkey copied to clipboard

cmk ignores the CLI profile arg when loading the cache file

Open kgranroth-tm opened this issue 2 years ago • 0 comments

Summary

Cloudmonkey requires a default profile in .cmk/config to read the .cmk/profiles/*.cache files and it will ignore the command line arg when loading said cache, preferring the default profile.

System

OS: MacOS 11 CPU: darwin.x86-64 CMK Version: 6.2.0 (build: 8aae61e, 2021-09-23T10:50:26+0530)

Setup

Create a sample .cmk/config with something like:

profile = dev
[dev]
...
[prod]
...

Then perform a sync:

$ cmk sync
Discovered 194 APIs

Verify that it did create the sync cache for dev:

$ ls ~/.cmk/profiles
dev.cache

Verify that my custom command did sync and is cached:

$ jq '.api[] | select(.name=="myCustomCommand") | {name: .name}' ~/.cmk/profiles/dev.cache   
{
  "name": "myCustomCommand"
}

Attempt 1: Shouldn't work but does

Let's try to use one of those recently synced custom commands for prod, which doesn't have any cache file at all (as seen above):

cmk -d -p prod myCustomCommand
[debug] Trying to load profile: prod
[debug] cmdline args:cmk, -d, -p, prod, myCustomCommand
[debug] ExecCmd args: myCustomCommand
<actual results of working command>

That works, even though it shouldn't. The debug output shows that it is loading prod profile but I would expect it to fail since there is no prod.cache with the custom commands. Instead, it is reading dev.cache for the custom commands and only use prod as the profile for the actual execution of the command.

Attempt 2: Should work, but doesn't

Let's remove the default profile from .cmk/config (setting it to profile = or entirely deleting the line both do the same):

profile =
[dev]
...
[prod]
...

Then, since dev.cache already exists, let's try and use that:

$ cmk -d -p dev myCustomCommand 
[debug] Trying to load profile: dev
[debug] cmdline args:cmk, -d, -p, dev, myCustomCommand
[debug] ExecCmd args: myCustomCommand
Error: unknown command or API requested

That should work since there is a dev.cache file; there is a [dev] profile defined in the .cmk/config; and it does recognize that -p dev is valid. But because there is no profile = dev in .cmk/config, it will not work.

Workarounds

If myCustomCommand is the same in all profiles, then I can simply arbitrarily set profile = dev in .cmk/config and the command line -p <profile> will work for all profiles.

Alternatively, I can have separate config files per profile and use the new -c <config> option, but that only works starting recently.

kgranroth-tm avatar May 10 '22 20:05 kgranroth-tm