"examples/lm_finetuning/simple_lm_finetuning.py" did not exist on "786cc41299510ef8ee9973519d202b6ae683a293"
config.go 5.42 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
8
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package config

import (
	"bufio"
9
10
	"dcu-container-toolkit/internal/logger"
	"dcu-container-toolkit/internal/lookup"
songlinfeng's avatar
songlinfeng committed
11
12
13
14
15
16
17
18
19
	"os"
	"path/filepath"
	"strings"

	"tags.cncf.io/container-device-interface/pkg/cdi"
)

const (
	configOverride = "XDG_CONFIG_HOME"
20
	configFilePath = "dcu-container-runtime/config.toml"
songlinfeng's avatar
songlinfeng committed
21

22
23
24
	dtkCTKExecutable          = "dcu-ctk"
	dtkCTKDefaultFilePath     = "/usr/bin/dcu-ctk"
	dtkCDIHookDefaultFilePath = "/usr/bin/dcu-cdi-hook"
songlinfeng's avatar
songlinfeng committed
25

26
27
	// dtkContainerRuntimeHookExecutable  = "dcu-container-runtime-hook"
	// dtkContainerRuntimeHookDefaultPath = "/usr/bin/dcu-container-runtime-hook"
songlinfeng's avatar
songlinfeng committed
28
29
30
31
32
33
34
)

var (
	// DefaultExecutableDir specifies the default path to use for executables if they cannot be located in the path.
	DefaultExecutableDir = "/usr/bin"

	// DTKContainerRuntimeHookExecutable is the executable name for the DTK Container Runtime Hook
35
	DTKContainerRuntimeHookExecutable = "dcu-container-runtime-hook"
songlinfeng's avatar
songlinfeng committed
36
	// DTKContainerToolkitExecutable is the executable name for the DTK Container Toolkit (an alias for the DTK Container Runtime Hook)
37
	DTKContainerToolkitExecutable = "dcu-container-toolkit"
songlinfeng's avatar
songlinfeng committed
38
39
40
41
42
43
44
45
46
47
)

// Config represents the contents of the config.toml file for the DTK Container Toolkit
type Config struct {
	DisableRequire                 bool   `toml:"disable-require"`
	SwarmResource                  string `toml:"swarm-resource"`
	AcceptEnvvarUnprivileged       bool   `toml:"accept-dtk-visible-devices-envvar-when-unprivileged"`
	AcceptDeviceListAsVolumeMounts bool   `toml:"accept-dtk-visible-devices-as-volume-mounts"`
	// SupportedDriverCapabilities    string `toml:"supported-driver-capabilities"`

48
49
	DTKCTKConfig              CTKConfig     `toml:"dcu-ctk"`
	DTKContainerRuntimeConfig RuntimeConfig `toml:"dcu-container-runtime"`
songlinfeng's avatar
songlinfeng committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

// GetConfigFilePath returns the path to the config file for the configured system
func GetConfigFilePath() string {
	if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 {
		return filepath.Join(XDGConfigDir, configFilePath)
	}

	return filepath.Join("/etc", configFilePath)
}

// GetConfig sets up the config struct. Values are read from a toml file
// or set via the environment.
func GetConfig() (*Config, error) {
	cfg, err := New(
		WithConfigFile(GetConfigFilePath()),
	)
	if err != nil {
		return nil, err
	}

	return cfg.Config()
}

// GetDefault defines the default values for the config
func GetDefault() (*Config, error) {
	d := Config{
		AcceptEnvvarUnprivileged: true,
		DTKCTKConfig: CTKConfig{
			Path: dtkCTKExecutable,
		},
		DTKContainerRuntimeConfig: RuntimeConfig{
			DebugFilePath: "/dev/null",
			LogLevel:      "info",
			Runtimes:      []string{"docker-runc", "runc", "crun"},
			Mode:          "auto",
			Modes: modesConfig{
				CSV: csvModeConfig{
88
					MountSpecPath: "/etc/dcu-container-runtime/host-files-for-container.d",
songlinfeng's avatar
songlinfeng committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
				},
				CDI: cdiModeConfig{
					DefaultKind:        "c-3000.com/hcu",
					AnnotationPrefixes: []string{cdi.AnnotationPrefix},
					SpecDirs:           cdi.DefaultSpecDirs,
				},
			},
		},
	}
	return &d, nil
}

func GetUserGroup() string {
	if isSuse() {
		return "root:video"
	}
	return ""
}

// isSuse returns whether a SUSE-based distribution was detected.
func isSuse() bool {
	suseDists := map[string]bool{
		"suse":     true,
		"opensuse": true,
	}

	idsLike := getDistIDLike()
	for _, id := range idsLike {
		if suseDists[id] {
			return true
		}
	}
	return false
}

// getDistIDLike returns the ID_LIKE field from /etc/os-release.
// We can override this for testing.
var getDistIDLike = func() []string {
	releaseFile, err := os.Open("/etc/os-release")
	if err != nil {
		return nil
	}
	defer releaseFile.Close()

	scanner := bufio.NewScanner(releaseFile)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "ID_LIKE=") {
			value := strings.Trim(strings.TrimPrefix(line, "ID_LIKE="), "\"")
			return strings.Split(value, " ")
		}
	}
	return nil
}

144
// ResolveDTKCDIHookPath resolves the path to the dcu-cdi-hook binary.
songlinfeng's avatar
songlinfeng committed
145
146
147
148
// This executable is used in hooks and needs to be an absolute path.
// If the path is specified as an absolute path, it is used directly
// without checking for existence of an executable at that path.
func ResolveDTKCDIHookPath(logger logger.Interface, dtkCDIHookPath string) string {
149
	if filepath.Base(dtkCDIHookPath) == "dcu-ctk" {
songlinfeng's avatar
songlinfeng committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
		return resolveWithDefault(
			logger,
			"DTK Container Toolkit CLI",
			dtkCDIHookPath,
			dtkCTKDefaultFilePath,
		)
	}
	return resolveWithDefault(
		logger,
		"DTK CDI Hook CLI",
		dtkCDIHookPath,
		dtkCDIHookDefaultFilePath,
	)
}

// resolveWithDefault resolves the path to the specified binary.
// If an absolute path is specified, it is used directly without searching for the binary.
// If the binary cannot be found in the path, the specified default is used instead.
func resolveWithDefault(logger logger.Interface, label string, path string, defaultPath string) string {
	if filepath.IsAbs(path) {
		logger.Debugf("Using specified %v path %v", label, path)
		return path
	}

	if path == "" {
		path = filepath.Base(defaultPath)
	}
	logger.Debugf("Locating %v as %v", label, path)
	lookup := lookup.NewExecutableLocator(logger, "")

	resolvedPath := defaultPath
	targets, err := lookup.Locate(path)
	if err != nil {
		logger.Warningf("Failed to locate %v: %v", path, err)
	} else {
		logger.Debugf("Found %v candidates: %v", path, targets)
		resolvedPath = targets[0]
	}
	logger.Debugf("Using %v path %v", label, resolvedPath)

	return resolvedPath
}