canonical / jhack

Chock-full of Juju hackery.
Apache License 2.0
51 stars 24 forks source link

jhack could not get current model #44

Closed rgildein closed 1 year ago

rgildein commented 1 year ago

It seems that jack has a problem finding the name of the current model. I have seen this error with multiple commands.

x1:➜  ~ jhack fire ubuntu/0 update-status                   
Traceback (most recent call last):                                                                                                          
  File "/snap/jhack/135/bin/jhack", line 8, in <module>    
    sys.exit(main())   
  File "/snap/jhack/135/lib/python3.8/site-packages/jhack/main.py", line 140, in main
    app()                                                                                                                                   
  File "/snap/jhack/135/lib/python3.8/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "/snap/jhack/135/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)                                                                                                       
  File "/snap/jhack/135/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/snap/jhack/135/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/snap/jhack/135/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/jhack/135/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/snap/jhack/135/lib/python3.8/site-packages/typer/main.py", line 500, in wrapper
    return callback(**use_params)  # type: ignore
  File "/snap/jhack/135/lib/python3.8/site-packages/jhack/utils/simulate_event.py", line 191, in simulate_event
    return _simulate_event(                                           
  File "/snap/jhack/135/lib/python3.8/site-packages/jhack/utils/simulate_event.py", line 122, in _simulate_event
    env = _get_env(                                                                                                                         
  File "/snap/jhack/135/lib/python3.8/site-packages/jhack/utils/simulate_event.py", line 52, in _get_env
    "JUJU_MODEL_NAME": current_model(),
  File "/snap/jhack/135/lib/python3.8/site-packages/jhack/helpers.py", line 187, in current_model
    return next(filter(key, all_models)).strip("*")
StopIteration                                                                                                                        [1.66s]

x1:➜  ~ juju models
Controller: u1

Model        Cloud/Region  Type  Status     Machines  Units  Access  Last connection
controller   u1-lxd/u1     lxd   available         1      -  admin   just now
default      u1-lxd/u1     lxd   available         0      -  admin   2023-01-20
juju-spell*  u1-lxd/u1     lxd   available         2      4  admin   4 seconds ago
                                                                                                                                     [1.02s]
x1:➜  ~ snap info jhack
name:      jhack
summary:   Make charming charming again.
publisher: Pietro Pasotti (ppasotti)
store-url: https://snapcraft.io/jhack
contact:   pietro.pasotti@canonical.com
license:   unset
description: |
  An opinionated collection of scripts and utilities for charmers.

  In order to use the cli tools that hit the juju api, you'll need to connect the
  dot-local-share-juju interface.

  Command: `sudo snap connect jhack:dot-local-share-juju snapd`

  Jhack needs that interface to obtain WRITE access to `~/.local/share/juju`, which is the folder
  where your local juju client stores its state, including:
    - cloud credentials
    - what controllers are registered, which controller is current
    - what models are present on each controller, which model is current

  If you see errors like:

      ERROR cannot load ssh client keys: open /home/pietro/.local/share/juju/ssh: permission denied
      No relations found in model None.

  Then you probably forgot to connect that plug.
commands:
  - jhack
snap-id:      gnxWJ9Sdlew4qfduWHEW8oczvdo2mvTT
tracking:     latest/candidate
refresh-date: today at 10:13 CET
channels:
  latest/stable:    0.3.2 2022-11-02 (120) 99MB -
  latest/candidate: 0.3.4 2023-01-02 (135) 99MB -
  latest/beta:      0.3.4 2023-01-02 (135) 99MB -
  latest/edge:      0.3.7 2023-01-25 (166) 99MB -
installed:          0.3.4            (135) 99MB -                                                                                    [0.27s]
aieri commented 1 year ago

@rgildein I can reproduce this if I disconnect the snap plug (sudo snap disconnect jhack:dot-local-share-juju snapd:personal-files) although oddly I also get a friendly "ERROR:jhack:It seems like the snap doesn't have access to /home/aieri/.local/share/juju;to grant it, run 'sudo snap connect jhack:dot-local-share-juju snapd'.Some Jhack commands will still work, but those that interact with the juju client will not.". Do you have the snap connection in place?

FTR the command fails for me even with the connection in place, but that's because I have a juju 2.x controller.

PietroPasotti commented 1 year ago

the get_models code is very messy either way, I'm busy cleaning it up in #49

PietroPasotti commented 1 year ago

This should be fixed by #49. Could you please try again and let me know if there's still an issue?

rgildein commented 1 year ago

I tried to install latest/edge and it seems working fine. I hit another issue, but I think it's because I'm using juju < 3.