Files
topgrade/src/steps/os/linux.rs

296 lines
8.3 KiB
Rust
Raw Normal View History

2018-12-15 21:52:21 +02:00
use crate::error::{Error, ErrorKind};
use crate::executor::RunType;
2018-12-15 21:52:21 +02:00
use crate::terminal::{print_separator, print_warning};
2019-02-11 14:10:06 +02:00
use crate::utils::{require, require_option, which};
2018-12-11 16:43:26 +02:00
use failure::ResultExt;
use std::fs;
2018-06-12 21:28:32 +03:00
use std::path::PathBuf;
use walkdir::WalkDir;
#[derive(Copy, Clone, Debug)]
pub enum Distribution {
Arch,
CentOS,
Fedora,
Debian,
Ubuntu,
2018-10-21 15:08:36 +03:00
Gentoo,
2018-11-03 22:57:09 +02:00
OpenSuse,
2018-11-20 14:38:23 +02:00
Void,
}
impl Distribution {
2018-12-11 16:43:26 +02:00
pub fn detect() -> Result<Self, Error> {
let content = fs::read_to_string("/etc/os-release").context(ErrorKind::UnknownLinuxDistribution)?;
2018-07-11 07:35:30 +03:00
if content.contains("Arch") | content.contains("Manjaro") | content.contains("Antergos") {
return Ok(Distribution::Arch);
}
2018-11-07 10:22:13 +02:00
if content.contains("CentOS") || content.contains("Oracle Linux") {
return Ok(Distribution::CentOS);
}
if content.contains("Fedora") {
return Ok(Distribution::Fedora);
}
if content.contains("Ubuntu") {
return Ok(Distribution::Ubuntu);
}
if content.contains("Debian") {
return Ok(Distribution::Debian);
}
2018-11-03 22:57:09 +02:00
if content.contains("openSUSE") {
return Ok(Distribution::OpenSuse);
}
2018-11-20 14:38:23 +02:00
if content.contains("void") {
return Ok(Distribution::Void);
}
2018-10-21 15:08:36 +03:00
if PathBuf::from("/etc/gentoo-release").exists() {
return Ok(Distribution::Gentoo);
}
2018-12-11 16:43:26 +02:00
Err(ErrorKind::UnknownLinuxDistribution)?
}
#[must_use]
2019-02-11 14:10:06 +02:00
pub fn upgrade(self, sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> Result<(), Error> {
2018-12-05 11:34:08 +02:00
print_separator("System update");
2019-02-11 14:10:06 +02:00
match self {
Distribution::Arch => upgrade_arch_linux(&sudo, cleanup, run_type),
Distribution::CentOS => upgrade_redhat(&sudo, run_type),
Distribution::Fedora => upgrade_fedora(&sudo, run_type),
Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, cleanup, run_type),
Distribution::Gentoo => upgrade_gentoo(&sudo, run_type),
Distribution::OpenSuse => upgrade_opensuse(&sudo, run_type),
Distribution::Void => upgrade_void(&sudo, run_type),
2019-02-11 14:10:06 +02:00
}
}
pub fn show_summary(self) {
if let Distribution::Arch = self {
show_pacnew();
}
}
}
pub fn show_pacnew() {
let mut iter = WalkDir::new("/etc")
.into_iter()
.filter_map(|e| e.ok())
.filter(|f| {
f.path()
.extension()
.filter(|ext| ext == &"pacnew" || ext == &"pacsave")
.is_some()
2018-12-11 16:00:19 +02:00
})
.peekable();
if iter.peek().is_some() {
println!("\nPacman backup configuration files found:");
for entry in iter {
println!("{}", entry.path().display());
}
}
}
2018-06-12 21:28:32 +03:00
fn upgrade_arch_linux(sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> Result<(), Error> {
2018-06-17 14:17:36 +03:00
if let Some(yay) = which("yay") {
if let Some(python) = which("python") {
if python != PathBuf::from("/usr/bin/python") {
2018-12-05 11:34:08 +02:00
print_warning(format!(
"Python detected at {:?}, which is probably not the system Python.
It's dangerous to run yay since Python based AUR packages will be installed in the wrong location",
python
));
2018-12-11 16:43:26 +02:00
return Err(ErrorKind::NotSystemPython)?;
}
}
2018-12-31 22:00:34 +02:00
run_type.execute(yay).check_run()?;
2018-07-10 07:29:41 +03:00
} else if let Some(sudo) = &sudo {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/pacman", "-Syu"]).check_run()?;
2018-06-12 21:28:32 +03:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo or yay detected. Skipping system upgrade");
2018-06-12 21:28:32 +03:00
}
2018-12-23 10:47:45 +02:00
if cleanup {
if let Some(sudo) = &sudo {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/pacman", "-Scc"]).check_run()?;
2018-12-23 10:47:45 +02:00
}
}
2018-06-12 21:28:32 +03:00
Ok(())
}
fn upgrade_redhat(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
2018-06-12 21:28:32 +03:00
if let Some(sudo) = &sudo {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/yum", "upgrade"]).check_run()?;
2018-06-12 21:28:32 +03:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-11-03 22:57:09 +02:00
}
Ok(())
}
fn upgrade_opensuse(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
2018-11-03 22:57:09 +02:00
if let Some(sudo) = &sudo {
run_type
.execute(&sudo)
2018-11-03 22:57:09 +02:00
.args(&["/usr/bin/zypper", "refresh"])
2018-12-31 22:00:34 +02:00
.check_run()?;
2018-11-03 22:57:09 +02:00
run_type
.execute(&sudo)
2018-11-03 22:57:09 +02:00
.args(&["/usr/bin/zypper", "dist-upgrade"])
2018-12-31 22:00:34 +02:00
.check_run()?;
2018-11-03 22:57:09 +02:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-11-20 14:38:23 +02:00
}
Ok(())
}
fn upgrade_void(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
2018-11-20 14:38:23 +02:00
if let Some(sudo) = &sudo {
run_type
.execute(&sudo)
2018-11-20 14:38:23 +02:00
.args(&["/usr/bin/xbps-install", "-Su"])
2018-12-31 22:00:34 +02:00
.check_run()?;
2018-11-20 14:38:23 +02:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-06-12 21:28:32 +03:00
}
Ok(())
}
fn upgrade_fedora(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
2018-06-12 21:28:32 +03:00
if let Some(sudo) = &sudo {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/dnf", "upgrade"]).check_run()?;
2018-06-12 21:28:32 +03:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-06-12 21:28:32 +03:00
}
Ok(())
}
fn upgrade_gentoo(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
2018-10-21 15:08:36 +03:00
if let Some(sudo) = &sudo {
if let Some(layman) = which("layman") {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).arg(layman).args(&["-s", "ALL"]).check_run()?;
2018-10-21 15:08:36 +03:00
}
println!("Syncing portage");
run_type
.execute(&sudo)
2018-10-21 15:08:36 +03:00
.arg("/usr/bin/emerge")
.args(&["-q", "--sync"])
2018-12-31 22:00:34 +02:00
.check_run()?;
2018-10-21 15:08:36 +03:00
if let Some(eix_update) = which("eix-update") {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).arg(eix_update).check_run()?;
2018-10-21 15:08:36 +03:00
}
run_type
.execute(&sudo)
2018-10-21 15:08:36 +03:00
.arg("/usr/bin/emerge")
.args(&["-uDNa", "world"])
2018-12-31 22:00:34 +02:00
.check_run()?;
2018-10-21 15:08:36 +03:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-10-21 15:08:36 +03:00
}
Ok(())
}
fn upgrade_debian(sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType) -> Result<(), Error> {
2018-06-12 21:28:32 +03:00
if let Some(sudo) = &sudo {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/apt", "update"]).check_run()?;
2018-06-12 21:28:32 +03:00
run_type
.execute(&sudo)
.args(&["/usr/bin/apt", "dist-upgrade"])
2018-12-31 22:00:34 +02:00
.check_run()?;
if cleanup {
2018-12-31 22:00:34 +02:00
run_type.execute(&sudo).args(&["/usr/bin/apt", "clean"]).check_run()?;
run_type
.execute(&sudo)
.args(&["/usr/bin/apt", "autoremove"])
2018-12-31 22:00:34 +02:00
.check_run()?;
}
2018-06-12 21:28:32 +03:00
} else {
2018-12-05 11:34:08 +02:00
print_warning("No sudo detected. Skipping system upgrade");
2018-06-12 21:28:32 +03:00
}
Ok(())
}
2018-06-28 12:16:54 +03:00
2019-02-11 14:10:06 +02:00
pub fn run_needrestart(sudo: Option<&PathBuf>, run_type: RunType) -> Result<(), Error> {
let sudo = require_option(sudo)?;
let needrestart = require("needrestart")?;
2018-06-28 12:16:54 +03:00
2019-02-11 14:10:06 +02:00
print_separator("Check for needed restarts");
2018-06-28 12:16:54 +03:00
2019-02-11 14:10:06 +02:00
run_type.execute(&sudo).arg(needrestart).check_run()?;
2018-08-19 14:45:23 +03:00
2019-02-11 14:10:06 +02:00
Ok(())
2018-06-28 12:16:54 +03:00
}
2018-08-19 14:45:23 +03:00
#[must_use]
2019-02-25 15:10:28 +02:00
pub fn run_fwupdmgr(run_type: RunType) -> Result<(), Error> {
let fwupdmgr = require("fwupdmgr")?;
print_separator("Firmware upgrades");
2018-08-19 14:45:23 +03:00
2019-02-25 15:10:28 +02:00
run_type.execute(&fwupdmgr).arg("refresh").check_run()?;
run_type.execute(&fwupdmgr).arg("get-updates").check_run()
2018-06-28 12:16:54 +03:00
}
2018-08-19 14:45:23 +03:00
#[must_use]
2019-02-25 15:10:28 +02:00
pub fn flatpak_update(run_type: RunType) -> Result<(), Error> {
let flatpak = require("flatpak")?;
print_separator("Flatpak User Packages");
run_type
.execute(&flatpak)
.args(&["update", "--user", "-y"])
.check_run()?;
run_type
.execute(&flatpak)
.args(&["update", "--system", "-y"])
.check_run()
}
2018-08-19 14:45:23 +03:00
#[must_use]
2019-02-25 15:10:28 +02:00
pub fn run_snap(sudo: Option<&PathBuf>, run_type: RunType) -> Result<(), Error> {
let sudo = require_option(sudo)?;
let snap = require("snap")?;
if !PathBuf::from("/var/snapd.socket").exists() {
Err(ErrorKind::SkipStep)?;
2018-08-19 14:45:23 +03:00
}
2019-02-25 15:10:28 +02:00
print_separator("snap");
2018-08-19 14:45:23 +03:00
2019-02-25 15:10:28 +02:00
run_type
.execute(sudo)
.args(&[snap.to_str().unwrap(), "refresh"])
.check_run()
2018-06-28 12:16:54 +03:00
}
2018-10-02 13:25:02 +03:00
#[must_use]
2019-02-25 15:10:28 +02:00
pub fn run_etc_update(sudo: Option<&PathBuf>, run_type: RunType) -> Result<(), Error> {
let sudo = require_option(sudo)?;
let etc_update = require("etc_update")?;
print_separator("etc-update");
2018-10-02 13:25:02 +03:00
2019-02-25 15:10:28 +02:00
run_type.execute(sudo).arg(&etc_update.to_str().unwrap()).check_run()
2018-10-02 13:25:02 +03:00
}