← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:dhclient-hook-on-azure-only into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:dhclient-hook-on-azure-only into cloud-init:master.

Commit message:
dhclient-hook: only run if on azure and cloud-init is enabled.

This adds some function to the generator to maintain the presense
of a flag file '/run/cloud-init/enabled' indicating that cloud-init
is enabled.

Then, in the hook scripts, only run the cloud-init dhclient-hook
if we are on azure and cloud-init is enabled.   The test for is_azure
currently only checks to see that the board vendor is Microsoft, not
actually that we are on azure.

The value of this additional code is that then dhclient having run
does not task the system with the load of cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/303303
-- 
Your team cloud init development team is requested to review the proposed merge of ~smoser/cloud-init:dhclient-hook-on-azure-only into cloud-init:master.
diff --git a/systemd/cloud-init-generator b/systemd/cloud-init-generator
index 2d31969..fedb630 100755
--- a/systemd/cloud-init-generator
+++ b/systemd/cloud-init-generator
@@ -6,6 +6,7 @@ DEBUG_LEVEL=1
 LOG_D="/run/cloud-init"
 ENABLE="enabled"
 DISABLE="disabled"
+RUN_ENABLED_FILE="$LOG_D/$ENABLE"
 CLOUD_SYSTEM_TARGET="/lib/systemd/system/cloud-init.target"
 CLOUD_TARGET_NAME="cloud-init.target"
 # lxc sets 'container', but lets make that explicitly a global
@@ -107,6 +108,7 @@ main() {
                     "ln $CLOUD_SYSTEM_TARGET $link_path"
             fi
         fi
+        : > "$RUN_ENABLED_FILE"
     elif [ "$result" = "$DISABLE" ]; then
         if [ -f "$link_path" ]; then
             if rm -f "$link_path"; then
@@ -118,6 +120,9 @@ main() {
         else
             debug 1 "already disabled: no change needed [no $link_path]"
         fi
+        if [ -e "$RUN_ENABLED_FILE" ]; then
+            rm -f "$RUN_ENABLED_FILE"
+        fi
     else
         debug 0 "unexpected result '$result'"
         ret=3
diff --git a/tools/hook-dhclient b/tools/hook-dhclient
index d099979..6a4626c 100755
--- a/tools/hook-dhclient
+++ b/tools/hook-dhclient
@@ -1,9 +1,24 @@
 #!/bin/sh
 # This script writes DHCP lease information into the cloud-init run directory
 # It is sourced, not executed.  For more information see dhclient-script(8).
+is_azure() {
+    local dmi_path="/sys/class/dmi/id/board_vendor" vendor=""
+    if [ -e "$dmi_path" ] && read vendor < "$dmi_path"; then
+        [ "$vendor" = "Microsoft Corporation" ] && return 0
+    fi
+    return 1
+}
 
-case "$reason" in
-   BOUND) cloud-init dhclient-hook up "$interface";;
-   DOWN|RELEASE|REBOOT|STOP|EXPIRE)
-      cloud-init dhclient-hook down "$interface";;
-esac
+is_enabled() {
+    # only execute hooks if cloud-init is enabled and on azure
+    [ -e /run/cloud-init/enabled ] || return 1
+    is_azure
+}
+
+if is_enabled; then
+   case "$reason" in
+      BOUND) cloud-init dhclient-hook up "$interface";;
+      DOWN|RELEASE|REBOOT|STOP|EXPIRE)
+         cloud-init dhclient-hook down "$interface";;
+   esac
+fi
diff --git a/tools/hook-network-manager b/tools/hook-network-manager
index 447b134..98a36c8 100755
--- a/tools/hook-network-manager
+++ b/tools/hook-network-manager
@@ -2,8 +2,23 @@
 # This script hooks into NetworkManager(8) via its scripts
 # arguments are 'interface-name' and 'action'
 #
+is_azure() {
+    local dmi_path="/sys/class/dmi/id/board_vendor" vendor=""
+    if [ -e "$dmi_path" ] && read vendor < "$dmi_path"; then
+        [ "$vendor" = "Microsoft Corporation" ] && return 0
+    fi
+    return 1
+}
 
-case "$1:$2" in
-   *:up) exec cloud-init dhclient-hook up "$1";;
-   *:down) exec cloud-init dhclient-hook down "$1";;
-esac
+is_enabled() {
+    # only execute hooks if cloud-init is enabled and on azure
+    [ -e /run/cloud-init/enabled ] || return 1
+    is_azure
+}
+
+if is_enabled; then
+    case "$1:$2" in
+        *:up) exec cloud-init dhclient-hook up "$1";;
+        *:down) exec cloud-init dhclient-hook down "$1";;
+    esac
+fi
diff --git a/tools/hook-rhel.sh b/tools/hook-rhel.sh
index 5e963a8..8232414 100755
--- a/tools/hook-rhel.sh
+++ b/tools/hook-rhel.sh
@@ -2,11 +2,26 @@
 # Current versions of RHEL and CentOS do not honor the directory
 # /etc/dhcp/dhclient-exit-hooks.d so this file can be placed in
 # /etc/dhcp/dhclient.d instead
+is_azure() {
+    local dmi_path="/sys/class/dmi/id/board_vendor" vendor=""
+    if [ -e "$dmi_path" ] && read vendor < "$dmi_path"; then
+        [ "$vendor" = "Microsoft Corporation" ] && return 0
+    fi
+    return 1
+}
+
+is_enabled() {
+    # only execute hooks if cloud-init is enabled and on azure
+    [ -e /run/cloud-init/enabled ] || return 1
+    is_azure
+}
 
 hook-rhel_config(){
+    is_enabled || return 0
     cloud-init dhclient-hook up "$interface"
 }
 
 hook-rhel_restore(){
+    is_enabled || return 0
     cloud-init dhclient-hook down "$interface"
 }

References