Merge ~mitchdz/ubuntu/+source/hibagent:mitch/mantic-imdsv2 into ubuntu/+source/hibagent:ubuntu/mantic-devel

Proposed by Mitchell Dzurick
Status: Merged
Merge reported by: Mitchell Dzurick
Merged at revision: 135171b6fb2cbf4e246f49bdeee6dded9e132a68
Proposed branch: ~mitchdz/ubuntu/+source/hibagent:mitch/mantic-imdsv2
Merge into: ubuntu/+source/hibagent:ubuntu/mantic-devel
Diff against target: 121 lines (+88/-0)
4 files modified
debian/changelog (+9/-0)
debian/control (+1/-0)
debian/patches/do-nothing-if-ODH-is-configured.patch (+77/-0)
debian/patches/series (+1/-0)
Reviewer Review Type Date Requested Status
git-ubuntu import Pending
Review via email: mp+455810@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/debian/changelog b/debian/changelog
index 3c8851c..62608ce 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,12 @@
1hibagent (1.0.1+git20230216.9ac1209f7-0ubuntu1.23.10.1) mantic; urgency=medium
2
3 * d/p/do-nothing-if-ODH-is-configured.patch: do nothing if ODH is configured,
4 this fixes an issue when this package and ec2-hibinit-agent are installed
5 and configured at the same time (LP: #2043739).
6 * d/control: add python3-requests as Depends.
7
8 -- Mitchell Dzurick <mitchell.dzurick@canonical.com> Thu, 16 Nov 2023 15:35:33 -0700
9
1hibagent (1.0.1+git20230216.9ac1209f7-0ubuntu1) lunar; urgency=medium10hibagent (1.0.1+git20230216.9ac1209f7-0ubuntu1) lunar; urgency=medium
211
3 * New upstream snapshot from their `ubuntu` branch (LP: #1896638)12 * New upstream snapshot from their `ubuntu` branch (LP: #1896638)
diff --git a/debian/control b/debian/control
index 60fac58..eb13a13 100644
--- a/debian/control
+++ b/debian/control
@@ -7,6 +7,7 @@ Build-Depends: debhelper (>= 9),
7 dh-python,7 dh-python,
8 python3-all,8 python3-all,
9 python3-pytest,9 python3-pytest,
10 python3-requests,
10 python3-setuptools11 python3-setuptools
11Standards-Version: 3.9.612Standards-Version: 3.9.6
1213
diff --git a/debian/patches/do-nothing-if-ODH-is-configured.patch b/debian/patches/do-nothing-if-ODH-is-configured.patch
13new file mode 10064414new file mode 100644
index 0000000..8be4ec2
--- /dev/null
+++ b/debian/patches/do-nothing-if-ODH-is-configured.patch
@@ -0,0 +1,77 @@
1Description: Update the Spot hibernate agent to do nothing if ODH is configured
2Author: Jeongin Cho <achojeon@amazon.com>
3Origin: upstream, https://github.com/aws/ec2-hibernate-linux-agent/commit/2ee4ae3fd1333fb3c9aab25bf02b109c3b7b8d9f
4Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/hibagent/+bug/2043739
5Last-Update: 2023-11-16
6---
7This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
8--- a/agent/hibagent
9+++ b/agent/hibagent
10@@ -18,6 +18,7 @@
11 import struct
12 import sys
13 import syslog
14+import requests
15 from subprocess import check_call, check_output
16 from threading import Thread
17 from math import ceil
18@@ -39,6 +40,9 @@
19 log_to_syslog = True
20 log_to_stderr = True
21
22+IMDS_BASEURL = 'http://169.254.169.254'
23+IMDS_API_TOKEN_PATH = 'latest/api/token'
24+IMDS_SPOT_ACTION_PATH = 'latest/meta-data/hibernation/configured'
25
26 def log(message):
27 if log_to_syslog:
28@@ -511,6 +515,37 @@
29 log("Failed to adjust pm_freeze_timeout to %d. Error: %s" % (timeout, str(e)))
30 exit(1)
31
32+def get_imds_token(seconds=21600):
33+ """ Get a token to access instance metadata. """
34+ log("Requesting new IMDSv2 token.")
35+ request_header = {'X-aws-ec2-metadata-token-ttl-seconds': '{}'.format(seconds)}
36+ token_url = '{}/{}'.format(IMDS_BASEURL, IMDS_API_TOKEN_PATH)
37+ response = requests.put(token_url, headers=request_header)
38+ response.close()
39+ if response.status_code != requests.codes.ok:
40+ return None
41+
42+ return response.text
43+
44+def hibernation_enabled():
45+ """Returns a boolean indicating whether hibernation-option.configured is enabled or not."""
46+
47+ imds_token = get_imds_token()
48+ if imds_token is None:
49+ log("IMDS_V2 http endpoint is disabled")
50+ # IMDS http endpoint is disabled
51+ return False
52+
53+ request_header = {'X-aws-ec2-metadata-token': imds_token}
54+ response = requests.get("{}/{}".format(IMDS_BASEURL, IMDS_SPOT_ACTION_PATH),
55+ headers=request_header)
56+ response.close()
57+ if response.status_code != requests.codes.ok or response.text.lower() == "false":
58+ return False
59+
60+ log("Hibernation Configured Flag found")
61+
62+ return True
63
64 def main():
65 # Parse arguments
66@@ -550,6 +585,11 @@
67
68 log("Effective config: %s" % config)
69
70+ # Let's first check if we need to kill the Spot Hibernate Agent
71+ if hibernation_enabled():
72+ log("Spot Instance Launch has enabled Hibernation Configured Flag. hibagent exiting!!")
73+ exit(0)
74+
75 target_swap_size = config.swap_mb * 1024 * 1024
76 ram_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
77 swap_percentage_size = ram_bytes * config.swap_percentage // 100
diff --git a/debian/patches/series b/debian/patches/series
index 5568008..0b9774f 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -9,3 +9,4 @@ fix-enable-ec2-spot-hibernation.patch
90009-Use-Thread.is_alive-for-Python-3.9-compatibility.patch90009-Use-Thread.is_alive-for-Python-3.9-compatibility.patch
10setuptools-fix-package-discovery.patch10setuptools-fix-package-discovery.patch
11disable-hibernate-test.patch11disable-hibernate-test.patch
12do-nothing-if-ODH-is-configured.patch

Subscribers

People subscribed via source and target branches