Get the node running

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-10-19 22:27:00 +02:00
parent 961c18319f
commit 706fbc6550
No known key found for this signature in database
3 changed files with 21 additions and 7 deletions

View File

@ -4,7 +4,7 @@ use std::io::BufReader;
use std::io::LineWriter; use std::io::LineWriter;
use std::os::unix::net::UnixListener; use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::Path; use std::path::{Path, PathBuf};
use std::{fs, io, net}; use std::{fs, io, net};
use crate::client; use crate::client;
@ -17,12 +17,22 @@ use crate::service::FetchResult;
pub enum Error { pub enum Error {
#[error("failed to bind control socket listener: {0}")] #[error("failed to bind control socket listener: {0}")]
Bind(io::Error), Bind(io::Error),
#[error("invalid socket path specified: {0}")]
InvalidPath(PathBuf),
} }
/// Listen for commands on the control socket, and process them. /// Listen for commands on the control socket, and process them.
pub fn listen<P: AsRef<Path>, H: Handle>(path: P, handle: H) -> Result<(), Error> { pub fn listen<P: AsRef<Path>, H: Handle>(path: P, handle: H) -> Result<(), Error> {
// Remove the socket file on startup before rebinding. // Remove the socket file on startup before rebinding.
fs::remove_file(&path).ok(); fs::remove_file(&path).ok();
fs::create_dir_all(
path.as_ref()
.parent()
.ok_or_else(|| Error::InvalidPath(path.as_ref().to_path_buf()))?,
)
.ok();
log::info!("Binding control socket {}..", path.as_ref().display());
let listener = UnixListener::bind(path).map_err(Error::Bind)?; let listener = UnixListener::bind(path).map_err(Error::Bind)?;
for incoming in listener.incoming() { for incoming in listener.incoming() {

View File

@ -1,7 +1,9 @@
use std::thread; use std::thread;
use std::{env, net, process}; use std::{net, process};
use radicle_node::node; use anyhow::Context as _;
use radicle_node::logger;
use radicle_node::prelude::Address; use radicle_node::prelude::Address;
use radicle_node::{client, control, git, service}; use radicle_node::{client, control, git, service};
@ -53,9 +55,12 @@ impl Options {
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
logger::init(log::Level::Debug)?;
let options = Options::from_env()?; let options = Options::from_env()?;
let profile = radicle::Profile::load()?; let profile = radicle::Profile::load().context("Failed to load node profile")?;
let client = client::Client::<Reactor>::new(profile)?; let socket = profile.socket();
let client = client::Client::<Reactor>::new(profile).context("Failed to initialize client")?;
let handle = client.handle(); let handle = client.handle();
let config = client::Config { let config = client::Config {
service: service::Config { service: service::Config {
@ -65,7 +70,6 @@ fn main() -> anyhow::Result<()> {
}, },
listen: options.listen, listen: options.listen,
}; };
let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| node::DEFAULT_SOCKET_NAME.to_owned());
let t1 = thread::spawn(move || control::listen(socket, handle)); let t1 = thread::spawn(move || control::listen(socket, handle));
let t2 = thread::spawn(move || client.run(config)); let t2 = thread::spawn(move || client.run(config));

View File

@ -84,7 +84,7 @@ impl Profile {
} }
/// Get the path to the radicle node socket. /// Get the path to the radicle node socket.
fn socket(&self) -> PathBuf { pub fn socket(&self) -> PathBuf {
env::var_os("RAD_SOCKET") env::var_os("RAD_SOCKET")
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| self.home.join("node").join(node::DEFAULT_SOCKET_NAME)) .unwrap_or_else(|| self.home.join("node").join(node::DEFAULT_SOCKET_NAME))