-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtools-installer.go
More file actions
461 lines (400 loc) · 14.2 KB
/
tools-installer.go
File metadata and controls
461 lines (400 loc) · 14.2 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package config
import (
"bytes"
"codacy/cli-v2/constants"
"codacy/cli-v2/plugins"
"codacy/cli-v2/utils"
"codacy/cli-v2/utils/logger"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/sirupsen/logrus"
)
// InstallTools installs all tools defined in the configuration
func InstallTools(config *ConfigType, registry string) error {
var failedTools []string
for name, toolInfo := range config.Tools() {
logger.Info("Starting tool installation", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
})
fmt.Printf("Installing tool: %s v%s...\n", name, toolInfo.Version)
err := InstallTool(name, toolInfo, registry)
if err != nil {
logger.Error("Failed to install tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
"error": err.Error(),
})
failedTools = append(failedTools, name)
continue
}
logger.Info("Successfully installed tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
})
fmt.Printf("Successfully installed %s v%s\n", name, toolInfo.Version)
}
if len(failedTools) > 0 {
return fmt.Errorf("failed to install the following tools: %v", failedTools)
}
return nil
}
// InstallTool installs a specific tool
func InstallTool(name string, toolInfo *plugins.ToolInfo, registry string) error {
// If toolInfo is nil, it means the tool is not in the configuration
// Add it with the default version
if toolInfo == nil {
logger.Info("Tool not found in configuration, adding with default version", logrus.Fields{
"tool": name,
})
// Add the tool to the configuration with its default version
err := Config.AddToolWithDefaultVersion(name)
if err != nil {
return fmt.Errorf("failed to add tool %s to configuration: %w", name, err)
}
// Get the tool info from the updated configuration
toolInfo = Config.Tools()[name]
if toolInfo == nil {
return fmt.Errorf("tool %s still not found in configuration after adding", name)
}
}
// Check if the tool is already installed AND its runtime is available
isToolInstalled := Config.IsToolInstalled(name, toolInfo)
// Also check if the required runtime is installed
var isRuntimeInstalled bool
if toolInfo.Runtime != "" {
runtimeInfo, exists := Config.Runtimes()[toolInfo.Runtime]
isRuntimeInstalled = exists && Config.IsRuntimeInstalled(toolInfo.Runtime, runtimeInfo)
} else {
// No runtime dependency
isRuntimeInstalled = true
}
if isToolInstalled && isRuntimeInstalled {
logger.Info("Tool and runtime already installed", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
})
fmt.Printf("Tool %s v%s and its runtime are already installed\n", name, toolInfo.Version)
return nil
}
// Make sure the installation directory exists
err := os.MkdirAll(toolInfo.InstallDir, constants.DefaultDirPerms)
if err != nil {
return fmt.Errorf("failed to create installation directory: %w", err)
}
// Check if this is a download-based tool (like trivy) or a runtime-based tool (like eslint)
if toolInfo.DownloadURL != "" {
// This is a download-based tool
logger.Debug("Installing download-based tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"downloadURL": toolInfo.DownloadURL,
})
fmt.Printf("Downloading %s...\n", name)
return installDownloadBasedTool(toolInfo)
}
// Handle Python tools differently
if toolInfo.Runtime == "python" {
logger.Debug("Installing Python tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
})
fmt.Printf("Installing Python tool %s...\n", name)
return installPythonTool(name, toolInfo)
}
// For runtime-based tools
logger.Debug("Installing runtime-based tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
})
fmt.Printf("Installing %s using %s runtime...\n", name, toolInfo.Runtime)
return installRuntimeTool(name, toolInfo, registry)
}
func installRuntimeTool(name string, toolInfo *plugins.ToolInfo, registry string) error {
// Get the runtime configuration for this tool
runtimeInfo, ok := Config.Runtimes()[toolInfo.Runtime]
if !ok {
return fmt.Errorf("required runtime %s not found for tool %s", toolInfo.Runtime, name)
}
// Check if the runtime is actually installed
if !Config.IsRuntimeInstalled(toolInfo.Runtime, runtimeInfo) {
fmt.Printf("Runtime %s v%s is not installed, installing...\n", toolInfo.Runtime, runtimeInfo.Version)
err := InstallRuntime(toolInfo.Runtime, runtimeInfo)
if err != nil {
return fmt.Errorf("failed to install runtime %s: %w", toolInfo.Runtime, err)
}
fmt.Printf("Runtime %s v%s installed successfully, proceeding with tool installation...\n", toolInfo.Runtime, runtimeInfo.Version)
}
// Prepare template data
templateData := map[string]string{
"InstallDir": toolInfo.InstallDir,
"PackageName": toolInfo.Name,
"Version": toolInfo.Version,
"Registry": registry,
}
// Get package manager binary based on the tool configuration
packageManagerName := toolInfo.PackageManager
packageManagerBinary, ok := runtimeInfo.Binaries[packageManagerName]
if !ok {
return fmt.Errorf("package manager binary %s not found in runtime %s", packageManagerName, toolInfo.Runtime)
}
// Set registry if provided
if registry != "" {
regCmd, err := executeToolTemplate(toolInfo.RegistryCommand, templateData)
if err != nil {
return fmt.Errorf("failed to prepare registry command: %w", err)
}
logger.Debug("Setting registry", logrus.Fields{
"tool": name,
"packageManager": packageManagerName,
"packageManagerBin": packageManagerBinary,
"command": regCmd,
})
if regCmd != "" {
registryCmd := exec.Command(packageManagerBinary, strings.Split(regCmd, " ")...)
if output, err := registryCmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to set registry: %s: %w", string(output), err)
}
}
}
// Execute installation command
installCmd, err := executeToolTemplate(toolInfo.InstallCommand, templateData)
if err != nil {
return fmt.Errorf("failed to prepare install command: %w", err)
}
logger.Debug("Installing tool", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"packageManager": packageManagerName,
"packageManagerBin": packageManagerBinary,
"command": installCmd,
})
// Execute the installation command using the package manager
cmd := exec.Command(packageManagerBinary, strings.Split(installCmd, " ")...)
// Special handling for Go tools: set GOBIN so the binary is installed in the tool's install directory
if toolInfo.Runtime == "go" {
env := os.Environ()
env = append(env, "GOBIN="+toolInfo.InstallDir)
cmd.Env = env
}
// Special handling for ESLint installation in Linux (WSL) environment
if toolInfo.Name == "eslint" && runtime.GOOS == "linux" {
// Get node binary directory to add to PATH
nodeBinary, exist := runtimeInfo.Binaries["node"]
if exist {
nodeDir := filepath.Dir(nodeBinary)
// Get current PATH
currentPath := os.Getenv("PATH")
// For Linux (WSL), always use Linux path separator
pathSeparator := ":"
newPath := nodeDir + pathSeparator + currentPath
cmd.Env = append(os.Environ(), "PATH="+newPath)
logger.Debug("Setting PATH environment for ESLint installation", logrus.Fields{
"nodeDir": nodeDir,
"currentPath": currentPath,
"newPath": newPath,
})
}
}
log.Printf("Installing %s v%s...\n", toolInfo.Name, toolInfo.Version)
logger.Debug("Running command", logrus.Fields{
"command": fmt.Sprintf("%s %s", packageManagerBinary, installCmd),
})
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to install tool: %s: %w", string(output), err)
}
logger.Debug("Tool installation completed", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
})
return nil
}
// installDownloadBasedTool installs a tool by downloading and extracting it
func installDownloadBasedTool(toolInfo *plugins.ToolInfo) error {
// Create a file name for the downloaded archive
fileName := filepath.Base(toolInfo.DownloadURL)
downloadPath := filepath.Join(Config.ToolsDirectory(), fileName)
// Check if the file already exists
_, err := os.Stat(downloadPath)
if os.IsNotExist(err) {
// Download the file
logger.Debug("Downloading tool", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"downloadURL": toolInfo.DownloadURL,
"downloadPath": downloadPath,
})
downloadPath, err = utils.DownloadFile(toolInfo.DownloadURL, Config.ToolsDirectory())
if err != nil {
return fmt.Errorf("failed to download tool: %w", err)
}
} else if err != nil {
return fmt.Errorf("error checking for existing download: %w", err)
} else {
logger.Debug("Using existing tool download", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"downloadPath": downloadPath,
})
}
// Open the downloaded file
file, err := os.Open(downloadPath)
if err != nil {
return fmt.Errorf("failed to open downloaded file: %w", err)
}
defer file.Close()
// Create the installation directory
err = os.MkdirAll(toolInfo.InstallDir, constants.DefaultDirPerms)
if err != nil {
return fmt.Errorf("failed to create installation directory: %w", err)
}
isArchive := strings.HasSuffix(fileName, ".zip") || strings.HasSuffix(fileName, ".tar.gz") || strings.HasSuffix(fileName, ".tgz")
if isArchive {
// Extract based on file extension
logger.Debug("Extracting tool", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"fileName": fileName,
"extractDirectory": toolInfo.InstallDir,
})
if strings.HasSuffix(fileName, ".zip") {
err = utils.ExtractZip(file.Name(), toolInfo.InstallDir)
} else {
err = utils.ExtractTarGz(file, toolInfo.InstallDir)
}
if err != nil {
return fmt.Errorf("failed to extract tool: %w", err)
}
// Make sure all binaries are executable
for _, binaryPath := range toolInfo.Binaries {
err = os.Chmod(filepath.Join(toolInfo.InstallDir, filepath.Base(binaryPath)), constants.DefaultDirPerms)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to make binary executable: %w", err)
}
}
} else {
// Bare binary — copy directly to the binary destination path
logger.Debug("Installing bare binary", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"downloadPath": downloadPath,
})
if err = installBareBinary(downloadPath, toolInfo); err != nil {
return fmt.Errorf("failed to install binary: %w", err)
}
}
logger.Debug("Tool extraction completed", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
})
return nil
}
// installBareBinary copies a downloaded bare binary to its destination path and makes it executable.
func installBareBinary(downloadPath string, toolInfo *plugins.ToolInfo) error {
var destPath string
for _, p := range toolInfo.Binaries {
destPath = p
break
}
if destPath == "" {
return fmt.Errorf("no binary destination defined for tool %s", toolInfo.Name)
}
if err := os.MkdirAll(filepath.Dir(destPath), constants.DefaultDirPerms); err != nil {
return fmt.Errorf("failed to create binary directory: %w", err)
}
src, err := os.Open(downloadPath)
if err != nil {
return fmt.Errorf("failed to open downloaded binary: %w", err)
}
defer src.Close()
dst, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("failed to create binary file: %w", err)
}
defer dst.Close()
if _, err = io.Copy(dst, src); err != nil {
return fmt.Errorf("failed to copy binary: %w", err)
}
if err = os.Chmod(destPath, constants.DefaultDirPerms); err != nil {
return fmt.Errorf("failed to make binary executable: %w", err)
}
return nil
}
func installPythonTool(name string, toolInfo *plugins.ToolInfo) error {
logger.Debug("Starting Python tool installation", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
})
// Get the runtime configuration for this tool
runtimeInfo, ok := Config.Runtimes()[toolInfo.Runtime]
if !ok {
return fmt.Errorf("required runtime %s not found for tool %s", toolInfo.Runtime, name)
}
// Check if the Python runtime is actually installed
if !Config.IsRuntimeInstalled(toolInfo.Runtime, runtimeInfo) {
fmt.Printf("Python runtime %s v%s is not installed, installing...\n", toolInfo.Runtime, runtimeInfo.Version)
err := InstallRuntime(toolInfo.Runtime, runtimeInfo)
if err != nil {
return fmt.Errorf("failed to install runtime %s: %w", toolInfo.Runtime, err)
}
fmt.Printf("Python runtime %s v%s installed successfully, proceeding with tool installation...\n", toolInfo.Runtime, runtimeInfo.Version)
}
pythonBinary, ok := runtimeInfo.Binaries["python3"]
if !ok {
return fmt.Errorf("python3 binary not found in runtime")
}
// Create venv
logger.Debug("Creating Python virtual environment", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"venvDir": filepath.Join(toolInfo.InstallDir, "venv"),
})
cmd := exec.Command(pythonBinary, "-m", "venv", filepath.Join(toolInfo.InstallDir, "venv"))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to create venv: %s\nError: %w", string(output), err)
}
// Install the tool using pip from venv
pipPath := filepath.Join(toolInfo.InstallDir, "venv", "bin", "pip")
logger.Debug("Installing Python package", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
"pipPath": pipPath,
})
cmd = exec.Command(pipPath, "install", fmt.Sprintf("%s==%s", toolInfo.Name, toolInfo.Version))
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to install tool: %s\nError: %w", string(output), err)
}
logger.Debug("Python tool installation completed", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
})
return nil
}
// executeToolTemplate executes a template with the given data
func executeToolTemplate(tmplStr string, data map[string]string) (string, error) {
tmpl, err := template.New("command").Parse(tmplStr)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}