-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #982 from xinau/xinau/add-support-for-scaleway
providers: add support for scaleway
- Loading branch information
Showing
11 changed files
with
296 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ nav_order: 8 | |
|
||
Major changes: | ||
|
||
- Add support for Scaleway | ||
|
||
Minor changes: | ||
|
||
|
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
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,71 @@ | ||
use mockito::{self, Matcher}; | ||
|
||
use crate::providers::scaleway::ScalewayProvider; | ||
use crate::providers::MetadataProvider; | ||
|
||
#[test] | ||
fn test_attributes() { | ||
let metadata = r#"{ | ||
"commercial_type": "GP1-M", | ||
"hostname": "frontend-0", | ||
"id": "11111111-1111-1111-1111-111111111111", | ||
"ipv6": { | ||
"address": "2001:db8::1" | ||
}, | ||
"location": { | ||
"zone_id": "par1" | ||
}, | ||
"private_ip": "10.0.0.2", | ||
"public_ip": { | ||
"address": "93.184.216.34" | ||
}, | ||
"ssh_public_keys": [] | ||
}"#; | ||
|
||
let want = maplit::hashmap! { | ||
"SCALEWAY_INSTANCE_ID".to_string() => "11111111-1111-1111-1111-111111111111".to_string(), | ||
"SCALEWAY_INSTANCE_TYPE".to_string() => "GP1-M".to_string(), | ||
"SCALEWAY_HOSTNAME".to_string() => "frontend-0".to_string(), | ||
"SCALEWAY_IPV4_PRIVATE".to_string() => "10.0.0.2".to_string(), | ||
"SCALEWAY_IPV4_PUBLIC".to_string() => "93.184.216.34".to_string(), | ||
"SCALEWAY_IPV6_PUBLIC".to_string() => "2001:db8::1".to_string(), | ||
"SCALEWAY_ZONE_ID".to_string() => "par1".to_string(), | ||
}; | ||
|
||
let mut server = mockito::Server::new(); | ||
server | ||
.mock("GET", "/conf?format=json") | ||
.with_status(200) | ||
.with_body(metadata) | ||
.create(); | ||
|
||
let mut provider = ScalewayProvider::try_new().unwrap(); | ||
provider.client = provider.client.max_retries(0).mock_base_url(server.url()); | ||
let got = provider.attributes().unwrap(); | ||
|
||
assert_eq!(got, want); | ||
|
||
server.reset(); | ||
} | ||
|
||
#[test] | ||
fn test_boot_checkin() { | ||
let mut server = mockito::Server::new(); | ||
let mock = server | ||
.mock("PATCH", "/state") | ||
.match_header( | ||
"content-type", | ||
Matcher::Regex("application/json".to_string()), | ||
) | ||
.match_body(r#"{"state_detail":"booted"}"#) | ||
.with_status(200) | ||
.create(); | ||
|
||
let mut provider = ScalewayProvider::try_new().unwrap(); | ||
provider.client = provider.client.max_retries(0).mock_base_url(server.url()); | ||
|
||
provider.boot_checkin().unwrap(); | ||
mock.assert(); | ||
|
||
server.reset(); | ||
} |
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,162 @@ | ||
// Copyright 2023 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Metadata fetcher for Scaleway. | ||
//! | ||
//! The metadata API specification follows the instance one described | ||
//! [their docs](https://www.scaleway.com/en/developers/api/instance/#path-instances-get-an-instance) | ||
//! | ||
//! An implementation for the metadata retrival and boot check-in can be found | ||
//! in the image-tools | ||
//! [`scw-metadata-json`](https://github.com/scaleway/image-tools/blob/cloud-init-18.3%2B24.gf6249277/bases/overlay-common/usr/local/bin/scw-metadata-json) | ||
//! and | ||
//! [`scw-signal-state`](https://github.com/scaleway/image-tools/blob/cloud-init-18.3%2B24.gf6249277/bases/overlay-common/usr/local/sbin/scw-signal-state) | ||
|
||
use std::collections::HashMap; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use openssh_keys::PublicKey; | ||
use serde::Deserialize; | ||
|
||
use crate::providers::MetadataProvider; | ||
use crate::retry; | ||
|
||
#[cfg(test)] | ||
mod mock_tests; | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalewaySSHPublicKey { | ||
key: String, | ||
} | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalwayInterfaces { | ||
private_ip: Option<String>, | ||
public_ip: Option<ScalewayIPv4Public>, | ||
ipv6: Option<ScalewayIPv6Public>, | ||
} | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalewayIPv4Public { | ||
address: String, | ||
} | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalewayIPv6Public { | ||
address: String, | ||
} | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalewayLocation { | ||
zone_id: String, | ||
} | ||
|
||
#[derive(Clone, Deserialize)] | ||
struct ScalewayInstanceMetadata { | ||
commercial_type: String, | ||
hostname: String, | ||
id: String, | ||
#[serde(flatten)] | ||
interfaces: ScalwayInterfaces, | ||
location: ScalewayLocation, | ||
ssh_public_keys: Vec<ScalewaySSHPublicKey>, | ||
} | ||
|
||
pub struct ScalewayProvider { | ||
client: retry::Client, | ||
} | ||
|
||
impl ScalewayProvider { | ||
pub fn try_new() -> Result<ScalewayProvider> { | ||
let client = retry::Client::try_new()?; | ||
Ok(ScalewayProvider { client }) | ||
} | ||
|
||
fn fetch_metadata(&self) -> Result<ScalewayInstanceMetadata> { | ||
let data: ScalewayInstanceMetadata = self | ||
.client | ||
.get( | ||
retry::Json, | ||
"http://169.254.42.42/conf?format=json".to_string(), | ||
) | ||
.send()? | ||
.ok_or_else(|| anyhow!("not found"))?; | ||
|
||
Ok(data) | ||
} | ||
|
||
fn parse_attrs(&self) -> Result<Vec<(String, String)>> { | ||
let data = self.fetch_metadata()?; | ||
|
||
let instance_type = data.commercial_type; | ||
let zone_id = data.location.zone_id; | ||
|
||
let mut attrs = vec![ | ||
("SCALEWAY_HOSTNAME".to_string(), data.hostname.clone()), | ||
("SCALEWAY_INSTANCE_ID".to_string(), data.id.clone()), | ||
("SCALEWAY_INSTANCE_TYPE".to_string(), instance_type.clone()), | ||
("SCALEWAY_ZONE_ID".to_string(), zone_id.clone()), | ||
]; | ||
|
||
if let Some(ref ip) = data.interfaces.private_ip { | ||
attrs.push(("SCALEWAY_IPV4_PRIVATE".to_string(), ip.clone())); | ||
} | ||
|
||
if let Some(ref ip) = data.interfaces.public_ip { | ||
attrs.push(("SCALEWAY_IPV4_PUBLIC".to_string(), ip.address.clone())); | ||
} | ||
|
||
if let Some(ref ip) = data.interfaces.ipv6 { | ||
attrs.push(("SCALEWAY_IPV6_PUBLIC".to_string(), ip.address.clone())); | ||
} | ||
|
||
Ok(attrs) | ||
} | ||
} | ||
|
||
impl MetadataProvider for ScalewayProvider { | ||
fn attributes(&self) -> Result<HashMap<String, String>> { | ||
let attrs = self.parse_attrs()?; | ||
Ok(attrs.into_iter().collect()) | ||
} | ||
|
||
fn boot_checkin(&self) -> Result<()> { | ||
self.client | ||
.patch( | ||
retry::Json, | ||
"http://169.254.42.42/state".to_string(), | ||
Some(r#"{"state_detail":"booted"}"#.into()), | ||
) | ||
.dispatch_patch()?; | ||
Ok(()) | ||
} | ||
|
||
fn hostname(&self) -> Result<Option<String>> { | ||
let data = self.fetch_metadata()?; | ||
Ok(Some(data.hostname.clone())) | ||
} | ||
|
||
fn ssh_keys(&self) -> Result<Vec<PublicKey>> { | ||
let mut out = Vec::new(); | ||
|
||
let data = self.fetch_metadata()?; | ||
|
||
for key in data.ssh_public_keys { | ||
let key = PublicKey::parse(&key.key)?; | ||
out.push(key); | ||
} | ||
|
||
Ok(out) | ||
} | ||
} |
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