-
Notifications
You must be signed in to change notification settings - Fork 265
/
mod_installer.go
208 lines (167 loc) · 7.29 KB
/
mod_installer.go
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
package mod_installer
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/turbot/steampipe/constants"
git "github.com/go-git/go-git/v5"
"github.com/turbot/steampipe/steampipeconfig/modconfig"
"github.com/turbot/steampipe/steampipeconfig/parse"
"github.com/turbot/steampipe/utils"
)
/*
mog get
A user may install a mod with steampipe mod get modname[@version]
version may be:
- Not specified: steampipe mod get aws-cis
The latest version (highest version tag) will be installed.
A dependency is added to the requires block specifying the version that was downloaded
- A major version: steampipe mod get aws-cis@3
The latest release (highest version tag) of the specified major version will be installed.
A dependency is added to the requires block specifying the version that was downloaded
- A monotonic version tag: steampipe mod get aws-cis@v2.21
The specified version is downloaded and added as requires dependency.
- A branch name: steampipe mod get aws-cis@staging
The current version of the specified branch is downloaded.
The branch dependency is added to the requires list. Note that a branch is considered a distinct "major" release, it is not cached in the registry, and has no minor version.
Branch versions do not auto-update - you have to run steampipe mod update to get a newer version.
Branch versioning is meant to simplify development and testing - published mods should ONLY include version tag dependencies, NOT branch dependencies.
- A local file path: steampipe mod get "file:~/my_path/aws-core"
The mod from the local filesystem is added to the namespace, but nothing is downloaded.
The local dependency is added to the requires list. Note that a local mod is considered a distinct "major" release, it is not cached in the registry, and has no minor version.
Local versioning is meant to simplify development and testing - published mods should ONLY include version tag dependencies, NOT local dependencies.
Steampipe Version Dependency
If the installed version of Steampipe does not meet the dependency criteria, the user will be warned and the mod will NOT be installed.
Plugin Dependency5
If the mod specifies plugin versions that are not installed, or have no connections, the user will be warned but the mod will be installed. The user should be warned at installation time, and also when starting Steampipe in the workspace.
Detecting conflicts
mod 1 require a@1.0
mod 2 require a@file:/foo
-> how do we detect if the file version satisfied constrainst of a - this is for dev purposes so always pass?
mod 1 require a@1.0
mod 2 require a@<branch>
-> how do we detect if the file version satisfied constraints of a - check branch?
*/
type ModInstaller struct {
ModsDir string
InstalledDependencies []*ResolvedModRef
}
func NewModInstaller(workspacePath string) *ModInstaller {
return &ModInstaller{
ModsDir: constants.WorkspaceModPath(workspacePath),
}
}
// InstallModDependencies installs all dependencies of the mod
func (i *ModInstaller) InstallModDependencies(mod *modconfig.Mod) error {
dependencyMap := make(map[string]*ResolvedModRef)
return i.installModDependenciesRecursively(mod, dependencyMap)
}
func (i *ModInstaller) installModDependenciesRecursively(mod *modconfig.Mod, dependencyMap map[string]*ResolvedModRef) error {
if mod.Requires == nil {
return nil
}
// first check our Steampipe version is sufficient
if err := mod.Requires.ValidateSteampipeVersion(mod.Name()); err != nil {
return err
}
var errors []error
for _, modVersion := range mod.Requires.Mods {
// get a resolved mod ref for this mod version
resolvedRef, err := i.GetModRefForVersion(modVersion)
if err != nil {
return fmt.Errorf("dependency %s %s cannot be satisfied: %s", mod.Name(), modVersion.VersionString, err.Error())
}
// install this mod
// NOTE - this mutates dependency map
if err := i.installDependency(resolvedRef, dependencyMap); err != nil {
errors = append(errors, err)
}
}
return utils.CombineErrorsWithPrefix(fmt.Sprintf("%d dependencies failed to install", len(errors)), errors...)
}
func (i *ModInstaller) GetModRefForVersion(modVersion *modconfig.ModVersion) (*ResolvedModRef, error) {
// NOTE check whether the lock file contains this dependency and if so
// does the locked version satisy this version requirement
// return error if not
// NOTE check whether we are replacing this version
// if so does the locked version satisfy this version requirement
// return error if not
// so we need to resolve this mod version
// NOTE for now assume github
// get the most recent minor version fo rthis major version from github
return i.getLatestCompatibleVersionFromGithub(modVersion)
}
func (i *ModInstaller) getLatestCompatibleVersionFromGithub(modVersion *modconfig.ModVersion) (*ResolvedModRef, error) {
// NOTE for now assume the mod is specified with a full version
return NewResolvedModRef(modVersion)
}
func (i *ModInstaller) installDependency(dependency *ResolvedModRef, dependencyMap map[string]*ResolvedModRef) error {
// have we already installed a mod which satisfies this dependency
if modRef, ok := dependencyMap[dependency.Name]; ok {
if modRef.Version.GreaterThanOrEqual(dependency.Version) {
return nil
}
}
// add this dependency into the map (if we fail to install,m the whole installation process will terminate,
// so no need to check for errors
dependencyMap[dependency.Name] = dependency
var modPath string
if dependency.FilePath != "" {
// if there is a file path, verify it exists
if _, err := os.Stat(dependency.FilePath); os.IsNotExist(err) {
return fmt.Errorf("dependency %s file path %s does not exist", dependency.Name, dependency.FilePath)
}
modPath = dependency.FilePath
} else {
modPath = filepath.Join(i.ModsDir, dependency.FullName())
if err := i.installDependencyFromGit(dependency, modPath); err != nil {
return err
}
}
// no load the installed mod and install _its_ dependencies
if !parse.ModfileExists(modPath) {
log.Printf("[TRACE] dependency %s does not define a mod defintion - so there are no dependencies to install", dependency.Name)
return nil
}
mod, err := parse.ParseModDefinition(modPath)
if err != nil {
return err
}
err = i.installModDependenciesRecursively(mod, dependencyMap)
// if we succeeded, update our list
if err == nil {
i.InstalledDependencies = append(i.InstalledDependencies, dependency)
}
return err
}
func (i *ModInstaller) installDependencyFromGit(dependency *ResolvedModRef, installPath string) error {
// ensure mod directory exists - create if necessary
if err := os.MkdirAll(i.ModsDir, os.ModePerm); err != nil {
return err
}
// NOTE: we need to check existing installed mods
// get the mod from git
gitUrl := fmt.Sprintf("https://%s", dependency.Name)
_, err := git.PlainClone(installPath,
false,
&git.CloneOptions{
URL: gitUrl,
//Progress: os.Stdout,
ReferenceName: dependency.GitReference,
Depth: 1,
SingleBranch: true,
})
return err
}
func (i *ModInstaller) InstallReport() string {
if len(i.InstalledDependencies) == 0 {
return "No dependencies installed"
}
strs := make([]string, len(i.InstalledDependencies))
for idx, dep := range i.InstalledDependencies {
strs[idx] = dep.FullName()
}
return fmt.Sprintf("\nInstalled %d dependencies:\n - %s\n", len(i.InstalledDependencies), strings.Join(strs, "\n - "))
}