-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from abehnia/lndeng-1550-create-cloud-init-me…
…ssage feat: add cloud-init message
- Loading branch information
Showing
7 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
import subprocess | ||
|
||
from landscape.client.monitor.plugin import DataWatcher | ||
|
||
|
||
class CloudInit(DataWatcher): | ||
|
||
message_type = "cloud-init" | ||
message_key = message_type | ||
scope = message_type | ||
persist_name = message_type | ||
run_immediately = True | ||
run_interval = 3600 * 24 # 24h | ||
|
||
def get_data(self) -> str: | ||
return json.dumps(get_cloud_init(), sort_keys=True) | ||
|
||
def register(self, monitor): | ||
super().register(monitor) | ||
self.call_on_accepted("cloud-init", self.exchange, True) | ||
|
||
|
||
def get_cloud_init(): | ||
""" | ||
cloud-init returns all the information the instance has been initialized | ||
with, in JSON format. This function takes the the output and parses it | ||
into a python dictionary and sticks it in "output" along with error and | ||
return code information. | ||
""" | ||
|
||
data = {} | ||
output = {} | ||
|
||
try: | ||
completed_process = subprocess.run( | ||
["cloud-init", "query", "-a"], | ||
encoding="utf8", | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
except FileNotFoundError as exc: | ||
data["return_code"] = -1 | ||
data["error"] = str(exc) | ||
data["output"] = output | ||
except Exception as exc: | ||
data["return_code"] = -2 | ||
data["error"] = str(exc) | ||
data["output"] = output | ||
else: | ||
string_output = completed_process.stdout.strip() | ||
try: | ||
# INFO: We don't want to parse an empty string. | ||
if string_output: | ||
json_output = json.loads(string_output) | ||
# INFO: Only return relevant information from cloud init. | ||
output["availability_zone"] = json_output.get( | ||
"availability_zone", | ||
"", | ||
) or json_output.get("availability-zone", "") | ||
data["return_code"] = completed_process.returncode | ||
data["error"] = completed_process.stderr | ||
data["output"] = output | ||
except json.decoder.JSONDecodeError as exc: | ||
data["return_code"] = completed_process.returncode | ||
data["error"] = str(exc) | ||
data["output"] = output | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"CephUsage", | ||
"ComputerTags", | ||
"SnapServicesMonitor", | ||
"CloudInit", | ||
] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import json | ||
from unittest import mock | ||
|
||
from landscape.client.monitor.cloudinit import CloudInit | ||
from landscape.client.tests.helpers import LandscapeTest | ||
from landscape.client.tests.helpers import MonitorHelper | ||
|
||
|
||
def subprocess_cloud_init_mock(*args, **kwargs): | ||
"""Mock a cloud-init subprocess output.""" | ||
data = {"availability_zone": "us-east-1"} | ||
output = json.dumps(data) | ||
return mock.Mock(stdout=output, stderr="", returncode=0) | ||
|
||
|
||
class CloudInitTest(LandscapeTest): | ||
"""Cloud init plugin tests.""" | ||
|
||
helpers = [MonitorHelper] | ||
|
||
def setUp(self): | ||
super(CloudInitTest, self).setUp() | ||
self.mstore.set_accepted_types(["cloud-init"]) | ||
|
||
def test_cloud_init(self): | ||
"""Test calling cloud-init.""" | ||
plugin = CloudInit() | ||
|
||
with mock.patch("subprocess.run") as run_mock: | ||
run_mock.side_effect = subprocess_cloud_init_mock | ||
self.monitor.add(plugin) | ||
plugin.exchange() | ||
|
||
messages = self.mstore.get_pending_messages() | ||
self.assertTrue(len(messages) > 0) | ||
message = json.loads(messages[0]["cloud-init"]) | ||
self.assertEqual(message["output"]["availability_zone"], "us-east-1") | ||
self.assertEqual(message["return_code"], 0) | ||
self.assertFalse(message["error"]) | ||
|
||
def test_cloud_init_when_not_installed(self): | ||
"""Tests calling cloud-init when it is not installed.""" | ||
plugin = CloudInit() | ||
|
||
with mock.patch("subprocess.run") as run_mock: | ||
run_mock.side_effect = FileNotFoundError("Not found!") | ||
self.monitor.add(plugin) | ||
plugin.exchange() | ||
|
||
messages = self.mstore.get_pending_messages() | ||
message = json.loads(messages[0]["cloud-init"]) | ||
self.assertTrue(len(messages) > 0) | ||
self.assertTrue(message["error"]) | ||
self.assertEqual(message["return_code"], -1) | ||
self.assertEqual({}, message["output"]) | ||
|
||
def test_undefined_exception(self): | ||
"""Test calling cloud-init when a random exception occurs.""" | ||
plugin = CloudInit() | ||
|
||
with mock.patch("subprocess.run") as run_mock: | ||
run_mock.side_effect = ValueError("Not found!") | ||
self.monitor.add(plugin) | ||
plugin.exchange() | ||
|
||
messages = self.mstore.get_pending_messages() | ||
message = json.loads(messages[0]["cloud-init"]) | ||
self.assertTrue(len(messages) > 0) | ||
self.assertTrue(message["error"]) | ||
self.assertEqual(message["return_code"], -2) | ||
self.assertEqual({}, message["output"]) | ||
|
||
def test_json_parse_error(self): | ||
""" | ||
If a Json parsing error occurs, show the exception and unparsed data. | ||
""" | ||
plugin = CloudInit() | ||
|
||
with mock.patch("subprocess.run") as run_mock: | ||
run_mock.return_value = mock.Mock(stdout="'") | ||
run_mock.return_value.returncode = 0 | ||
self.monitor.add(plugin) | ||
plugin.exchange() | ||
|
||
messages = self.mstore.get_pending_messages() | ||
message = json.loads(messages[0]["cloud-init"]) | ||
self.assertTrue(len(messages) > 0) | ||
self.assertTrue(message["error"]) | ||
self.assertEqual({}, message["output"]) | ||
|
||
def test_empty_string(self): | ||
""" | ||
If cloud-init is disabled, stdout is an empty string. | ||
""" | ||
plugin = CloudInit() | ||
|
||
with mock.patch("subprocess.run") as run_mock: | ||
run_mock.return_value = mock.Mock(stdout="", stderr="Error") | ||
run_mock.return_value.returncode = 1 | ||
self.monitor.add(plugin) | ||
plugin.exchange() | ||
|
||
messages = self.mstore.get_pending_messages() | ||
message = json.loads(messages[0]["cloud-init"]) | ||
self.assertTrue(len(messages) > 0) | ||
self.assertTrue(message["error"]) | ||
self.assertEqual(message["return_code"], 1) | ||
self.assertEqual({}, message["output"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters