-
Notifications
You must be signed in to change notification settings - Fork 725
[feat] Add wait-ready command to check Multipass daemon readiness #4145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df8da29
[feat] Add wait-ready command to check Multipass daemon readiness
rluna319 6b4d58b
[feat] Add improvements and unit testing for wait-ready command
rluna319 a3e6337
[feat] Refactor timeout option handling and wait-ready command
rluna319 3736c56
[feat] Add client retry on DownloadException
rluna319 5b6556a
[feat] Remove error string checks and resolve linter errors
rluna319 dcca413
[feat] Add test coverage for all of wait_ready.cpp
rluna319 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (C) Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "wait_ready.h" | ||
#include "animated_spinner.h" | ||
#include "common_cli.h" | ||
|
||
#include <multipass/cli/argparser.h> | ||
#include <multipass/exceptions/cmd_exceptions.h> | ||
#include <multipass/timer.h> | ||
|
||
#include <chrono> | ||
#include <thread> | ||
|
||
namespace mp = multipass; | ||
namespace cmd = multipass::cmd; | ||
|
||
mp::ReturnCode cmd::WaitReady::run(mp::ArgParser* parser) | ||
{ | ||
auto ret = parse_args(parser); | ||
if (ret != ParseCode::Ok) | ||
{ | ||
return parser->returnCodeFrom(ret); | ||
} | ||
|
||
mp::AnimatedSpinner spinner{cout}; | ||
spinner.start("Waiting for the Multipass daemon to be ready"); | ||
|
||
std::unique_ptr<mp::utils::Timer> timer; | ||
|
||
// If the user has specified a timeout, we will create a timer | ||
if (parser->isSet("timeout")) | ||
{ | ||
timer = cmd::make_timer(parser->value("timeout").toInt(), | ||
&spinner, | ||
cerr, | ||
"Timed out waiting for the Multipass daemon to be ready."); | ||
timer->start(); | ||
} | ||
|
||
auto on_success = [&spinner, &timer](WaitReadyReply& reply) { | ||
if (timer) | ||
timer->stop(); | ||
spinner.stop(); | ||
return ReturnCode::Ok; | ||
}; | ||
|
||
auto on_failure = [this, &spinner, &timer](grpc::Status& status) { | ||
if (status.error_code() == grpc::StatusCode::NOT_FOUND) | ||
{ | ||
// This is the expected state for when the daemon is not yet ready | ||
// Sleep for a short duration and signal to retry | ||
std::this_thread::sleep_for(std::chrono::milliseconds(500)); | ||
return ReturnCode::Retry; | ||
} | ||
|
||
if (timer) | ||
timer->stop(); | ||
spinner.stop(); | ||
|
||
// For any other error, we will handle it as a standard failure | ||
return standard_failure_handler_for(name(), cerr, status); | ||
}; | ||
|
||
request.set_verbosity_level(parser->verbosityLevel()); | ||
|
||
ReturnCode return_code; | ||
|
||
while ((return_code = dispatch(&RpcMethod::wait_ready, request, on_success, on_failure)) == | ||
ReturnCode::Retry) | ||
; | ||
|
||
return return_code; | ||
} | ||
|
||
std::string cmd::WaitReady::name() const | ||
{ | ||
return "wait-ready"; | ||
} | ||
|
||
QString cmd::WaitReady::short_help() const | ||
{ | ||
return QStringLiteral("Wait for the Multipass daemon to be ready"); | ||
} | ||
|
||
QString cmd::WaitReady::description() const | ||
{ | ||
return QStringLiteral( | ||
"Wait for the Multipass daemon to be ready. This command will block until the\n" | ||
"daemon has initialized, fetched up-to-date image information, and is ready to\n" | ||
"accept requests. Its main use is to prevent failures caused by incomplete\n" | ||
"initialization in batch operations. An optional timeout aborts the command if\n" | ||
"reached."); | ||
} | ||
|
||
mp::ParseCode cmd::WaitReady::parse_args(mp::ArgParser* parser) | ||
{ | ||
mp::cmd::add_timeout(parser); | ||
|
||
auto status = parser->commandParse(this); | ||
|
||
// Check if the command was parsed successfully | ||
if (status != ParseCode::Ok) | ||
{ | ||
return status; | ||
} | ||
|
||
try | ||
{ | ||
mp::cmd::parse_timeout(parser); | ||
rluna319 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
catch (const mp::ValidationException& e) | ||
{ | ||
cerr << "error: " << e.what() << "\n"; | ||
return ParseCode::CommandLineError; | ||
} | ||
|
||
return status; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_WAIT_READY_H | ||
#define MULTIPASS_WAIT_READY_H | ||
|
||
#include <multipass/cli/command.h> | ||
|
||
namespace multipass | ||
{ | ||
class Formatter; | ||
|
||
namespace cmd | ||
{ | ||
class WaitReady final : public Command | ||
{ | ||
public: | ||
using Command::Command; | ||
ReturnCode run(ArgParser* parser) override; | ||
|
||
std::string name() const override; | ||
QString short_help() const override; | ||
QString description() const override; | ||
|
||
private: | ||
WaitReadyRequest request; | ||
|
||
ParseCode parse_args(ArgParser* parser); | ||
}; | ||
} // namespace cmd | ||
} // namespace multipass | ||
#endif // MULTIPASS_WAIT_READY_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.