configure.go 4.37 KB
Newer Older
songlf's avatar
songlf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
88
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
144
145
146
147
148
149
150
151
152
153
154
155
/**
# Copyright (c) Advanced Micro Devices, Inc. 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 configure

import (
	"fmt"

	"developer.sourcefind.cn/codes/songlinfeng/container-toolkit/cmd/dcu-ctk/runtime/engine"
	"developer.sourcefind.cn/codes/songlinfeng/container-toolkit/cmd/dcu-ctk/runtime/engine/docker"
	"github.com/urfave/cli/v2"
)

const (
	defaultRuntime              = "docker"
	defaultDcuRuntimeName       = "dcu"
    defaultDcuRuntimeExecutable = "dcu-container-runtime"
	defaultDockerConfigFilePath = "/etc/docker/daemon.json"
)

type configOptions struct {
	runtime        string
	configFilepath string
	setAsDefault   bool
	unSetAsDefault bool
	remove         bool 
}

func AddNewCommand() *cli.Command {
    cfgOptions := configOptions{}

	// Add the configure subcommand
	configureCmd := cli.Command{
		Name: "configure",
		Usage: "Configure a runtime to the container engine",
		Before: func(c *cli.Context) error {
			return validateConfigOptions(c, &cfgOptions)
		},
		Action: func(c *cli.Context) error {
			return performAction(c, &cfgOptions)
		},
	}
	
	configureCmd.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:        "runtime",
			Usage:       "target runtime engine, [docker for now]",
			Value:       defaultRuntime,
			Destination: &cfgOptions.runtime,
		},
		&cli.BoolFlag{
			Name:        "remove",
			Usage:       "remove from target runtimes",
			Destination: &cfgOptions.remove,
		},
		&cli.StringFlag{
			Name:        "config-path",
			Usage:       "path to the configuration file for the target engine",
			Value:       defaultDockerConfigFilePath,
			Destination: &cfgOptions.configFilepath,
		},
		&cli.BoolFlag{
			Name:        "dcu-set-as-default",
			Aliases:     []string{"set-as-default"},
			Usage:       "set DCU runtime as the default",
			Destination: &cfgOptions.setAsDefault,
		},
		&cli.BoolFlag{
			Name:        "unset-dcu-as-default",
			Aliases:     []string{"unset-as-default"},
			Usage:       "remove DCU runtime as the default",
			Destination: &cfgOptions.unSetAsDefault,
		},
	}
	return &configureCmd
}

func validateConfigOptions(c *cli.Context, cfgOptions *configOptions) error {
	if cfgOptions.runtime != "docker" {
		return fmt.Errorf("unsupported runtime engine: %v", cfgOptions.runtime)
	}
	if cfgOptions.setAsDefault && cfgOptions.unSetAsDefault {
		return fmt.Errorf("both set and unset as default cannot be used at the same time")
	}
	if cfgOptions.remove {
		if cfgOptions.setAsDefault || cfgOptions.unSetAsDefault {
			return fmt.Errorf("remove flag cannot be used along with set-as-default flag")
		}
	}
	return nil
}

func performAction(c *cli.Context, cfgOptions *configOptions) error {
	var (
		err           error
		runtimeEngine engine.Interface
		doNotUpdate   bool
	)

	switch cfgOptions.runtime {
	case "docker":
		runtimeEngine, err = docker.New(cfgOptions.configFilepath)
	default:
		return fmt.Errorf("unsupported runtime engine: %v", cfgOptions.runtime)
	}

	if err != nil || runtimeEngine == nil {
		return fmt.Errorf("failed to init config for runtime engine: %v | err: %v", cfgOptions.runtime, err)
	}

	if cfgOptions.unSetAsDefault {
		err = runtimeEngine.UnsetDefaultRuntime()

		if err != nil {
			return fmt.Errorf("failed to unset default runtime: %v", err)
		}
	} else {
		if cfgOptions.remove {
			err, doNotUpdate = runtimeEngine.RemoveRuntime(defaultDcuRuntimeName)
		}  else {
			err = runtimeEngine.ConfigRuntime(defaultDcuRuntimeName, defaultDcuRuntimeExecutable, cfgOptions.setAsDefault)
		}

		if err != nil {
			return fmt.Errorf("failed to update configuration: %v", err)
		}
	}

	// Save the config
	if !doNotUpdate {
		num, err := runtimeEngine.Update(cfgOptions.configFilepath)
		if err != nil {
			return fmt.Errorf("failed to save the config: %v", err)
		}

		if num != 0 {
			fmt.Printf("Updated the config file: %v\n", cfgOptions.configFilepath)
		}
		fmt.Printf("Please restart %v daemon\n", cfgOptions.runtime)
	}
	return nil
}