From ebf930cf6d713cbe140aed5e69ba606fbb2cf04d Mon Sep 17 00:00:00 2001 From: Riccardo Piccoli Date: Thu, 14 Nov 2024 11:38:26 +0100 Subject: [PATCH] openstack provider: ignore ec2 metadata if not present Signed-off-by: Riccardo Piccoli --- docs/release-notes.md | 2 ++ src/providers/openstack/configdrive.rs | 34 +++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 348a7860..6914b62c 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -10,6 +10,8 @@ Major changes: Minor changes: +- Openstack: do not fail if ec2 metadata is not found + Packaging changes: ## Afterburn 5.7.0 diff --git a/src/providers/openstack/configdrive.rs b/src/providers/openstack/configdrive.rs index 3a633b91..2f7a8555 100644 --- a/src/providers/openstack/configdrive.rs +++ b/src/providers/openstack/configdrive.rs @@ -101,20 +101,27 @@ impl OpenstackConfigDrive { } /// The metadata is stored as key:value pair in ec2/latest/meta-data.json file - fn read_metadata_ec2(&self) -> Result { + fn read_metadata_ec2(&self) -> Result> { + use std::io::ErrorKind::NotFound; + let filename = self.metadata_dir("ec2").join("meta-data.json"); - let file = - File::open(&filename).with_context(|| format!("failed to open file '{filename:?}'"))?; + let file = match File::open(&filename) { + Ok(file) => file, + Err(e) if e.kind() == NotFound => return Ok(None), + Err(e) => return Err(anyhow::Error::new(e).context(format!("failed to open file '{filename:?}'"))), + }; let bufrd = BufReader::new(file); - Self::parse_metadata_ec2(bufrd) - .with_context(|| format!("failed to parse file '{filename:?}'")) + match Self::parse_metadata_ec2(bufrd) + .with_context(|| format!("failed to parse file '{filename:?}'")) { + Ok(metadata) => Ok(Some(metadata)), + Err(e) => Err(e), + } } /// The metadata is stored as key:value pair in openstack/latest/meta_data.json file fn read_metadata_openstack(&self) -> Result { let filename = self.metadata_dir("openstack").join("meta_data.json"); - let file = - File::open(&filename).with_context(|| format!("failed to open file '{filename:?}'"))?; + let file = File::open(&filename).with_context(|| format!("failed to open file '{filename:?}'"))?; let bufrd = BufReader::new(file); Self::parse_metadata_openstack(bufrd) .with_context(|| format!("failed to parse file '{filename:?}'")) @@ -144,17 +151,22 @@ impl OpenstackConfigDrive { impl MetadataProvider for OpenstackConfigDrive { fn attributes(&self) -> Result> { let mut out = HashMap::with_capacity(6); - let metadata_ec2: MetadataEc2JSON = self.read_metadata_ec2()?; let metadata_openstack: MetadataOpenstackJSON = self.read_metadata_openstack()?; if let Some(hostname) = metadata_openstack.hostname { out.insert("OPENSTACK_HOSTNAME".to_string(), hostname); } - if let Some(instance_id) = metadata_ec2.instance_id { - out.insert("OPENSTACK_INSTANCE_ID".to_string(), instance_id); - } if let Some(uuid) = metadata_openstack.uuid { out.insert("OPENSTACK_INSTANCE_UUID".to_string(), uuid); } + + let metadata_ec2 = match self.read_metadata_ec2() { + Ok(Some(metadata)) => metadata, + Ok(None) => return Ok(out), + Err(e) => return Err(e), + }; + if let Some(instance_id) = metadata_ec2.instance_id { + out.insert("OPENSTACK_INSTANCE_ID".to_string(), instance_id); + } if let Some(instance_type) = metadata_ec2.instance_type { out.insert("OPENSTACK_INSTANCE_TYPE".to_string(), instance_type); }