1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::convert::TryInto;
use std::io::BufReader;
use std::io::Lines;
use std::path::Path;
use std::process::Child;
use std::process::ChildStderr;
use std::process::ChildStdout;
use std::process::Command;
use std::process::Stdio;
use std::sync::Arc;
use nix::sys::signal;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
use tempdir::TempDir;
use tracing::{debug, instrument};
use crate::errors::{ProcessCapture, TmpPostgrustError, TmpPostgrustResult};
use crate::search::find_postgresql_command;
#[instrument(skip(command, fail))]
fn exec_process(
command: &mut Command,
fail: impl FnOnce(ProcessCapture) -> TmpPostgrustError,
) -> TmpPostgrustResult<()> {
debug!("running command: {:?}", command);
let output = command
.output()
.map_err(|err| TmpPostgrustError::ExecSubprocessFailed {
source: err,
command: format!("{:?}", command),
})?;
if output.status.success() {
for line in String::from_utf8(output.stdout).unwrap().lines() {
debug!("{}", line);
}
Ok(())
} else {
Err(fail(ProcessCapture {
stdout: String::from_utf8(output.stdout).unwrap(),
stderr: String::from_utf8(output.stderr).unwrap(),
}))
}
}
#[instrument]
pub(crate) fn start_postgres_subprocess(
data_directory: &'_ Path,
port: u32,
) -> TmpPostgrustResult<Child> {
let postgres_path =
find_postgresql_command("bin", "postgres").expect("failed to find postgres");
Command::new(postgres_path)
.env("PGDATA", data_directory.to_str().unwrap())
.arg("-p")
.arg(port.to_string())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(TmpPostgrustError::SpawnSubprocessFailed)
}
#[instrument]
pub(crate) fn exec_init_db(data_directory: &'_ Path) -> TmpPostgrustResult<()> {
let initdb_path = find_postgresql_command("bin", "initdb").expect("failed to find initdb");
debug!("Initializing database in: {:?}", data_directory);
exec_process(
&mut Command::new(initdb_path)
.env("PGDATA", data_directory.to_str().unwrap())
.arg("--username=postgres"),
TmpPostgrustError::InitDBFailed,
)
}
#[instrument]
pub(crate) fn exec_copy_dir(src_dir: &'_ Path, dst_dir: &'_ Path) -> TmpPostgrustResult<()> {
for read_dir in src_dir
.read_dir()
.map_err(TmpPostgrustError::CopyCachedInitDBFailedFileNotFound)?
{
let mut cmd = Command::new("cp");
#[cfg(target_os = "macos")]
cmd.arg("-R")
.arg("-c")
.arg(
read_dir
.map_err(TmpPostgrustError::CopyCachedInitDBFailedFileNotFound)?
.path(),
)
.arg(dst_dir);
#[cfg(not(target_os = "macos"))]
cmd.arg("-R")
.arg("--reflink=auto")
.arg(
read_dir
.map_err(TmpPostgrustError::CopyCachedInitDBFailedFileNotFound)?
.path(),
)
.arg(dst_dir);
exec_process(&mut cmd, TmpPostgrustError::CopyCachedInitDBFailed)?;
}
Ok(())
}
#[instrument]
pub(crate) fn exec_create_db(
socket: &'_ Path,
port: u32,
owner: &'_ str,
dbname: &'_ str,
) -> TmpPostgrustResult<()> {
exec_process(
&mut Command::new("createdb")
.arg("-h")
.arg(socket)
.arg("-p")
.arg(port.to_string())
.arg("-U")
.arg("postgres")
.arg("-O")
.arg(owner)
.arg("--echo")
.arg(dbname),
TmpPostgrustError::CreateDBFailed,
)
}
#[instrument]
pub(crate) fn exec_create_user(
socket: &'_ Path,
port: u32,
username: &'_ str,
) -> TmpPostgrustResult<()> {
exec_process(
&mut Command::new("createuser")
.arg("-h")
.arg(socket)
.arg("-p")
.arg(port.to_string())
.arg("-U")
.arg("postgres")
.arg("--superuser")
.arg("--echo")
.arg(username),
TmpPostgrustError::CreateDBFailed,
)
}
pub struct ProcessGuard {
pub stdout_reader: Option<Lines<BufReader<ChildStdout>>>,
pub stderr_reader: Option<Lines<BufReader<ChildStderr>>>,
pub connection_string: String,
pub(crate) postgres_process: Child,
pub(crate) _data_directory: TempDir,
pub(crate) _socket_dir: Arc<TempDir>,
}
impl Drop for ProcessGuard {
fn drop(&mut self) {
signal::kill(
Pid::from_raw(self.postgres_process.id().try_into().unwrap()),
Signal::SIGINT,
)
.unwrap();
self.postgres_process.wait().unwrap();
}
}