summaryrefslogtreecommitdiff
path: root/cloudinit/dhclient_hook.py
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2016-08-10 16:36:49 -0600
committerScott Moser <smoser@brickies.net>2016-08-15 10:16:19 -0400
commit648dbbf6b090c81e989f1ab70bf99f4de16a6a70 (patch)
treec69218dffe430477054483edd7876cc56b902370 /cloudinit/dhclient_hook.py
parentbc2c3267549b9067c017a34e22bbee18890aec06 (diff)
downloadvyos-cloud-init-648dbbf6b090c81e989f1ab70bf99f4de16a6a70.tar.gz
vyos-cloud-init-648dbbf6b090c81e989f1ab70bf99f4de16a6a70.zip
Get Azure endpoint server from DHCP client
It is more efficient and cross-distribution safe to use the hooks function from dhclient to obtain the Azure endpoint server (DHCP option 245). This is done by providing shell scritps that are called by the hooks infrastructure of both dhclient and NetworkManager. The hooks then invoke 'cloud-init dhclient-hook' that maintains json data with the dhclient options in /run/cloud-init/dhclient.hooks/<interface>.json . The azure helper then pulls the value from /run/cloud-init/dhclient.hooks/<interface>.json file(s). If that file does not exist or the value is not present, it will then fall back to the original method of scraping the dhcp client lease file.
Diffstat (limited to 'cloudinit/dhclient_hook.py')
-rw-r--r--cloudinit/dhclient_hook.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/cloudinit/dhclient_hook.py b/cloudinit/dhclient_hook.py
new file mode 100644
index 00000000..9dcbe39c
--- /dev/null
+++ b/cloudinit/dhclient_hook.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+# vi: ts=4 expandtab
+
+import os
+
+from cloudinit.atomic_helper import atomic_write_json
+from cloudinit import log as logging
+from cloudinit import stages
+
+LOG = logging.getLogger(__name__)
+
+
+class LogDhclient(object):
+
+ def __init__(self, cli_args):
+ self.hooks_dir = self._get_hooks_dir()
+ self.net_interface = cli_args.net_interface
+ self.net_action = cli_args.net_action
+ self.hook_file = os.path.join(self.hooks_dir,
+ self.net_interface + ".json")
+
+ @staticmethod
+ def _get_hooks_dir():
+ i = stages.Init()
+ return os.path.join(i.paths.get_runpath(), 'dhclient.hooks')
+
+ def check_hooks_dir(self):
+ if not os.path.exists(self.hooks_dir):
+ os.makedirs(self.hooks_dir)
+ else:
+ # If the action is down and the json file exists, we need to
+ # delete the file
+ if self.net_action is 'down' and os.path.exists(self.hook_file):
+ os.remove(self.hook_file)
+
+ @staticmethod
+ def get_vals(info):
+ new_info = {}
+ for k, v in info.items():
+ if k.startswith("DHCP4_") or k.startswith("new_"):
+ key = (k.replace('DHCP4_', '').replace('new_', '')).lower()
+ new_info[key] = v
+ return new_info
+
+ def record(self):
+ envs = os.environ
+ if self.hook_file is None:
+ return
+ atomic_write_json(self.hook_file, self.get_vals(envs))
+ LOG.debug("Wrote dhclient options in %s", self.hook_file)