Skip to content

Commit

Permalink
openstack provider: ignore ec2 metadata if not present
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Piccoli <[email protected]>
  • Loading branch information
rccrdpccl committed Nov 18, 2024
1 parent de95d78 commit 5158090
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Major changes:

Minor changes:

- OpenStack: do not fail if ec2 metadata is not found

Packaging changes:

## Afterburn 5.7.0
Expand Down
27 changes: 17 additions & 10 deletions src/providers/openstack/configdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,24 @@ impl OpenstackConfigDrive {
}

/// The metadata is stored as key:value pair in ec2/latest/meta-data.json file
fn read_metadata_ec2(&self) -> Result<MetadataEc2JSON> {
fn read_metadata_ec2(&self) -> Result<Option<MetadataEc2JSON>> {
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(e).with_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:?}'"))
.with_context(|| format!("failed to parse file '{filename:?}'")).map(|metadata| Some(metadata))
}

/// The metadata is stored as key:value pair in openstack/latest/meta_data.json file
fn read_metadata_openstack(&self) -> Result<MetadataOpenstackJSON> {
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:?}'"))
Expand Down Expand Up @@ -144,17 +148,20 @@ impl OpenstackConfigDrive {
impl MetadataProvider for OpenstackConfigDrive {
fn attributes(&self) -> Result<HashMap<String, String>> {
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 Some(metadata_ec2) = self.read_metadata_ec2()? else {
return Ok(out);
};
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);
}
Expand Down

0 comments on commit 5158090

Please sign in to comment.