61 lines
1.9 KiB
Rust
61 lines
1.9 KiB
Rust
use crate::db::Instances;
|
|
use crate::entity::*;
|
|
use crate::llv6::*;
|
|
use crate::meta::*;
|
|
use crate::types::DefaultKey;
|
|
use rocket::serde::json::Json;
|
|
use rocket_db_pools::Connection;
|
|
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
|
|
|
/// standard cloud-init metadata
|
|
#[get("/meta-data")]
|
|
pub async fn metadata(
|
|
db: Connection<Instances>,
|
|
client_addr: ClientEUI64,
|
|
) -> MetaResponse<Json<CIMetadata>> {
|
|
match instance_info::Entity::find()
|
|
.filter(instance_info::Column::MacAddress.eq(client_addr.mac_address.to_string()))
|
|
.one(&*db)
|
|
.await
|
|
{
|
|
Ok(Some(v)) => Ok(Json(CIMetadata {
|
|
instance_id: format!("i-{:x}", v.id),
|
|
local_hostname: v.hostname,
|
|
public_keys: {
|
|
match v.ssh_keys {
|
|
Some(keys) => Some(keys),
|
|
// no keys for instance -> try to get from defaults
|
|
None => defaults::Entity::find_by_id(DefaultKey::SshKeys)
|
|
.one(&*db)
|
|
.await
|
|
.map_err(|_| MetaError::InternalServerError(()))?
|
|
.map(|x| x.value),
|
|
}
|
|
.map(|k| k.split('\n').map(|s| s.to_owned()).collect())
|
|
},
|
|
})),
|
|
_ => Err(MetaError::NotFound(())),
|
|
}
|
|
}
|
|
|
|
/// standard cloud-init userdata
|
|
#[get("/user-data")]
|
|
pub async fn userdata(
|
|
db: Connection<Instances>,
|
|
client_addr: ClientEUI64,
|
|
) -> MetaResponse<Vec<u8>> {
|
|
match instance_info::Entity::find()
|
|
.filter(instance_info::Column::MacAddress.eq(client_addr.mac_address.to_string()))
|
|
.one(&*db)
|
|
.await
|
|
.map_err(|_| MetaError::InternalServerError(()))?
|
|
{
|
|
Some(v) => v.user_data.map_or(Err(MetaError::NotFound(())), Ok),
|
|
None => Err(MetaError::NotFound(())),
|
|
}
|
|
}
|
|
|
|
pub fn all() -> Vec<rocket::Route> {
|
|
routes![metadata, userdata,]
|
|
}
|