meta-google: gbmc-ncsi-config: Optionally purge stale networkd configs

On many machines it's safe to purge all configurations that do not match
the NCSI interface, to keep a tidy config directory and purge
potentially problematic configs.

Change-Id: I5f714516eb3718544f0787ac38eb69d485086b42
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-purge.service.in b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-purge.service.in
new file mode 100644
index 0000000..bbbd992
--- /dev/null
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-purge.service.in
@@ -0,0 +1,10 @@
+[Unit]
+Before=systemd-network.service
+After=gbmc-ncsi-old.service
+
+[Service]
+Type=oneshot
+ExecStart=find /etc/systemd/network -mindepth 1 ! -name 00-bmc-@NCSI_IF@.network -exec rm -vrf {} \; -prune
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
index cb547b1..607f1b7 100644
--- a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
+++ b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
@@ -6,6 +6,7 @@
 inherit systemd
 
 GBMC_NCSI_IF_OLD ??= ""
+GBMC_NCSI_PURGE_ETC ??= ""
 
 SRC_URI += " \
   file://-bmc-gbmcbrncsidhcp.netdev \
@@ -26,6 +27,7 @@
   file://gbmc-ncsi-alias.service.in \
   file://50-gbmc-ncsi-clear-ip.sh.in \
   file://gbmc-ncsi-old.service.in \
+  file://gbmc-ncsi-purge.service.in \
   "
 
 S = "${WORKDIR}"
@@ -54,6 +56,7 @@
   gbmc-ncsi-set-nicenabled.service \
   gbmc-ncsi-ip-from-ra.service \
   ${@'' if d.getVar('GBMC_NCSI_IF_OLD') == '' else 'gbmc-ncsi-old.service'} \
+  ${@'' if d.getVar('GBMC_NCSI_PURGE_ETC') == '' else 'gbmc-ncsi-purge.service'} \
   "
 
 do_install:append() {
@@ -137,6 +140,11 @@
       >${D}${systemd_system_unitdir}/gbmc-ncsi-old.service
   fi
 
+  if [ -n "${GBMC_NCSI_PURGE_ETC}" ]; then
+    sed -e "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-purge.service.in \
+      >${D}${systemd_system_unitdir}/gbmc-ncsi-purge.service
+  fi
+
   sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-ip-from-ra.service.in \
     >${WORKDIR}/gbmc-ncsi-ip-from-ra.service
   install -m0644 ${WORKDIR}/gbmc-ncsi-ip-from-ra.service ${D}${systemd_system_unitdir}