2022-01-14 15:29:20 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
2022-01-14 15:29:20 +00:00
|
|
|
)
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
const (
|
|
|
|
defaultDockerRuntime = "runc"
|
|
|
|
)
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// Config defines a docker config file.
|
|
|
|
// TODO: This should not be public, but we need to access it from the tests in tools/container/docker
|
|
|
|
type Config map[string]interface{}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// New creates a docker config with the specified options
|
|
|
|
func New(opts ...Option) (engine.Interface, error) {
|
|
|
|
b := &builder{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(b)
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
if b.logger == nil {
|
|
|
|
b.logger = logger.New()
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
return b.build()
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// AddRuntime adds a new runtime to the docker config
|
|
|
|
func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("config is nil")
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
config := *c
|
2022-01-14 15:29:20 +00:00
|
|
|
|
|
|
|
// Read the existing runtimes
|
|
|
|
runtimes := make(map[string]interface{})
|
|
|
|
if _, exists := config["runtimes"]; exists {
|
|
|
|
runtimes = config["runtimes"].(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add / update the runtime definitions
|
2023-02-23 13:43:15 +00:00
|
|
|
runtimes[name] = map[string]interface{}{
|
|
|
|
"path": path,
|
2022-07-14 14:19:19 +00:00
|
|
|
"args": []string{},
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
config["runtimes"] = runtimes
|
2022-07-14 14:19:19 +00:00
|
|
|
|
|
|
|
if setAsDefault {
|
2023-02-23 13:43:15 +00:00
|
|
|
config["default-runtime"] = name
|
2022-07-14 14:19:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
*c = config
|
2022-01-14 15:29:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// DefaultRuntime returns the default runtime for the docker config
|
|
|
|
func (c Config) DefaultRuntime() string {
|
|
|
|
r, ok := c["default-runtime"].(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// RemoveRuntime removes a runtime from the docker config
|
|
|
|
func (c *Config) RemoveRuntime(name string) error {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
config := *c
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
if _, exists := config["default-runtime"]; exists {
|
|
|
|
defaultRuntime := config["default-runtime"].(string)
|
|
|
|
if defaultRuntime == name {
|
|
|
|
config["default-runtime"] = defaultDockerRuntime
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := config["runtimes"]; exists {
|
|
|
|
runtimes := config["runtimes"].(map[string]interface{})
|
|
|
|
|
|
|
|
delete(runtimes, name)
|
|
|
|
|
|
|
|
if len(runtimes) == 0 {
|
|
|
|
delete(config, "runtimes")
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*c = config
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes the config to the specified path
|
|
|
|
func (c Config) Save(path string) (int64, error) {
|
|
|
|
output, err := json.MarshalIndent(c, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-07-03 13:14:22 +00:00
|
|
|
n, err := engine.Config(path).Write(output)
|
|
|
|
return int64(n), err
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|