Merge ~smoser/cloud-init:bug/1732964-kill-dhclient-on-ec2 into cloud-init:master

Proposed by Scott Moser
Status: Merged
Approved by: Chad Smith
Approved revision: c0a23f6efe8002370abebdfff8607b36a711df19
Merged at revision: d3a0958c09c73a78fda6e922b749a1b98036e984
Proposed branch: ~smoser/cloud-init:bug/1732964-kill-dhclient-on-ec2
Merge into: cloud-init:master
Diff against target: 69 lines (+16/-2)
2 files modified
cloudinit/net/dhcp.py (+8/-1)
cloudinit/net/tests/test_dhcp.py (+8/-1)
Reviewer Review Type Date Requested Status
Chad Smith Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+333905@code.launchpad.net

Commit message

EC2: Kill dhclient process used in sandbox dhclient.

dhclient runs, obtains a address and then backgrounds itself.
cloud-init did not take care to kill it after it was done with it.
After it has run and created the leases, we can kill it.

LP: #1732964

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:b383afc9f88adba046f862bb62ff90c4a6d22775
https://jenkins.ubuntu.com/server/job/cloud-init-ci/509/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/509/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:c0a23f6efe8002370abebdfff8607b36a711df19
https://jenkins.ubuntu.com/server/job/cloud-init-ci/511/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/511/rebuild

review: Approve (continuous-integration)
Revision history for this message
Chad Smith (chad.smith) wrote :

This is good work, thanks for the fix. Tested EC2 across upgrade path and watched the pid read and kill of dhcp process.

review: Approve

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/net/dhcp.py b/cloudinit/net/dhcp.py
2index 0cba703..f3a412a 100644
3--- a/cloudinit/net/dhcp.py
4+++ b/cloudinit/net/dhcp.py
5@@ -8,6 +8,7 @@ import configobj
6 import logging
7 import os
8 import re
9+import signal
10
11 from cloudinit.net import find_fallback_nic, get_devicelist
12 from cloudinit import temp_utils
13@@ -119,7 +120,13 @@ def dhcp_discovery(dhclient_cmd_path, interface, cleandir):
14 cmd = [sandbox_dhclient_cmd, '-1', '-v', '-lf', lease_file,
15 '-pf', pid_file, interface, '-sf', '/bin/true']
16 util.subp(cmd, capture=True)
17- return parse_dhcp_lease_file(lease_file)
18+ pid = None
19+ try:
20+ pid = int(util.load_file(pid_file).strip())
21+ return parse_dhcp_lease_file(lease_file)
22+ finally:
23+ if pid:
24+ os.kill(pid, signal.SIGKILL)
25
26
27 def networkd_parse_lease(content):
28diff --git a/cloudinit/net/tests/test_dhcp.py b/cloudinit/net/tests/test_dhcp.py
29index 1c1f504..3d8e15c 100644
30--- a/cloudinit/net/tests/test_dhcp.py
31+++ b/cloudinit/net/tests/test_dhcp.py
32@@ -2,6 +2,7 @@
33
34 import mock
35 import os
36+import signal
37 from textwrap import dedent
38
39 from cloudinit.net.dhcp import (
40@@ -114,8 +115,9 @@ class TestDHCPDiscoveryClean(CiTestCase):
41 self.assertEqual('eth9', call[0][1])
42 self.assertIn('/var/tmp/cloud-init/cloud-init-dhcp-', call[0][2])
43
44+ @mock.patch('cloudinit.net.dhcp.os.kill')
45 @mock.patch('cloudinit.net.dhcp.util.subp')
46- def test_dhcp_discovery_run_in_sandbox(self, m_subp):
47+ def test_dhcp_discovery_run_in_sandbox(self, m_subp, m_kill):
48 """dhcp_discovery brings up the interface and runs dhclient.
49
50 It also returns the parsed dhcp.leases file generated in the sandbox.
51@@ -134,6 +136,10 @@ class TestDHCPDiscoveryClean(CiTestCase):
52 """)
53 lease_file = os.path.join(tmpdir, 'dhcp.leases')
54 write_file(lease_file, lease_content)
55+ pid_file = os.path.join(tmpdir, 'dhclient.pid')
56+ my_pid = 1
57+ write_file(pid_file, "%d\n" % my_pid)
58+
59 self.assertItemsEqual(
60 [{'interface': 'eth9', 'fixed-address': '192.168.2.74',
61 'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1'}],
62@@ -149,6 +155,7 @@ class TestDHCPDiscoveryClean(CiTestCase):
63 [os.path.join(tmpdir, 'dhclient'), '-1', '-v', '-lf',
64 lease_file, '-pf', os.path.join(tmpdir, 'dhclient.pid'),
65 'eth9', '-sf', '/bin/true'], capture=True)])
66+ m_kill.assert_has_calls([mock.call(my_pid, signal.SIGKILL)])
67
68
69 class TestSystemdParseLeases(CiTestCase):

Subscribers

People subscribed via source and target branches