2024-05-22 08:41:30 +01:00
|
|
|
//
|
2024-07-11 18:54:44 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2024-05-05 18:08:05 +01:00
|
|
|
use actix::prelude::*;
|
2024-07-11 08:06:36 +01:00
|
|
|
use derive_more::Constructor;
|
2024-06-29 10:49:12 +01:00
|
|
|
use git_next_actor_macros::message;
|
|
|
|
use git_next_config as config;
|
2024-06-29 11:14:09 +01:00
|
|
|
use git_next_config::server::{ServerConfig, ServerStorage, Webhook};
|
|
|
|
use git_next_config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig};
|
2024-06-29 10:57:18 +01:00
|
|
|
use git_next_file_watcher_actor::FileUpdated;
|
2024-07-03 07:42:11 +01:00
|
|
|
use git_next_git::{Generation, RepoDetails};
|
2024-06-29 10:49:12 +01:00
|
|
|
use git_next_repo_actor::{messages::CloneRepo, RepoActor};
|
2024-06-29 08:20:56 +01:00
|
|
|
use git_next_webhook_actor as webhook;
|
2024-05-05 18:08:05 +01:00
|
|
|
use kxio::{fs::FileSystem, network::Network};
|
2024-07-11 18:54:44 +01:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
|
|
|
path::PathBuf,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2024-07-11 08:06:36 +01:00
|
|
|
use tracing::{error, info};
|
2024-06-29 08:20:56 +01:00
|
|
|
use webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter};
|
2024-05-05 18:08:05 +01:00
|
|
|
|
2024-07-05 20:31:16 +01:00
|
|
|
pub use git_next_git::repository::{factory::real as repository_factory, RepositoryFactory};
|
2024-07-03 07:42:11 +01:00
|
|
|
|
2024-06-29 14:56:20 +01:00
|
|
|
message!(ReceiveServerConfig: ServerConfig: "Notification of newly loaded server configuration.
|
|
|
|
|
2024-07-11 08:06:36 +01:00
|
|
|
|
2024-06-29 14:56:20 +01:00
|
|
|
This message will prompt the `git-next` server to stop and restart all repo-actors.
|
|
|
|
|
|
|
|
Contains the new server configuration.");
|
2024-06-29 10:49:12 +01:00
|
|
|
|
2024-07-11 08:06:36 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Constructor)]
|
|
|
|
pub struct ValidServerConfig {
|
|
|
|
server_config: ServerConfig,
|
|
|
|
socket_address: SocketAddr,
|
|
|
|
server_storage: ServerStorage,
|
|
|
|
}
|
|
|
|
message!(ReceiveValidServerConfig: ValidServerConfig: "Notification of validated server configuration.");
|
|
|
|
|
2024-05-05 18:08:05 +01:00
|
|
|
#[derive(Debug, derive_more::Display, derive_more::From)]
|
|
|
|
pub enum Error {
|
|
|
|
#[display("Failed to create data directories")]
|
|
|
|
FailedToCreateDataDirectory(kxio::fs::Error),
|
|
|
|
|
|
|
|
#[display("The forge data path is not a directory: {path:?}")]
|
|
|
|
ForgeDirIsNotDirectory {
|
|
|
|
path: PathBuf,
|
|
|
|
},
|
|
|
|
|
2024-05-22 08:41:30 +01:00
|
|
|
Config(config::server::Error),
|
2024-05-05 18:08:05 +01:00
|
|
|
|
|
|
|
Io(std::io::Error),
|
|
|
|
}
|
|
|
|
type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
2024-07-11 18:54:44 +01:00
|
|
|
#[derive(derive_with::With)]
|
|
|
|
#[with(message_log)]
|
2024-05-05 18:08:05 +01:00
|
|
|
pub struct Server {
|
2024-05-11 19:46:20 +01:00
|
|
|
generation: Generation,
|
2024-05-07 19:32:15 +01:00
|
|
|
webhook: Option<Addr<WebhookActor>>,
|
2024-05-05 18:08:05 +01:00
|
|
|
fs: FileSystem,
|
|
|
|
net: Network,
|
2024-06-19 16:40:10 +01:00
|
|
|
repository_factory: Box<dyn RepositoryFactory>,
|
2024-06-19 07:03:08 +01:00
|
|
|
sleep_duration: std::time::Duration,
|
2024-07-11 18:54:44 +01:00
|
|
|
|
|
|
|
// testing
|
|
|
|
message_log: Option<Arc<RwLock<Vec<String>>>>,
|
2024-05-05 18:08:05 +01:00
|
|
|
}
|
|
|
|
impl Actor for Server {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
2024-05-07 19:32:15 +01:00
|
|
|
impl Handler<FileUpdated> for Server {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, _msg: FileUpdated, ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
let server_config = match ServerConfig::load(&self.fs) {
|
|
|
|
Ok(server_config) => server_config,
|
|
|
|
Err(err) => {
|
|
|
|
error!("Failed to load config file. Error: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2024-07-11 18:54:44 +01:00
|
|
|
self.do_send(ReceiveServerConfig::new(server_config), ctx);
|
2024-05-07 19:32:15 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-19 07:03:08 +01:00
|
|
|
impl Handler<ReceiveServerConfig> for Server {
|
2024-05-05 18:08:05 +01:00
|
|
|
type Result = ();
|
|
|
|
|
2024-07-11 18:54:44 +01:00
|
|
|
#[allow(clippy::cognitive_complexity)]
|
2024-07-11 08:06:36 +01:00
|
|
|
fn handle(&mut self, msg: ReceiveServerConfig, ctx: &mut Self::Context) -> Self::Result {
|
2024-07-11 18:54:44 +01:00
|
|
|
tracing::info!("recieved server config");
|
2024-05-10 22:01:47 +01:00
|
|
|
let Ok(socket_addr) = msg.http() else {
|
2024-07-11 08:06:36 +01:00
|
|
|
error!("Unable to parse http.addr");
|
2024-05-10 22:01:47 +01:00
|
|
|
return;
|
|
|
|
};
|
2024-07-11 08:06:36 +01:00
|
|
|
|
|
|
|
let Some(server_storage) = self.server_storage(&msg) else {
|
|
|
|
error!("Server storage not available");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2024-07-11 18:54:44 +01:00
|
|
|
if msg.webhook().base_url().ends_with('/') {
|
|
|
|
error!("webhook.url must not end with a '/'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.do_send(
|
|
|
|
ReceiveValidServerConfig::new(ValidServerConfig::new(
|
2024-07-11 08:06:36 +01:00
|
|
|
msg.0,
|
|
|
|
socket_addr,
|
|
|
|
server_storage,
|
2024-07-11 18:54:44 +01:00
|
|
|
)),
|
|
|
|
ctx,
|
|
|
|
);
|
2024-07-11 08:06:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Handler<ReceiveValidServerConfig> for Server {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: ReceiveValidServerConfig, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
let ValidServerConfig {
|
|
|
|
server_config,
|
|
|
|
socket_address,
|
|
|
|
server_storage,
|
|
|
|
} = msg.0;
|
2024-05-07 19:32:15 +01:00
|
|
|
if let Some(webhook) = self.webhook.take() {
|
|
|
|
webhook.do_send(ShutdownWebhook);
|
|
|
|
}
|
|
|
|
self.generation.inc();
|
2024-05-05 18:08:05 +01:00
|
|
|
// Webhook Server
|
|
|
|
info!("Starting Webhook Server...");
|
2024-06-29 08:20:56 +01:00
|
|
|
let webhook_router = WebhookRouter::default().start();
|
2024-05-05 18:08:05 +01:00
|
|
|
let webhook = server_config.webhook();
|
|
|
|
// Forge Actors
|
2024-05-29 19:22:05 +01:00
|
|
|
for (forge_alias, forge_config) in server_config.forges() {
|
2024-07-11 08:06:36 +01:00
|
|
|
self.create_forge_repos(forge_config, forge_alias.clone(), &server_storage, webhook)
|
2024-05-05 18:08:05 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|a| self.start_actor(a))
|
2024-05-29 19:22:05 +01:00
|
|
|
.map(|(repo_alias, addr)| {
|
|
|
|
AddWebhookRecipient::new(forge_alias.clone(), repo_alias, addr.recipient())
|
|
|
|
})
|
2024-05-05 18:08:05 +01:00
|
|
|
.for_each(|msg| webhook_router.do_send(msg));
|
|
|
|
}
|
2024-07-11 08:06:36 +01:00
|
|
|
let webhook = WebhookActor::new(socket_address, webhook_router.recipient()).start();
|
2024-05-07 19:32:15 +01:00
|
|
|
self.webhook.replace(webhook);
|
2024-05-05 18:08:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Server {
|
2024-06-19 07:03:08 +01:00
|
|
|
pub fn new(
|
|
|
|
fs: FileSystem,
|
|
|
|
net: Network,
|
|
|
|
repo: Box<dyn RepositoryFactory>,
|
|
|
|
sleep_duration: std::time::Duration,
|
|
|
|
) -> Self {
|
2024-06-20 19:03:11 +01:00
|
|
|
let generation = Generation::default();
|
2024-05-07 08:17:29 +01:00
|
|
|
Self {
|
|
|
|
generation,
|
2024-05-07 19:32:15 +01:00
|
|
|
webhook: None,
|
2024-05-07 08:17:29 +01:00
|
|
|
fs,
|
|
|
|
net,
|
2024-06-19 16:40:10 +01:00
|
|
|
repository_factory: repo,
|
2024-06-19 07:03:08 +01:00
|
|
|
sleep_duration,
|
2024-07-11 18:54:44 +01:00
|
|
|
message_log: None,
|
2024-05-07 08:17:29 +01:00
|
|
|
}
|
2024-05-05 18:08:05 +01:00
|
|
|
}
|
|
|
|
fn create_forge_data_directories(
|
|
|
|
&self,
|
|
|
|
server_config: &ServerConfig,
|
|
|
|
server_dir: &std::path::Path,
|
|
|
|
) -> Result<()> {
|
|
|
|
for (forge_name, _forge_config) in server_config.forges() {
|
|
|
|
let forge_dir: PathBuf = (&forge_name).into();
|
|
|
|
let path = server_dir.join(&forge_dir);
|
|
|
|
if self.fs.path_exists(&path)? {
|
|
|
|
if !self.fs.path_is_dir(&path)? {
|
|
|
|
return Err(Error::ForgeDirIsNotDirectory { path });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info!(%forge_name, ?path, "creating storage");
|
|
|
|
self.fs.dir_create_all(&path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_forge_repos(
|
|
|
|
&self,
|
|
|
|
forge_config: &ForgeConfig,
|
2024-05-29 19:22:05 +01:00
|
|
|
forge_name: ForgeAlias,
|
2024-05-05 18:08:05 +01:00
|
|
|
server_storage: &ServerStorage,
|
|
|
|
webhook: &Webhook,
|
2024-05-29 19:22:05 +01:00
|
|
|
) -> Vec<(ForgeAlias, RepoAlias, RepoActor)> {
|
2024-05-05 18:08:05 +01:00
|
|
|
let span =
|
|
|
|
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
|
|
|
|
|
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Forge");
|
|
|
|
let mut repos = vec![];
|
|
|
|
let creator = self.create_actor(forge_name, forge_config.clone(), server_storage, webhook);
|
|
|
|
for (repo_alias, server_repo_config) in forge_config.repos() {
|
|
|
|
let forge_repo = creator((repo_alias, server_repo_config));
|
|
|
|
info!(
|
|
|
|
alias = %forge_repo.1,
|
|
|
|
"Created Repo"
|
|
|
|
);
|
|
|
|
repos.push(forge_repo);
|
|
|
|
}
|
|
|
|
repos
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_actor(
|
|
|
|
&self,
|
2024-05-29 19:22:05 +01:00
|
|
|
forge_name: ForgeAlias,
|
2024-05-05 18:08:05 +01:00
|
|
|
forge_config: ForgeConfig,
|
|
|
|
server_storage: &ServerStorage,
|
|
|
|
webhook: &Webhook,
|
2024-05-29 19:22:05 +01:00
|
|
|
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeAlias, RepoAlias, RepoActor) {
|
2024-05-05 18:08:05 +01:00
|
|
|
let server_storage = server_storage.clone();
|
|
|
|
let webhook = webhook.clone();
|
|
|
|
let net = self.net.clone();
|
2024-06-19 16:40:10 +01:00
|
|
|
let repository_factory = self.repository_factory.duplicate();
|
2024-05-07 08:17:29 +01:00
|
|
|
let generation = self.generation;
|
2024-06-19 07:03:08 +01:00
|
|
|
let sleep_duration = self.sleep_duration;
|
2024-05-05 18:08:05 +01:00
|
|
|
move |(repo_alias, server_repo_config)| {
|
|
|
|
let span = tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
|
|
|
|
let _guard = span.enter();
|
|
|
|
info!("Creating Repo");
|
|
|
|
let gitdir = server_repo_config.gitdir().map_or_else(
|
|
|
|
|| {
|
2024-07-06 14:25:43 +01:00
|
|
|
GitDir::new(
|
2024-05-05 18:08:05 +01:00
|
|
|
server_storage
|
|
|
|
.path()
|
|
|
|
.join(forge_name.to_string())
|
|
|
|
.join(repo_alias.to_string()),
|
2024-07-06 14:25:43 +01:00
|
|
|
config::git_dir::StoragePathType::Internal,
|
2024-05-05 18:08:05 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|gitdir| gitdir,
|
|
|
|
);
|
|
|
|
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
|
|
|
|
// have cloned the repo yet
|
|
|
|
let repo_details = RepoDetails::new(
|
2024-05-07 08:17:29 +01:00
|
|
|
generation,
|
2024-05-05 18:08:05 +01:00
|
|
|
&repo_alias,
|
|
|
|
server_repo_config,
|
|
|
|
&forge_name,
|
|
|
|
&forge_config,
|
|
|
|
gitdir,
|
|
|
|
);
|
2024-06-19 07:03:08 +01:00
|
|
|
let forge = git_next_forge::Forge::create(repo_details.clone(), net.clone());
|
2024-05-05 18:08:05 +01:00
|
|
|
info!("Starting Repo Actor");
|
2024-05-19 20:02:06 +01:00
|
|
|
let actor = RepoActor::new(
|
|
|
|
repo_details,
|
2024-06-19 07:03:08 +01:00
|
|
|
forge,
|
2024-05-19 20:02:06 +01:00
|
|
|
webhook.clone(),
|
|
|
|
generation,
|
|
|
|
net.clone(),
|
2024-06-19 16:40:10 +01:00
|
|
|
repository_factory.duplicate(),
|
2024-06-19 07:03:08 +01:00
|
|
|
sleep_duration,
|
2024-05-19 20:02:06 +01:00
|
|
|
);
|
2024-05-05 18:08:05 +01:00
|
|
|
(forge_name.clone(), repo_alias, actor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_actor(
|
|
|
|
&self,
|
2024-05-29 19:22:05 +01:00
|
|
|
actor: (ForgeAlias, RepoAlias, RepoActor),
|
2024-05-05 18:08:05 +01:00
|
|
|
) -> (RepoAlias, Addr<RepoActor>) {
|
|
|
|
let (forge_name, repo_alias, actor) = actor;
|
|
|
|
let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias);
|
|
|
|
let _guard = span.enter();
|
|
|
|
let addr = actor.start();
|
|
|
|
addr.do_send(CloneRepo);
|
|
|
|
info!("Started");
|
|
|
|
(repo_alias, addr)
|
|
|
|
}
|
2024-07-11 07:27:11 +01:00
|
|
|
|
2024-07-11 08:06:36 +01:00
|
|
|
fn server_storage(&self, server_config: &ReceiveServerConfig) -> Option<ServerStorage> {
|
|
|
|
let server_storage = server_config.storage().clone();
|
2024-07-11 07:27:11 +01:00
|
|
|
let dir = server_storage.path();
|
|
|
|
if !dir.exists() {
|
|
|
|
if let Err(err) = self.fs.dir_create(dir) {
|
|
|
|
error!(?err, ?dir, "Failed to create server storage");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let Ok(canon) = dir.canonicalize() else {
|
|
|
|
error!(?dir, "Failed to confirm server storage");
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
if let Err(err) = self.create_forge_data_directories(server_config, &canon) {
|
|
|
|
error!(?err, "Failure creating forge storage");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(server_storage)
|
|
|
|
}
|
2024-07-11 18:54:44 +01:00
|
|
|
|
|
|
|
fn do_send<M>(&mut self, msg: M, _ctx: &mut <Self as actix::Actor>::Context)
|
|
|
|
where
|
|
|
|
M: actix::Message + Send + 'static + std::fmt::Debug,
|
|
|
|
Self: actix::Handler<M>,
|
|
|
|
<M as actix::Message>::Result: Send,
|
|
|
|
{
|
|
|
|
tracing::info!(?msg, "send");
|
|
|
|
if let Some(message_log) = &self.message_log {
|
|
|
|
let log_message = format!("send: {:?}", msg);
|
|
|
|
tracing::debug!(log_message);
|
|
|
|
if let Ok(mut log) = message_log.write() {
|
|
|
|
log.push(log_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(test))]
|
|
|
|
_ctx.address().do_send(msg);
|
|
|
|
tracing::info!("sent");
|
|
|
|
}
|
2024-05-05 18:08:05 +01:00
|
|
|
}
|