Skip to content

Commit 2907b20

Browse files
committed
Move the reset operation to the instance manager
1 parent c55cfba commit 2907b20

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

base/cvd/cuttlefish/host/commands/cvd/cli/commands/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ cf_cc_library(
303303
"//cuttlefish/host/commands/cvd/cli:types",
304304
"//cuttlefish/host/commands/cvd/cli/commands:command_handler",
305305
"//cuttlefish/host/commands/cvd/instances",
306-
"//cuttlefish/host/commands/cvd/instances:reset_client_utils",
307306
"//cuttlefish/host/commands/cvd/utils",
308307
"//libbase",
309308
],

base/cvd/cuttlefish/host/commands/cvd/cli/commands/clear.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CvdClearCommandHandler::CvdClearCommandHandler(
5353

5454
Result<void> CvdClearCommandHandler::Handle(const CommandRequest& request) {
5555
CF_EXPECT(CanHandle(request));
56-
CF_EXPECT(instance_manager_.CvdClear());
56+
CF_EXPECT(instance_manager_.Clear());
5757
return {};
5858
}
5959

base/cvd/cuttlefish/host/commands/cvd/cli/commands/reset.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727

2828
#include <android-base/logging.h>
2929

30-
#include "cuttlefish/common/libs/utils/files.h"
3130
#include "cuttlefish/common/libs/utils/flag_parser.h"
3231
#include "cuttlefish/common/libs/utils/result.h"
3332
#include "cuttlefish/host/commands/cvd/cli/command_request.h"
3433
#include "cuttlefish/host/commands/cvd/cli/commands/command_handler.h"
3534
#include "cuttlefish/host/commands/cvd/cli/types.h"
3635
#include "cuttlefish/host/commands/cvd/instances/instance_manager.h"
37-
#include "cuttlefish/host/commands/cvd/instances/reset_client_utils.h"
3836
#include "cuttlefish/host/commands/cvd/utils/common.h"
3937

4038
namespace cuttlefish {
@@ -142,15 +140,11 @@ class CvdResetCommandHandler : public CvdCommandHandler {
142140
return {};
143141
}
144142

145-
instance_manager_.CvdClear();
146-
// The instance database is obsolete now, clear it.
147-
auto instance_db_deleted = RemoveFile(InstanceDatabasePath());
148-
if (!instance_db_deleted) {
149-
LOG(ERROR) << "Error deleting instance database file";
143+
if (options.clean_runtime_dir) {
144+
CF_EXPECT(instance_manager_.ResetAndClearInstanceDirs());
145+
} else {
146+
CF_EXPECT(instance_manager_.Reset());
150147
}
151-
152-
CF_EXPECT(KillAllCuttlefishInstances(
153-
/* clear_instance_dirs*/ options.clean_runtime_dir));
154148
return {};
155149
}
156150
cvd_common::Args CmdList() const override { return {kResetSubcmd}; }

base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ cf_cc_library(
138138
"//cuttlefish/host/commands/cvd/instances:config_path",
139139
"//cuttlefish/host/commands/cvd/instances:data_viewer",
140140
"//cuttlefish/host/commands/cvd/instances:device_name",
141+
"//cuttlefish/host/commands/cvd/instances:reset_client_utils",
141142
"//cuttlefish/host/commands/cvd/instances:run_cvd_proc_collector",
142143
"//cuttlefish/host/commands/cvd/instances/lock",
143144
"//cuttlefish/host/commands/cvd/utils",
@@ -165,7 +166,6 @@ cf_cc_library(
165166
"//cuttlefish/common/libs/utils:result",
166167
"//cuttlefish/common/libs/utils:subprocess",
167168
"//cuttlefish/common/libs/utils:subprocess_managed_stdio",
168-
"//cuttlefish/host/commands/cvd/instances",
169169
"//cuttlefish/host/commands/cvd/instances:run_cvd_proc_collector",
170170
"//cuttlefish/host/commands/cvd/utils",
171171
"//libbase",

base/cvd/cuttlefish/host/commands/cvd/instances/instance_manager.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "cuttlefish/host/commands/cvd/instances/instance_record.h"
4040
#include "cuttlefish/host/commands/cvd/instances/lock/instance_lock.h"
4141
#include "cuttlefish/host/commands/cvd/instances/lock/lock_file.h"
42+
#include "cuttlefish/host/commands/cvd/instances/reset_client_utils.h"
4243
#include "cuttlefish/host/commands/cvd/utils/common.h"
4344
#include "cuttlefish/host/libs/config/config_constants.h"
4445
#include "cuttlefish/host/libs/config/config_utils.h"
@@ -259,7 +260,7 @@ Result<void> InstanceManager::IssueStopCommand(
259260
return {};
260261
}
261262

262-
Result<void> InstanceManager::CvdClear() {
263+
Result<void> InstanceManager::Clear() {
263264
const std::string config_json_name =
264265
android::base::Basename(GetGlobalConfigFileLink());
265266
auto instance_groups =
@@ -289,12 +290,32 @@ Result<void> InstanceManager::CvdClear() {
289290
RemoveFile(group.HomeDir() + config_json_name);
290291
RemoveGroupDirectory(group);
291292
}
292-
// TODO(kwstephenkim): we need a better mechanism to make sure that
293-
// we clear all run_cvd processes.
294293
std::cerr << "Stopped all known instances\n";
295294
return {};
296295
}
297296

297+
Result<void> InstanceManager::Reset() {
298+
CF_EXPECT(Clear());
299+
auto instance_db_deleted = RemoveFile(InstanceDatabasePath());
300+
if (!instance_db_deleted) {
301+
LOG(ERROR) << "Error deleting instance database file";
302+
}
303+
304+
CF_EXPECT(KillAllCuttlefishInstances(false));
305+
return {};
306+
}
307+
308+
Result<void> InstanceManager::ResetAndClearInstanceDirs() {
309+
CF_EXPECT(Clear());
310+
auto instance_db_deleted = RemoveFile(InstanceDatabasePath());
311+
if (!instance_db_deleted) {
312+
LOG(ERROR) << "Error deleting instance database file";
313+
}
314+
315+
CF_EXPECT(KillAllCuttlefishInstances(true));
316+
return {};
317+
}
318+
298319
Result<std::vector<LocalInstanceGroup>> InstanceManager::FindGroups(
299320
const InstanceDatabase::Filter& filter) const {
300321
return instance_db_.FindGroups(filter);

base/cvd/cuttlefish/host/commands/cvd/instances/instance_manager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ class InstanceManager {
5757
Result<void> UpdateInstanceGroup(const LocalInstanceGroup& group);
5858
Result<bool> RemoveInstanceGroup(LocalInstanceGroup group);
5959

60-
Result<void> CvdClear();
60+
// Stops and removes all known instance instance groups
61+
Result<void> Clear();
62+
// Similar to Clear(), but also attempts to stop devices owned by the current
63+
// user and not tracked in the instance database.
64+
Result<void> Reset();
65+
Result<void> ResetAndClearInstanceDirs();
6166

6267
Result<std::vector<LocalInstanceGroup>> FindGroups(
6368
const InstanceDatabase::Filter& filter) const;

0 commit comments

Comments
 (0)