initialize database better

This commit is contained in:
Aiden McClelland
2021-08-31 11:53:49 -06:00
committed by Aiden McClelland
parent 6093518e46
commit e9db083004
3 changed files with 25 additions and 11 deletions

View File

@@ -5,8 +5,10 @@ use anyhow::anyhow;
use embassy::context::{EitherContext, RpcContext};
use embassy::db::model::Database;
use embassy::db::subscribe;
use embassy::hostname::{get_hostname, get_id};
use embassy::middleware::auth::auth;
use embassy::middleware::cors::cors;
use embassy::net::tor::os_key;
use embassy::status::{check_all, synchronize_all};
use embassy::util::daemon;
use embassy::{Error, ErrorKind, ResultExt};
@@ -32,7 +34,15 @@ async fn inner_main(cfg_path: Option<&str>) -> Result<(), Error> {
if !rpc_ctx.db.exists(&<JsonPointer>::default()).await? {
rpc_ctx
.db
.put(&<JsonPointer>::default(), &Database::init(), None)
.put(
&<JsonPointer>::default(),
&Database::init(
get_id().await?,
&get_hostname().await?,
&os_key(&mut rpc_ctx.secret_store.acquire().await?).await?,
),
None,
)
.await?;
}
let auth = auth(rpc_ctx.clone());

View File

@@ -7,6 +7,7 @@ use patch_db::{DbHandle, HasModel, Map, MapModel, OptionModel};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use torut::onion::TorSecretKeyV3;
use crate::config::spec::{PackagePointerSpecVariant, SystemPointerSpec};
use crate::install::progress::InstallProgress;
@@ -28,17 +29,16 @@ pub struct Database {
pub ui: Value,
}
impl Database {
pub fn init() -> Self {
pub fn init(id: String, hostname: &str, tor_key: &TorSecretKeyV3) -> Self {
// TODO
Database {
server_info: ServerInfo {
id: "c3ad21d8".to_owned(),
id,
version: emver::Version::new(0, 3, 0, 0).into(),
lan_address: "https://start9-c3ad21d8.local".parse().unwrap(),
tor_address:
"http://privacy34kn4ez3y3nijweec6w4g54i3g54sdv7r5mr6soma3w4begyd.onion"
.parse()
.unwrap(),
lan_address: format!("https://{}.local", hostname).parse().unwrap(),
tor_address: format!("http://{}", tor_key.public().get_onion_address())
.parse()
.unwrap(),
status: ServerStatus::Running,
eos_marketplace: "https://beta-registry-0-3.start9labs.com".parse().unwrap(),
package_marketplace: None,

View File

@@ -26,12 +26,16 @@ pub async fn get_product_key() -> Result<String, Error> {
Ok(out.trim().to_owned())
}
// cat /boot/product_key.txt | shasum -a 256 | head -c 8 | awk '{print "start9-"$1}' | xargs hostnamectl set-hostname
pub async fn sync_hostname() -> Result<(), Error> {
pub async fn get_id() -> Result<String, Error> {
let key = get_product_key().await?;
let mut hasher = sha2::Sha256::new();
hasher.update(key.as_bytes());
let res = hasher.finalize();
set_hostname(&format!("start9-{}", hex::encode(&res[0..4]))).await?;
Ok(hex::encode(&res[0..4]))
}
// cat /boot/product_key.txt | shasum -a 256 | head -c 8 | awk '{print "start9-"$1}' | xargs hostnamectl set-hostname
pub async fn sync_hostname() -> Result<(), Error> {
set_hostname(&format!("start9-{}", get_id().await?)).await?;
Ok(())
}