提交 2f66b5f3 authored 作者: Serhij S's avatar Serhij S

rollback

上级 4263a94b
...@@ -50,6 +50,11 @@ pub enum SubCommand { ...@@ -50,6 +50,11 @@ pub enum SubCommand {
)] )]
Exec(ExecCommand), Exec(ExecCommand),
#[clap(name = "purge", about = "Purge program data directory")] #[clap(name = "purge", about = "Purge program data directory")]
#[clap(
name = "rollback",
about = "Rollback to the previous program version (requires RoboPLC Pro)"
)]
Rollback(RollbackCommand),
Purge, Purge,
} }
...@@ -148,6 +153,8 @@ pub struct FlashCommand { ...@@ -148,6 +153,8 @@ pub struct FlashCommand {
pub run: bool, pub run: bool,
#[clap(long, help = "Perform live update (requires RoboPLC Pro)")] #[clap(long, help = "Perform live update (requires RoboPLC Pro)")]
pub live: bool, pub live: bool,
#[clap(long, help = "Skip current program backup (RoboPLC Pro)")]
pub skip_backup: bool,
} }
#[derive(Parser)] #[derive(Parser)]
...@@ -180,6 +187,24 @@ pub struct ExecCommand { ...@@ -180,6 +187,24 @@ pub struct ExecCommand {
pub args: Vec<String>, pub args: Vec<String>,
} }
#[derive(Parser)]
pub struct RollbackCommand {
#[clap(
short = 'f',
long,
help = "Force flash (automatically put remote in CONFIG mode), for Docker: run privileged"
)]
pub force: bool,
#[clap(
short = 'r',
long,
help = "Put remote in RUN mode after flashing, for Docker: run the container"
)]
pub run: bool,
#[clap(long, help = "Perform live update (requires RoboPLC Pro)")]
pub live: bool,
}
pub struct FlashExec { pub struct FlashExec {
pub cargo: Option<PathBuf>, pub cargo: Option<PathBuf>,
pub cargo_target: Option<String>, pub cargo_target: Option<String>,
...@@ -188,6 +213,7 @@ pub struct FlashExec { ...@@ -188,6 +213,7 @@ pub struct FlashExec {
pub force: bool, pub force: bool,
pub run: bool, pub run: bool,
pub live: bool, pub live: bool,
pub skip_backup: bool,
pub program_args: Vec<String>, pub program_args: Vec<String>,
pub program_env: BTreeMap<String, String>, pub program_env: BTreeMap<String, String>,
} }
...@@ -202,6 +228,7 @@ impl From<FlashCommand> for FlashExec { ...@@ -202,6 +228,7 @@ impl From<FlashCommand> for FlashExec {
force: cmd.force, force: cmd.force,
run: cmd.run, run: cmd.run,
live: cmd.live, live: cmd.live,
skip_backup: cmd.skip_backup,
program_args: Vec::new(), program_args: Vec::new(),
program_env: BTreeMap::new(), program_env: BTreeMap::new(),
} }
...@@ -231,6 +258,7 @@ impl From<ExecCommand> for FlashExec { ...@@ -231,6 +258,7 @@ impl From<ExecCommand> for FlashExec {
force: cmd.force, force: cmd.force,
run: false, run: false,
live: false, live: false,
skip_backup: false,
program_args: cmd.args, program_args: cmd.args,
program_env, program_env,
} }
......
...@@ -11,7 +11,7 @@ use ureq_multipart::MultipartBuilder; ...@@ -11,7 +11,7 @@ use ureq_multipart::MultipartBuilder;
use which::which; use which::which;
use crate::{ use crate::{
arguments::FlashExec, arguments::{FlashExec, RollbackCommand},
common::{report_ok, KernelInfo}, common::{report_ok, KernelInfo},
config, config,
ureq_err::PrintErr, ureq_err::PrintErr,
...@@ -27,6 +27,7 @@ fn flash_file( ...@@ -27,6 +27,7 @@ fn flash_file(
force: bool, force: bool,
run: bool, run: bool,
live: bool, live: bool,
skip_backup: bool,
exec_only: bool, exec_only: bool,
program_args: Vec<String>, program_args: Vec<String>,
program_env: BTreeMap<String, String>, program_env: BTreeMap<String, String>,
...@@ -93,12 +94,19 @@ fn flash_file( ...@@ -93,12 +94,19 @@ fn flash_file(
run: bool, run: bool,
#[serde(skip_serializing_if = "std::ops::Not::not")] #[serde(skip_serializing_if = "std::ops::Not::not")]
live: bool, live: bool,
#[serde(skip_serializing_if = "std::ops::Not::not")]
skip_backup: bool,
} }
let (content_type, data) = MultipartBuilder::new() let (content_type, data) = MultipartBuilder::new()
.add_file("file", file)? .add_file("file", file)?
.add_text( .add_text(
"params", "params",
&serde_json::to_string(&Payload { force, run, live })?, &serde_json::to_string(&Payload {
force,
run,
live,
skip_backup,
})?,
)? )?
.finish()?; .finish()?;
agent agent
...@@ -111,6 +119,34 @@ fn flash_file( ...@@ -111,6 +119,34 @@ fn flash_file(
Ok(()) Ok(())
} }
pub fn rollback(
url: &str,
key: &str,
agent: Agent,
opts: RollbackCommand,
) -> Result<(), Box<dyn std::error::Error>> {
#[derive(Serialize)]
struct Payload {
#[serde(skip_serializing_if = "std::ops::Not::not")]
force: bool,
#[serde(skip_serializing_if = "std::ops::Not::not")]
run: bool,
#[serde(skip_serializing_if = "std::ops::Not::not")]
live: bool,
}
agent
.post(&format!("{}{}/rollback", url, API_PREFIX))
.set("x-auth-key", key)
.send_json(&Payload {
force: opts.force,
run: opts.run,
live: opts.live,
})
.process_error()?;
report_ok()?;
Ok(())
}
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)] #[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
fn run_build_custom( fn run_build_custom(
url: &str, url: &str,
...@@ -119,6 +155,7 @@ fn run_build_custom( ...@@ -119,6 +155,7 @@ fn run_build_custom(
force: bool, force: bool,
run: bool, run: bool,
live: bool, live: bool,
skip_backup: bool,
cmd: &str, cmd: &str,
file: &Path, file: &Path,
exec_only: bool, exec_only: bool,
...@@ -147,6 +184,7 @@ fn run_build_custom( ...@@ -147,6 +184,7 @@ fn run_build_custom(
force, force,
run, run,
live, live,
skip_backup,
exec_only, exec_only,
program_args, program_args,
program_env, program_env,
...@@ -173,6 +211,7 @@ pub fn flash( ...@@ -173,6 +211,7 @@ pub fn flash(
opts.force, opts.force,
opts.run, opts.run,
opts.live, opts.live,
opts.skip_backup,
exec_only, exec_only,
opts.program_args, opts.program_args,
opts.program_env, opts.program_env,
...@@ -185,6 +224,7 @@ pub fn flash( ...@@ -185,6 +224,7 @@ pub fn flash(
opts.force, opts.force,
opts.run, opts.run,
opts.live, opts.live,
opts.skip_backup,
&custom_cmd, &custom_cmd,
&build_custom &build_custom
.file .file
...@@ -265,6 +305,7 @@ pub fn flash( ...@@ -265,6 +305,7 @@ pub fn flash(
opts.force, opts.force,
opts.run, opts.run,
opts.live, opts.live,
opts.skip_backup,
exec_only, exec_only,
opts.program_args, opts.program_args,
opts.program_env, opts.program_env,
......
...@@ -126,6 +126,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { ...@@ -126,6 +126,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
false, false,
)?; )?;
} }
SubCommand::Rollback(opts) => {
flashing::rollback(&url, &key, agent, opts)?;
}
SubCommand::Exec(opts) => { SubCommand::Exec(opts) => {
flashing::flash( flashing::flash(
&url, &url,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论