Skip to content

Commit

Permalink
Merge pull request #1131 from rccrdpccl/ignore-ec2-if-not-present
Browse files Browse the repository at this point in the history
  • Loading branch information
jlebon authored Feb 19, 2025
2 parents e8bfb71 + a3a2a95 commit 29bbb39
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Major changes:
Minor changes:

- ProxmoxVE: Fixed instance boot without config drive
- OpenStack: Support config drive without EC2 metadata

Packaging changes:

Expand Down
42 changes: 35 additions & 7 deletions src/providers/openstack/configdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,19 @@ 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:?}'"))
.map(Some)
}

/// The metadata is stored as key:value pair in openstack/latest/meta_data.json file
Expand Down Expand Up @@ -144,17 +150,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 Expand Up @@ -254,4 +263,23 @@ mod tests {

assert_eq!(parsed.public_keys.unwrap_or_default(), expect);
}

#[test]
fn test_attributes_openstack_without_ec2() {
let provider = OpenstackConfigDrive {
drive_path: PathBuf::from(
"./tests/fixtures/openstack-config-drive/openstack-config-drive2",
),
temp_dir: None,
};
let result = provider.attributes();
assert!(result.is_ok(), "Expected Ok, got Err: {:?}", result.err());
let attributes = result.unwrap();
assert_eq!(
attributes
.get("OPENSTACK_INSTANCE_UUID")
.unwrap_or(&String::new()),
"b3f43d9c-9198-4cd4-ac9f-bed14960794b"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"uuid": "b3f43d9c-9198-4cd4-ac9f-bed14960794b"}

0 comments on commit 29bbb39

Please sign in to comment.