status.go 1.18 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
package status

import (
4
	dcuTracker "dcu-container-toolkit/internal/dcu-tracker"
songlinfeng's avatar
songlinfeng committed
5
6
7
	"fmt"
	"os/user"

8
	"dcu-container-toolkit/internal/logger"
songlinfeng's avatar
songlinfeng committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

	"github.com/urfave/cli/v2"
)

type command struct {
	logger logger.Interface
}

func NewCommand(logger logger.Interface) *cli.Command {
	c := command{
		logger: logger,
	}
	return c.build()
}
func (c *command) build() *cli.Command {
	dcuTrackerStatusCmd := cli.Command{
		Name:      "status",
		Usage:     "Show Status of DCUs",
27
		UsageText: "dcu-ctk dcu-tracker status [options]",
songlinfeng's avatar
songlinfeng committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
		Before: func(c *cli.Context) error {
			return validateGenOptions(c)
		},
		Action: func(c *cli.Context) error {
			return performAction(c)
		},
	}

	return &dcuTrackerStatusCmd
}

func validateGenOptions(c *cli.Context) error {
	curUser, err := user.Current()
	if err != nil || curUser.Uid != "0" {
		return fmt.Errorf("Permission denied: Not running as root")
	}

	return nil
}

func performAction(c *cli.Context) error {
	dcuTracker, err := dcuTracker.New()
	if err != nil {
		return fmt.Errorf("Failed to create GPU tracker, Error: %v", err)
	}

	err = dcuTracker.ShowStatus()
	if err != nil {
		return fmt.Errorf("Failed to show GPUs status, Error: %v", err)
	}

	return nil
}