2020-10-21 09:41:16 +00:00
|
|
|
use anyhow::{Error, bail, format_err};
|
2020-07-21 11:41:07 +00:00
|
|
|
use serde_json::{json, Value};
|
2020-11-09 09:35:28 +00:00
|
|
|
use std::collections::HashMap;
|
2020-07-21 11:41:07 +00:00
|
|
|
|
2020-10-31 19:40:05 +00:00
|
|
|
use proxmox::list_subdirs_api_method;
|
2020-07-23 09:19:52 +00:00
|
|
|
use proxmox::api::{api, RpcEnvironment, RpcEnvironmentType, Permission};
|
|
|
|
use proxmox::api::router::{Router, SubdirMap};
|
2020-07-21 11:41:07 +00:00
|
|
|
|
2020-07-23 09:19:52 +00:00
|
|
|
use crate::server::WorkerTask;
|
2020-11-09 09:35:28 +00:00
|
|
|
use crate::tools::{apt, http, subscription};
|
2020-07-23 09:19:52 +00:00
|
|
|
|
|
|
|
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
|
2020-10-23 11:33:21 +00:00
|
|
|
use crate::api2::types::{Authid, APTUpdateInfo, NODE_SCHEMA, UPID_SCHEMA};
|
2020-07-21 11:41:07 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "A list of packages with available updates.",
|
|
|
|
type: Array,
|
2020-10-31 19:40:05 +00:00
|
|
|
items: {
|
|
|
|
type: APTUpdateInfo
|
|
|
|
},
|
2020-07-21 11:41:07 +00:00
|
|
|
},
|
2020-10-31 20:02:25 +00:00
|
|
|
protected: true,
|
2020-07-21 11:41:07 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List available APT updates
|
|
|
|
fn apt_update_available(_param: Value) -> Result<Value, Error> {
|
2020-10-31 20:02:25 +00:00
|
|
|
|
2021-01-19 11:09:33 +00:00
|
|
|
if let Ok(false) = apt::pkg_cache_expired() {
|
|
|
|
if let Ok(Some(cache)) = apt::read_pkg_state() {
|
|
|
|
return Ok(json!(cache.package_status));
|
|
|
|
}
|
2020-10-31 20:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let cache = apt::update_cache()?;
|
|
|
|
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(json!(cache.package_status))
|
2020-07-21 11:41:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:54:53 +00:00
|
|
|
fn do_apt_update(worker: &WorkerTask, quiet: bool) -> Result<(), Error> {
|
|
|
|
if !quiet { worker.log("starting apt-get update") }
|
|
|
|
|
|
|
|
// TODO: set proxy /etc/apt/apt.conf.d/76pbsproxy like PVE
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new("apt-get");
|
|
|
|
command.arg("update");
|
|
|
|
|
|
|
|
// apt "errors" quite easily, and run_command is a bit rigid, so handle this inline for now.
|
|
|
|
let output = command.output()
|
|
|
|
.map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
|
|
|
|
|
|
|
|
if !quiet {
|
|
|
|
worker.log(String::from_utf8(output.stdout)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: improve run_command to allow outputting both, stderr and stdout
|
|
|
|
if !output.status.success() {
|
|
|
|
if output.status.code().is_some() {
|
|
|
|
let msg = String::from_utf8(output.stderr)
|
|
|
|
.map(|m| if m.is_empty() { String::from("no error message") } else { m })
|
|
|
|
.unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
|
|
|
|
worker.warn(msg);
|
|
|
|
} else {
|
|
|
|
bail!("terminated by signal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-23 09:19:52 +00:00
|
|
|
#[api(
|
2020-07-23 09:45:46 +00:00
|
|
|
protected: true,
|
2020-07-23 09:19:52 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
2020-10-31 20:09:21 +00:00
|
|
|
notify: {
|
|
|
|
type: bool,
|
|
|
|
description: r#"Send notification mail about new package updates availanle to the
|
|
|
|
email address configured for 'root@pam')."#,
|
|
|
|
default: false,
|
2021-01-18 13:12:27 +00:00
|
|
|
optional: true,
|
2020-10-31 20:09:21 +00:00
|
|
|
},
|
2020-07-23 09:19:52 +00:00
|
|
|
quiet: {
|
|
|
|
description: "Only produces output suitable for logging, omitting progress indicators.",
|
|
|
|
type: bool,
|
|
|
|
default: false,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Update the APT database
|
|
|
|
pub fn apt_update_database(
|
2021-01-20 16:23:52 +00:00
|
|
|
notify: bool,
|
|
|
|
quiet: bool,
|
2020-07-23 09:19:52 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
2021-01-18 13:12:27 +00:00
|
|
|
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
|
2020-07-23 09:19:52 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let upid_str = WorkerTask::new_thread("aptupdate", None, auth_id, to_stdout, move |worker| {
|
2020-10-31 19:54:53 +00:00
|
|
|
do_apt_update(&worker, quiet)?;
|
2020-10-31 20:09:21 +00:00
|
|
|
|
|
|
|
let mut cache = apt::update_cache()?;
|
|
|
|
|
|
|
|
if notify {
|
|
|
|
let mut notified = match cache.notified {
|
|
|
|
Some(notified) => notified,
|
|
|
|
None => std::collections::HashMap::new(),
|
|
|
|
};
|
|
|
|
let mut to_notify: Vec<&APTUpdateInfo> = Vec::new();
|
|
|
|
|
|
|
|
for pkg in &cache.package_status {
|
|
|
|
match notified.insert(pkg.package.to_owned(), pkg.version.to_owned()) {
|
|
|
|
Some(notified_version) => {
|
|
|
|
if notified_version != pkg.version {
|
|
|
|
to_notify.push(pkg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => to_notify.push(pkg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !to_notify.is_empty() {
|
2020-10-31 21:55:54 +00:00
|
|
|
to_notify.sort_unstable_by_key(|k| &k.package);
|
2020-10-31 20:09:21 +00:00
|
|
|
crate::server::send_updates_available(&to_notify)?;
|
|
|
|
}
|
|
|
|
cache.notified = Some(notified);
|
|
|
|
apt::write_pkg_cache(&cache)?;
|
|
|
|
}
|
|
|
|
|
2020-07-23 09:19:52 +00:00
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(upid_str)
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:41:16 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
description: "Package name to get changelog of.",
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
version: {
|
|
|
|
description: "Package version to get changelog of. Omit to use candidate version.",
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Retrieve the changelog of the specified package.
|
|
|
|
fn apt_get_changelog(
|
|
|
|
param: Value,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let name = crate::tools::required_string_param(¶m, "name")?.to_owned();
|
|
|
|
let version = param["version"].as_str();
|
|
|
|
|
2020-10-31 19:40:05 +00:00
|
|
|
let pkg_info = apt::list_installed_apt_packages(|data| {
|
2020-10-21 09:41:16 +00:00
|
|
|
match version {
|
|
|
|
Some(version) => version == data.active_version,
|
|
|
|
None => data.active_version == data.candidate_version
|
|
|
|
}
|
|
|
|
}, Some(&name));
|
|
|
|
|
2021-01-19 09:27:59 +00:00
|
|
|
if pkg_info.is_empty() {
|
2020-10-21 09:41:16 +00:00
|
|
|
bail!("Package '{}' not found", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let changelog_url = &pkg_info[0].change_log_url;
|
|
|
|
// FIXME: use 'apt-get changelog' for proxmox packages as well, once repo supports it
|
|
|
|
if changelog_url.starts_with("http://download.proxmox.com/") {
|
2020-11-09 09:35:28 +00:00
|
|
|
let changelog = crate::tools::runtime::block_on(http::get_string(changelog_url, None))
|
2020-10-22 15:13:26 +00:00
|
|
|
.map_err(|err| format_err!("Error downloading changelog from '{}': {}", changelog_url, err))?;
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(json!(changelog))
|
2020-11-09 09:35:28 +00:00
|
|
|
|
|
|
|
} else if changelog_url.starts_with("https://enterprise.proxmox.com/") {
|
|
|
|
let sub = match subscription::read_subscription()? {
|
|
|
|
Some(sub) => sub,
|
|
|
|
None => bail!("cannot retrieve changelog from enterprise repo: no subscription info found")
|
|
|
|
};
|
|
|
|
let (key, id) = match sub.key {
|
|
|
|
Some(key) => {
|
|
|
|
match sub.serverid {
|
|
|
|
Some(id) => (key, id),
|
|
|
|
None =>
|
|
|
|
bail!("cannot retrieve changelog from enterprise repo: no server id found")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => bail!("cannot retrieve changelog from enterprise repo: no subscription key found")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut auth_header = HashMap::new();
|
|
|
|
auth_header.insert("Authorization".to_owned(),
|
|
|
|
format!("Basic {}", base64::encode(format!("{}:{}", key, id))));
|
|
|
|
|
|
|
|
let changelog = crate::tools::runtime::block_on(http::get_string(changelog_url, Some(&auth_header)))
|
|
|
|
.map_err(|err| format_err!("Error downloading changelog from '{}': {}", changelog_url, err))?;
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(json!(changelog))
|
2020-11-09 09:35:28 +00:00
|
|
|
|
2020-10-21 09:41:16 +00:00
|
|
|
} else {
|
|
|
|
let mut command = std::process::Command::new("apt-get");
|
|
|
|
command.arg("changelog");
|
|
|
|
command.arg("-qq"); // don't display download progress
|
|
|
|
command.arg(name);
|
|
|
|
let output = crate::tools::run_command(command, None)?;
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(json!(output))
|
2020-10-21 09:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:31:51 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "List of more relevant packages.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: APTUpdateInfo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get package information for important Proxmox Backup Server packages.
|
2020-11-12 09:12:54 +00:00
|
|
|
pub fn get_versions() -> Result<Vec<APTUpdateInfo>, Error> {
|
2020-11-09 15:31:51 +00:00
|
|
|
const PACKAGES: &[&str] = &[
|
|
|
|
"ifupdown2",
|
|
|
|
"libjs-extjs",
|
|
|
|
"proxmox-backup",
|
|
|
|
"proxmox-backup-docs",
|
|
|
|
"proxmox-backup-client",
|
|
|
|
"proxmox-backup-server",
|
|
|
|
"proxmox-mini-journalreader",
|
|
|
|
"proxmox-widget-toolkit",
|
|
|
|
"pve-xtermjs",
|
|
|
|
"smartmontools",
|
|
|
|
"zfsutils-linux",
|
|
|
|
];
|
|
|
|
|
2020-11-10 13:22:17 +00:00
|
|
|
fn unknown_package(package: String, extra_info: Option<String>) -> APTUpdateInfo {
|
2020-11-09 15:31:51 +00:00
|
|
|
APTUpdateInfo {
|
|
|
|
package,
|
|
|
|
title: "unknown".into(),
|
|
|
|
arch: "unknown".into(),
|
|
|
|
description: "unknown".into(),
|
|
|
|
version: "unknown".into(),
|
|
|
|
old_version: "unknown".into(),
|
|
|
|
origin: "unknown".into(),
|
|
|
|
priority: "unknown".into(),
|
|
|
|
section: "unknown".into(),
|
|
|
|
change_log_url: "unknown".into(),
|
2020-11-10 13:22:17 +00:00
|
|
|
extra_info,
|
2020-11-09 15:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let is_kernel = |name: &str| name.starts_with("pve-kernel-");
|
|
|
|
|
|
|
|
let mut packages: Vec<APTUpdateInfo> = Vec::new();
|
|
|
|
let pbs_packages = apt::list_installed_apt_packages(
|
|
|
|
|filter| {
|
|
|
|
filter.installed_version == Some(filter.active_version)
|
|
|
|
&& (is_kernel(filter.package) || PACKAGES.contains(&filter.package))
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
);
|
2020-11-10 13:22:17 +00:00
|
|
|
|
2020-11-12 09:52:20 +00:00
|
|
|
let running_kernel = format!(
|
2020-11-12 10:02:45 +00:00
|
|
|
"running kernel: {}",
|
2020-11-12 09:52:20 +00:00
|
|
|
nix::sys::utsname::uname().release().to_owned()
|
|
|
|
);
|
2020-11-11 16:20:53 +00:00
|
|
|
if let Some(proxmox_backup) = pbs_packages.iter().find(|pkg| pkg.package == "proxmox-backup") {
|
2020-11-10 13:22:17 +00:00
|
|
|
let mut proxmox_backup = proxmox_backup.clone();
|
2020-11-12 09:52:20 +00:00
|
|
|
proxmox_backup.extra_info = Some(running_kernel);
|
2020-11-10 13:22:17 +00:00
|
|
|
packages.push(proxmox_backup);
|
2020-11-09 15:31:51 +00:00
|
|
|
} else {
|
2020-11-11 16:20:53 +00:00
|
|
|
packages.push(unknown_package("proxmox-backup".into(), Some(running_kernel)));
|
2020-11-09 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:54:43 +00:00
|
|
|
let version = crate::api2::version::PROXMOX_PKG_VERSION;
|
|
|
|
let release = crate::api2::version::PROXMOX_PKG_RELEASE;
|
|
|
|
let daemon_version_info = Some(format!("running version: {}.{}", version, release));
|
2020-11-11 16:20:53 +00:00
|
|
|
if let Some(pkg) = pbs_packages.iter().find(|pkg| pkg.package == "proxmox-backup-server") {
|
2020-11-10 13:22:17 +00:00
|
|
|
let mut pkg = pkg.clone();
|
2020-11-11 16:54:43 +00:00
|
|
|
pkg.extra_info = daemon_version_info;
|
2020-11-10 13:22:17 +00:00
|
|
|
packages.push(pkg);
|
2020-11-11 16:54:43 +00:00
|
|
|
} else {
|
|
|
|
packages.push(unknown_package("proxmox-backup".into(), daemon_version_info));
|
2020-11-09 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut kernel_pkgs: Vec<APTUpdateInfo> = pbs_packages
|
|
|
|
.iter()
|
|
|
|
.filter(|pkg| is_kernel(&pkg.package))
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
// make sure the cache mutex gets dropped before the next call to list_installed_apt_packages
|
|
|
|
{
|
|
|
|
let cache = apt_pkg_native::Cache::get_singleton();
|
|
|
|
kernel_pkgs.sort_by(|left, right| {
|
|
|
|
cache
|
|
|
|
.compare_versions(&left.old_version, &right.old_version)
|
|
|
|
.reverse()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
packages.append(&mut kernel_pkgs);
|
|
|
|
|
|
|
|
// add entry for all packages we're interested in, even if not installed
|
|
|
|
for pkg in PACKAGES.iter() {
|
|
|
|
if pkg == &"proxmox-backup" || pkg == &"proxmox-backup-server" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match pbs_packages.iter().find(|item| &item.package == pkg) {
|
|
|
|
Some(apt_pkg) => packages.push(apt_pkg.to_owned()),
|
2020-11-10 13:22:17 +00:00
|
|
|
None => packages.push(unknown_package(pkg.to_string(), None)),
|
2020-11-09 15:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:12:54 +00:00
|
|
|
Ok(packages)
|
2020-11-09 15:31:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 11:41:07 +00:00
|
|
|
const SUBDIRS: SubdirMap = &[
|
2020-10-21 09:41:16 +00:00
|
|
|
("changelog", &Router::new().get(&API_METHOD_APT_GET_CHANGELOG)),
|
2020-07-23 09:19:52 +00:00
|
|
|
("update", &Router::new()
|
|
|
|
.get(&API_METHOD_APT_UPDATE_AVAILABLE)
|
|
|
|
.post(&API_METHOD_APT_UPDATE_DATABASE)
|
|
|
|
),
|
2020-11-09 15:31:51 +00:00
|
|
|
("versions", &Router::new().get(&API_METHOD_GET_VERSIONS)),
|
2020-07-21 11:41:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
|
|
|
.subdirs(SUBDIRS);
|