-
Notifications
You must be signed in to change notification settings - Fork 34
/
mod.rs
210 lines (194 loc) · 6.39 KB
/
mod.rs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (C) 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.
pub mod auditor;
pub mod daemon;
pub mod faucet;
pub mod local;
pub mod nat_detection;
pub mod node;
use crate::{
helpers::{download_and_extract_release, get_bin_version},
print_banner, VerbosityLevel,
};
use color_eyre::{eyre::eyre, Result};
use colored::Colorize;
use semver::Version;
use sn_releases::{ReleaseType, SafeReleaseRepoActions};
use sn_service_management::UpgradeResult;
use std::{
path::PathBuf,
process::{Command, Stdio},
};
#[cfg(unix)]
pub fn is_running_as_root() -> bool {
users::get_effective_uid() == 0
}
#[cfg(windows)]
pub fn is_running_as_root() -> bool {
true
}
pub async fn download_and_get_upgrade_bin_path(
custom_bin_path: Option<PathBuf>,
release_type: ReleaseType,
url: Option<String>,
version: Option<String>,
verbosity: VerbosityLevel,
) -> Result<(PathBuf, Version)> {
if let Some(path) = custom_bin_path {
println!(
"Using the supplied custom binary at {}",
path.to_string_lossy()
);
let bin_version = get_bin_version(&path)?;
return Ok((path, bin_version.parse()?));
}
let release_repo = <dyn SafeReleaseRepoActions>::default_config();
if let Some(version) = version {
let (upgrade_bin_path, version) = download_and_extract_release(
release_type,
None,
Some(version),
&*release_repo,
verbosity,
None,
)
.await?;
Ok((upgrade_bin_path, Version::parse(&version)?))
} else if let Some(url) = url {
let (upgrade_bin_path, version) = download_and_extract_release(
release_type,
Some(url),
None,
&*release_repo,
verbosity,
None,
)
.await?;
Ok((upgrade_bin_path, Version::parse(&version)?))
} else {
println!("Retrieving latest version of {}...", release_type);
let latest_version = release_repo.get_latest_version(&release_type).await?;
println!("Latest version is {latest_version}");
let (upgrade_bin_path, _) = download_and_extract_release(
release_type,
None,
Some(latest_version.to_string()),
&*release_repo,
verbosity,
None,
)
.await?;
Ok((upgrade_bin_path, latest_version))
}
}
pub fn print_upgrade_summary(upgrade_summary: Vec<(String, UpgradeResult)>) {
println!("Upgrade summary:");
for (service_name, upgrade_result) in upgrade_summary {
match upgrade_result {
UpgradeResult::NotRequired => {
println!("- {} did not require an upgrade", service_name);
}
UpgradeResult::Upgraded(previous_version, new_version) => {
println!(
"{} {} upgraded from {previous_version} to {new_version}",
"✓".green(),
service_name
);
}
UpgradeResult::UpgradedButNotStarted(previous_version, new_version, _) => {
println!(
"{} {} was upgraded from {previous_version} to {new_version} but it did not start",
"✕".red(),
service_name
);
}
UpgradeResult::Forced(previous_version, target_version) => {
println!(
"{} Forced {} version change from {previous_version} to {target_version}.",
"✓".green(),
service_name
);
}
UpgradeResult::Error(msg) => {
println!("{} {} was not upgraded: {}", "✕".red(), service_name, msg);
}
}
}
}
pub async fn get_bin_path(
build: bool,
path: Option<PathBuf>,
release_type: ReleaseType,
version: Option<String>,
release_repo: &dyn SafeReleaseRepoActions,
verbosity: VerbosityLevel,
) -> Result<PathBuf> {
if build {
build_binary(&release_type)?;
Ok(PathBuf::from("target")
.join("release")
.join(release_type.to_string()))
} else if let Some(path) = path {
Ok(path)
} else {
let (download_path, _) = download_and_extract_release(
release_type,
None,
version,
release_repo,
verbosity,
None,
)
.await?;
Ok(download_path)
}
}
fn build_binary(bin_type: &ReleaseType) -> Result<()> {
let mut args = vec!["build", "--release"];
let bin_name = bin_type.to_string();
args.push("--bin");
args.push(&bin_name);
// Keep features consistent to avoid recompiling.
if cfg!(feature = "chaos") {
println!("*** Building testnet with CHAOS enabled. Watch out. ***");
args.push("--features");
args.push("chaos");
}
if cfg!(feature = "statemap") {
args.extend(["--features", "statemap"]);
}
if cfg!(feature = "otlp") {
args.extend(["--features", "otlp"]);
}
if cfg!(feature = "local-discovery") {
args.extend(["--features", "local-discovery"]);
}
if cfg!(feature = "network-contacts") {
args.extend(["--features", "network-contacts"]);
}
if cfg!(feature = "websockets") {
args.extend(["--features", "websockets"]);
}
if cfg!(feature = "open-metrics") {
args.extend(["--features", "open-metrics"]);
}
print_banner(&format!("Building {} binary", bin_name));
let mut build_result = Command::new("cargo");
let _ = build_result.args(args.clone());
if let Ok(val) = std::env::var("CARGO_TARGET_DIR") {
let _ = build_result.env("CARGO_TARGET_DIR", val);
}
let build_result = build_result
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
if !build_result.status.success() {
return Err(eyre!("Failed to build binaries"));
}
Ok(())
}