Skip to content

Jhack exception if no SSH access to Juju unit (default in Juju 4+) #234

@taurus-forever

Description

@taurus-forever

Bug Description

Hi,

usage jhack on default Juju 4 deployment generates a complex trace due to lack of unit access (SSH is not configured by default).

To Reproduce

juju version # 4.0.1-genericlinux-amd64
juju add-model test
juju deploy hello-juju
jhack fire hello-juju/0 update-status
hello-juju/0 update-status
╭───────────────────────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ /snap/jhack/601/lib/python3.12/site-packages/jhack/utils/simulate_event.py:570 in simulate_event                                                                                                                                                     │
│                                                                                                                                                                                                                                                      │
│   567 │   Especially useful in combination with jhack charm sync and/or debug-code/debug-hooks ╭───────────── locals ──────────────╮                                                                                                                 │
│   568 │   """                                                                                  │      check_name = None            │                                                                                                                 │
│   569 │   print(target, event)                                                                 │         dry_run = None            │                                                                                                                 │
│ ❱ 570 │   return _simulate_event(                                                              │    env_override = None            │                                                                                                                 │
│   571 │   │   target,                                                                          │           event = 'update-status' │                                                                                                                 │
│   572 │   │   event,                                                                           │           model = None            │                                                                                                                 │
│   573 │   │   relation_remote=relation_remote,                                                 │       notice_id = None            │                                                                                                                 │
│                                                                                                │        parallel = None            │                                                                                                                 │
│                                                                                                │     relation_id = None            │                                                                                                                 │
│                                                                                                │ relation_remote = None            │                                                                                                                 │
│                                                                                                │          secret = None            │                                                                                                                 │
│                                                                                                │     show_output = True            │                                                                                                                 │
│                                                                                                │          target = 'hello-juju/0'  │                                                                                                                 │
│                                                                                                ╰───────────────────────────────────╯                                                                                                                 │
│                                                                                                                                                                                                                                                      │
│ /snap/jhack/601/lib/python3.12/site-packages/jhack/utils/simulate_event.py:421 in _simulate_event                                                                                                                                                    │
│                                                                                                                                                                                                                                                      │
│   418 │   if not targets:                                                                      ╭───────────────── locals ─────────────────╮                                                                                                          │
│   419 │   │   sys.exit(f"invalid target: {target!r} not found in {model or '<the current model │            check_name = None             │                                                                                                          │
│   420 │                                                                                        │               dry_run = None             │                                                                                                          │
│ ❱ 421 │   cmds = tuple(                                                                        │         emit_juju_log = True             │                                                                                                          │
│   422 │   │   _build_command(                                                                  │          env_override = None             │                                                                                                          │
│   423 │   │   │   unit=unit_name,                                                              │                 event = 'update-status'  │                                                                                                          │
│   424 │   │   │   event=event,                                                                 │                 model = None             │                                                                                                          │
│                                                                                                │             notice_id = None             │                                                                                                          │
│                                                                                                │     operator_dispatch = False            │                                                                                                          │
│                                                                                                │              parallel = None             │                                                                                                          │
│                                                                                                │ print_captured_stderr = True             │                                                                                                          │
│                                                                                                │ print_captured_stdout = True             │                                                                                                          │
│                                                                                                │           relation_id = None             │                                                                                                          │
│                                                                                                │       relation_remote = None             │                                                                                                          │
│                                                                                                │    secret_id_or_label = None             │                                                                                                          │
│                                                                                                │                target = 'hello-juju/0'   │                                                                                                          │
│                                                                                                │               targets = ['hello-juju/0'] │                                                                                                          │
│                                                                                                ╰──────────────────────────────────────────╯                                                                                                          │
│                                                                                                                                                                                                                                                      │
│ /snap/jhack/601/lib/python3.12/site-packages/jhack/utils/simulate_event.py:433 in <genexpr>                                                                                                                                                          │
│                                                                                                                                                                                                                                                      │
│   430 │   │   │   secret_id_or_label=secret_id_or_label,                                       ╭─────────────────────────── locals ────────────────────────────╮                                                                                     │
│   431 │   │   │   operator_dispatch=operator_dispatch,                                         │                 .0 = <list_iterator object at 0x70339f4cdea0> │                                                                                     │
│   432 │   │   │   env_override=env_override,                                                   │         check_name = None                                     │                                                                                     │
│ ❱ 433 │   │   │   has_dispatch=is_dispatch_aware(unit_name, model),                            │       env_override = None                                     │                                                                                     │
│   434 │   │   )                                                                                │              event = 'update-status'                          │                                                                                     │
│   435 │   │   for unit_name in targets                                                         │              model = None                                     │                                                                                     │
│   436 │   )                                                                                    │          notice_id = None                                     │                                                                                     │
│                                                                                                │  operator_dispatch = False                                    │                                                                                     │
│                                                                                                │        relation_id = None                                     │                                                                                     │
│                                                                                                │    relation_remote = None                                     │                                                                                     │
│                                                                                                │ secret_id_or_label = None                                     │                                                                                     │
│                                                                                                │          unit_name = 'hello-juju/0'                           │                                                                                     │
│                                                                                                ╰───────────────────────────────────────────────────────────────╯                                                                                     │
│                                                                                                                                                                                                                                                      │
│ /snap/jhack/601/lib/python3.12/site-packages/jhack/helpers.py:279 in is_dispatch_aware                                                                                                                                                               │
│                                                                                                                                                                                                                                                      │
│   276 │   except CalledProcessError as e:                                                      ╭────────────────────────────────────────────── locals ──────────────────────────────────────────────╮                                                │
│   277 │   │   if e.returncode == 1:                                                            │         _model = ''                                                                                │                                                │
│   278 │   │   │   return False                                                                 │            cmd = 'juju ssh hello-juju/0 cat /var/lib/juju/agents/unit-hello-juju-0/charm/dispatch' │                                                │
│ ❱ 279 │   │   raise e                                                                          │          model = None                                                                              │                                                │
│   280                                                                                          │           unit = 'hello-juju/0'                                                                    │                                                │
│   281                                                                                          │ unit_sanitized = 'unit-hello-juju-0'                                                               │                                                │
│   282 @contextlib.contextmanager                                                               ╰────────────────────────────────────────────────────────────────────────────────────────────────────╯                                                │
│                                                                                                                                                                                                                                                      │
│ /snap/jhack/601/lib/python3.12/site-packages/jhack/helpers.py:274 in is_dispatch_aware                                                                                                                                                               │
│                                                                                                                                                                                                                                                      │
│   271 │   cmd = f"juju ssh{_model} {unit} cat /var/lib/juju/agents/{unit_sanitized}/charm/disp ╭────────────────────────────────────────────── locals ──────────────────────────────────────────────╮                                                │
│   272 │   logger.debug(f"running {cmd}")                                                       │         _model = ''                                                                                │                                                │
│   273 │   try:                                                                                 │            cmd = 'juju ssh hello-juju/0 cat /var/lib/juju/agents/unit-hello-juju-0/charm/dispatch' │                                                │
│ ❱ 274 │   │   check_call(shlex.split(cmd), stdout=PIPE, stderr=PIPE)                           │          model = None                                                                              │                                                │
│   275 │   │   return True                                                                      │           unit = 'hello-juju/0'                                                                    │                                                │
│   276 │   except CalledProcessError as e:                                                      │ unit_sanitized = 'unit-hello-juju-0'                                                               │                                                │
│   277 │   │   if e.returncode == 1:                                                            ╰────────────────────────────────────────────────────────────────────────────────────────────────────╯                                                │
│                                                                                                                                                                                                                                                      │
│ /usr/lib/python3.12/subprocess.py:413 in check_call                                                                                                                                                                                                  │
│                                                                                                                                                                                                                                                      │
│    410 │   │   cmd = kwargs.get("args")                                                         ╭──────────────────────────────────────────────────── locals ────────────────────────────────────────────────────╮                                   │
│    411 │   │   if cmd is None:                                                                  │       cmd = ['juju', 'ssh', 'hello-juju/0', 'cat', '/var/lib/juju/agents/unit-hello-juju-0/charm/dispatch']    │                                   │
│    412 │   │   │   cmd = popenargs[0]                                                           │    kwargs = {'stdout': -1, 'stderr': -1}                                                                       │                                   │
│ ❱  413 │   │   raise CalledProcessError(retcode, cmd)                                           │ popenargs = (['juju', 'ssh', 'hello-juju/0', 'cat', '/var/lib/juju/agents/unit-hello-juju-0/charm/dispatch'],) │                                   │
│    414 │   return 0                                                                             │   retcode = 255                                                                                                │                                   │
│    415                                                                                          ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯                                   │
│    416                                                                                                                                                                                                                                               │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
CalledProcessError: Command '['juju', 'ssh', 'hello-juju/0', 'cat', '/var/lib/juju/agents/unit-hello-juju-0/charm/dispatch']' returned non-zero exit status 255.
ubuntu@juju400:~$ 

Fix:

ssh-keygen -b 4096
juju add-ssh-key "$(cat ~/.ssh/id_ed25519.pub)"
jhack fire hello-juju/0 update-status # produces no more exception.

Environment

jhack 0.4.4.0.24 601 latest/edge ppasotti -
juju 4.0.1-63a1d56 33390 4.0/edge canonical✓ -
lxd 6.5-ccdfb39 36020 latest/stable canonical✓ -

Relevant log output

See above

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions