public inbox for kdevops@lists.linux.dev
 help / color / mirror / Atom feed
From: Luis Chamberlain <mcgrof@kernel.org>
To: kdevops@lists.linux.dev
Cc: Luis Chamberlain <mcgrof@kernel.org>
Subject: [PATCH 8/8] guestfs: verify new line on ssh include directive
Date: Thu,  7 Mar 2024 16:03:59 -0800	[thread overview]
Message-ID: <20240308000400.1646823-9-mcgrof@kernel.org> (raw)
In-Reply-To: <20240308000400.1646823-1-mcgrof@kernel.org>

If the ansible task added the include directive for kdevops and later a new
host entry was added (say with Vagrant), it means the Include directive is
followed by an entry without a new line. This will mean ssh won't use
that include file.

So we need to be a bit paranoid with this effort. So we are going to
first check if this sanity check was done first by looking for a special
new tag we're going to add now, if that exists we know we've our job and
can bail. Otherwise we're going to remove the old stale line, move it to
the top and ensure its at the top of the file. To ensure a new line is
used we use the ansible block module, and we take advantage of this by
adding the version of kdevops we use to add this. That's our marker that
the include directive is OK.

Fixes: e9390b898f98 ("guestfs: add the Include directive to ~/.ssh/config")
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 .../update_ssh_config_guestfs/tasks/main.yml  | 58 ++++++++++++++++++-
 scripts/bringup_guestfs.sh                    |  1 -
 scripts/guestfs.Makefile                      |  1 +
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/playbooks/roles/update_ssh_config_guestfs/tasks/main.yml b/playbooks/roles/update_ssh_config_guestfs/tasks/main.yml
index 368f9941..4ac1ce44 100644
--- a/playbooks/roles/update_ssh_config_guestfs/tasks/main.yml
+++ b/playbooks/roles/update_ssh_config_guestfs/tasks/main.yml
@@ -1,6 +1,60 @@
-- name: Add Include directive to ~/.ssh/config
+# Check if the include directive is already presetn
+- name: Check if the kdevops include directive was used
+  lineinfile:
+    path: ~/.ssh/config
+    regexp: "Include ~/.ssh/config_kdevops_*"
+    state: absent
+  check_mode: yes
+  changed_when: false
+  register: kdevops_ssh_include
+
+# Check if the the kdevops_version was added in a comment
+- name: Check if the new include directive was used with a kdevops_version comment
+  lineinfile:
+    path: ~/.ssh/config
+    regexp: "^#(.*)kdevops_version(.*)"
+    state: absent
+  check_mode: yes
+  changed_when: false
+  register: fixed_ssh_entry
+
+# If both the include directive was found and kdevops version comment was found
+# we bail right away to avoid updating the ssh config file always.
+- name: Check if the new fixed include directive was used
+  meta: end_play
+  when:
+    - kdevops_ssh_include.found
+    - fixed_ssh_entry.found
+
+# If we're still running it means the correct include directive following a new
+# line was not found. So remove old stale include directives which may be
+# buggy.
+- name: Add remove buggy stale include directive to ~/.ssh/config without a new line which was buggy
   lineinfile:
     path: ~/.ssh/config
     line: "Include ~/.ssh/config_kdevops_*"
-    insertbefore: "BOF"
+    state: absent
+
+- name: Remove any stale kdevops comments
+  lineinfile:
+    path: ~/.ssh/config
+    regexp: "^#(.*)kdevops(.*)"
+    state: absent
+
+- name: Remove any extra new lines
+  replace:
+    path: ~/.ssh/config
+    regexp: '(^\s*$)'
+    replace: ''
+
+# ssh include directives must follow a new line.
+- name: Add Include directive to ~/.ssh/config
+  blockinfile:
+    path: ~/.ssh/config
+    insertbefore: BOF
+    marker: "{mark}"
+    marker_begin: "# Automatically added by kdevops\n# kdevops_version: {{ kdevops_version }}"
+    marker_end: ""
     create: true
+    block: |
+      Include ~/.ssh/config_kdevops_*
diff --git a/scripts/bringup_guestfs.sh b/scripts/bringup_guestfs.sh
index b55b6a92..2b5b3857 100755
--- a/scripts/bringup_guestfs.sh
+++ b/scripts/bringup_guestfs.sh
@@ -109,7 +109,6 @@ do
 	cp --reflink=auto $BASE_IMAGE $ROOTIMG
 	virt-sysprep -a $ROOTIMG --hostname $name --ssh-inject "kdevops:file:$SSH_KEY.pub"
 
-
 	if [[ "$CONFIG_LIBVIRT_ENABLE_LARGEIO" == "y" ]]; then
 		lbs_idx=1
 		for i in $(seq 1 $(($CONFIG_QEMU_LARGEIO_MAX_POW_LIMIT+1))); do
diff --git a/scripts/guestfs.Makefile b/scripts/guestfs.Makefile
index 6328cfd5..cfa59cc6 100644
--- a/scripts/guestfs.Makefile
+++ b/scripts/guestfs.Makefile
@@ -66,6 +66,7 @@ $(KDEVOPS_PROVISIONED_SSH):
 		ansible-playbook $(ANSIBLE_VERBOSE) --connection=local \
 			--inventory localhost, \
 			playbooks/update_ssh_config_guestfs.yml \
+			--extra-vars=@./extra_vars.yaml \
 			-e 'ansible_python_interpreter=/usr/bin/python3' ;\
 		LIBVIRT_DEFAULT_URI=$(CONFIG_LIBVIRT_URI) $(TOPDIR)/scripts/update_ssh_config_guestfs.py; \
 	fi
-- 
2.43.0


  parent reply	other threads:[~2024-03-08  0:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-08  0:03 [PATCH 0/8] guestfs: fixes and enhancements Luis Chamberlain
2024-03-08  0:03 ` [PATCH 1/8] guestfs: use macros for drives for aarch64 Luis Chamberlain
2024-03-08  0:03 ` [PATCH 2/8] bringup: disable ZNS and CXL for guestfs Luis Chamberlain
2024-03-08  0:03 ` [PATCH 3/8] libvirt: move zns, largio and cxl to its own files Luis Chamberlain
2024-03-08  0:03 ` [PATCH 4/8] guestfs: move options to its own file Luis Chamberlain
2024-03-08  0:03 ` [PATCH 5/8] bringup: match default distro to user's distro Luis Chamberlain
2024-03-08  0:03 ` [PATCH 6/8] guestfs: remove explicit tap0 device name Luis Chamberlain
2024-03-08  0:03 ` [PATCH 7/8] destroy_guestfs.sh: remove known ssh key Luis Chamberlain
2024-03-08  0:03 ` Luis Chamberlain [this message]
2024-03-08  9:55 ` [PATCH 0/8] guestfs: fixes and enhancements Luis Chamberlain
2024-03-08 14:14   ` Chuck Lever III
2024-03-08 14:26     ` Chuck Lever III
2024-03-08 15:44       ` Luis Chamberlain
2024-03-08 15:46         ` Chuck Lever III
2024-03-08 15:56           ` Luis Chamberlain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240308000400.1646823-9-mcgrof@kernel.org \
    --to=mcgrof@kernel.org \
    --cc=kdevops@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox