From 8ec3750dda4d5824f4f018709c53eb231ea7124f Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Wed, 21 Nov 2018 10:36:12 +1100
Subject: [PATCH] simple-init: allow for NetworkManager support

This plumbs through an "--use-nm" flag to glean which instructs it to
setup interface bringup with NetworkManager rather than legacy network
enablement scripts.

In this case, install the NetworkManager package.  In the non-nm case,
also install the network-scripts for Fedora 29 -- this has stopped
being installed by default (it's been deprecated since forever).

As noted in the docs, this is currently really only relevant on the
supported rpm distros which are using the ifcfg-rh NetworkManager
plugin to effectively re-use old config files.  However,
NetworkManager has similar plugins for other platforms, so support can
be expanded if changes are proposed.

Depends-On: https://review.openstack.org/618964
Change-Id: I4d76e88ce25e5675fd5ef48924acd09915a62a4b
---
 diskimage_builder/elements/simple-init/README.rst   | 10 ++++++++++
 .../environment.d/15-simple-init-networkmanager     |  1 +
 .../elements/simple-init/install.d/50-simple-init   |  7 ++++++-
 .../elements/simple-init/package-installs.yaml      |  4 ++++
 diskimage_builder/elements/simple-init/pkg-map      | 13 +++++++++++--
 .../simple-init/post-install.d/80-simple-init       |  8 +++++---
 .../notes/simple-init-nm-f0896124dee92a03.yaml      |  5 +++++
 7 files changed, 42 insertions(+), 6 deletions(-)
 create mode 100644 diskimage_builder/elements/simple-init/environment.d/15-simple-init-networkmanager
 create mode 100644 releasenotes/notes/simple-init-nm-f0896124dee92a03.yaml

diff --git a/diskimage_builder/elements/simple-init/README.rst b/diskimage_builder/elements/simple-init/README.rst
index dd131d9f8..ad8dfc502 100644
--- a/diskimage_builder/elements/simple-init/README.rst
+++ b/diskimage_builder/elements/simple-init/README.rst
@@ -58,3 +58,13 @@ Then set your DIB env vars like this before running DIB::
   DIB_INSTALLTYPE_simple_init=repo
   DIB_REPOLOCATION_glean=/tmp/glean
   DIB_REPOREF_glean=my-test-ref
+
+NetworkManager
+--------------
+
+By default, this uses the "legacy" scripts on each platform.  To use
+NetworkManager instead, set ``DIB_SIMPLE_INIT_NETWORKMANAGER`` to
+non-zero.  See the glean documentation for what the implications for
+this are on each platform.
+
+This is currently only implemented for CentOS and Fedora platforms.
diff --git a/diskimage_builder/elements/simple-init/environment.d/15-simple-init-networkmanager b/diskimage_builder/elements/simple-init/environment.d/15-simple-init-networkmanager
new file mode 100644
index 000000000..3aaeaa767
--- /dev/null
+++ b/diskimage_builder/elements/simple-init/environment.d/15-simple-init-networkmanager
@@ -0,0 +1 @@
+export DIB_SIMPLE_INIT_NETWORKMANAGER=${DIB_SIMPLE_INIT_NETWORKMANAGER:-0}
diff --git a/diskimage_builder/elements/simple-init/install.d/50-simple-init b/diskimage_builder/elements/simple-init/install.d/50-simple-init
index 8e89296b2..cae36b837 100755
--- a/diskimage_builder/elements/simple-init/install.d/50-simple-init
+++ b/diskimage_builder/elements/simple-init/install.d/50-simple-init
@@ -24,4 +24,9 @@ SCRIPTDIR=$(dirname $0)
 # Ensure the 8021q module is loaded so that glean can support tagged interfaces
 echo "8021q" >> /etc/modules
 
-glean-install
+enable_nm=""
+if [[ ${DIB_SIMPLE_INIT_NETWORKMANAGER} != 0 ]]; then
+    enable_nm="--use-nm"
+fi
+
+glean-install ${enable_nm}
diff --git a/diskimage_builder/elements/simple-init/package-installs.yaml b/diskimage_builder/elements/simple-init/package-installs.yaml
index 8690364b5..ad868c0aa 100644
--- a/diskimage_builder/elements/simple-init/package-installs.yaml
+++ b/diskimage_builder/elements/simple-init/package-installs.yaml
@@ -1,3 +1,7 @@
 dhcp-client:
 net-tools:
 ifupdown:
+network-scripts:
+  when: DIB_SIMPLE_INIT_NETWORKMANAGER = 0
+NetworkManager:
+  when: DIB_SIMPLE_INIT_NETWORKMANAGER != 0
diff --git a/diskimage_builder/elements/simple-init/pkg-map b/diskimage_builder/elements/simple-init/pkg-map
index 906ca1211..b42f33c49 100644
--- a/diskimage_builder/elements/simple-init/pkg-map
+++ b/diskimage_builder/elements/simple-init/pkg-map
@@ -5,7 +5,8 @@
     },
     "debian": {
       "dhcp-client": "isc-dhcp-client",
-      "ifupdown": "ifupdown"
+      "ifupdown": "ifupdown",
+      "NetworkManager": "network-manager"
     },
     "gentoo": {
       "dhcp-client": "net-misc/dhcp",
@@ -16,8 +17,16 @@
       "dhcp-client": "dhcp-client"
     }
   },
+  "release": {
+    "fedora": {
+      "29": {
+        "network-scripts": "network-scripts"
+      }
+    }
+  },
   "default": {
     "dhcp-client": "isc-dhcp-client",
-    "ifupdown": ""
+    "ifupdown": "",
+    "network-scripts": ""
   }
 }
diff --git a/diskimage_builder/elements/simple-init/post-install.d/80-simple-init b/diskimage_builder/elements/simple-init/post-install.d/80-simple-init
index 898dd41b2..4c5a79ae3 100755
--- a/diskimage_builder/elements/simple-init/post-install.d/80-simple-init
+++ b/diskimage_builder/elements/simple-init/post-install.d/80-simple-init
@@ -13,9 +13,11 @@ case "$DIB_INIT_SYSTEM" in
         ;;
     systemd)
         if [[ ${DISTRO_NAME} =~ (centos|rhel7|fedora) ]]; then
-            # NOTE(pabelanger): Glean requires network.service for
-            # these platforms.
-            systemctl enable network.service
+            if [[ ${DIB_SIMPLE_INIT_NETWORKMANAGER} != 1 ]]; then
+                # NOTE(pabelanger): Glean requires network.service for
+                # these platforms when not using networkmanager
+                systemctl enable network.service
+            fi
         elif [[ ${DISTRO_NAME} =~ (opensuse) ]]; then
             # on suse, this is named wicked.service, but it's the same
             # as network.service.
diff --git a/releasenotes/notes/simple-init-nm-f0896124dee92a03.yaml b/releasenotes/notes/simple-init-nm-f0896124dee92a03.yaml
new file mode 100644
index 000000000..1fb86e77a
--- /dev/null
+++ b/releasenotes/notes/simple-init-nm-f0896124dee92a03.yaml
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    The `simple-init` element can now use NetworkManager instead of
+    legacy scripts on Red Hat platforms.