-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtools-installer.go
More file actions
365 lines (318 loc) · 10.6 KB
/
tools-installer.go
File metadata and controls
365 lines (318 loc) · 10.6 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
package config
import (
"bytes"
"codacy/cli-v2/plugins"
"codacy/cli-v2/utils"
"codacy/cli-v2/utils/logger"
"fmt"
"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 {
// Check if the tool is already installed
if Config.IsToolInstalled(name, toolInfo) {
logger.Info("Tool already installed", logrus.Fields{
"tool": name,
"version": toolInfo.Version,
"runtime": toolInfo.Runtime,
})
fmt.Printf("Tool %s v%s is already installed\n", name, toolInfo.Version)
return nil
}
// Make sure the installation directory exists
err := os.MkdirAll(toolInfo.InstallDir, 0755)
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 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)
}
// 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 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, 0755)
if err != nil {
return fmt.Errorf("failed to create installation directory: %w", err)
}
// 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)), 0755)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to make binary executable: %w", err)
}
}
logger.Debug("Tool extraction completed", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
})
return nil
}
func installPythonTool(name string, toolInfo *plugins.ToolInfo) error {
logger.Debug("Starting Python tool installation", logrus.Fields{
"tool": toolInfo.Name,
"version": toolInfo.Version,
})
runtimeInfo, ok := Config.Runtimes()[toolInfo.Runtime]
if !ok {
return fmt.Errorf("required runtime %s not found for tool %s", toolInfo.Runtime, name)
}
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
}
// isToolInstalled checks if a tool is already installed by checking for the binary
func isToolInstalled(toolInfo *plugins.ToolInfo) bool {
// If there are no binaries, check the install directory
if len(toolInfo.Binaries) == 0 {
_, err := os.Stat(toolInfo.InstallDir)
return err == nil
}
// Check if at least one binary exists
for _, binaryPath := range toolInfo.Binaries {
_, err := os.Stat(binaryPath)
if err == nil {
return true
}
}
return false
}
// 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
}