Files
topgrade/src/steps/vagrant.rs

186 lines
5.2 KiB
Rust
Raw Normal View History

2020-06-10 11:51:52 +03:00
use crate::execution_context::ExecutionContext;
use crate::executor::CommandExt;
use crate::terminal::print_separator;
use crate::utils;
use anyhow::Result;
use log::debug;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fmt::Display, str::FromStr};
use strum::EnumString;
#[derive(Debug, Copy, Clone, EnumString)]
#[strum(serialize_all = "lowercase")]
enum BoxStatus {
PowerOff,
Running,
2020-06-12 15:35:39 +03:00
Saved,
2020-06-13 07:40:03 +03:00
Aborted,
2020-06-10 11:51:52 +03:00
}
impl BoxStatus {
fn powered_on(self) -> bool {
match self {
BoxStatus::Running => true,
2020-06-13 07:40:03 +03:00
_ => false,
2020-06-10 11:51:52 +03:00
}
}
}
#[derive(Debug)]
struct VagrantBox<'a> {
path: &'a str,
name: String,
}
impl<'a> Display for VagrantBox<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} @ {}", self.name, self.path)
}
}
struct Vagrant {
path: PathBuf,
}
impl Vagrant {
fn get_boxes<'a>(&self, directory: &'a str) -> Result<Vec<(VagrantBox<'a>, BoxStatus)>> {
let output = Command::new(&self.path)
.arg("status")
.current_dir(directory)
.check_output()?;
debug!("Vagrant output in {}: {}", directory, output);
let boxes = output
.split('\n')
.skip(2)
.take_while(|line| !(line.is_empty() || line.starts_with('\r')))
.map(|line| {
debug!("Vagrant line: {:?}", line);
let mut elements = line.split_whitespace();
let vagrant_box = VagrantBox {
name: elements.next().unwrap().to_string(),
path: directory,
};
let box_status = BoxStatus::from_str(elements.next().unwrap()).unwrap();
debug!("{:?}: {:?}", vagrant_box, box_status);
(vagrant_box, box_status)
})
.collect();
Ok(boxes)
}
fn temporary_power_on<'a>(
&'a self,
vagrant_box: &'a VagrantBox,
2020-06-12 15:35:39 +03:00
status: BoxStatus,
2020-06-10 11:51:52 +03:00
ctx: &'a ExecutionContext,
) -> Result<TemporaryPowerOn<'a>> {
2020-06-12 15:35:39 +03:00
TemporaryPowerOn::create(&self.path, vagrant_box, status, ctx)
2020-06-10 11:51:52 +03:00
}
}
struct TemporaryPowerOn<'a> {
vagrant: &'a Path,
vagrant_box: &'a VagrantBox<'a>,
2020-06-12 15:35:39 +03:00
status: BoxStatus,
2020-06-10 11:51:52 +03:00
ctx: &'a ExecutionContext<'a>,
}
impl<'a> TemporaryPowerOn<'a> {
2020-06-12 15:35:39 +03:00
fn create(
vagrant: &'a Path,
vagrant_box: &'a VagrantBox<'a>,
status: BoxStatus,
ctx: &'a ExecutionContext<'a>,
) -> Result<Self> {
let subcommand = match status {
2020-06-13 07:40:03 +03:00
BoxStatus::PowerOff | BoxStatus::Aborted => "up",
2020-06-12 15:35:39 +03:00
BoxStatus::Saved => "resume",
2020-06-13 07:40:03 +03:00
BoxStatus::Running => unreachable!(),
2020-06-12 15:35:39 +03:00
};
2020-06-10 11:51:52 +03:00
println!("Powering on {}", vagrant_box);
2020-06-12 15:35:39 +03:00
2020-06-10 11:51:52 +03:00
ctx.run_type()
.execute(vagrant)
2020-06-12 15:35:39 +03:00
.args(&[subcommand, &vagrant_box.name])
2020-06-10 11:51:52 +03:00
.current_dir(vagrant_box.path)
.check_run()?;
Ok(TemporaryPowerOn {
vagrant,
vagrant_box,
2020-06-12 15:35:39 +03:00
status,
2020-06-10 11:51:52 +03:00
ctx,
})
}
}
impl<'a> Drop for TemporaryPowerOn<'a> {
fn drop(&mut self) {
let subcommand = if self.ctx.config().vagrant_always_suspend().unwrap_or(false) {
"suspend"
} else {
match self.status {
BoxStatus::PowerOff | BoxStatus::Aborted => "halt",
BoxStatus::Saved => "suspend",
BoxStatus::Running => unreachable!(),
}
2020-06-12 15:35:39 +03:00
};
2020-06-10 11:51:52 +03:00
println!("Powering off {}", self.vagrant_box);
self.ctx
.run_type()
.execute(self.vagrant)
2020-06-12 15:35:39 +03:00
.args(&[subcommand, &self.vagrant_box.name])
2020-06-10 11:51:52 +03:00
.current_dir(self.vagrant_box.path)
.check_run()
.ok();
}
}
pub fn topgrade_vagrant_boxes(ctx: &ExecutionContext) -> Result<()> {
let directories = utils::require_option(ctx.config().vagrant_directories())?;
let vagrant = Vagrant {
path: utils::require("vagrant")?,
};
print_separator("Vagrant");
for directory in directories {
let boxes = vagrant.get_boxes(directory)?;
debug!("{:?}", boxes);
for (vagrant_box, status) in boxes {
let mut _poweron = None;
if !status.powered_on() {
if !(ctx.config().vagrant_power_on().unwrap_or(true)) {
debug!("Skipping powered off box {}", vagrant_box);
continue;
} else {
2020-06-12 15:35:39 +03:00
_poweron = Some(vagrant.temporary_power_on(&vagrant_box, status, ctx)?);
2020-06-10 11:51:52 +03:00
}
}
2020-06-12 11:35:32 +03:00
println!("Running Topgrade in {}", vagrant_box);
2020-06-13 07:28:52 +03:00
let pathbuf = PathBuf::from(directory);
let prefix = if vagrant_box.name == "default" {
pathbuf.file_name().unwrap().to_str().unwrap()
} else {
&vagrant_box.name
};
let mut command = format!("env TOPGRADE_PREFIX={} topgrade", prefix);
2020-06-10 11:51:52 +03:00
if ctx.config().yes() {
command.push_str(" -y");
}
ctx.run_type()
.execute(&vagrant.path)
2020-06-12 15:33:58 +03:00
.current_dir(directory)
2020-06-10 11:51:52 +03:00
.args(&["ssh", "-c", &command])
.check_run()?;
}
}
Ok(())
}