From 6a803e2fba17ab74973038c1a93654c3127c223f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sun, 26 Aug 2012 15:04:06 -0700 Subject: Add the capability to understand and filter on userdata based on a launch-index (or leave userdata alone if none is provided by the datasource). This works by doing the following. 1. Adjusting the userdata processor to attempt to inject a "Launch-Index" header into the messages headers (by either taking a header that already exists or by looking into the payload to see if it exists there). 2. Adjust the get_userdata ds function to apply a filter on the returned userdata (defaulting to false) that will now use the datasources get_launch_index value to restrict the 'final' message used in consuming user data (the same behavior if not existent). 3. Further down the line processes that use the 'resultant' userdata now will only see the ones for there own launch index (ie cloud-config will be restricted automatically and so on) and are unaffected (although they can now ask the cloud object or the datasource for its launch index via the above new ds method. --- cloudinit/sources/DataSourceEc2.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'cloudinit/sources/DataSourceEc2.py') diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py index 556dcafb..3e450e7e 100644 --- a/cloudinit/sources/DataSourceEc2.py +++ b/cloudinit/sources/DataSourceEc2.py @@ -77,6 +77,9 @@ class DataSourceEc2(sources.DataSource): self.metadata_address) return False + def get_launch_index(self): + return self.metadata.get('ami-launch-index') + def get_instance_id(self): return self.metadata['instance-id'] -- cgit v1.2.3 From 92b99b325b2d437825cc87253e76c756a136ff28 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 27 Aug 2012 10:56:47 -0700 Subject: Update so that the content types searched for launch-index variable has a little more meaning and by default look in metadata for 'launch-index' and have ec2 instead look for a different variable (thus allowing more datasources to just work). --- cloudinit/cloud.py | 5 +++-- cloudinit/sources/DataSourceEc2.py | 5 ++++- cloudinit/sources/__init__.py | 9 +++++++-- cloudinit/user_data.py | 5 +++-- 4 files changed, 17 insertions(+), 7 deletions(-) (limited to 'cloudinit/sources/DataSourceEc2.py') diff --git a/cloudinit/cloud.py b/cloudinit/cloud.py index af69a541..95e0cfb2 100644 --- a/cloudinit/cloud.py +++ b/cloudinit/cloud.py @@ -76,8 +76,9 @@ class Cloud(object): def get_instance_id(self): return self.datasource.get_instance_id() - def get_launch_index(self): - return self.datasource.get_launch_index() + @property + def launch_index(self): + return self.datasource.launch_index def get_public_ssh_keys(self): return self.datasource.get_public_ssh_keys() diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py index 3e450e7e..2a9a70f7 100644 --- a/cloudinit/sources/DataSourceEc2.py +++ b/cloudinit/sources/DataSourceEc2.py @@ -77,7 +77,10 @@ class DataSourceEc2(sources.DataSource): self.metadata_address) return False - def get_launch_index(self): + @property + def launch_index(self): + if not self.metadata: + return None return self.metadata.get('ami-launch-index') def get_instance_id(self): diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py index d49b67b2..74944e38 100644 --- a/cloudinit/sources/__init__.py +++ b/cloudinit/sources/__init__.py @@ -68,13 +68,18 @@ class DataSource(object): return self._filter_userdata(self.userdata) return self.userdata - def get_launch_index(self): + @property + def launch_index(self): + if not self.metadata: + return None + if 'launch-index' in self.metadata: + return self.metadata['launch-index'] return None def _filter_userdata(self, processed_ud): if not processed_ud: return processed_ud - idx = self.get_launch_index() + idx = self.launch_index if idx is None: return processed_ud # First do a scan to see if any one with launch-index diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py index 2b76482a..5d550e1d 100644 --- a/cloudinit/user_data.py +++ b/cloudinit/user_data.py @@ -53,7 +53,8 @@ ARCHIVE_UNDEF_TYPE = "text/cloud-config" ATTACHMENT_FIELD = 'Number-Attachments' # Only the following content types can have there launch index examined -CAN_HAVE_LAUNCH_INDEX = ["text/cloud-config", "text/cloud-config-archive"] +# in there payload, evey other content type can still provide a header +EXAMINE_FOR_LAUNCH_INDEX = ["text/cloud-config", "text/cloud-config-archive"] class UserDataProcessor(object): @@ -101,7 +102,7 @@ class UserDataProcessor(object): def _attach_launch_index(self, msg): header_idx = msg.get('Launch-Index', None) payload_idx = None - if msg.get_content_type() in CAN_HAVE_LAUNCH_INDEX: + if msg.get_content_type() in EXAMINE_FOR_LAUNCH_INDEX: try: # See if it has a launch-index field # that might affect the final header -- cgit v1.2.3