Skip to content

Commit a7eb1cf

Browse files
committed
Improvements to the HO's cvd command wrapper
Clearly separate what operations act on a group vs on an instance or are global. Use different selector types to find instances vs groups.
1 parent 4e0d83d commit a7eb1cf

16 files changed

+546
-345
lines changed

frontend/src/host_orchestrator/orchestrator/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ go_library(
2121
"//api/v1:api",
2222
"//orchestrator/artifacts",
2323
"//orchestrator/cvd",
24+
"//orchestrator/cvd/output",
2425
"//orchestrator/debug",
2526
"//orchestrator/exec",
2627
"@com_github_google_android_cuttlefish_frontend_src_liboperator//operator",

frontend/src/host_orchestrator/orchestrator/controller.go

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,24 @@ func (c *Controller) AddRoutes(router *mux.Router) {
7070
router.Handle("/cvds/{group}", httpHandler(&listCVDsHandler{Config: c.Config})).Methods("GET")
7171
router.PathPrefix("/cvds/{group}/{name}/logs").Handler(&getCVDLogsHandler{Config: c.Config}).Methods("GET")
7272
router.Handle("/cvds/{group}/:start",
73-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &startCvdCommand{}))).Methods("POST")
73+
httpHandler(newExecCVDGroupCommandHandler(c.Config, c.OperationManager, &startCvdCommand{}))).Methods("POST")
7474
router.Handle("/cvds/{group}/:stop",
75-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &stopCvdCommand{}))).Methods("POST")
75+
httpHandler(newExecCVDGroupCommandHandler(c.Config, c.OperationManager, &stopCvdCommand{}))).Methods("POST")
7676
router.Handle("/cvds/{group}",
77-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &removeCvdCommand{}))).Methods("DELETE")
77+
httpHandler(newExecCVDGroupCommandHandler(c.Config, c.OperationManager, &removeCvdCommand{}))).Methods("DELETE")
7878
// Append `include_adb_bugreport=true` query parameter to include a device `adb bugreport` in the cvd bugreport.
7979
router.Handle("/cvds/{group}/:bugreport",
8080
httpHandler(newCreateCVDBugReportHandler(c.Config, c.OperationManager))).Methods("POST")
8181
router.Handle("/cvds/{group}/{name}",
82-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &removeCvdCommand{}))).Methods("DELETE")
82+
httpHandler(newExecCVDGroupCommandHandler(c.Config, c.OperationManager, &removeCvdCommand{}))).Methods("DELETE")
8383
router.Handle("/cvds/{group}/{name}/:start",
8484
httpHandler(newStartCVDHandler(c.Config, c.OperationManager))).Methods("POST")
8585
router.Handle("/cvds/{group}/{name}/:stop",
86-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &stopCvdCommand{}))).Methods("POST")
86+
httpHandler(newExecCVDGroupCommandHandler(c.Config, c.OperationManager, &stopCvdCommand{}))).Methods("POST")
8787
router.Handle("/cvds/{group}/{name}/:powerwash",
88-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &powerwashCvdCommand{}))).Methods("POST")
88+
httpHandler(newExecCVDInstanceCommandHandler(c.Config, c.OperationManager, &powerwashCvdCommand{}))).Methods("POST")
8989
router.Handle("/cvds/{group}/{name}/:powerbtn",
90-
httpHandler(newExecCVDCommandHandler(c.Config, c.OperationManager, &powerbtnCvdCommand{}))).Methods("POST")
90+
httpHandler(newExecCVDInstanceCommandHandler(c.Config, c.OperationManager, &powerbtnCvdCommand{}))).Methods("POST")
9191
router.Handle("/cvds/{group}/{name}/displays",
9292
httpHandler(newCreateDisplayAddHandler(c.Config, c.OperationManager))).Methods("POST")
9393
router.Handle("/cvds/{group}/{name}/displays",
@@ -291,32 +291,57 @@ func (h *listCVDsHandler) Handle(r *http.Request) (interface{}, error) {
291291
return NewListCVDsAction(opts).Run()
292292
}
293293

294-
type execCVDCommandHandler struct {
294+
type execCVDGroupCommandHandler struct {
295295
Config Config
296296
OM OperationManager
297-
Command execCvdCommand
297+
Command execCvdGroupCommand
298298
}
299299

300-
func newExecCVDCommandHandler(c Config, om OperationManager, command execCvdCommand) *execCVDCommandHandler {
301-
return &execCVDCommandHandler{
300+
func newExecCVDGroupCommandHandler(c Config, om OperationManager, command execCvdGroupCommand) *execCVDGroupCommandHandler {
301+
return &execCVDGroupCommandHandler{
302302
Config: c,
303303
OM: om,
304304
Command: command,
305305
}
306306
}
307307

308-
func (h *execCVDCommandHandler) Handle(r *http.Request) (interface{}, error) {
308+
func (h *execCVDGroupCommandHandler) Handle(r *http.Request) (interface{}, error) {
309+
vars := mux.Vars(r)
310+
group := vars["group"]
311+
opts := ExecCVDGroupCommandActionOpts{
312+
Command: h.Command,
313+
Selector: cvd.GroupSelector{Name: group},
314+
OperationManager: h.OM,
315+
ExecContext: exec.CommandContext,
316+
}
317+
return NewExecCVDGroupCommandAction(opts).Run()
318+
}
319+
320+
type execCVDInstanceCommandHandler struct {
321+
Config Config
322+
OM OperationManager
323+
Command execCvdInstanceCommand
324+
}
325+
326+
func newExecCVDInstanceCommandHandler(c Config, om OperationManager, command execCvdInstanceCommand) *execCVDInstanceCommandHandler {
327+
return &execCVDInstanceCommandHandler{
328+
Config: c,
329+
OM: om,
330+
Command: command,
331+
}
332+
}
333+
334+
func (h *execCVDInstanceCommandHandler) Handle(r *http.Request) (interface{}, error) {
309335
vars := mux.Vars(r)
310336
group := vars["group"]
311337
name := vars["name"]
312-
opts := ExecCVDCommandActionOpts{
338+
opts := ExecCVDInstanceCommandActionOpts{
313339
Command: h.Command,
314-
Selector: cvd.Selector{Group: group, Instance: name},
315-
Paths: h.Config.Paths,
340+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
316341
OperationManager: h.OM,
317342
ExecContext: exec.CommandContext,
318343
}
319-
return NewExecCVDCommandAction(opts).Run()
344+
return NewExecCVDInstanceCommandAction(opts).Run()
320345
}
321346

322347
type createSnapshotHandler struct {
@@ -339,7 +364,7 @@ func (h *createSnapshotHandler) Handle(r *http.Request) (interface{}, error) {
339364
name := vars["name"]
340365
opts := CreateSnapshotActionOpts{
341366
Request: req,
342-
Selector: cvd.Selector{Group: group, Instance: name},
367+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
343368
Paths: h.Config.Paths,
344369
OperationManager: h.OM,
345370
ExecContext: exec.CommandContext,
@@ -368,7 +393,7 @@ func (h *displayAddHandler) Handle(r *http.Request) (interface{}, error) {
368393
name := vars["name"]
369394
opts := DisplayAddActionOpts{
370395
Request: req,
371-
Selector: cvd.Selector{Group: group, Instance: name},
396+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
372397
Paths: h.Config.Paths,
373398
OperationManager: h.OM,
374399
ExecContext: exec.CommandContext,
@@ -390,7 +415,7 @@ func (h *displayListHandler) Handle(r *http.Request) (interface{}, error) {
390415
group := vars["group"]
391416
name := vars["name"]
392417
opts := DisplayListActionOpts{
393-
Selector: cvd.Selector{Group: group, Instance: name},
418+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
394419
Paths: h.Config.Paths,
395420
OperationManager: h.OM,
396421
ExecContext: exec.CommandContext,
@@ -420,7 +445,7 @@ func (h *displayRemoveHandler) Handle(r *http.Request) (interface{}, error) {
420445

421446
opts := DisplayRemoveActionOpts{
422447
DisplayNumber: displayNumber,
423-
Selector: cvd.Selector{Group: group, Instance: name},
448+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
424449
Paths: h.Config.Paths,
425450
OperationManager: h.OM,
426451
ExecContext: exec.CommandContext,
@@ -453,7 +478,7 @@ func (h *displayScreenshotHandler) Handle(r *http.Request) (interface{}, error)
453478
}
454479
opts := DisplayScreenshotActionOpts{
455480
Request: req,
456-
Selector: cvd.Selector{Group: group, Instance: name},
481+
Selector: cvd.InstanceSelector{GroupName: group, Name: name},
457482
Paths: h.Config.Paths,
458483
OperationManager: h.OM,
459484
ExecContext: exec.CommandContext,
@@ -478,10 +503,9 @@ func (h *startCVDHandler) Handle(r *http.Request) (interface{}, error) {
478503
}
479504
vars := mux.Vars(r)
480505
group := vars["group"]
481-
name := vars["name"]
482506
opts := StartCVDActionOpts{
483507
Request: req,
484-
Selector: cvd.Selector{Group: group, Instance: name},
508+
Selector: cvd.GroupSelector{Name: group},
485509
Paths: h.Config.Paths,
486510
OperationManager: h.OM,
487511
ExecContext: exec.CommandContext,

frontend/src/host_orchestrator/orchestrator/createcvdbugreportaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (a *CreateCVDBugReportAction) Run() (apiv1.Operation, error) {
7070
go func(uuid string, op apiv1.Operation) {
7171
dst := filepath.Join(a.paths.CVDBugReportsDir, uuid, BugReportZipFileName)
7272
result := &OperationResult{}
73-
if err := a.cvdCLI.BugReport(cvd.Selector{Group: a.group}, a.includeADBBugreport, dst); err != nil {
73+
if err := a.cvdCLI.LazySelectGroup(cvd.GroupSelector{Name: a.group}).BugReport(a.includeADBBugreport, dst); err != nil {
7474
result.Error = operator.NewInternalError("`cvd host_bugreport` failed: ", err)
7575
} else {
7676
result.Value = uuid

frontend/src/host_orchestrator/orchestrator/createsnapshotaction.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import (
2626

2727
type CreateSnapshotActionOpts struct {
2828
Request *apiv1.CreateSnapshotRequest
29-
Selector cvd.Selector
29+
Selector cvd.InstanceSelector
3030
Paths IMPaths
3131
OperationManager OperationManager
3232
ExecContext exec.ExecContext
3333
}
3434

3535
type CreateSnapshotAction struct {
3636
req *apiv1.CreateSnapshotRequest
37-
selector cvd.Selector
37+
selector cvd.InstanceSelector
3838
paths IMPaths
3939
om OperationManager
4040
cvdCLI *cvd.CLI
@@ -69,7 +69,8 @@ func (a *CreateSnapshotAction) Run() (apiv1.Operation, error) {
6969
}
7070

7171
func (a *CreateSnapshotAction) createSnapshot(op apiv1.Operation) (*apiv1.CreateSnapshotResponse, error) {
72-
if err := a.cvdCLI.Suspend(a.selector); err != nil {
72+
instance := a.cvdCLI.LazySelectInstance(a.selector)
73+
if err := instance.Suspend(); err != nil {
7374
return nil, operator.NewInternalError("cvd suspend failed", err)
7475
}
7576
snapshotID := a.req.SnapshotID
@@ -79,10 +80,10 @@ func (a *CreateSnapshotAction) createSnapshot(op apiv1.Operation) (*apiv1.Create
7980
dir := filepath.Join(a.paths.SnapshotsRootDir, snapshotID)
8081
// snapshot_util_cvd makes sure the directory is not being used before.
8182
// https://github.com/google/android-cuttlefish/blob/7fb47855a2c937e4531044437616bd82e11e3b2e/base/cvd/cuttlefish/host/commands/snapshot_util_cvd/main.cc#L97
82-
if err := a.cvdCLI.TakeSnapshot(a.selector, dir); err != nil {
83+
if err := instance.TakeSnapshot(dir); err != nil {
8384
return nil, operator.NewInternalError("cvd snapshot_take failed", err)
8485
}
85-
if err := a.cvdCLI.Resume(a.selector); err != nil {
86+
if err := instance.Resume(); err != nil {
8687
return nil, operator.NewInternalError("cvd resume failed", err)
8788
}
8889
return &apiv1.CreateSnapshotResponse{SnapshotID: snapshotID}, nil

frontend/src/host_orchestrator/orchestrator/cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"//orchestrator/exec",
10+
"//orchestrator/cvd/output",
1011
],
1112
)
1213

0 commit comments

Comments
 (0)