Line data    Source code 
       1              : use std::fs::{File, OpenOptions};
       2              : use std::io;
       3              : use std::io::prelude::*;
       4              : use std::path::Path;
       5              : 
       6              : use anyhow::Result;
       7              : 
       8              : use crate::pg_helpers::escape_conf_value;
       9              : use crate::pg_helpers::PgOptionsSerialize;
      10              : use compute_api::spec::{ComputeMode, ComputeSpec};
      11              : 
      12              : /// Check that `line` is inside a text file and put it there if it is not.
      13              : /// Create file if it doesn't exist.
      14            6 : pub fn line_in_file(path: &Path, line: &str) -> Result<bool> {
      15            6 :     let mut file = OpenOptions::new()
      16            6 :         .read(true)
      17            6 :         .write(true)
      18            6 :         .create(true)
      19            6 :         .append(false)
      20            6 :         .open(path)?;
      21            6 :     let buf = io::BufReader::new(&file);
      22            6 :     let mut count: usize = 0;
      23              : 
      24           10 :     for l in buf.lines() {
      25           10 :         if l? == line {
      26            2 :             return Ok(false);
      27            8 :         }
      28            8 :         count = 1;
      29              :     }
      30              : 
      31            4 :     write!(file, "{}{}", "\n".repeat(count), line)?;
      32            4 :     Ok(true)
      33            6 : }
      34              : 
      35              : /// Create or completely rewrite configuration file specified by `path`
      36            0 : pub fn write_postgres_conf(
      37            0 :     path: &Path,
      38            0 :     spec: &ComputeSpec,
      39            0 :     extension_server_port: Option<u16>,
      40            0 : ) -> Result<()> {
      41              :     // File::create() destroys the file content if it exists.
      42            0 :     let mut file = File::create(path)?;
      43              : 
      44              :     // Write the postgresql.conf content from the spec file as is.
      45            0 :     if let Some(conf) = &spec.cluster.postgresql_conf {
      46            0 :         writeln!(file, "{}", conf)?;
      47            0 :     }
      48              : 
      49              :     // Add options for connecting to storage
      50            0 :     writeln!(file, "# Neon storage settings")?;
      51            0 :     if let Some(s) = &spec.pageserver_connstring {
      52            0 :         writeln!(file, "neon.pageserver_connstring={}", escape_conf_value(s))?;
      53            0 :     }
      54            0 :     if let Some(stripe_size) = spec.shard_stripe_size {
      55            0 :         writeln!(file, "neon.stripe_size={stripe_size}")?;
      56            0 :     }
      57            0 :     if !spec.safekeeper_connstrings.is_empty() {
      58            0 :         writeln!(
      59            0 :             file,
      60            0 :             "neon.safekeepers={}",
      61            0 :             escape_conf_value(&spec.safekeeper_connstrings.join(","))
      62            0 :         )?;
      63            0 :     }
      64            0 :     if let Some(s) = &spec.tenant_id {
      65            0 :         writeln!(file, "neon.tenant_id={}", escape_conf_value(&s.to_string()))?;
      66            0 :     }
      67            0 :     if let Some(s) = &spec.timeline_id {
      68            0 :         writeln!(
      69            0 :             file,
      70            0 :             "neon.timeline_id={}",
      71            0 :             escape_conf_value(&s.to_string())
      72            0 :         )?;
      73            0 :     }
      74              : 
      75            0 :     match spec.mode {
      76            0 :         ComputeMode::Primary => {}
      77            0 :         ComputeMode::Static(lsn) => {
      78            0 :             // hot_standby is 'on' by default, but let's be explicit
      79            0 :             writeln!(file, "hot_standby=on")?;
      80            0 :             writeln!(file, "recovery_target_lsn='{lsn}'")?;
      81              :         }
      82              :         ComputeMode::Replica => {
      83              :             // hot_standby is 'on' by default, but let's be explicit
      84            0 :             writeln!(file, "hot_standby=on")?;
      85              :         }
      86              :     }
      87              : 
      88              :     // If there are any extra options in the 'settings' field, append those
      89            0 :     if spec.cluster.settings.is_some() {
      90            0 :         writeln!(file, "# Managed by compute_ctl: begin")?;
      91            0 :         write!(file, "{}", spec.cluster.settings.as_pg_settings())?;
      92            0 :         writeln!(file, "# Managed by compute_ctl: end")?;
      93            0 :     }
      94              : 
      95            0 :     if let Some(port) = extension_server_port {
      96            0 :         writeln!(file, "neon.extension_server_port={}", port)?;
      97            0 :     }
      98              : 
      99              :     // This is essential to keep this line at the end of the file,
     100              :     // because it is intended to override any settings above.
     101            0 :     writeln!(file, "include_if_exists = 'compute_ctl_temp_override.conf'")?;
     102              : 
     103            0 :     Ok(())
     104            0 : }
     105              : 
     106              : /// create file compute_ctl_temp_override.conf in pgdata_dir
     107              : /// add provided options to this file
     108            0 : pub fn compute_ctl_temp_override_create(pgdata_path: &Path, options: &str) -> Result<()> {
     109            0 :     let path = pgdata_path.join("compute_ctl_temp_override.conf");
     110            0 :     let mut file = File::create(path)?;
     111            0 :     write!(file, "{}", options)?;
     112            0 :     Ok(())
     113            0 : }
     114              : 
     115              : /// remove file compute_ctl_temp_override.conf in pgdata_dir
     116            0 : pub fn compute_ctl_temp_override_remove(pgdata_path: &Path) -> Result<()> {
     117            0 :     let path = pgdata_path.join("compute_ctl_temp_override.conf");
     118            0 :     std::fs::remove_file(path)?;
     119            0 :     Ok(())
     120            0 : }
        
               |