Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foundation of new syn2mas tool #3636

Open
wants to merge 10 commits into
base: rei/target_syn2mas
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mas-tasks = { path = "./crates/tasks/", version = "=0.12.0" }
mas-templates = { path = "./crates/templates/", version = "=0.12.0" }
mas-tower = { path = "./crates/tower/", version = "=0.12.0" }
oauth2-types = { path = "./crates/oauth2-types/", version = "=0.12.0" }
syn2mas = { path = "./crates/syn2mas", version = "=0.12.0" }

# OpenAPI schema generation and validation
[workspace.dependencies.aide]
Expand All @@ -66,6 +67,9 @@ features = ["axum", "axum-headers", "macros"]
version = "7.0.11"
features = ["chrono", "url", "tracing"]

[workspace.dependencies.async-stream]
version = "0.3.6"

# Utility to write and implement async traits
[workspace.dependencies.async-trait]
version = "0.1.83"
Expand All @@ -91,6 +95,10 @@ version = "1.9.0"
[workspace.dependencies.camino]
version = "1.1.9"

# Memory optimisation for short strings
[workspace.dependencies.compact_str]
version = "0.8.0"

# Time utilities
[workspace.dependencies.chrono]
version = "0.4.38"
Expand Down Expand Up @@ -309,11 +317,17 @@ features = [
[workspace.dependencies.thiserror]
version = "2.0.3"

[workspace.dependencies.thiserror-ext]
version = "0.2.0"

# Async runtime
[workspace.dependencies.tokio]
version = "1.41.1"
features = ["full"]

[workspace.dependencies.tokio-stream]
reivilibre marked this conversation as resolved.
Show resolved Hide resolved
version = "0.1.16"

# Useful async utilities
[workspace.dependencies.tokio-util]
version = "0.7.13"
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
doc-valid-idents = ["OpenID", "OAuth", "..", "PostgreSQL"]
doc-valid-idents = ["OpenID", "OAuth", "..", "PostgreSQL", "SQLite"]

disallowed-methods = [
{ path = "rand::thread_rng", reason = "do not create rngs on the fly, pass them as parameters" },
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ serde_yaml = "0.9.34"
sqlx.workspace = true
tokio.workspace = true
tokio-util.workspace = true
tokio-stream.workspace = true
tower.workspace = true
tower-http.workspace = true
url.workspace = true
Expand Down Expand Up @@ -89,6 +90,7 @@ mas-tasks.workspace = true
mas-templates.workspace = true
mas-tower.workspace = true
oauth2-types.workspace = true
syn2mas.workspace = true

[features]
# Features used for the prebuilt binaries
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod debug;
mod doctor;
mod manage;
mod server;
mod syn2mas;
mod templates;
mod worker;

Expand Down Expand Up @@ -48,6 +49,10 @@ enum Subcommand {

/// Run diagnostics on the deployment
Doctor(self::doctor::Options),

/// Migrate from Synapse's built-in auth system to MAS.
#[clap(name = "syn2mas")]
Syn2Mas(self::syn2mas::Options),
}

#[derive(Parser, Debug)]
Expand All @@ -74,6 +79,7 @@ impl Options {
Some(S::Templates(c)) => Box::pin(c.run(figment)).await,
Some(S::Debug(c)) => Box::pin(c.run(figment)).await,
Some(S::Doctor(c)) => Box::pin(c.run(figment)).await,
Some(S::Syn2Mas(c)) => Box::pin(c.run(figment)).await,
None => Box::pin(self::server::Options::default().run(figment)).await,
}
}
Expand Down
83 changes: 83 additions & 0 deletions crates/cli/src/commands/syn2mas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::process::ExitCode;

use anyhow::Context;
use clap::Parser;
use figment::Figment;
use mas_config::{ConfigurationSectionExt, DatabaseConfig};
use rand::thread_rng;
use sqlx::{Connection, Either, PgConnection};
use syn2mas::{LockedMasDatabase, MasWriter, SynapseReader};
use tracing::{error, warn};

use crate::util::database_connection_from_config;

#[derive(Parser, Debug)]
pub(super) struct Options {
#[command(subcommand)]
subcommand: Subcommand,

/// This version of the syn2mas tool is EXPERIMENTAL and INCOMPLETE. It is only suitable for TESTING.
/// If you want to use this tool anyway, please pass this argument.
///
/// If you want to migrate from Synapse to MAS today, please use the Node.js-based tool in the MAS repository.
#[clap(long = "i-swear-i-am-just-testing-in-a-staging-environment")]
sandhose marked this conversation as resolved.
Show resolved Hide resolved
experimental_accepted: bool,
}

#[derive(Parser, Debug)]
enum Subcommand {
Check,
Migrate,
}

/// The number of parallel writing transactions active against the MAS database.
const NUM_WRITER_CONNECTIONS: usize = 8;

impl Options {
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
sandhose marked this conversation as resolved.
Show resolved Hide resolved
warn!("This version of the syn2mas tool is EXPERIMENTAL and INCOMPLETE. Do not use it, except for TESTING.");
if !self.experimental_accepted {
error!("Please agree that you can only use this tool for testing.");
return Ok(ExitCode::FAILURE);
}

// TODO allow configuring the synapse database location
let mut syn_conn = PgConnection::connect("postgres:///fakesyn").await.unwrap();

let config = DatabaseConfig::extract_or_default(figment)?;

let mut mas_connection = database_connection_from_config(&config).await?;

let Either::Left(mut mas_connection) = LockedMasDatabase::try_new(&mut mas_connection)
.await
.context("failed to issue query to lock database")?
else {
error!("Failed to acquire syn2mas lock on the database.");
error!("This likely means that another syn2mas instance is already running!");
return Ok(ExitCode::FAILURE);
};

syn2mas::mas_pre_migration_checks(&mut mas_connection).await?;
syn2mas::synapse_pre_migration_checks(&mut syn_conn).await?;

let mut reader = SynapseReader::new(&mut syn_conn, true).await?;
let mut writer_mas_connections = Vec::with_capacity(NUM_WRITER_CONNECTIONS);
for _ in 0..NUM_WRITER_CONNECTIONS {
writer_mas_connections.push(database_connection_from_config(&config).await?);
}
let mut writer = MasWriter::new(mas_connection, writer_mas_connections).await?;

// TODO is this rng ok?
#[allow(clippy::disallowed_methods)]
let mut rng = thread_rng();

// TODO progress reporting
// TODO allow configuring the server name
syn2mas::migrate(&mut reader, &mut writer, "matrix.org", &mut rng).await?;

reader.finish().await?;
writer.finish().await?;

Ok(ExitCode::SUCCESS)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading