Merge ~smoser/cloud-init:bug/1674861-ds-identify-gce into cloud-init:master

Proposed by Scott Moser
Status: Merged
Merged at revision: 328fe5ab399b1f5b48d1985f41fc2ef66e368922
Proposed branch: ~smoser/cloud-init:bug/1674861-ds-identify-gce
Merge into: cloud-init:master
Diff against target: 113 lines (+44/-2)
3 files modified
cloudinit/sources/DataSourceGCE.py (+18/-0)
tests/unittests/test_datasource/test_gce.py (+13/-1)
tools/ds-identify (+13/-1)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
cloud-init Commiters Pending
Review via email: mp+321094@code.launchpad.net

Commit message

GCE: Search GCE in ds-identify, consider serial number in check.

While documentation indicates that the smbios product name should
contain 'Google Compute Engine', experimentation and bug reports
indicate that is not always the case. The change here is to change
the check for GCE to also consider a serial number that starts with
'GoogleCompute-'.

Also, ds-identify was not currently searching for GCE if no config of
datasource_list was found. Most images have a datasource_list defined.
So update the list to include GCE.

LP: #1674861

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

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
2index b1a1c8f..637c950 100644
3--- a/cloudinit/sources/DataSourceGCE.py
4+++ b/cloudinit/sources/DataSourceGCE.py
5@@ -62,6 +62,9 @@ class DataSourceGCE(sources.DataSource):
6 return public_key
7
8 def get_data(self):
9+ if not platform_reports_gce():
10+ return False
11+
12 # url_map: (our-key, path, required, is_text)
13 url_map = [
14 ('instance-id', ('instance/id',), True, True),
15@@ -144,6 +147,21 @@ class DataSourceGCE(sources.DataSource):
16 return self.availability_zone.rsplit('-', 1)[0]
17
18
19+def platform_reports_gce():
20+ pname = util.read_dmi_data('system-product-name') or "N/A"
21+ if pname == "Google Compute Engine":
22+ return True
23+
24+ # system-product-name is not always guaranteed (LP: #1674861)
25+ serial = util.read_dmi_data('system-serial-number') or "N/A"
26+ if serial.startswith("GoogleCloud-"):
27+ return True
28+
29+ LOG.debug("Not running on google cloud. product-name=%s serial=%s",
30+ pname, serial)
31+ return False
32+
33+
34 # Used to match classes to dependencies
35 datasources = [
36 (DataSourceGCE, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
37diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py
38index 4f83454..3eaa58e 100644
39--- a/tests/unittests/test_datasource/test_gce.py
40+++ b/tests/unittests/test_datasource/test_gce.py
41@@ -5,6 +5,7 @@
42 # This file is part of cloud-init. See LICENSE file for license information.
43
44 import httpretty
45+import mock
46 import re
47
48 from base64 import b64encode, b64decode
49@@ -71,6 +72,11 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase):
50 self.ds = DataSourceGCE.DataSourceGCE(
51 settings.CFG_BUILTIN, None,
52 helpers.Paths({}))
53+ self.m_platform_reports_gce = mock.patch(
54+ 'cloudinit.sources.DataSourceGCE.platform_reports_gce',
55+ return_value=True)
56+ self.m_platform_reports_gce.start()
57+ self.addCleanup(self.m_platform_reports_gce.stop)
58 super(TestDataSourceGCE, self).setUp()
59
60 def test_connection(self):
61@@ -153,7 +159,13 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase):
62
63 def test_only_last_part_of_zone_used_for_availability_zone(self):
64 _set_mock_metadata()
65- self.ds.get_data()
66+ r = self.ds.get_data()
67+ self.assertEqual(True, r)
68 self.assertEqual('bar', self.ds.availability_zone)
69
70+ def test_get_data_returns_false_if_not_on_gce(self):
71+ self.m_platform_reports_gce.return_value = False
72+ self.assertEqual(False, self.ds.get_data())
73+
74+
75 # vi: ts=4 expandtab
76diff --git a/tools/ds-identify b/tools/ds-identify
77index bf09a3a..15d6600 100755
78--- a/tools/ds-identify
79+++ b/tools/ds-identify
80@@ -108,7 +108,7 @@ DI_DSNAME=""
81 # this has to match the builtin list in cloud-init, it is what will
82 # be searched if there is no setting found in config.
83 DI_DSLIST_DEFAULT="MAAS ConfigDrive NoCloud AltCloud Azure Bigstep \
84-CloudSigma CloudStack DigitalOcean Ec2 OpenNebula OpenStack OVF SmartOS"
85+CloudSigma CloudStack DigitalOcean Ec2 GCE OpenNebula OpenStack OVF SmartOS"
86 DI_DSLIST=""
87 DI_MODE=""
88 DI_ON_FOUND=""
89@@ -383,6 +383,14 @@ dmi_product_name_matches() {
90 return 1
91 }
92
93+dmi_product_serial_matches() {
94+ is_container && return 1
95+ case "${DI_DMI_PRODUCT_SERIAL}" in
96+ $1) return 0;;
97+ esac
98+ return 1
99+}
100+
101 dmi_product_name_is() {
102 is_container && return 1
103 [ "${DI_DMI_PRODUCT_NAME}" = "$1" ]
104@@ -770,6 +778,10 @@ dscheck_GCE() {
105 if dmi_product_name_is "Google Compute Engine"; then
106 return ${DS_FOUND}
107 fi
108+ # product name is not guaranteed (LP: #1674861)
109+ if dmi_product_serial_matches "GoogleCloud-*"; then
110+ return ${DS_FOUND}
111+ fi
112 return ${DS_NOT_FOUND}
113 }
114

Subscribers

People subscribed via source and target branches