Line data Source code
1 : use crate::background_process::{self, start_process, stop_process};
2 : use crate::local_env::LocalEnv;
3 : use anyhow::{Context, Result};
4 : use camino::Utf8PathBuf;
5 : use std::io::Write;
6 : use std::net::SocketAddr;
7 : use std::time::Duration;
8 :
9 : /// Directory within .neon which will be used by default for LocalFs remote storage.
10 : pub const ENDPOINT_STORAGE_REMOTE_STORAGE_DIR: &str = "local_fs_remote_storage/endpoint_storage";
11 : pub const ENDPOINT_STORAGE_DEFAULT_ADDR: SocketAddr =
12 : SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), 9993);
13 :
14 : pub struct EndpointStorage {
15 : pub bin: Utf8PathBuf,
16 : pub data_dir: Utf8PathBuf,
17 : pub pemfile: Utf8PathBuf,
18 : pub addr: SocketAddr,
19 : }
20 :
21 : impl EndpointStorage {
22 0 : pub fn from_env(env: &LocalEnv) -> EndpointStorage {
23 0 : EndpointStorage {
24 0 : bin: Utf8PathBuf::from_path_buf(env.endpoint_storage_bin()).unwrap(),
25 0 : data_dir: Utf8PathBuf::from_path_buf(env.endpoint_storage_data_dir()).unwrap(),
26 0 : pemfile: Utf8PathBuf::from_path_buf(env.public_key_path.clone()).unwrap(),
27 0 : addr: env.endpoint_storage.listen_addr,
28 0 : }
29 0 : }
30 :
31 0 : fn config_path(&self) -> Utf8PathBuf {
32 0 : self.data_dir.join("endpoint_storage.json")
33 0 : }
34 :
35 0 : fn listen_addr(&self) -> Utf8PathBuf {
36 0 : format!("{}:{}", self.addr.ip(), self.addr.port()).into()
37 0 : }
38 :
39 0 : pub fn init(&self) -> Result<()> {
40 0 : println!("Initializing object storage in {:?}", self.data_dir);
41 0 : let parent = self.data_dir.parent().unwrap();
42 :
43 : #[derive(serde::Serialize)]
44 : struct Cfg {
45 : listen: Utf8PathBuf,
46 : pemfile: Utf8PathBuf,
47 : local_path: Utf8PathBuf,
48 : r#type: String,
49 : }
50 0 : let cfg = Cfg {
51 0 : listen: self.listen_addr(),
52 0 : pemfile: parent.join(self.pemfile.clone()),
53 0 : local_path: parent.join(ENDPOINT_STORAGE_REMOTE_STORAGE_DIR),
54 0 : r#type: "LocalFs".to_string(),
55 0 : };
56 0 : std::fs::create_dir_all(self.config_path().parent().unwrap())?;
57 0 : std::fs::write(self.config_path(), serde_json::to_string(&cfg)?)
58 0 : .context("write object storage config")?;
59 0 : Ok(())
60 0 : }
61 :
62 0 : pub async fn start(&self, retry_timeout: &Duration) -> Result<()> {
63 0 : println!("Starting endpoint_storage at {}", self.listen_addr());
64 0 : std::io::stdout().flush().context("flush stdout")?;
65 :
66 0 : let process_status_check = || async {
67 0 : let res = reqwest::Client::new().get(format!("http://{}/metrics", self.listen_addr()));
68 0 : match res.send().await {
69 0 : Ok(res) => Ok(res.status().is_success()),
70 0 : Err(_) => Ok(false),
71 : }
72 0 : };
73 :
74 0 : let res = start_process(
75 0 : "endpoint_storage",
76 0 : &self.data_dir.clone().into_std_path_buf(),
77 0 : &self.bin.clone().into_std_path_buf(),
78 0 : vec![self.config_path().to_string()],
79 0 : vec![("RUST_LOG".into(), "debug".into())],
80 0 : background_process::InitialPidFile::Create(self.pid_file()),
81 0 : retry_timeout,
82 0 : process_status_check,
83 0 : )
84 0 : .await;
85 0 : if res.is_err() {
86 0 : eprintln!("Logs:\n{}", std::fs::read_to_string(self.log_file())?);
87 0 : }
88 :
89 0 : res
90 0 : }
91 :
92 0 : pub fn stop(&self, immediate: bool) -> anyhow::Result<()> {
93 0 : stop_process(immediate, "endpoint_storage", &self.pid_file())
94 0 : }
95 :
96 0 : fn log_file(&self) -> Utf8PathBuf {
97 0 : self.data_dir.join("endpoint_storage.log")
98 0 : }
99 :
100 0 : fn pid_file(&self) -> Utf8PathBuf {
101 0 : self.data_dir.join("endpoint_storage.pid")
102 0 : }
103 : }
|