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