Merge ~chad.smith/cloud-init:fix-instance-identity-upgrade-path into cloud-init:master

Proposed by Chad Smith
Status: Merged
Approved by: Scott Moser
Approved revision: e0c764b2e05d8e02c78a043c25e6fe9077049dcc
Merged at revision: e0c764b2e05d8e02c78a043c25e6fe9077049dcc
Proposed branch: ~chad.smith/cloud-init:fix-instance-identity-upgrade-path
Merge into: cloud-init:master
Diff against target: 108 lines (+64/-1)
2 files modified
cloudinit/sources/DataSourceEc2.py (+6/-0)
tests/unittests/test_datasource/test_ec2.py (+58/-1)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Needs Fixing
cloud-init Commiters Pending
Review via email: mp+337475@code.launchpad.net

Description of the change

ec2: fix upgrade path to initialize datasource.identity when no present in cache

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:eea2bd8b2e6273371151f1fe77096daaa294db80
https://jenkins.ubuntu.com/server/job/cloud-init-ci/764/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

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

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

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

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

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

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

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

review: Needs Fixing (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

typo in comment

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/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
2index e14553b..21e9ef8 100644
3--- a/cloudinit/sources/DataSourceEc2.py
4+++ b/cloudinit/sources/DataSourceEc2.py
5@@ -147,6 +147,12 @@ class DataSourceEc2(sources.DataSource):
6 def get_instance_id(self):
7 if self.cloud_platform == Platforms.AWS:
8 # Prefer the ID from the instance identity document, but fall back
9+ if not getattr(self, 'identity', None):
10+ # If re-using cached datasource, it's get_data run didn't
11+ # setup self.identity. So we need to do that now.
12+ api_version = self.get_metadata_api_version()
13+ self.identity = ec2.get_instance_identity(
14+ api_version, self.metadata_address).get('document', {})
15 return self.identity.get(
16 'instanceId', self.metadata['instance-id'])
17 else:
18diff --git a/tests/unittests/test_datasource/test_ec2.py b/tests/unittests/test_datasource/test_ec2.py
19index 0f7267b..8fcf60b 100644
20--- a/tests/unittests/test_datasource/test_ec2.py
21+++ b/tests/unittests/test_datasource/test_ec2.py
22@@ -2,6 +2,7 @@
23
24 import copy
25 import httpretty
26+import json
27 import mock
28
29 from cloudinit import helpers
30@@ -9,6 +10,29 @@ from cloudinit.sources import DataSourceEc2 as ec2
31 from cloudinit.tests import helpers as test_helpers
32
33
34+DYNAMIC_METADATA = {
35+ "instance-identity": {
36+ "document": json.dumps({
37+ "devpayProductCodes": None,
38+ "marketplaceProductCodes": ["1abc2defghijklm3nopqrs4tu"],
39+ "availabilityZone": "us-west-2b",
40+ "privateIp": "10.158.112.84",
41+ "version": "2017-09-30",
42+ "instanceId": "my-identity-id",
43+ "billingProducts": None,
44+ "instanceType": "t2.micro",
45+ "accountId": "123456789012",
46+ "imageId": "ami-5fb8c835",
47+ "pendingTime": "2016-11-19T16:32:11Z",
48+ "architecture": "x86_64",
49+ "kernelId": None,
50+ "ramdiskId": None,
51+ "region": "us-west-2"
52+ })
53+ }
54+}
55+
56+
57 # collected from api version 2016-09-02/ with
58 # python3 -c 'import json
59 # from cloudinit.ec2_utils import get_instance_metadata as gm
60@@ -85,7 +109,7 @@ DEFAULT_METADATA = {
61 "public-keys": {"brickies": ["ssh-rsa AAAAB3Nz....w== brickies"]},
62 "reservation-id": "r-01efbc9996bac1bd6",
63 "security-groups": "my-wide-open",
64- "services": {"domain": "amazonaws.com", "partition": "aws"}
65+ "services": {"domain": "amazonaws.com", "partition": "aws"},
66 }
67
68
69@@ -341,6 +365,39 @@ class TestEc2(test_helpers.HttprettyTestCase):
70 self.assertEqual(expected, ds.network_config)
71
72 @httpretty.activate
73+ def test_ec2_get_instance_id_refreshes_identity_on_upgrade(self):
74+ """get_instance-id gets DataSourceEc2Local.identity if not present.
75+
76+ This handles an upgrade case where the old pickled datasource didn't
77+ set up self.identity, but 'systemctl cloud-init init' runs
78+ get_instance_id which traces on missing self.identity. lp:1748354.
79+ """
80+ self.datasource = ec2.DataSourceEc2Local
81+ ds = self._setup_ds(
82+ platform_data=self.valid_platform_data,
83+ sys_cfg={'datasource': {'Ec2': {'strict_id': False}}},
84+ md=DEFAULT_METADATA)
85+ # Mock 404s on all versions except latest
86+ all_versions = (
87+ [ds.min_metadata_version] + ds.extended_metadata_versions)
88+ for ver in all_versions[:-1]:
89+ register_mock_metaserver(
90+ 'http://169.254.169.254/{0}/meta-data/instance-id'.format(ver),
91+ None)
92+ ds.metadata_address = 'http://169.254.169.254'
93+ register_mock_metaserver(
94+ '{0}/{1}/meta-data/'.format(ds.metadata_address, all_versions[-1]),
95+ DEFAULT_METADATA)
96+ # Register dynamic/instance-identity document which we now read.
97+ register_mock_metaserver(
98+ '{0}/{1}/dynamic/'.format(ds.metadata_address, all_versions[-1]),
99+ DYNAMIC_METADATA)
100+ ds._cloud_platform = ec2.Platforms.AWS
101+ # Setup cached metadata on the Datasource
102+ ds.metadata = DEFAULT_METADATA
103+ self.assertEqual('my-identity-id', ds.get_instance_id())
104+
105+ @httpretty.activate
106 @mock.patch('cloudinit.net.dhcp.maybe_perform_dhcp_discovery')
107 def test_valid_platform_with_strict_true(self, m_dhcp):
108 """Valid platform data should return true with strict_id true."""

Subscribers

People subscribed via source and target branches