forked from datarobot-oss/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprerequisites.go
More file actions
303 lines (242 loc) · 8.94 KB
/
Copy pathprerequisites.go
File metadata and controls
303 lines (242 loc) · 8.94 KB
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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2026 DataRobot, Inc. and its affiliates.
//
// 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 tools
import (
"fmt"
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/datarobot/cli/internal/log"
"github.com/datarobot/cli/internal/misc/regexp2"
"github.com/datarobot/cli/internal/repo"
"github.com/datarobot/cli/internal/state"
"github.com/datarobot/cli/internal/version"
)
// InstallCommands holds platform-specific install commands for a dependency.
type InstallCommands struct {
MacOS string `yaml:"macos"`
Linux string `yaml:"linux"`
Windows string `yaml:"windows"`
}
// Prerequisite represents a required tool
type Prerequisite struct {
Key string
Name string `yaml:"name"`
MinimumVersion string `yaml:"minimum-version"`
Command string `yaml:"command"`
URL string `yaml:"url"`
Install InstallCommands `yaml:"install"`
}
// PlatformInstallCommand returns the install command for the current OS.
// Returns an error if no command is defined for this platform.
func (p Prerequisite) PlatformInstallCommand() (string, error) {
var cmd string
switch runtime.GOOS {
case "darwin":
cmd = p.Install.MacOS
case "linux":
cmd = p.Install.Linux
case "windows":
cmd = p.Install.Windows
default:
return "", fmt.Errorf("unsupported platform %q", runtime.GOOS)
}
if cmd == "" {
return "", fmt.Errorf("no install command defined for %q on %s", p.Name, runtime.GOOS)
}
return cmd, nil
}
var pythonInstallCmd = InstallCommands{
MacOS: "brew install python",
Linux: "sudo apt-get install python3",
// Windows: "Download and install Python from https://www.python.org/downloads/windows/",
}
var uvInstallCmd = InstallCommands{
MacOS: "brew install uv",
Linux: "curl -Ls https://astral.sh/uv/install.sh | sh",
// Windows: "iwr -useb https://astral.sh/uv/install.ps1 | iex",
}
var taskInstallCmd = InstallCommands{
MacOS: "brew install go-task/tap/go-task",
Linux: "curl -sL https://taskfile.dev/install.sh | sh",
// Windows: "iwr -useb https://taskfile.dev/install.ps1 | iex",
}
var pulumiInstallCmd = InstallCommands{
MacOS: "brew install pulumi",
Linux: "curl -fsSL https://get.pulumi.com | sh",
// Windows: "iwr -useb https://get.pulumi.com/windows-installer.exe -OutFile pulumi-installer.exe; Start-Process -FilePath pulumi-installer.exe -Wait",
}
// RequiredTools lists all tools required for the quickstart process
var RequiredTools = []Prerequisite{
{Name: "Python", Command: "python3 --version", URL: "https://www.python.org/downloads/", MinimumVersion: "3.9.6", Install: pythonInstallCmd},
{Name: "uv", Command: "uv --version", URL: "https://docs.astral.sh/uv/getting-started/installation/", MinimumVersion: "0.11.20", Install: uvInstallCmd},
{Name: "task", Command: "task --version", URL: "https://taskfile.dev/docs/installation", MinimumVersion: "3.50.0", Install: taskInstallCmd},
{Name: "pulumi", Command: "pulumi version", URL: "https://www.pulumi.com/docs/get-started/download-install/", MinimumVersion: "3.245.0", Install: pulumiInstallCmd},
}
func CheckPrerequisite(name string) error {
for _, tool := range RequiredTools {
if tool.Name == name {
if !isInstalled(tool.Command) {
return fmt.Errorf("%s is not installed.", name)
}
}
}
return nil
}
// CheckResult holds the outcome of a CheckPrerequisites call.
type CheckResult struct {
MissingTools []Prerequisite
WrongVersionTools []Prerequisite
MissingMsgs []string
WrongVersionMsgs []string
ValidationViolations []string
}
// CheckPrerequisites returns lists of missing prerequisites, wrongVersion prerequisites, and error messages to display to the user.
func CheckPrerequisites() CheckResult {
prerequisites, violations, err := GetRequirements()
if err == nil {
RequiredTools = prerequisites
}
log.Debug("deps: checking prerequisites", "count", len(RequiredTools))
var result CheckResult
result.ValidationViolations = violations
for _, tool := range RequiredTools {
if !isInstalled(tool.Command) {
log.Debug("deps: tool missing", "name", tool.Name)
result.MissingTools = append(result.MissingTools, tool)
result.MissingMsgs = append(result.MissingMsgs, fmt.Sprintf("%s %s (%s)", tool.Name, tool.MinimumVersion, tool.URL))
} else if ver, ok := isVersionInstalled(tool); !ok {
log.Debug("deps: tool wrong version", "name", tool.Name, "msg", ver)
result.WrongVersionTools = append(result.WrongVersionTools, tool)
result.WrongVersionMsgs = append(result.WrongVersionMsgs, ver)
} else {
log.Debug("deps: tool ok", "name", tool.Name)
}
}
if len(result.MissingMsgs) == 0 && len(result.WrongVersionMsgs) == 0 {
log.Debug("deps: all prerequisites satisfied")
if repoRoot, err := repo.FindRepoRoot(); err == nil {
err := state.UpdateAfterSuccessDepsCheck(repoRoot)
if err != nil {
log.Errorf("Failed to update state AfterSuccessDepsCheck: %v", err)
}
}
}
return result
}
// PrerequisitesMsg formats the message to display to the user about missing/wrong-version prerequisites.
func PrerequisitesMsg(missingMsgs []string, wrongVersionMsgs []string) string {
result := make([]string, 0)
if len(missingMsgs) > 0 {
result = append(result, "\n ❌ Missing required tools:\n")
for _, msg := range missingMsgs {
result = append(result, "\t- "+msg)
}
}
if len(wrongVersionMsgs) > 0 {
result = append(result, "\n ⚠️ Wrong versions of tools:\n")
for _, msg := range wrongVersionMsgs {
result = append(result, "\t- "+msg)
}
}
return strings.Join(result, "\n") + "\n"
}
func commandArgs(fullCommand string) (string, []string) {
command := strings.Split(fullCommand, " ")
if len(command) == 0 {
return "", nil
}
return command[0], command[1:]
}
// isInstalled checks if a command is available in the system PATH
func isInstalled(fullCommand string) bool {
command, _ := commandArgs(fullCommand)
if command == "dr" {
return true
}
_, err := exec.LookPath(command)
return err == nil
}
// isVersionInstalled checks if a command has proper version installed
func isVersionInstalled(tool Prerequisite) (string, bool) {
// Return success result if no version or no version command specified
if tool.MinimumVersion == "" || tool.Command == "" {
return "", true
}
if tool.Key == "dr" {
if !SufficientSelfVersion(tool.MinimumVersion) {
return fmt.Sprintf("%s (minimal: v%s, installed: %s)\n%s\n",
tool.Name, tool.MinimumVersion, version.Version, tool.URL), false
}
return "", true
}
command, args := commandArgs(tool.Command)
versionOutput, err := exec.Command(command, args...).Output()
if err != nil {
return fmt.Sprintf("%s (minimal: v%s, installed: unknown)\n%s\n",
tool.Name, tool.MinimumVersion, tool.URL), false
}
if versionInstalled, ok := sufficientVersion(string(versionOutput), tool.MinimumVersion); !ok {
return fmt.Sprintf("%s (minimal: v%s, installed: %s)\n%s\n",
tool.Name, tool.MinimumVersion, versionInstalled, tool.URL), false
}
return "", true
}
func SufficientSelfVersion(minimal string) bool {
if version.Version == "dev" {
return true
}
if minimal == "" {
return false
}
_, sufficient := sufficientVersion(version.Version, minimal)
return sufficient
}
func sufficientVersion(versionOutput, minimalStr string) (string, bool) {
expr := regexp.MustCompile(`v?(?P<major>\d+)(.(?P<minor>\d+)(.(?P<patch>\d+))?)?`)
installed := regexp2.NamedIntMatches(expr, versionOutput)
minimal := regexp2.NamedIntMatches(expr, minimalStr)
installedStr := fmt.Sprintf("v%d.%d.%d", installed["major"], installed["minor"], installed["patch"])
if installed["major"] < minimal["major"] {
return installedStr, false
} else if installed["major"] == minimal["major"] && installed["minor"] < minimal["minor"] {
return installedStr, false
} else if installed["major"] == minimal["major"] && installed["minor"] == minimal["minor"] && installed["patch"] < minimal["patch"] {
return installedStr, false
}
return installedStr, true
}
// CheckTool verifies if a specific tool is installed
func CheckTool(name string) error {
for _, tool := range RequiredTools {
if tool.Name == name {
if !isInstalled(tool.Command) {
return fmt.Errorf("%s is not installed.", name)
}
return nil
}
}
return fmt.Errorf("Unknown tool: %s.", name)
}
// GetMissingTools returns a list of missing prerequisite tools
func GetMissingTools() []string {
var missing []string
for _, tool := range RequiredTools {
if !isInstalled(tool.Command) {
missing = append(missing, tool.Name)
}
}
return missing
}