Skip to content

Commit

Permalink
feat: implement remove runtime when host not have nvidia GPU (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Jan 25, 2024
1 parent 5f73e12 commit dc874df
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
After=casaos-message-bus.service
After=docker.service
Description=CasaOS App Management Service

[Service]
ExecStartPre=/usr/bin/casaos-app-management -v
ExecStart=/usr/bin/casaos-app-management -c /etc/casaos/app-management.conf --removeRuntimeIfNoNvidiaGPU true
PIDFile=/var/run/casaos/app-management.pid
Restart=always
Type=notify

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ require (
)

require (
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha8
github.com/compose-spec/compose-go v1.20.2
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/docker/cli v24.0.7+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg6
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2 h1:8PuukQvQIlkw9mkK4THKTbSDMio9I6kbXulDTBxG2d0=
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha8 h1:LS04XphINO7Z0yemDifFloWh59ucBv5DYWEw67Kt0gw=
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha8/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
{
configFlag := flag.String("c", "", "config file path")
versionFlag := flag.Bool("v", false, "version")
removeRuntimeIfNoNvidiaGPUFlag := flag.Bool("removeRuntimeIfNoNvidiaGPU", false, "remove runtime with nvidia gpu")

flag.Parse()

Expand All @@ -65,6 +66,8 @@ func main() {
logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)

service.MyService = service.NewService(config.CommonInfo.RuntimePath)

config.RemoveRuntimeIfNoNvidiaGPUFlag = *removeRuntimeIfNoNvidiaGPUFlag
}

// setup cron
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import (
var (
AppManagementConfigFilePath = filepath.Join(constants.DefaultConfigPath, "app-management.conf")
AppManagementGlobalEnvFilePath = filepath.Join(constants.DefaultConfigPath, "env")
RemoveRuntimeIfNoNvidiaGPUFlag = false
)
27 changes: 27 additions & 0 deletions service/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/IceWhaleTech/CasaOS-AppManagement/common"
"github.com/IceWhaleTech/CasaOS-AppManagement/pkg/config"
"github.com/IceWhaleTech/CasaOS-AppManagement/pkg/docker"
"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/IceWhaleTech/CasaOS-Common/utils"
"github.com/IceWhaleTech/CasaOS-Common/utils/file"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
composeCmd "github.com/docker/compose/v2/cmd/compose"

"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -945,6 +947,14 @@ func LoadComposeAppFromConfigFile(appID string, configFile string) (*ComposeApp,
return (*ComposeApp)(project), err
}

func removeRuntime(a *ComposeApp) {
for i := range a.Services {
a.Services[i].Runtime = ""
}
}

var gpuCache *([]external.GPUInfo) = nil

func NewComposeAppFromYAML(yaml []byte, skipInterpolation, skipValidation bool) (*ComposeApp, error) {
tmpWorkingDir, err := os.MkdirTemp("", "casaos-compose-app-*")
if err != nil {
Expand Down Expand Up @@ -1025,6 +1035,23 @@ func NewComposeAppFromYAML(yaml []byte, skipInterpolation, skipValidation bool)
composeApp.SetTitle(composeApp.Name, common.DefaultLanguage)
}

if config.RemoveRuntimeIfNoNvidiaGPUFlag {
// if gpuCache is nil, it means it is first time fetching gpu info
if gpuCache == nil {
value, err := external.GPUInfoList()
if err != nil {
gpuCache = &([]external.GPUInfo{})
} else {
gpuCache = &value
}
}

// without nvidia-smi // no gpu or first time fetching gpu info failed
if err != nil || len(*gpuCache) == 0 {
removeRuntime(composeApp)
}
}

// pass icon information to v1 label for backward compatibility, because we are
// still using `func getContainerStats()` from `container.go` to get container stats
// (we are being lazy to upgrade that v1 API to v2 - please help if you can :D)
Expand Down

0 comments on commit dc874df

Please sign in to comment.