-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openstack: use config-drive by default
This changes afterburn to use config-drive as default for scraping metadata and then try metadata API if config-drive is not available. Previously supported platform id 'openstack-metadata' will continue to be supported. Closes: coreos/fedora-coreos-tracker#422 Signed-off-by: Allen Bai <abai@redhat.com>
- Loading branch information
Allen Bai
committed
Jul 29, 2020
1 parent
ff48266
commit 8b7276e
Showing
8 changed files
with
293 additions
and
11 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
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,209 @@ | ||
//! configdrive metadata fetcher for OpenStack | ||
//! reference: https://docs.openstack.org/nova/latest/user/metadata.html | ||
|
||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::{BufReader, Read}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use openssh_keys::PublicKey; | ||
use slog_scope::{error, warn}; | ||
use tempfile::TempDir; | ||
|
||
use crate::errors::*; | ||
use crate::network; | ||
use crate::providers::MetadataProvider; | ||
|
||
const CONFIG_DRIVE_LABEL: &str = "config-2"; | ||
|
||
/// Partial object for ec2 `meta_data.json` | ||
#[derive(Debug, Deserialize)] | ||
pub struct MetadataEc2JSON { | ||
/// Local hostname. | ||
pub hostname: Option<String>, | ||
/// Instance ID. | ||
#[serde(rename = "instance-id")] | ||
pub instance_id: Option<String>, | ||
/// Instance type. | ||
#[serde(rename = "instance-type")] | ||
pub instance_type: Option<String>, | ||
/// Local IPV4. | ||
#[serde(rename = "local-ipv4")] | ||
pub local_ipv4: Option<String>, | ||
/// Public IPV4. | ||
#[serde(rename = "public-ipv4")] | ||
pub public_ipv4: Option<String>, | ||
} | ||
|
||
/// Partial object for openstack `meta_data.json` | ||
#[derive(Debug, Deserialize)] | ||
pub struct MetadataOpenstackJSON { | ||
/// SSH public keys. | ||
pub public_keys: Option<HashMap<String, String>>, | ||
} | ||
|
||
/// OpenStack config-drive. | ||
#[derive(Debug)] | ||
pub struct OpenstackConfigDrive { | ||
/// Path to the top directory of the mounted config-drive. | ||
drive_path: PathBuf, | ||
/// Temporary directory for own mountpoint (if any). | ||
temp_dir: Option<TempDir>, | ||
} | ||
|
||
impl OpenstackConfigDrive { | ||
/// Try to build a new provider client. | ||
/// | ||
/// This internally tries to mount (and own) the config-drive. | ||
pub fn try_new() -> Result<Self> { | ||
const TARGET_FS: &str = "iso9660"; | ||
let target = tempfile::Builder::new() | ||
.prefix("afterburn-") | ||
.tempdir() | ||
.chain_err(|| "failed to create temporary directory")?; | ||
crate::util::mount_ro( | ||
&Path::new("/dev/disk/by-label/").join(CONFIG_DRIVE_LABEL), | ||
target.path(), | ||
TARGET_FS, | ||
3, | ||
)?; | ||
|
||
let cd = OpenstackConfigDrive { | ||
drive_path: target.path().to_owned(), | ||
temp_dir: Some(target), | ||
}; | ||
Ok(cd) | ||
} | ||
|
||
/// Return the path to the metadata directory. | ||
fn metadata_dir(&self, platform: &str) -> PathBuf { | ||
self.drive_path.clone().join(platform).join("latest") | ||
} | ||
|
||
/// Parse metadata attributes | ||
/// | ||
/// Metadata file contains a JSON object, corresponding to `MetadataEc2JSON`. | ||
fn parse_metadata_ec2<T: Read>(input: BufReader<T>) -> Result<MetadataEc2JSON> { | ||
serde_json::from_reader(input).chain_err(|| "failed parse JSON metadata") | ||
} | ||
|
||
/// Parse metadata attributes | ||
/// | ||
/// Metadata file contains a JSON object, corresponding to `MetadataOpenstackJSON`. | ||
fn parse_metadata_openstack<T: Read>(input: BufReader<T>) -> Result<MetadataOpenstackJSON> { | ||
serde_json::from_reader(input).chain_err(|| "failed parse JSON metadata") | ||
} | ||
|
||
/// The metadata is stored as key:value pair in ec2/latest/meta-data.json file | ||
fn read_metadata(&self) -> Result<MetadataEc2JSON> { | ||
let filename = self.metadata_dir("ec2").join("meta-data.json"); | ||
let file = | ||
File::open(&filename).chain_err(|| format!("failed to open file '{:?}'", filename))?; | ||
let bufrd = BufReader::new(file); | ||
Self::parse_metadata_ec2(bufrd) | ||
.chain_err(|| format!("failed to parse file '{:?}'", filename)) | ||
} | ||
|
||
/// The public key is stored as key:value pair in openstack/latest/meta_data.json file | ||
fn fetch_publickeys(&self) -> Result<Vec<PublicKey>> { | ||
let filename = self.metadata_dir("openstack").join("meta_data.json"); | ||
let file = | ||
File::open(&filename).chain_err(|| format!("failed to open file '{:?}'", filename))?; | ||
|
||
let bufrd = BufReader::new(file); | ||
let metadata: MetadataOpenstackJSON = Self::parse_metadata_openstack(bufrd) | ||
.chain_err(|| format!("failed to parse file '{:?}'", filename))?; | ||
|
||
let public_keys_map = metadata.public_keys.unwrap_or_default(); | ||
let public_keys_vec: Vec<&std::string::String> = public_keys_map.values().collect(); | ||
let mut out = vec![]; | ||
for key in public_keys_vec { | ||
let key = PublicKey::parse(key)?; | ||
out.push(key); | ||
} | ||
Ok(out) | ||
} | ||
} | ||
|
||
impl MetadataProvider for OpenstackConfigDrive { | ||
fn attributes(&self) -> Result<HashMap<String, String>> { | ||
let metadata: MetadataEc2JSON = self.read_metadata()?; | ||
Ok(maplit::hashmap! { | ||
"OPENSTACK_HOSTNAME".to_string() => metadata.hostname.unwrap_or_default(), | ||
"OPENSTACK_INSTANCE_ID".to_string() => metadata.instance_id.unwrap_or_default(), | ||
"OPENSTACK_INSTANCE_TYPE".to_string() => metadata.instance_type.unwrap_or_default(), | ||
"OPENSTACK_IPV4_LOCAL".to_string() => metadata.local_ipv4.unwrap_or_default(), | ||
"OPENSTACK_IPV4_PUBLIC".to_string() => metadata.public_ipv4.unwrap_or_default(), | ||
}) | ||
} | ||
|
||
fn hostname(&self) -> Result<Option<String>> { | ||
let metadata: MetadataEc2JSON = self.read_metadata()?; | ||
Ok(metadata.hostname) | ||
} | ||
|
||
fn ssh_keys(&self) -> Result<Vec<PublicKey>> { | ||
self.fetch_publickeys() | ||
} | ||
|
||
fn networks(&self) -> Result<Vec<network::Interface>> { | ||
Ok(vec![]) | ||
} | ||
|
||
fn virtual_network_devices(&self) -> Result<Vec<network::VirtualNetDev>> { | ||
warn!("virtual network devices metadata requested, but not supported on this platform"); | ||
Ok(vec![]) | ||
} | ||
|
||
fn boot_checkin(&self) -> Result<()> { | ||
warn!("boot check-in requested, but not supported on this platform"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for OpenstackConfigDrive { | ||
fn drop(&mut self) { | ||
if self.temp_dir.is_some() { | ||
if let Err(e) = crate::util::unmount(&self.drive_path, 3) { | ||
error!("failed to cleanup OpenStack config-drive: {:?}", e); | ||
}; | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_attributes() { | ||
let fixture = | ||
File::open("./tests/fixtures/openstack-config-drive/ec2/meta_data.json").unwrap(); | ||
let bufrd = BufReader::new(fixture); | ||
let parsed = OpenstackConfigDrive::parse_metadata_ec2(bufrd).unwrap(); | ||
|
||
assert_eq!( | ||
parsed.hostname.unwrap_or_default(), | ||
"abai-fcos-afterburn-test" | ||
); | ||
assert_eq!(parsed.instance_id.unwrap_or_default(), "i-022da7a2"); | ||
assert_eq!(parsed.instance_type.unwrap_or_default(), "m1.small"); | ||
assert_eq!(parsed.local_ipv4.unwrap_or_default(), "10.0.151.35"); | ||
assert_eq!(parsed.public_ipv4.unwrap_or_default(), ""); | ||
} | ||
|
||
#[test] | ||
fn test_ssh_keys() { | ||
let fixture = | ||
File::open("./tests/fixtures/openstack-config-drive/openstack/meta_data.json").unwrap(); | ||
let bufrd = BufReader::new(fixture); | ||
let parsed = OpenstackConfigDrive::parse_metadata_openstack(bufrd).unwrap(); | ||
|
||
let expect = maplit::hashmap! { | ||
"mykey".to_string() => "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDYVEprvtYJXVOBN0XNKVVRNCRX6BlnNbI+USLGais1sUWPwtSg7z9K9vhbYAPUZcq8c/s5S9dg5vTHbsiyPCIDOKyeHba4MUJq8Oh5b2i71/3BISpyxTBH/uZDHdslW2a+SrPDCeuMMoss9NFhBdKtDkdG9zyi0ibmCP6yMdEX8Q== Generated by Nova\n".to_string(), | ||
}; | ||
|
||
assert_eq!(parsed.public_keys.unwrap_or_default(), expect); | ||
} | ||
} |
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,28 @@ | ||
{ | ||
"reservation-id": "r-bxkh1822", | ||
"security-groups": [ | ||
"wide-open", | ||
"default", | ||
"wide-open", | ||
"default" | ||
], | ||
"public-ipv4": "", | ||
"ami-manifest-path": "FIXME", | ||
"instance-type": "m1.small", | ||
"instance-id": "i-022da7a2", | ||
"local-ipv4": "10.0.151.35", | ||
"local-hostname": "abai-fcos-afterburn-test", | ||
"placement": { | ||
"availability-zone": "nova" | ||
}, | ||
"ami-launch-index": 0, | ||
"public-hostname": "abai-fcos-afterburn-test", | ||
"hostname": "abai-fcos-afterburn-test", | ||
"ami-id": null, | ||
"instance-action": "none", | ||
"block-device-mapping": { | ||
"ami": "vda", | ||
"ebs0": "/dev/vda", | ||
"root": "/dev/vda" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/fixtures/openstack-config-drive/openstack/meta_data.json
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,14 @@ | ||
{ | ||
"admin_pass": "Qs634jUssypC", | ||
"random_seed": "stYL6s4C4tTyttfG9s7QzngiR/2X7X1fGkWl7Ok/bfl5dCevFBDYh5BvGnzd4/vNYWAjtDpe4/5mTogmF0uew4eMD4p96mLtQYjj9JaQRd18xvyULk1jJOBr+G3BVtyOSqDuBwJ6e6fsH2bmbhW6RSSq80t1tqo/qrhJP8i/jrH2DS7iQ8RvmZjbYJ1zSxyV2axNSWwie //WQXbB3sffb3+sDYhoAc+wUjGLK7MuPhNlaJC193Hvuqd7CVTYWuh4lPEjTe7QB4oGBNOHkfF4xixiKXns6UBtFwOJh4xiHKOL2Y4hz9gGDO1N0XprxZManAXc/BzW8AoTMWvsMzYgG6ZxF2kc60OP5x6y8naBV1GxAmJvURBjxZ245pHAmJ4ruduy0XKnvZQO7EIaE7xKxm8Va4f7rKSvvhUFXNURAlP3RrF1Nu4XRirXST0SnoYrwwljCJNG2ywa1VP46GgN/Zj6brUzVjXN8YiMYObgkcnkgNWTBmYkn2zniENVaWSpCpr+RDC8iEkZIAVZA9/uBHHTbsfmzjzvdWtE6woRkRCC9lwTsuwAmmbrJa3bdACQ+kACVy7VSx7RdUyoYQw0k5bWsZchZzxCp2KnDn60Qi1OL5oej0Wn9ZUczHy+P2vDl1GNhqvPFK513Oa+tqmw8NnJNJftgg9fCmgMbZjWSlA=", | ||
"uuid": "b3c7f4de-da0b-44ae-a42c-fa3806b61d7f", | ||
"availability_zone": "nova", | ||
"hostname": "abai-fcos-afterburn-test", | ||
"launch_index": 0, | ||
"devices": [], | ||
"project_id": "8aa32be0972144a99ce5001d0f07905f", | ||
"name": "abai-fcos-afterburn-test", | ||
"public_keys": { | ||
"mykey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDYVEprvtYJXVOBN0XNKVVRNCRX6BlnNbI+USLGais1sUWPwtSg7z9K9vhbYAPUZcq8c/s5S9dg5vTHbsiyPCIDOKyeHba4MUJq8Oh5b2i71/3BISpyxTBH/uZDHdslW2a+SrPDCeuMMoss9NFhBdKtDkdG9zyi0ibmCP6yMdEX8Q== Generated by Nova\n" | ||
} | ||
} |