2024-05-05 18:08:05 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_config::{ForgeConfig, ForgeName, GitDir, RepoAlias, ServerRepoConfig};
|
2024-05-18 11:41:18 +01:00
|
|
|
use git_next_git::{Generation, RepoDetails, Repository};
|
2024-05-05 18:08:05 +01:00
|
|
|
use kxio::{fs::FileSystem, network::Network};
|
2024-05-10 22:01:47 +01:00
|
|
|
use tracing::{error, info, warn};
|
2024-05-05 18:08:05 +01:00
|
|
|
|
2024-05-11 18:57:18 +01:00
|
|
|
use crate::{
|
2024-05-05 18:08:05 +01:00
|
|
|
actors::{
|
2024-05-07 19:32:15 +01:00
|
|
|
file_watcher::FileUpdated,
|
2024-05-05 18:08:05 +01:00
|
|
|
repo::{CloneRepo, RepoActor},
|
2024-05-07 19:32:15 +01:00
|
|
|
webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter},
|
2024-05-05 18:08:05 +01:00
|
|
|
},
|
2024-05-11 19:46:20 +01:00
|
|
|
config::{ServerConfig, ServerStorage, Webhook},
|
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-11 18:57:18 +01:00
|
|
|
Config(crate::config::Error),
|
2024-05-05 18:08:05 +01:00
|
|
|
|
|
|
|
Io(std::io::Error),
|
|
|
|
}
|
|
|
|
type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
|
|
|
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-05-18 11:41:18 +01:00
|
|
|
repo: Repository,
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ctx.notify(server_config);
|
|
|
|
}
|
|
|
|
}
|
2024-05-05 18:08:05 +01:00
|
|
|
impl Handler<ServerConfig> for Server {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
#[allow(clippy::cognitive_complexity)] // TODO: (#75) reduce complexity
|
|
|
|
fn handle(&mut self, msg: ServerConfig, _ctx: &mut Self::Context) -> Self::Result {
|
2024-05-10 22:01:47 +01:00
|
|
|
let Ok(socket_addr) = msg.http() else {
|
|
|
|
warn!("Unable to parse http.addr");
|
|
|
|
return;
|
|
|
|
};
|
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
|
|
|
let server_config = msg;
|
|
|
|
// Server Storage
|
|
|
|
let dir = server_config.storage().path();
|
|
|
|
if !dir.exists() {
|
|
|
|
info!(?dir, "server storage doesn't exist - creating it");
|
|
|
|
if let Err(err) = self.fs.dir_create(dir) {
|
|
|
|
error!(?err, ?dir, "Failed to create server storage");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let Ok(canon) = dir.canonicalize() else {
|
|
|
|
error!(?dir, "Failed to confirm server storage");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
info!(dir = ?canon, "server storage");
|
|
|
|
|
|
|
|
// Forge directories in Server Storage
|
|
|
|
let server_storage = server_config.storage();
|
|
|
|
if let Err(err) = self.create_forge_data_directories(&server_config, dir) {
|
|
|
|
error!(?err, "Failure creating forge storage");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Webhook Server
|
|
|
|
info!("Starting Webhook Server...");
|
|
|
|
let webhook_router = WebhookRouter::new().start();
|
|
|
|
let webhook = server_config.webhook();
|
|
|
|
|
|
|
|
// Forge Actors
|
|
|
|
for (forge_name, forge_config) in server_config.forges() {
|
|
|
|
self.create_forge_repos(forge_config, forge_name.clone(), server_storage, webhook)
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| self.start_actor(a))
|
|
|
|
.map(|(alias, addr)| AddWebhookRecipient(alias, addr.recipient()))
|
|
|
|
.for_each(|msg| webhook_router.do_send(msg));
|
|
|
|
}
|
|
|
|
|
2024-05-10 22:01:47 +01:00
|
|
|
let webhook = WebhookActor::new(socket_addr, 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-05-18 11:41:18 +01:00
|
|
|
pub fn new(fs: FileSystem, net: Network, repo: Repository) -> Self {
|
2024-05-11 19:46:20 +01:00
|
|
|
let generation = Generation::new();
|
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-05-18 11:41:18 +01:00
|
|
|
repo,
|
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,
|
|
|
|
forge_name: ForgeName,
|
|
|
|
server_storage: &ServerStorage,
|
|
|
|
webhook: &Webhook,
|
|
|
|
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
|
|
|
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,
|
|
|
|
forge_name: ForgeName,
|
|
|
|
forge_config: ForgeConfig,
|
|
|
|
server_storage: &ServerStorage,
|
|
|
|
webhook: &Webhook,
|
|
|
|
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
|
|
|
let server_storage = server_storage.clone();
|
|
|
|
let webhook = webhook.clone();
|
|
|
|
let net = self.net.clone();
|
2024-05-19 20:02:06 +01:00
|
|
|
let repo = self.repo.clone();
|
2024-05-07 08:17:29 +01:00
|
|
|
let generation = self.generation;
|
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(
|
|
|
|
|| {
|
|
|
|
GitDir::from(
|
|
|
|
server_storage
|
|
|
|
.path()
|
|
|
|
.join(forge_name.to_string())
|
|
|
|
.join(repo_alias.to_string()),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|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,
|
|
|
|
);
|
|
|
|
info!("Starting Repo Actor");
|
2024-05-19 20:02:06 +01:00
|
|
|
let actor = RepoActor::new(
|
|
|
|
repo_details,
|
|
|
|
webhook.clone(),
|
|
|
|
generation,
|
|
|
|
net.clone(),
|
|
|
|
repo.clone(),
|
|
|
|
);
|
2024-05-05 18:08:05 +01:00
|
|
|
(forge_name.clone(), repo_alias, actor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_actor(
|
|
|
|
&self,
|
|
|
|
actor: (ForgeName, RepoAlias, RepoActor),
|
|
|
|
) -> (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)
|
|
|
|
}
|
|
|
|
}
|