Ubuntu Pastebin

Paste from smoser at Fri, 19 Aug 2016 17:15:13 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
commit b9e925fbaa284fbb40da9b3281decef2781983ec (HEAD -> dhclient-hook-on-azure-only)
Author: Scott Moser <smoser@brickies.net>
Date:   Thu Aug 18 12:25:29 2016 -0400

    azure dhclient-hook cleanups
    
    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, only run the dhclient hooks if 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.  Running should not be
    harmful anywhere, other than slowing down dhclient.
    
    The value of this additional code is that then dhclient having run
    does not task the system with the load of cloud-init.
    
    Additionally, some changes to config are done here.
     * rename 'dhclient_leases' to 'dhclient_lease_file'
     * move that to the datasource config (datasource/Azure/dhclient_lease_file)

diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
index a251fe0..cf3ea95 100644
--- a/cloudinit/sources/DataSourceAzure.py
+++ b/cloudinit/sources/DataSourceAzure.py
@@ -54,6 +54,7 @@ BUILTIN_DS_CONFIG = {
         'hostname_command': 'hostname',
     },
     'disk_aliases': {'ephemeral0': '/dev/sdb'},
+    'dhclient_lease_file': '/var/lib/dhcp/dhclient.eth0.leases',
 }
 
 BUILTIN_CLOUD_CONFIG = {
@@ -106,8 +107,6 @@ def temporary_hostname(temp_hostname, cfg, hostname_command='hostname'):
 
 
 class DataSourceAzureNet(sources.DataSource):
-    FALLBACK_LEASE = '/var/lib/dhcp/dhclient.eth0.leases'
-
     def __init__(self, sys_cfg, distro, paths):
         sources.DataSource.__init__(self, sys_cfg, distro, paths)
         self.seed_dir = os.path.join(paths.seed_dir, 'azure')
@@ -116,8 +115,8 @@ class DataSourceAzureNet(sources.DataSource):
         self.ds_cfg = util.mergemanydict([
             util.get_cfg_by_path(sys_cfg, DS_CFG_PATH, {}),
             BUILTIN_DS_CONFIG])
-        self.dhclient_lease_file = self.paths.cfgs.get('dhclient_lease',
-                                                       self.FALLBACK_LEASE)
+        LOG.info("ds_cfg = %s", self.ds_cfg)
+        self.dhclient_lease_file = self.ds_cfg.get('dhclient_lease_file')
 
     def __str__(self):
         root = sources.DataSource.__str__(self)
@@ -234,7 +233,10 @@ class DataSourceAzureNet(sources.DataSource):
                                     dhclient_lease_file)
         else:
             metadata_func = self.get_metadata_from_agent
+
         try:
+            LOG.debug("Attemping to get data via agent_command: %s",
+                      self.ds_cfg['agent_command'])
             fabric_data = metadata_func()
         except Exception as exc:
             LOG.info("Error communicating with Azure fabric; assume we aren't"
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
index 6e43440..689ed4c 100644
--- a/cloudinit/sources/helpers/azure.py
+++ b/cloudinit/sources/helpers/azure.py
@@ -190,7 +190,8 @@ class WALinuxAgentShim(object):
         '</Health>'])
 
     def __init__(self, fallback_lease_file=None):
-        LOG.debug('WALinuxAgentShim instantiated...')
+        LOG.debug('WALinuxAgentShim instantiated, fallback_lease_file=%s',
+                  fallback_lease_file)
         self.dhcpoptions = None
         self._endpoint = None
         self.openssl_manager = None
diff --git a/config/cloud.cfg b/config/cloud.cfg
index 93ef342..2d7fb47 100644
--- a/config/cloud.cfg
+++ b/config/cloud.cfg
@@ -98,7 +98,6 @@ system_info:
       cloud_dir: /var/lib/cloud/
       templates_dir: /etc/cloud/templates/
       upstart_dir: /etc/init/
-      dhclient_lease: 
    package_mirrors:
      - arches: [i386, amd64]
        failsafe:
@@ -115,8 +114,3 @@ system_info:
          primary: http://ports.ubuntu.com/ubuntu-ports
          security: http://ports.ubuntu.com/ubuntu-ports
    ssh_svcname: ssh
-datasource:
-  Azure:
-    set_hostname: False
-    agent_command: __builtin__
-
diff --git a/doc/sources/azure/README.rst b/doc/sources/azure/README.rst
index 48f3cc7..ec7d9e8 100644
--- a/doc/sources/azure/README.rst
+++ b/doc/sources/azure/README.rst
@@ -30,13 +30,10 @@ datasource:
 If those files are not available, the fallback is to check the leases file
 for the endpoint server (again option 245).
 
-You can define the path to the lease file with the 'dhclient_lease' configuration
-value under system_info: and paths:.  For example:
+You can define the path to the lease file with the 'dhclient_lease_file'
+configuration.  The default value is /var/lib/dhcp/dhclient.eth0.leases.
 
-      dhclient_lease: /var/lib/dhcp/dhclient.eth0.leases
-
-If no configuration value is provided, the dhclient_lease value will fallback to
-/var/lib/dhcp/dhclient.eth0.leases.
+    dhclient_lease_file: /var/lib/dhcp/dhclient.eth0.leases
 
 walinuxagent
 ------------
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"
 }
Download as text