Remoting refactor (#503)

This commit is contained in:
Roey Darwish Dror
2020-08-22 14:46:17 +03:00
committed by GitHub
parent 417ca1257a
commit f4a1f26e39
7 changed files with 58 additions and 48 deletions

View File

@@ -19,7 +19,7 @@ use self::error::StepFailed;
#[cfg(all(windows, feature = "self-update"))] #[cfg(all(windows, feature = "self-update"))]
use self::error::Upgraded; use self::error::Upgraded;
use self::steps::*; use self::steps::{remote::*, *};
use self::terminal::*; use self::terminal::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use log::debug; use log::debug;
@@ -115,7 +115,7 @@ fn run() -> Result<()> {
if let Some(topgrades) = config.remote_topgrades() { if let Some(topgrades) = config.remote_topgrades() {
for remote_topgrade in topgrades.iter().filter(|t| config.should_execute_remote(t)) { for remote_topgrade in topgrades.iter().filter(|t| config.should_execute_remote(t)) {
runner.execute(Step::Remotes, format!("Remote ({})", remote_topgrade), || { runner.execute(Step::Remotes, format!("Remote ({})", remote_topgrade), || {
generic::run_remote_topgrade(&ctx, remote_topgrade) remote::ssh::ssh_step(&ctx, remote_topgrade)
})?; })?;
} }
} }

View File

@@ -269,38 +269,3 @@ pub fn run_composer_update(ctx: &ExecutionContext) -> Result<()> {
Ok(()) Ok(())
} }
pub fn run_remote_topgrade(ctx: &ExecutionContext, hostname: &str) -> Result<()> {
let ssh = utils::require("ssh")?;
let topgrade = ctx.config().remote_topgrade_path();
if ctx.config().run_in_tmux() && !ctx.run_type().dry() {
#[cfg(unix)]
{
crate::tmux::run_remote_topgrade(hostname, &ssh, topgrade, ctx.config().tmux_arguments())?;
Err(SkipStep(String::from("Remote Topgrade launched in Tmux")).into())
}
#[cfg(not(unix))]
unreachable!("Tmux execution is only implemented in Unix");
} else {
let mut args = vec!["-t", hostname];
if let Some(ssh_arguments) = ctx.config().ssh_arguments() {
args.extend(ssh_arguments.split_whitespace());
}
let env = format!("TOPGRADE_PREFIX={}", hostname);
args.extend(&["env", &env, topgrade]);
if ctx.config().yes() {
args.push("-y");
}
print_separator(format!("Remote ({})", hostname));
println!("Connecting to {}...", hostname);
ctx.run_type().execute(&ssh).args(&args).check_run()
}
}

View File

@@ -4,9 +4,9 @@ pub mod git;
pub mod node; pub mod node;
pub mod os; pub mod os;
pub mod powershell; pub mod powershell;
pub mod remote;
#[cfg(unix)] #[cfg(unix)]
pub mod tmux; pub mod tmux;
pub mod vagrant;
pub mod vim; pub mod vim;
#[cfg(unix)] #[cfg(unix)]
pub mod zsh; pub mod zsh;

2
src/steps/remote/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod ssh;
pub mod vagrant;

46
src/steps/remote/ssh.rs Normal file
View File

@@ -0,0 +1,46 @@
#[cfg(unix)]
use crate::error::SkipStep;
use crate::{execution_context::ExecutionContext, terminal::print_separator, utils};
use anyhow::Result;
pub fn ssh_step(ctx: &ExecutionContext, hostname: &str) -> Result<()> {
let ssh = utils::require("ssh")?;
let topgrade = ctx.config().remote_topgrade_path();
if ctx.config().run_in_tmux() && !ctx.run_type().dry() {
#[cfg(unix)]
{
let command = format!(
"{ssh} -t {hostname} env TOPGRADE_PREFIX={hostname} TOPGRADE_KEEP_END=1 {topgrade}",
ssh = ssh.display(),
hostname = hostname,
topgrade = topgrade
);
crate::tmux::run_command(ctx, &command)?;
Err(SkipStep(String::from("Remote Topgrade launched in Tmux")).into())
}
#[cfg(not(unix))]
unreachable!("Tmux execution is only implemented in Unix");
} else {
let mut args = vec!["-t", hostname];
if let Some(ssh_arguments) = ctx.config().ssh_arguments() {
args.extend(ssh_arguments.split_whitespace());
}
let env = format!("TOPGRADE_PREFIX={}", hostname);
args.extend(&["env", &env, topgrade]);
if ctx.config().yes() {
args.push("-y");
}
print_separator(format!("Remote ({})", hostname));
println!("Connecting to {}...", hostname);
ctx.run_type().execute(&ssh).args(&args).check_run()
}
}

View File

@@ -1,12 +1,15 @@
use crate::executor::RunType; use crate::executor::RunType;
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{which, Check, PathExt}; use crate::{
execution_context::ExecutionContext,
utils::{which, Check, PathExt},
};
use anyhow::Result; use anyhow::Result;
use directories::BaseDirs; use directories::BaseDirs;
use std::env; use std::env;
use std::io; use std::io;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::process::{exit, Command}; use std::process::{exit, Command};
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> { pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
@@ -104,14 +107,8 @@ pub fn run_in_tmux(args: &Option<String>) -> ! {
} }
} }
pub fn run_remote_topgrade(hostname: &str, ssh: &Path, topgrade: &str, tmux_args: &Option<String>) -> Result<()> { pub fn run_command(ctx: &ExecutionContext, command: &str) -> Result<()> {
let command = format!( Tmux::new(ctx.config().tmux_arguments())
"{ssh} -t {hostname} env TOPGRADE_PREFIX={hostname} TOPGRADE_KEEP_END=1 {topgrade}",
ssh = ssh.display(),
hostname = hostname,
topgrade = topgrade
);
Tmux::new(tmux_args)
.build() .build()
.args(&["new-window", "-a", "-t", "topgrade:1", &command]) .args(&["new-window", "-a", "-t", "topgrade:1", &command])
.env_remove("TMUX") .env_remove("TMUX")