diff options
author | James Falcon <james.falcon@canonical.com> | 2023-01-30 11:43:32 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-30 10:43:32 -0700 |
commit | e73e873c07186120b02d24cd495671bc67db60d3 (patch) | |
tree | d651b24ac7745b5f96397536222c2d8f1a5afd85 /cloudinit | |
parent | 36014736e57aa526b59532895d85c462d5142f7f (diff) | |
download | cloud-init-git-e73e873c07186120b02d24cd495671bc67db60d3.tar.gz |
cc_set_passwords: Prevent traceback when restarting ssh (#1981)
On Bionic in GCE, when restarting ssh, it's not uncommon to see a
traceback with `ssh.service is not active, cannot reload.`. Instead log
a warning.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/config/cc_set_passwords.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py index 8524a080..3a0b3f5b 100644 --- a/cloudinit/config/cc_set_passwords.py +++ b/cloudinit/config/cc_set_passwords.py @@ -108,6 +108,18 @@ def get_users_by_type(users_list: list, pw_type: str) -> list: ) +def _restart_ssh_daemon(distro, service): + try: + distro.manage_service("restart", service) + LOG.debug("Restarted the SSH daemon.") + except subp.ProcessExecutionError as e: + LOG.warning( + "'ssh_pwauth' configuration may not be applied. Cloud-init was " + "unable to restart SSH daemon due to error: '%s'", + e, + ) + + def handle_ssh_pwauth(pw_auth, distro: Distro): """Apply sshd PasswordAuthentication changes. @@ -155,20 +167,9 @@ def handle_ssh_pwauth(pw_auth, distro: Distro): ] ).stdout.strip() if state.lower() in ["active", "activating", "reloading"]: - distro.manage_service("restart", service) - LOG.debug("Restarted the SSH daemon.") - else: - LOG.debug("Not restarting SSH service: service is stopped.") + _restart_ssh_daemon(distro, service) else: - try: - distro.manage_service("restart", service) - LOG.debug("Restarted the SSH daemon.") - except subp.ProcessExecutionError: - util.logexc( - LOG, - "Cloud-init was unable to restart SSH daemon. " - "'ssh_pwauth' configuration may not be applied.", - ) + _restart_ssh_daemon(distro, service) def handle( |