LCOV - code coverage report
Current view: top level - control_plane/src - local_env.rs (source / functions) Coverage Total Hit
Test: feead26e04cdef6e988ff1765b1cb7075eb48d3d.info Lines: 0.0 % 499 0
Test Date: 2025-02-28 12:11:00 Functions: 0.0 % 136 0

            Line data    Source code
       1              : //! This module is responsible for locating and loading paths in a local setup.
       2              : //!
       3              : //! Now it also provides init method which acts like a stub for proper installation
       4              : //! script which will use local paths.
       5              : 
       6              : use std::collections::HashMap;
       7              : use std::net::{IpAddr, Ipv4Addr, SocketAddr};
       8              : use std::path::{Path, PathBuf};
       9              : use std::process::{Command, Stdio};
      10              : use std::time::Duration;
      11              : use std::{env, fs};
      12              : 
      13              : use anyhow::{Context, bail};
      14              : use clap::ValueEnum;
      15              : use postgres_backend::AuthType;
      16              : use reqwest::Url;
      17              : use serde::{Deserialize, Serialize};
      18              : use utils::auth::{Claims, encode_from_key_file};
      19              : use utils::id::{NodeId, TenantId, TenantTimelineId, TimelineId};
      20              : 
      21              : use crate::pageserver::{PAGESERVER_REMOTE_STORAGE_DIR, PageServerNode};
      22              : use crate::safekeeper::SafekeeperNode;
      23              : 
      24              : pub const DEFAULT_PG_VERSION: u32 = 16;
      25              : 
      26              : //
      27              : // This data structures represents neon_local CLI config
      28              : //
      29              : // It is deserialized from the .neon/config file, or the config file passed
      30              : // to 'neon_local init --config=<path>' option. See control_plane/simple.conf for
      31              : // an example.
      32              : //
      33              : #[derive(PartialEq, Eq, Clone, Debug)]
      34              : pub struct LocalEnv {
      35              :     // Base directory for all the nodes (the pageserver, safekeepers and
      36              :     // compute endpoints).
      37              :     //
      38              :     // This is not stored in the config file. Rather, this is the path where the
      39              :     // config file itself is. It is read from the NEON_REPO_DIR env variable which
      40              :     // must be an absolute path. If the env var is not set, $PWD/.neon is used.
      41              :     pub base_data_dir: PathBuf,
      42              : 
      43              :     // Path to postgres distribution. It's expected that "bin", "include",
      44              :     // "lib", "share" from postgres distribution are there. If at some point
      45              :     // in time we will be able to run against vanilla postgres we may split that
      46              :     // to four separate paths and match OS-specific installation layout.
      47              :     pub pg_distrib_dir: PathBuf,
      48              : 
      49              :     // Path to pageserver binary.
      50              :     pub neon_distrib_dir: PathBuf,
      51              : 
      52              :     // Default tenant ID to use with the 'neon_local' command line utility, when
      53              :     // --tenant_id is not explicitly specified.
      54              :     pub default_tenant_id: Option<TenantId>,
      55              : 
      56              :     // used to issue tokens during e.g pg start
      57              :     pub private_key_path: PathBuf,
      58              : 
      59              :     pub broker: NeonBroker,
      60              : 
      61              :     // Configuration for the storage controller (1 per neon_local environment)
      62              :     pub storage_controller: NeonStorageControllerConf,
      63              : 
      64              :     /// This Vec must always contain at least one pageserver
      65              :     /// Populdated by [`Self::load_config`] from the individual `pageserver.toml`s.
      66              :     /// NB: not used anymore except for informing users that they need to change their `.neon/config`.
      67              :     pub pageservers: Vec<PageServerConf>,
      68              : 
      69              :     pub safekeepers: Vec<SafekeeperConf>,
      70              : 
      71              :     // Control plane upcall API for pageserver: if None, we will not run storage_controller  If set, this will
      72              :     // be propagated into each pageserver's configuration.
      73              :     pub control_plane_api: Url,
      74              : 
      75              :     // Control plane upcall API for storage controller.  If set, this will be propagated into the
      76              :     // storage controller's configuration.
      77              :     pub control_plane_compute_hook_api: Option<Url>,
      78              : 
      79              :     /// Keep human-readable aliases in memory (and persist them to config), to hide ZId hex strings from the user.
      80              :     // A `HashMap<String, HashMap<TenantId, TimelineId>>` would be more appropriate here,
      81              :     // but deserialization into a generic toml object as `toml::Value::try_from` fails with an error.
      82              :     // https://toml.io/en/v1.0.0 does not contain a concept of "a table inside another table".
      83              :     pub branch_name_mappings: HashMap<String, Vec<(TenantId, TimelineId)>>,
      84              : }
      85              : 
      86              : /// On-disk state stored in `.neon/config`.
      87            0 : #[derive(PartialEq, Eq, Clone, Debug, Default, Serialize, Deserialize)]
      88              : #[serde(default, deny_unknown_fields)]
      89              : pub struct OnDiskConfig {
      90              :     pub pg_distrib_dir: PathBuf,
      91              :     pub neon_distrib_dir: PathBuf,
      92              :     pub default_tenant_id: Option<TenantId>,
      93              :     pub private_key_path: PathBuf,
      94              :     pub broker: NeonBroker,
      95              :     pub storage_controller: NeonStorageControllerConf,
      96              :     #[serde(
      97              :         skip_serializing,
      98              :         deserialize_with = "fail_if_pageservers_field_specified"
      99              :     )]
     100              :     pub pageservers: Vec<PageServerConf>,
     101              :     pub safekeepers: Vec<SafekeeperConf>,
     102              :     pub control_plane_api: Option<Url>,
     103              :     pub control_plane_compute_hook_api: Option<Url>,
     104              :     branch_name_mappings: HashMap<String, Vec<(TenantId, TimelineId)>>,
     105              : }
     106              : 
     107            0 : fn fail_if_pageservers_field_specified<'de, D>(_: D) -> Result<Vec<PageServerConf>, D::Error>
     108            0 : where
     109            0 :     D: serde::Deserializer<'de>,
     110            0 : {
     111            0 :     Err(serde::de::Error::custom(
     112            0 :         "The 'pageservers' field is no longer used; pageserver.toml is now authoritative; \
     113            0 :          Please remove the `pageservers` from your .neon/config.",
     114            0 :     ))
     115            0 : }
     116              : 
     117              : /// The description of the neon_local env to be initialized by `neon_local init --config`.
     118            0 : #[derive(Clone, Debug, Deserialize)]
     119              : #[serde(deny_unknown_fields)]
     120              : pub struct NeonLocalInitConf {
     121              :     // TODO: do we need this? Seems unused
     122              :     pub pg_distrib_dir: Option<PathBuf>,
     123              :     // TODO: do we need this? Seems unused
     124              :     pub neon_distrib_dir: Option<PathBuf>,
     125              :     pub default_tenant_id: TenantId,
     126              :     pub broker: NeonBroker,
     127              :     pub storage_controller: Option<NeonStorageControllerConf>,
     128              :     pub pageservers: Vec<NeonLocalInitPageserverConf>,
     129              :     pub safekeepers: Vec<SafekeeperConf>,
     130              :     pub control_plane_api: Option<Url>,
     131              :     pub control_plane_compute_hook_api: Option<Option<Url>>,
     132              : }
     133              : 
     134              : /// Broker config for cluster internal communication.
     135            0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
     136              : #[serde(default)]
     137              : pub struct NeonBroker {
     138              :     /// Broker listen address for storage nodes coordination, e.g. '127.0.0.1:50051'.
     139              :     pub listen_addr: SocketAddr,
     140              : }
     141              : 
     142              : /// Broker config for cluster internal communication.
     143            0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
     144              : #[serde(default)]
     145              : pub struct NeonStorageControllerConf {
     146              :     /// Heartbeat timeout before marking a node offline
     147              :     #[serde(with = "humantime_serde")]
     148              :     pub max_offline: Duration,
     149              : 
     150              :     #[serde(with = "humantime_serde")]
     151              :     pub max_warming_up: Duration,
     152              : 
     153              :     pub start_as_candidate: bool,
     154              : 
     155              :     /// Database url used when running multiple storage controller instances
     156              :     pub database_url: Option<SocketAddr>,
     157              : 
     158              :     /// Threshold for auto-splitting a tenant into shards
     159              :     pub split_threshold: Option<u64>,
     160              : 
     161              :     pub max_secondary_lag_bytes: Option<u64>,
     162              : 
     163              :     #[serde(with = "humantime_serde")]
     164              :     pub heartbeat_interval: Duration,
     165              : 
     166              :     #[serde(with = "humantime_serde")]
     167              :     pub long_reconcile_threshold: Option<Duration>,
     168              : }
     169              : 
     170              : impl NeonStorageControllerConf {
     171              :     // Use a shorter pageserver unavailability interval than the default to speed up tests.
     172              :     const DEFAULT_MAX_OFFLINE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(10);
     173              : 
     174              :     const DEFAULT_MAX_WARMING_UP_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30);
     175              : 
     176              :     // Very tight heartbeat interval to speed up tests
     177              :     const DEFAULT_HEARTBEAT_INTERVAL: std::time::Duration = std::time::Duration::from_millis(1000);
     178              : }
     179              : 
     180              : impl Default for NeonStorageControllerConf {
     181            0 :     fn default() -> Self {
     182            0 :         Self {
     183            0 :             max_offline: Self::DEFAULT_MAX_OFFLINE_INTERVAL,
     184            0 :             max_warming_up: Self::DEFAULT_MAX_WARMING_UP_INTERVAL,
     185            0 :             start_as_candidate: false,
     186            0 :             database_url: None,
     187            0 :             split_threshold: None,
     188            0 :             max_secondary_lag_bytes: None,
     189            0 :             heartbeat_interval: Self::DEFAULT_HEARTBEAT_INTERVAL,
     190            0 :             long_reconcile_threshold: None,
     191            0 :         }
     192            0 :     }
     193              : }
     194              : 
     195              : // Dummy Default impl to satisfy Deserialize derive.
     196              : impl Default for NeonBroker {
     197            0 :     fn default() -> Self {
     198            0 :         NeonBroker {
     199            0 :             listen_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
     200            0 :         }
     201            0 :     }
     202              : }
     203              : 
     204              : impl NeonBroker {
     205            0 :     pub fn client_url(&self) -> Url {
     206            0 :         Url::parse(&format!("http://{}", self.listen_addr)).expect("failed to construct url")
     207            0 :     }
     208              : }
     209              : 
     210              : // neon_local needs to know this subset of pageserver configuration.
     211              : // For legacy reasons, this information is duplicated from `pageserver.toml` into `.neon/config`.
     212              : // It can get stale if `pageserver.toml` is changed.
     213              : // TODO(christian): don't store this at all in `.neon/config`, always load it from `pageserver.toml`
     214            0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
     215              : #[serde(default, deny_unknown_fields)]
     216              : pub struct PageServerConf {
     217              :     pub id: NodeId,
     218              :     pub listen_pg_addr: String,
     219              :     pub listen_http_addr: String,
     220              :     pub pg_auth_type: AuthType,
     221              :     pub http_auth_type: AuthType,
     222              :     pub no_sync: bool,
     223              : }
     224              : 
     225              : impl Default for PageServerConf {
     226            0 :     fn default() -> Self {
     227            0 :         Self {
     228            0 :             id: NodeId(0),
     229            0 :             listen_pg_addr: String::new(),
     230            0 :             listen_http_addr: String::new(),
     231            0 :             pg_auth_type: AuthType::Trust,
     232            0 :             http_auth_type: AuthType::Trust,
     233            0 :             no_sync: false,
     234            0 :         }
     235            0 :     }
     236              : }
     237              : 
     238              : /// The toml that can be passed to `neon_local init --config`.
     239              : /// This is a subset of the `pageserver.toml` configuration.
     240              : // TODO(christian): use pageserver_api::config::ConfigToml (PR #7656)
     241            0 : #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
     242              : pub struct NeonLocalInitPageserverConf {
     243              :     pub id: NodeId,
     244              :     pub listen_pg_addr: String,
     245              :     pub listen_http_addr: String,
     246              :     pub pg_auth_type: AuthType,
     247              :     pub http_auth_type: AuthType,
     248              :     #[serde(default, skip_serializing_if = "std::ops::Not::not")]
     249              :     pub no_sync: bool,
     250              :     #[serde(flatten)]
     251              :     pub other: HashMap<String, toml::Value>,
     252              : }
     253              : 
     254              : impl From<&NeonLocalInitPageserverConf> for PageServerConf {
     255            0 :     fn from(conf: &NeonLocalInitPageserverConf) -> Self {
     256            0 :         let NeonLocalInitPageserverConf {
     257            0 :             id,
     258            0 :             listen_pg_addr,
     259            0 :             listen_http_addr,
     260            0 :             pg_auth_type,
     261            0 :             http_auth_type,
     262            0 :             no_sync,
     263            0 :             other: _,
     264            0 :         } = conf;
     265            0 :         Self {
     266            0 :             id: *id,
     267            0 :             listen_pg_addr: listen_pg_addr.clone(),
     268            0 :             listen_http_addr: listen_http_addr.clone(),
     269            0 :             pg_auth_type: *pg_auth_type,
     270            0 :             http_auth_type: *http_auth_type,
     271            0 :             no_sync: *no_sync,
     272            0 :         }
     273            0 :     }
     274              : }
     275              : 
     276            0 : #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
     277              : #[serde(default)]
     278              : pub struct SafekeeperConf {
     279              :     pub id: NodeId,
     280              :     pub pg_port: u16,
     281              :     pub pg_tenant_only_port: Option<u16>,
     282              :     pub http_port: u16,
     283              :     pub sync: bool,
     284              :     pub remote_storage: Option<String>,
     285              :     pub backup_threads: Option<u32>,
     286              :     pub auth_enabled: bool,
     287              :     pub listen_addr: Option<String>,
     288              : }
     289              : 
     290              : impl Default for SafekeeperConf {
     291            0 :     fn default() -> Self {
     292            0 :         Self {
     293            0 :             id: NodeId(0),
     294            0 :             pg_port: 0,
     295            0 :             pg_tenant_only_port: None,
     296            0 :             http_port: 0,
     297            0 :             sync: true,
     298            0 :             remote_storage: None,
     299            0 :             backup_threads: None,
     300            0 :             auth_enabled: false,
     301            0 :             listen_addr: None,
     302            0 :         }
     303            0 :     }
     304              : }
     305              : 
     306              : #[derive(Clone, Copy)]
     307              : pub enum InitForceMode {
     308              :     MustNotExist,
     309              :     EmptyDirOk,
     310              :     RemoveAllContents,
     311              : }
     312              : 
     313              : impl ValueEnum for InitForceMode {
     314            0 :     fn value_variants<'a>() -> &'a [Self] {
     315            0 :         &[
     316            0 :             Self::MustNotExist,
     317            0 :             Self::EmptyDirOk,
     318            0 :             Self::RemoveAllContents,
     319            0 :         ]
     320            0 :     }
     321              : 
     322            0 :     fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
     323            0 :         Some(clap::builder::PossibleValue::new(match self {
     324            0 :             InitForceMode::MustNotExist => "must-not-exist",
     325            0 :             InitForceMode::EmptyDirOk => "empty-dir-ok",
     326            0 :             InitForceMode::RemoveAllContents => "remove-all-contents",
     327              :         }))
     328            0 :     }
     329              : }
     330              : 
     331              : impl SafekeeperConf {
     332              :     /// Compute is served by port on which only tenant scoped tokens allowed, if
     333              :     /// it is configured.
     334            0 :     pub fn get_compute_port(&self) -> u16 {
     335            0 :         self.pg_tenant_only_port.unwrap_or(self.pg_port)
     336            0 :     }
     337              : }
     338              : 
     339              : impl LocalEnv {
     340            0 :     pub fn pg_distrib_dir_raw(&self) -> PathBuf {
     341            0 :         self.pg_distrib_dir.clone()
     342            0 :     }
     343              : 
     344            0 :     pub fn pg_distrib_dir(&self, pg_version: u32) -> anyhow::Result<PathBuf> {
     345            0 :         let path = self.pg_distrib_dir.clone();
     346            0 : 
     347            0 :         #[allow(clippy::manual_range_patterns)]
     348            0 :         match pg_version {
     349            0 :             14 | 15 | 16 | 17 => Ok(path.join(format!("v{pg_version}"))),
     350            0 :             _ => bail!("Unsupported postgres version: {}", pg_version),
     351              :         }
     352            0 :     }
     353              : 
     354            0 :     pub fn pg_dir(&self, pg_version: u32, dir_name: &str) -> anyhow::Result<PathBuf> {
     355            0 :         Ok(self.pg_distrib_dir(pg_version)?.join(dir_name))
     356            0 :     }
     357              : 
     358            0 :     pub fn pg_bin_dir(&self, pg_version: u32) -> anyhow::Result<PathBuf> {
     359            0 :         self.pg_dir(pg_version, "bin")
     360            0 :     }
     361              : 
     362            0 :     pub fn pg_lib_dir(&self, pg_version: u32) -> anyhow::Result<PathBuf> {
     363            0 :         self.pg_dir(pg_version, "lib")
     364            0 :     }
     365              : 
     366            0 :     pub fn pageserver_bin(&self) -> PathBuf {
     367            0 :         self.neon_distrib_dir.join("pageserver")
     368            0 :     }
     369              : 
     370            0 :     pub fn storage_controller_bin(&self) -> PathBuf {
     371            0 :         // Irrespective of configuration, storage controller binary is always
     372            0 :         // run from the same location as neon_local.  This means that for compatibility
     373            0 :         // tests that run old pageserver/safekeeper, they still run latest storage controller.
     374            0 :         let neon_local_bin_dir = env::current_exe().unwrap().parent().unwrap().to_owned();
     375            0 :         neon_local_bin_dir.join("storage_controller")
     376            0 :     }
     377              : 
     378            0 :     pub fn safekeeper_bin(&self) -> PathBuf {
     379            0 :         self.neon_distrib_dir.join("safekeeper")
     380            0 :     }
     381              : 
     382            0 :     pub fn storage_broker_bin(&self) -> PathBuf {
     383            0 :         self.neon_distrib_dir.join("storage_broker")
     384            0 :     }
     385              : 
     386            0 :     pub fn endpoints_path(&self) -> PathBuf {
     387            0 :         self.base_data_dir.join("endpoints")
     388            0 :     }
     389              : 
     390            0 :     pub fn pageserver_data_dir(&self, pageserver_id: NodeId) -> PathBuf {
     391            0 :         self.base_data_dir
     392            0 :             .join(format!("pageserver_{pageserver_id}"))
     393            0 :     }
     394              : 
     395            0 :     pub fn safekeeper_data_dir(&self, data_dir_name: &str) -> PathBuf {
     396            0 :         self.base_data_dir.join("safekeepers").join(data_dir_name)
     397            0 :     }
     398              : 
     399            0 :     pub fn get_pageserver_conf(&self, id: NodeId) -> anyhow::Result<&PageServerConf> {
     400            0 :         if let Some(conf) = self.pageservers.iter().find(|node| node.id == id) {
     401            0 :             Ok(conf)
     402              :         } else {
     403            0 :             let have_ids = self
     404            0 :                 .pageservers
     405            0 :                 .iter()
     406            0 :                 .map(|node| format!("{}:{}", node.id, node.listen_http_addr))
     407            0 :                 .collect::<Vec<_>>();
     408            0 :             let joined = have_ids.join(",");
     409            0 :             bail!("could not find pageserver {id}, have ids {joined}")
     410              :         }
     411            0 :     }
     412              : 
     413              :     /// Inspect the base data directory and extract the instance id and instance directory path
     414              :     /// for all storage controller instances
     415            0 :     pub async fn storage_controller_instances(&self) -> std::io::Result<Vec<(u8, PathBuf)>> {
     416            0 :         let mut instances = Vec::default();
     417              : 
     418            0 :         let dir = std::fs::read_dir(self.base_data_dir.clone())?;
     419            0 :         for dentry in dir {
     420            0 :             let dentry = dentry?;
     421            0 :             let is_dir = dentry.metadata()?.is_dir();
     422            0 :             let filename = dentry.file_name().into_string().unwrap();
     423            0 :             let parsed_instance_id = match filename.strip_prefix("storage_controller_") {
     424            0 :                 Some(suffix) => suffix.parse::<u8>().ok(),
     425            0 :                 None => None,
     426              :             };
     427              : 
     428            0 :             let is_instance_dir = is_dir && parsed_instance_id.is_some();
     429              : 
     430            0 :             if !is_instance_dir {
     431            0 :                 continue;
     432            0 :             }
     433            0 : 
     434            0 :             instances.push((
     435            0 :                 parsed_instance_id.expect("Checked previously"),
     436            0 :                 dentry.path(),
     437            0 :             ));
     438              :         }
     439              : 
     440            0 :         Ok(instances)
     441            0 :     }
     442              : 
     443            0 :     pub fn register_branch_mapping(
     444            0 :         &mut self,
     445            0 :         branch_name: String,
     446            0 :         tenant_id: TenantId,
     447            0 :         timeline_id: TimelineId,
     448            0 :     ) -> anyhow::Result<()> {
     449            0 :         let existing_values = self
     450            0 :             .branch_name_mappings
     451            0 :             .entry(branch_name.clone())
     452            0 :             .or_default();
     453            0 : 
     454            0 :         let existing_ids = existing_values
     455            0 :             .iter()
     456            0 :             .find(|(existing_tenant_id, _)| existing_tenant_id == &tenant_id);
     457              : 
     458            0 :         if let Some((_, old_timeline_id)) = existing_ids {
     459            0 :             if old_timeline_id == &timeline_id {
     460            0 :                 Ok(())
     461              :             } else {
     462            0 :                 bail!(
     463            0 :                     "branch '{branch_name}' is already mapped to timeline {old_timeline_id}, cannot map to another timeline {timeline_id}"
     464            0 :                 );
     465              :             }
     466              :         } else {
     467            0 :             existing_values.push((tenant_id, timeline_id));
     468            0 :             Ok(())
     469              :         }
     470            0 :     }
     471              : 
     472            0 :     pub fn get_branch_timeline_id(
     473            0 :         &self,
     474            0 :         branch_name: &str,
     475            0 :         tenant_id: TenantId,
     476            0 :     ) -> Option<TimelineId> {
     477            0 :         self.branch_name_mappings
     478            0 :             .get(branch_name)?
     479            0 :             .iter()
     480            0 :             .find(|(mapped_tenant_id, _)| mapped_tenant_id == &tenant_id)
     481            0 :             .map(|&(_, timeline_id)| timeline_id)
     482            0 :     }
     483              : 
     484            0 :     pub fn timeline_name_mappings(&self) -> HashMap<TenantTimelineId, String> {
     485            0 :         self.branch_name_mappings
     486            0 :             .iter()
     487            0 :             .flat_map(|(name, tenant_timelines)| {
     488            0 :                 tenant_timelines.iter().map(|&(tenant_id, timeline_id)| {
     489            0 :                     (TenantTimelineId::new(tenant_id, timeline_id), name.clone())
     490            0 :                 })
     491            0 :             })
     492            0 :             .collect()
     493            0 :     }
     494              : 
     495              :     ///  Construct `Self` from on-disk state.
     496            0 :     pub fn load_config(repopath: &Path) -> anyhow::Result<Self> {
     497            0 :         if !repopath.exists() {
     498            0 :             bail!(
     499            0 :                 "Neon config is not found in {}. You need to run 'neon_local init' first",
     500            0 :                 repopath.to_str().unwrap()
     501            0 :             );
     502            0 :         }
     503              : 
     504              :         // TODO: check that it looks like a neon repository
     505              : 
     506              :         // load and parse file
     507            0 :         let config_file_contents = fs::read_to_string(repopath.join("config"))?;
     508            0 :         let on_disk_config: OnDiskConfig = toml::from_str(config_file_contents.as_str())?;
     509            0 :         let mut env = {
     510            0 :             let OnDiskConfig {
     511            0 :                 pg_distrib_dir,
     512            0 :                 neon_distrib_dir,
     513            0 :                 default_tenant_id,
     514            0 :                 private_key_path,
     515            0 :                 broker,
     516            0 :                 storage_controller,
     517            0 :                 pageservers,
     518            0 :                 safekeepers,
     519            0 :                 control_plane_api,
     520            0 :                 control_plane_compute_hook_api,
     521            0 :                 branch_name_mappings,
     522            0 :             } = on_disk_config;
     523            0 :             LocalEnv {
     524            0 :                 base_data_dir: repopath.to_owned(),
     525            0 :                 pg_distrib_dir,
     526            0 :                 neon_distrib_dir,
     527            0 :                 default_tenant_id,
     528            0 :                 private_key_path,
     529            0 :                 broker,
     530            0 :                 storage_controller,
     531            0 :                 pageservers,
     532            0 :                 safekeepers,
     533            0 :                 control_plane_api: control_plane_api.unwrap(),
     534            0 :                 control_plane_compute_hook_api,
     535            0 :                 branch_name_mappings,
     536            0 :             }
     537            0 :         };
     538            0 : 
     539            0 :         // The source of truth for pageserver configuration is the pageserver.toml.
     540            0 :         assert!(
     541            0 :             env.pageservers.is_empty(),
     542            0 :             "we ensure this during deserialization"
     543              :         );
     544            0 :         env.pageservers = {
     545            0 :             let iter = std::fs::read_dir(repopath).context("open dir")?;
     546            0 :             let mut pageservers = Vec::new();
     547            0 :             for res in iter {
     548            0 :                 let dentry = res?;
     549              :                 const PREFIX: &str = "pageserver_";
     550            0 :                 let dentry_name = dentry
     551            0 :                     .file_name()
     552            0 :                     .into_string()
     553            0 :                     .ok()
     554            0 :                     .with_context(|| format!("non-utf8 dentry: {:?}", dentry.path()))
     555            0 :                     .unwrap();
     556            0 :                 if !dentry_name.starts_with(PREFIX) {
     557            0 :                     continue;
     558            0 :                 }
     559            0 :                 if !dentry.file_type().context("determine file type")?.is_dir() {
     560            0 :                     anyhow::bail!("expected a directory, got {:?}", dentry.path());
     561            0 :                 }
     562            0 :                 let id = dentry_name[PREFIX.len()..]
     563            0 :                     .parse::<NodeId>()
     564            0 :                     .with_context(|| format!("parse id from {:?}", dentry.path()))?;
     565              :                 // TODO(christian): use pageserver_api::config::ConfigToml (PR #7656)
     566            0 :                 #[derive(serde::Serialize, serde::Deserialize)]
     567              :                 // (allow unknown fields, unlike PageServerConf)
     568              :                 struct PageserverConfigTomlSubset {
     569              :                     listen_pg_addr: String,
     570              :                     listen_http_addr: String,
     571              :                     pg_auth_type: AuthType,
     572              :                     http_auth_type: AuthType,
     573              :                     #[serde(default)]
     574              :                     no_sync: bool,
     575              :                 }
     576            0 :                 let config_toml_path = dentry.path().join("pageserver.toml");
     577            0 :                 let config_toml: PageserverConfigTomlSubset = toml_edit::de::from_str(
     578            0 :                     &std::fs::read_to_string(&config_toml_path)
     579            0 :                         .with_context(|| format!("read {:?}", config_toml_path))?,
     580              :                 )
     581            0 :                 .context("parse pageserver.toml")?;
     582            0 :                 let identity_toml_path = dentry.path().join("identity.toml");
     583            0 :                 #[derive(serde::Serialize, serde::Deserialize)]
     584              :                 struct IdentityTomlSubset {
     585              :                     id: NodeId,
     586              :                 }
     587            0 :                 let identity_toml: IdentityTomlSubset = toml_edit::de::from_str(
     588            0 :                     &std::fs::read_to_string(&identity_toml_path)
     589            0 :                         .with_context(|| format!("read {:?}", identity_toml_path))?,
     590              :                 )
     591            0 :                 .context("parse identity.toml")?;
     592              :                 let PageserverConfigTomlSubset {
     593            0 :                     listen_pg_addr,
     594            0 :                     listen_http_addr,
     595            0 :                     pg_auth_type,
     596            0 :                     http_auth_type,
     597            0 :                     no_sync,
     598            0 :                 } = config_toml;
     599            0 :                 let IdentityTomlSubset {
     600            0 :                     id: identity_toml_id,
     601            0 :                 } = identity_toml;
     602            0 :                 let conf = PageServerConf {
     603              :                     id: {
     604            0 :                         anyhow::ensure!(
     605            0 :                             identity_toml_id == id,
     606            0 :                             "id mismatch: identity.toml:id={identity_toml_id} pageserver_(.*) id={id}",
     607              :                         );
     608            0 :                         id
     609            0 :                     },
     610            0 :                     listen_pg_addr,
     611            0 :                     listen_http_addr,
     612            0 :                     pg_auth_type,
     613            0 :                     http_auth_type,
     614            0 :                     no_sync,
     615            0 :                 };
     616            0 :                 pageservers.push(conf);
     617              :             }
     618            0 :             pageservers
     619            0 :         };
     620            0 : 
     621            0 :         Ok(env)
     622            0 :     }
     623              : 
     624            0 :     pub fn persist_config(&self) -> anyhow::Result<()> {
     625            0 :         Self::persist_config_impl(
     626            0 :             &self.base_data_dir,
     627            0 :             &OnDiskConfig {
     628            0 :                 pg_distrib_dir: self.pg_distrib_dir.clone(),
     629            0 :                 neon_distrib_dir: self.neon_distrib_dir.clone(),
     630            0 :                 default_tenant_id: self.default_tenant_id,
     631            0 :                 private_key_path: self.private_key_path.clone(),
     632            0 :                 broker: self.broker.clone(),
     633            0 :                 storage_controller: self.storage_controller.clone(),
     634            0 :                 pageservers: vec![], // it's skip_serializing anyway
     635            0 :                 safekeepers: self.safekeepers.clone(),
     636            0 :                 control_plane_api: Some(self.control_plane_api.clone()),
     637            0 :                 control_plane_compute_hook_api: self.control_plane_compute_hook_api.clone(),
     638            0 :                 branch_name_mappings: self.branch_name_mappings.clone(),
     639            0 :             },
     640            0 :         )
     641            0 :     }
     642              : 
     643            0 :     pub fn persist_config_impl(base_path: &Path, config: &OnDiskConfig) -> anyhow::Result<()> {
     644            0 :         let conf_content = &toml::to_string_pretty(config)?;
     645            0 :         let target_config_path = base_path.join("config");
     646            0 :         fs::write(&target_config_path, conf_content).with_context(|| {
     647            0 :             format!(
     648            0 :                 "Failed to write config file into path '{}'",
     649            0 :                 target_config_path.display()
     650            0 :             )
     651            0 :         })
     652            0 :     }
     653              : 
     654              :     // this function is used only for testing purposes in CLI e g generate tokens during init
     655            0 :     pub fn generate_auth_token(&self, claims: &Claims) -> anyhow::Result<String> {
     656            0 :         let private_key_path = self.get_private_key_path();
     657            0 :         let key_data = fs::read(private_key_path)?;
     658            0 :         encode_from_key_file(claims, &key_data)
     659            0 :     }
     660              : 
     661            0 :     pub fn get_private_key_path(&self) -> PathBuf {
     662            0 :         if self.private_key_path.is_absolute() {
     663            0 :             self.private_key_path.to_path_buf()
     664              :         } else {
     665            0 :             self.base_data_dir.join(&self.private_key_path)
     666              :         }
     667            0 :     }
     668              : 
     669              :     /// Materialize the [`NeonLocalInitConf`] to disk. Called during [`neon_local init`].
     670            0 :     pub fn init(conf: NeonLocalInitConf, force: &InitForceMode) -> anyhow::Result<()> {
     671            0 :         let base_path = base_path();
     672            0 :         assert_ne!(base_path, Path::new(""));
     673            0 :         let base_path = &base_path;
     674            0 : 
     675            0 :         // create base_path dir
     676            0 :         if base_path.exists() {
     677            0 :             match force {
     678              :                 InitForceMode::MustNotExist => {
     679            0 :                     bail!(
     680            0 :                         "directory '{}' already exists. Perhaps already initialized?",
     681            0 :                         base_path.display()
     682            0 :                     );
     683              :                 }
     684              :                 InitForceMode::EmptyDirOk => {
     685            0 :                     if let Some(res) = std::fs::read_dir(base_path)?.next() {
     686            0 :                         res.context("check if directory is empty")?;
     687            0 :                         anyhow::bail!("directory not empty: {base_path:?}");
     688            0 :                     }
     689              :                 }
     690              :                 InitForceMode::RemoveAllContents => {
     691            0 :                     println!("removing all contents of '{}'", base_path.display());
     692              :                     // instead of directly calling `remove_dir_all`, we keep the original dir but removing
     693              :                     // all contents inside. This helps if the developer symbol links another directory (i.e.,
     694              :                     // S3 local SSD) to the `.neon` base directory.
     695            0 :                     for entry in std::fs::read_dir(base_path)? {
     696            0 :                         let entry = entry?;
     697            0 :                         let path = entry.path();
     698            0 :                         if path.is_dir() {
     699            0 :                             fs::remove_dir_all(&path)?;
     700              :                         } else {
     701            0 :                             fs::remove_file(&path)?;
     702              :                         }
     703              :                     }
     704              :                 }
     705              :             }
     706            0 :         }
     707            0 :         if !base_path.exists() {
     708            0 :             fs::create_dir(base_path)?;
     709            0 :         }
     710              : 
     711              :         let NeonLocalInitConf {
     712            0 :             pg_distrib_dir,
     713            0 :             neon_distrib_dir,
     714            0 :             default_tenant_id,
     715            0 :             broker,
     716            0 :             storage_controller,
     717            0 :             pageservers,
     718            0 :             safekeepers,
     719            0 :             control_plane_api,
     720            0 :             control_plane_compute_hook_api,
     721            0 :         } = conf;
     722            0 : 
     723            0 :         // Find postgres binaries.
     724            0 :         // Follow POSTGRES_DISTRIB_DIR if set, otherwise look in "pg_install".
     725            0 :         // Note that later in the code we assume, that distrib dirs follow the same pattern
     726            0 :         // for all postgres versions.
     727            0 :         let pg_distrib_dir = pg_distrib_dir.unwrap_or_else(|| {
     728            0 :             if let Some(postgres_bin) = env::var_os("POSTGRES_DISTRIB_DIR") {
     729            0 :                 postgres_bin.into()
     730              :             } else {
     731            0 :                 let cwd = env::current_dir().unwrap();
     732            0 :                 cwd.join("pg_install")
     733              :             }
     734            0 :         });
     735            0 : 
     736            0 :         // Find neon binaries.
     737            0 :         let neon_distrib_dir = neon_distrib_dir
     738            0 :             .unwrap_or_else(|| env::current_exe().unwrap().parent().unwrap().to_owned());
     739            0 : 
     740            0 :         // Generate keypair for JWT.
     741            0 :         //
     742            0 :         // The keypair is only needed if authentication is enabled in any of the
     743            0 :         // components. For convenience, we generate the keypair even if authentication
     744            0 :         // is not enabled, so that you can easily enable it after the initialization
     745            0 :         // step.
     746            0 :         generate_auth_keys(
     747            0 :             base_path.join("auth_private_key.pem").as_path(),
     748            0 :             base_path.join("auth_public_key.pem").as_path(),
     749            0 :         )
     750            0 :         .context("generate auth keys")?;
     751            0 :         let private_key_path = PathBuf::from("auth_private_key.pem");
     752            0 : 
     753            0 :         // create the runtime type because the remaining initialization code below needs
     754            0 :         // a LocalEnv instance op operation
     755            0 :         // TODO: refactor to avoid this, LocalEnv should only be constructed from on-disk state
     756            0 :         let env = LocalEnv {
     757            0 :             base_data_dir: base_path.clone(),
     758            0 :             pg_distrib_dir,
     759            0 :             neon_distrib_dir,
     760            0 :             default_tenant_id: Some(default_tenant_id),
     761            0 :             private_key_path,
     762            0 :             broker,
     763            0 :             storage_controller: storage_controller.unwrap_or_default(),
     764            0 :             pageservers: pageservers.iter().map(Into::into).collect(),
     765            0 :             safekeepers,
     766            0 :             control_plane_api: control_plane_api.unwrap(),
     767            0 :             control_plane_compute_hook_api: control_plane_compute_hook_api.unwrap_or_default(),
     768            0 :             branch_name_mappings: Default::default(),
     769            0 :         };
     770            0 : 
     771            0 :         // create endpoints dir
     772            0 :         fs::create_dir_all(env.endpoints_path())?;
     773              : 
     774              :         // create safekeeper dirs
     775            0 :         for safekeeper in &env.safekeepers {
     776            0 :             fs::create_dir_all(SafekeeperNode::datadir_path_by_id(&env, safekeeper.id))?;
     777              :         }
     778              : 
     779              :         // initialize pageserver state
     780            0 :         for (i, ps) in pageservers.into_iter().enumerate() {
     781            0 :             let runtime_ps = &env.pageservers[i];
     782            0 :             assert_eq!(&PageServerConf::from(&ps), runtime_ps);
     783            0 :             fs::create_dir(env.pageserver_data_dir(ps.id))?;
     784            0 :             PageServerNode::from_env(&env, runtime_ps)
     785            0 :                 .initialize(ps)
     786            0 :                 .context("pageserver init failed")?;
     787              :         }
     788              : 
     789              :         // setup remote remote location for default LocalFs remote storage
     790            0 :         std::fs::create_dir_all(env.base_data_dir.join(PAGESERVER_REMOTE_STORAGE_DIR))?;
     791              : 
     792            0 :         env.persist_config()
     793            0 :     }
     794              : }
     795              : 
     796            0 : pub fn base_path() -> PathBuf {
     797            0 :     let path = match std::env::var_os("NEON_REPO_DIR") {
     798            0 :         Some(val) => {
     799            0 :             let path = PathBuf::from(val);
     800            0 :             if !path.is_absolute() {
     801              :                 // repeat the env var in the error because our default is always absolute
     802            0 :                 panic!("NEON_REPO_DIR must be an absolute path, got {path:?}");
     803            0 :             }
     804            0 :             path
     805              :         }
     806              :         None => {
     807            0 :             let pwd = std::env::current_dir()
     808            0 :                 // technically this can fail but it's quite unlikeley
     809            0 :                 .expect("determine current directory");
     810            0 :             let pwd_abs = pwd.canonicalize().expect("canonicalize current directory");
     811            0 :             pwd_abs.join(".neon")
     812              :         }
     813              :     };
     814            0 :     assert!(path.is_absolute());
     815            0 :     path
     816            0 : }
     817              : 
     818              : /// Generate a public/private key pair for JWT authentication
     819            0 : fn generate_auth_keys(private_key_path: &Path, public_key_path: &Path) -> anyhow::Result<()> {
     820              :     // Generate the key pair
     821              :     //
     822              :     // openssl genpkey -algorithm ed25519 -out auth_private_key.pem
     823            0 :     let keygen_output = Command::new("openssl")
     824            0 :         .arg("genpkey")
     825            0 :         .args(["-algorithm", "ed25519"])
     826            0 :         .args(["-out", private_key_path.to_str().unwrap()])
     827            0 :         .stdout(Stdio::null())
     828            0 :         .output()
     829            0 :         .context("failed to generate auth private key")?;
     830            0 :     if !keygen_output.status.success() {
     831            0 :         bail!(
     832            0 :             "openssl failed: '{}'",
     833            0 :             String::from_utf8_lossy(&keygen_output.stderr)
     834            0 :         );
     835            0 :     }
     836              :     // Extract the public key from the private key file
     837              :     //
     838              :     // openssl pkey -in auth_private_key.pem -pubout -out auth_public_key.pem
     839            0 :     let keygen_output = Command::new("openssl")
     840            0 :         .arg("pkey")
     841            0 :         .args(["-in", private_key_path.to_str().unwrap()])
     842            0 :         .arg("-pubout")
     843            0 :         .args(["-out", public_key_path.to_str().unwrap()])
     844            0 :         .output()
     845            0 :         .context("failed to extract public key from private key")?;
     846            0 :     if !keygen_output.status.success() {
     847            0 :         bail!(
     848            0 :             "openssl failed: '{}'",
     849            0 :             String::from_utf8_lossy(&keygen_output.stderr)
     850            0 :         );
     851            0 :     }
     852            0 :     Ok(())
     853            0 : }
        

Generated by: LCOV version 2.1-beta