Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for --stop-before flag. #1667

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/mender-update/cli/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,14 @@ static error::Error ResultHandler(standalone::ResultAndError result) {
? operation_failure
: operation_done;

string prefix;
string additional;

if (contains(r::AutoCommitWanted) and contains(r::Installed)
and (contains(r::Committed) or contains(r::Failed))) {
prefix = "Update Module doesn't support rollback. Committing immediately.";
}

if (contains(r::RollbackFailed)) {
additional =
"Rollback failed. "
Expand Down Expand Up @@ -237,6 +243,9 @@ static error::Error ResultHandler(standalone::ResultAndError result) {
}
}

if (prefix.size() > 0) {
cout << prefix << endl;
}
if (operation.size() > 0) {
cout << operation << endl;
}
Expand All @@ -254,7 +263,7 @@ error::Error InstallAction::Execute(context::MenderContext &main_context) {
}
events::EventLoop loop;
standalone::Context ctx {main_context, loop};
ctx.stop_after = std::move(stop_after_);
ctx.stop_before = std::move(stop_before_);
auto result = standalone::Install(ctx, src_);
err = ResultHandler(result);
if (!reboot_exit_code_
Expand All @@ -269,7 +278,7 @@ error::Error InstallAction::Execute(context::MenderContext &main_context) {
error::Error ResumeAction::Execute(context::MenderContext &main_context) {
events::EventLoop loop;
standalone::Context ctx {main_context, loop};
ctx.stop_after = std::move(stop_after_);
ctx.stop_before = std::move(stop_before_);

auto result = standalone::Resume(ctx);
auto err = ResultHandler(result);
Expand All @@ -286,15 +295,15 @@ error::Error ResumeAction::Execute(context::MenderContext &main_context) {
error::Error CommitAction::Execute(context::MenderContext &main_context) {
events::EventLoop loop;
standalone::Context ctx {main_context, loop};
ctx.stop_after = std::move(stop_after_);
ctx.stop_before = std::move(stop_before_);
auto result = standalone::Commit(ctx);
return ResultHandler(result);
}

error::Error RollbackAction::Execute(context::MenderContext &main_context) {
events::EventLoop loop;
standalone::Context ctx {main_context, loop};
ctx.stop_after = std::move(stop_after_);
ctx.stop_before = std::move(stop_before_);
auto result = standalone::Rollback(ctx);
return ResultHandler(result);
}
Expand Down
6 changes: 3 additions & 3 deletions src/mender-update/cli/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class BaseInstallAction : virtual public Action {
reboot_exit_code_ = val;
}

void SetStopAfter(vector<string> val) {
stop_after_ = std::move(val);
void SetStopBefore(vector<string> val) {
stop_before_ = std::move(val);
}

protected:
bool reboot_exit_code_ {false};
vector<string> stop_after_;
vector<string> stop_before_;
};

class InstallAction : public BaseInstallAction {
Expand Down
55 changes: 31 additions & 24 deletions src/mender-update/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ const conf::CliCommand cmd_check_update {
.description = "Force update check",
};

const conf::CliOption opt_stop_after {
.long_option = "stop-after",
const conf::CliOption opt_stop_before {
.long_option = "stop-before",
.description =
"Stop after the given state has completed. "
"Choices are `Download`, `ArtifactInstall` and `ArtifactCommit`. "
"Stop before entering the given state. "
"Choices are "
"`ArtifactInstall_Enter`, "
"`ArtifactCommit_Enter`, "
"`ArtifactCommit_Leave`, "
"`ArtifactRollback_Enter`, "
"`ArtifactFailure_Enter`, "
"and `Cleanup`. "
"You can later resume the installation by using the `resume` command. "
"Note that the client always stops after `ArtifactInstall` if the update module supports rollback.",
.parameter = "STATE",
Expand All @@ -62,7 +68,7 @@ const conf::CliCommand cmd_commit {
.description = "Commit current Artifact. Returns (2) if no update in progress",
.options =
{
opt_stop_after,
opt_stop_before,
},
};

Expand All @@ -86,7 +92,7 @@ const conf::CliCommand cmd_install {
.description =
"Return exit code 4 if a manual reboot is required after the Artifact installation.",
},
opt_stop_after,
opt_stop_before,
},
};

Expand All @@ -100,7 +106,7 @@ const conf::CliCommand cmd_resume {
.description =
"Return exit code 4 if a manual reboot is required after the Artifact installation.",
},
opt_stop_after,
opt_stop_before,
},
};

Expand All @@ -109,7 +115,7 @@ const conf::CliCommand cmd_rollback {
.description = "Rollback current Artifact. Returns (2) if no update in progress",
.options =
{
opt_stop_after,
opt_stop_before,
},
};

Expand Down Expand Up @@ -155,7 +161,7 @@ static error::Error CommonInstallFlagsHandler(
conf::CmdlineOptionsIterator &iter,
string *filename,
bool *reboot_exit_code,
vector<string> *stop_after) {
vector<string> *stop_before) {
while (true) {
auto arg = iter.Next();
if (!arg) {
Expand All @@ -166,11 +172,12 @@ static error::Error CommonInstallFlagsHandler(
if (reboot_exit_code != nullptr and value.option == "--reboot-exit-code") {
*reboot_exit_code = true;
continue;
} else if (stop_after != nullptr and value.option == "--stop-after") {
} else if (stop_before != nullptr and value.option == "--stop-before") {
if (value.value == "") {
return conf::MakeError(conf::InvalidOptionsError, "--stop-after needs an argument");
return conf::MakeError(
conf::InvalidOptionsError, "--stop-before needs an argument");
}
stop_after->push_back(value.value);
stop_before->push_back(value.value);
continue;
} else if (value.option != "") {
return conf::MakeError(conf::InvalidOptionsError, "No such option: " + value.option);
Expand Down Expand Up @@ -229,53 +236,53 @@ ExpectedActionPtr ParseUpdateArguments(

string filename;
bool reboot_exit_code = false;
vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, &filename, &reboot_exit_code, &stop_after);
vector<string> stop_before;
auto err = CommonInstallFlagsHandler(iter, &filename, &reboot_exit_code, &stop_before);
if (err != error::NoError) {
return expected::unexpected(err);
}

auto install_action = make_shared<InstallAction>(filename);
install_action->SetRebootExitCode(reboot_exit_code);
install_action->SetStopAfter(std::move(stop_after));
install_action->SetStopBefore(std::move(stop_before));
return install_action;
} else if (start[0] == "resume") {
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_resume.options);

bool reboot_exit_code = false;
vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, nullptr, &reboot_exit_code, &stop_after);
vector<string> stop_before;
auto err = CommonInstallFlagsHandler(iter, nullptr, &reboot_exit_code, &stop_before);
if (err != error::NoError) {
return expected::unexpected(err);
}

auto resume_action = make_shared<ResumeAction>();
resume_action->SetRebootExitCode(reboot_exit_code);
resume_action->SetStopAfter(std::move(stop_after));
resume_action->SetStopBefore(std::move(stop_before));
return resume_action;
} else if (start[0] == "commit") {
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_commit.options);

vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_after);
vector<string> stop_before;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_before);
if (err != error::NoError) {
return expected::unexpected(err);
}

auto commit_action = make_shared<CommitAction>();
commit_action->SetStopAfter(std::move(stop_after));
commit_action->SetStopBefore(std::move(stop_before));
return commit_action;
} else if (start[0] == "rollback") {
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_rollback.options);

vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_after);
vector<string> stop_before;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_before);
if (err != error::NoError) {
return expected::unexpected(err);
}

auto rollback_action = make_shared<RollbackAction>();
rollback_action->SetStopAfter(std::move(stop_after));
rollback_action->SetStopBefore(std::move(stop_before));
return rollback_action;
} else if (start[0] == "daemon") {
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_daemon.options);
Expand Down
18 changes: 10 additions & 8 deletions src/mender-update/standalone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StateMachine {
StateMachine(Context &ctx);

error::Error SetStartStateFromStateData(const string &completed_state);
error::Error AddStopAfterState(const string &state);
error::Error AddStopBeforeState(const string &state);
void StartOnRollback();

void Run();
Expand All @@ -81,35 +81,37 @@ class StateMachine {
ScriptRunnerState download_leave_state_;
ScriptRunnerState download_error_state_;

SaveState save_artifact_install_state_;
JustSaveState save_before_artifact_install_state_;
JustSaveState save_artifact_install_state_;
ScriptRunnerState artifact_install_enter_state_;
ArtifactInstallState artifact_install_state_;
ScriptRunnerState artifact_install_leave_state_;
ScriptRunnerState artifact_install_error_state_;

RebootAndRollbackQueryState reboot_and_rollback_query_state_;

SaveState save_artifact_commit_state_;
JustSaveState save_before_artifact_commit_state_;
JustSaveState save_artifact_commit_state_;
ScriptRunnerState artifact_commit_enter_state_;
ArtifactCommitState artifact_commit_state_;
SaveState save_post_artifact_commit_state_;
SaveState save_artifact_commit_leave_state_;
JustSaveState save_before_artifact_commit_leave_state_;
JustSaveState save_artifact_commit_leave_state_;
ScriptRunnerState artifact_commit_leave_state_;
ScriptRunnerState artifact_commit_error_state_;

RollbackQueryState rollback_query_state_;

SaveState save_artifact_rollback_state_;
JustSaveState save_artifact_rollback_state_;
ScriptRunnerState artifact_rollback_enter_state_;
ArtifactRollbackState artifact_rollback_state_;
ScriptRunnerState artifact_rollback_leave_state_;

SaveState save_artifact_failure_state_;
JustSaveState save_artifact_failure_state_;
ScriptRunnerState artifact_failure_enter_state_;
ArtifactFailureState artifact_failure_state_;
ScriptRunnerState artifact_failure_leave_state_;

SaveState save_cleanup_state_;
JustSaveState save_cleanup_state_;
CleanupState cleanup_state_;

ExitState exit_state_;
Expand Down
4 changes: 3 additions & 1 deletion src/mender-update/standalone/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ const string StateDataKeys::in_state {"InState"};
const string StateDataKeys::failed {"Failed"};
const string StateDataKeys::rolled_back {"RolledBack"};

const string StateData::kBeforeStateArtifactInstall_Enter {"Before_ArtifactInstall_Enter"};
const string StateData::kInStateArtifactInstall_Enter {"ArtifactInstall_Enter"};
const string StateData::kBeforeStateArtifactCommit_Enter {"Before_ArtifactCommit_Enter"};
const string StateData::kInStateArtifactCommit_Enter {"ArtifactCommit_Enter"};
const string StateData::kInStatePostArtifactCommit {"PostArtifactCommit"};
const string StateData::kBeforeStateArtifactCommit_Leave {"Before_ArtifactCommit_Leave"};
const string StateData::kInStateArtifactCommit_Leave {"ArtifactCommit_Leave"};
const string StateData::kInStateArtifactRollback_Enter {"ArtifactRollback_Enter"};
const string StateData::kInStateArtifactFailure_Enter {"ArtifactFailure_Enter"};
Expand Down
7 changes: 5 additions & 2 deletions src/mender-update/standalone/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ struct StateData {
bool failed {false};
bool rolled_back {false};

static const string kBeforeStateArtifactInstall_Enter;
static const string kInStateArtifactInstall_Enter;
static const string kBeforeStateArtifactCommit_Enter;
static const string kInStateArtifactCommit_Enter;
static const string kInStatePostArtifactCommit;
static const string kBeforeStateArtifactCommit_Leave;
static const string kInStateArtifactCommit_Leave;
static const string kInStateArtifactRollback_Enter;
static const string kInStateArtifactFailure_Enter;
Expand Down Expand Up @@ -107,6 +109,7 @@ enum class Result {
RollbackFailed = 0x2000,
Cleaned = 0x4000,
CleanupFailed = 0x8000,
AutoCommitWanted = 0x10000,
};

// enum classes cannot ordinarily be used as bit flags, but let's provide some convenience functions
Expand Down Expand Up @@ -149,7 +152,7 @@ struct Context {

StateData state_data;

vector<string> stop_after;
vector<string> stop_before;

string artifact_src;

Expand Down
Loading