Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Enable updating Toolbx through topgrade (#830)
Browse files Browse the repository at this point in the history
* steps: toolbox: Add module to upgrade toolboxes

This adds a module to upgrade all toolboxes found on a host system.
[Toolbox][1] (More recently: `toolbx`) is a tool that easily provides
mutable, containerized command line environments. It is a means to work
and develop in a mutable environment on immutable operating systems such
as Fedora Silverblue, CoreOS, Kinoite and much more. However, it is not
limited to these use cases.

The integration into topgrade works as follows: Toolbx makes many of the
hosts folders available form inside the toolbx container by default. In
particular, all of the hosts filesystem is mapped under `/run/host`. This
module exploits this fact by:

- Gathering a list of toolboxes available on the host that runs
  `topgrade`
- Executing `toolbox run` for each of these toolboxes to run a command
  inside the toolboxes, **using the executing `topgrade` executable with
  the `--only system` arguments**

[1]: https://containertoolbx.org/

* toolbx: Integrate toolbx into the application

* Toolbx is Linux specific

Co-authored-by: Roey Darwish Dror <[email protected]>
  • Loading branch information
Funky185540 and r-darwish authored Jan 24, 2022
1 parent 4932be1 commit da8cf9d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub enum Step {
Tldr,
Tlmgr,
Tmux,
Toolbx,
Vagrant,
Vcpkg,
Vim,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ fn run() -> Result<()> {

#[cfg(target_os = "linux")]
{
runner.execute(Step::Toolbx, "toolbx", || toolbx::run_toolbx(&ctx))?;
runner.execute(Step::Flatpak, "Flatpak", || linux::flatpak_update(&ctx))?;
runner.execute(Step::Snap, "snap", || linux::run_snap(sudo.as_ref(), run_type))?;
runner.execute(Step::Pacstall, "pacstall", || linux::run_pacstall(&ctx))?;
Expand Down
2 changes: 2 additions & 0 deletions src/steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod powershell;
pub mod remote;
#[cfg(unix)]
pub mod tmux;
#[cfg(target_os = "linux")]
pub mod toolbx;
pub mod vim;
#[cfg(unix)]
pub mod zsh;
Expand Down
61 changes: 61 additions & 0 deletions src/steps/toolbx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use anyhow::Result;

use crate::config::Step;
use crate::terminal::print_separator;
use crate::{execution_context::ExecutionContext, utils::require};
use log::debug;
use std::path::Path;
use std::{path::PathBuf, process::Command};

fn list_toolboxes(toolbx: &Path) -> Result<Vec<String>> {
let output = Command::new(toolbx).args(&["list", "--containers"]).output()?;
let output_str = String::from_utf8(output.stdout)?;

let proc: Vec<String> = output_str
.lines()
// Skip the first line since that contains only status information
.skip(1)
.map(|line| match line.split_whitespace().nth(1) {
Some(word) => word.to_string(),
None => String::from(""),
})
.filter(|x| !x.is_empty())
.collect();

Ok(proc)
}

pub fn run_toolbx(ctx: &ExecutionContext) -> Result<()> {
let toolbx = require("toolbox")?;

print_separator("Toolbx");
let toolboxes = list_toolboxes(&toolbx)?;
debug!("Toolboxes to inspect: {:?}", toolboxes);

let mut topgrade_path = PathBuf::from("/run/host");
// Path of the running topgrade executable
// Skip 1 to eliminate the path root, otherwise push overwrites the path
topgrade_path.push(std::env::current_exe()?.components().skip(1).collect::<PathBuf>());
let topgrade_path = topgrade_path.to_str().unwrap();

for tb in toolboxes.iter() {
let topgrade_prefix = format!("TOPGRADE_PREFIX='Toolbx {}'", tb);
let mut args = vec![
"run",
"-c",
tb,
"env",
&topgrade_prefix,
topgrade_path,
"--only",
"system",
];
if ctx.config().yes(Step::Toolbx) {
args.push("--yes");
}

let _output = ctx.run_type().execute(&toolbx).args(&args).check_run();
}

Ok(())
}

0 comments on commit da8cf9d

Please sign in to comment.