-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathinstaller.go
More file actions
296 lines (275 loc) · 8.51 KB
/
Copy pathinstaller.go
File metadata and controls
296 lines (275 loc) · 8.51 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
package project
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/databricks/cli/cmd/labs/github"
"github.com/databricks/cli/cmd/labs/unpack"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg/cfgpickers"
"github.com/databricks/cli/libs/databrickscfg/profile"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/process"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/sql"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
const ownerRWXworldRX = 0o755
type whTypes []sql.EndpointInfoWarehouseType
type hook struct {
*Entrypoint `yaml:",inline"`
Script string `yaml:"script"`
RequireDatabricksConnect bool `yaml:"require_databricks_connect,omitempty"`
MinRuntimeVersion string `yaml:"min_runtime_version,omitempty"`
WarehouseTypes whTypes `yaml:"warehouse_types,omitempty"`
}
func (h *hook) RequireRunningCluster() bool {
if h.Entrypoint == nil {
return false
}
return h.Entrypoint.RequireRunningCluster
}
func (h *hook) HasPython() bool {
return strings.HasSuffix(h.Script, ".py")
}
func (h *hook) runHook(cmd *cobra.Command) error {
if h.Script == "" {
return nil
}
ctx := cmd.Context()
envs, err := h.Prepare(cmd)
if err != nil {
return fmt.Errorf("prepare: %w", err)
}
libDir := h.EffectiveLibDir()
args := []string{}
if strings.HasSuffix(h.Script, ".py") {
args = append(args, h.virtualEnvPython(ctx))
}
return process.Forwarded(ctx,
append(args, h.Script),
cmd.InOrStdin(),
cmd.OutOrStdout(),
cmd.ErrOrStderr(),
process.WithDir(libDir),
process.WithEnvs(envs))
}
type installer struct {
*Project
version string
// command instance is used for:
// - auth profile flag override
// - standard input, output, and error streams
cmd *cobra.Command
}
func (i *installer) Install(ctx context.Context) error {
err := i.EnsureFoldersExist()
if err != nil {
return fmt.Errorf("folders: %w", err)
}
i.folder, err = PathInLabs(ctx, i.Name)
if err != nil {
return err
}
w, err := i.login(ctx)
if err != nil && errors.Is(err, profile.ErrNoConfiguration) {
cfg, err := i.Installer.envAwareConfig(ctx)
if err != nil {
return err
}
w, err = databricks.NewWorkspaceClient((*databricks.Config)(cfg))
if err != nil {
return fmt.Errorf("no ~/.databrickscfg: %w", err)
}
} else if err != nil {
return fmt.Errorf("login: %w", err)
}
err = i.downloadLibrary(ctx)
if err != nil {
return fmt.Errorf("lib: %w", err)
}
err = i.setupPythonVirtualEnvironment(ctx, w)
if err != nil {
return fmt.Errorf("python: %w", err)
}
err = i.recordVersion(ctx)
if err != nil {
return fmt.Errorf("record version: %w", err)
}
// TODO: failing install hook for "clean installations" (not upgrages)
// should trigger removal of the project, otherwise users end up with
// misconfigured CLIs
err = i.runInstallHook(ctx)
if err != nil {
return fmt.Errorf("installer: %w", err)
}
return nil
}
func (i *installer) Upgrade(ctx context.Context) error {
err := i.downloadLibrary(ctx)
if err != nil {
return fmt.Errorf("lib: %w", err)
}
err = i.recordVersion(ctx)
if err != nil {
return fmt.Errorf("record version: %w", err)
}
err = i.installPythonDependencies(ctx, ".")
if err != nil {
return fmt.Errorf("python dependencies: %w", err)
}
err = i.runInstallHook(ctx)
if err != nil {
return fmt.Errorf("installer: %w", err)
}
return nil
}
func (i *installer) warningf(text string, v ...any) {
i.cmd.PrintErrln(color.YellowString(text, v...))
}
func (i *installer) cleanupLib(ctx context.Context) error {
libDir := i.LibDir()
err := os.RemoveAll(libDir)
if err != nil {
return fmt.Errorf("remove all: %w", err)
}
return os.MkdirAll(libDir, ownerRWXworldRX)
}
func (i *installer) recordVersion(ctx context.Context) error {
return i.writeVersionFile(ctx, i.version)
}
func (i *installer) login(ctx context.Context) (*databricks.WorkspaceClient, error) {
if !cmdio.IsPromptSupported(ctx) {
log.Debugf(ctx, "Skipping workspace profile prompts in non-interactive mode")
return nil, nil
}
cfg, err := i.metaEntrypoint(ctx).validLogin(i.cmd)
if errors.Is(err, ErrNoLoginConfig) {
cfg, err = i.Installer.envAwareConfig(ctx)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("valid: %w", err)
}
if !i.HasAccountLevelCommands() && cfg.IsAccountClient() {
return nil, errors.New("got account-level client, but no account-level commands")
}
lc := &loginConfig{Entrypoint: i.Installer.Entrypoint}
w, err := lc.askWorkspace(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("ask for workspace: %w", err)
}
err = lc.save(ctx)
if err != nil {
return nil, fmt.Errorf("save: %w", err)
}
return w, nil
}
func (i *installer) downloadLibrary(ctx context.Context) error {
feedback := cmdio.Spinner(ctx)
defer close(feedback)
feedback <- "Cleaning up previous installation if necessary"
err := i.cleanupLib(ctx)
if err != nil {
return fmt.Errorf("cleanup: %w", err)
}
libTarget := i.LibDir()
// we may support wheels, jars, and golang binaries. but those are not zipballs
if i.IsZipball() {
feedback <- "Downloading and unpacking zipball for " + i.version
return i.downloadAndUnpackZipball(ctx, libTarget)
}
return errors.New("we only support zipballs for now")
}
func (i *installer) downloadAndUnpackZipball(ctx context.Context, libTarget string) error {
raw, err := github.DownloadZipball(ctx, "databrickslabs", i.Name, i.version)
if err != nil {
return fmt.Errorf("download zipball from GitHub: %w", err)
}
zipball := unpack.GitHubZipball{Reader: bytes.NewBuffer(raw)}
log.Debugf(ctx, "Unpacking zipball to: %s", libTarget)
return zipball.UnpackTo(libTarget)
}
func (i *installer) setupPythonVirtualEnvironment(ctx context.Context, w *databricks.WorkspaceClient) error {
if !i.HasPython() {
return nil
}
feedback := cmdio.Spinner(ctx)
defer close(feedback)
feedback <- "Detecting all installed Python interpreters on the system"
pythonInterpreters, err := DetectInterpreters(ctx)
if err != nil {
return fmt.Errorf("detect: %w", err)
}
py, err := pythonInterpreters.AtLeast(i.MinPython)
if err != nil {
return fmt.Errorf("min version: %w", err)
}
log.Debugf(ctx, "Detected Python %s at: %s", py.Version, py.Path)
venvPath := i.virtualEnvPath(ctx)
log.Debugf(ctx, "Creating Python Virtual Environment at: %s", venvPath)
feedback <- "Creating Virtual Environment with Python " + py.Version
_, err = process.Background(ctx, []string{py.Path, "-m", "venv", venvPath})
if err != nil {
return fmt.Errorf("create venv: %w", err)
}
if i.Installer != nil && i.Installer.RequireDatabricksConnect {
feedback <- "Determining Databricks Connect version"
cluster, err := w.Clusters.Get(ctx, compute.GetClusterRequest{
ClusterId: w.Config.ClusterID,
})
if err != nil {
return fmt.Errorf("cluster: %w", err)
}
runtimeVersion, ok := cfgpickers.GetRuntimeVersion(*cluster)
if !ok {
return fmt.Errorf("unsupported runtime: %s", cluster.SparkVersion)
}
feedback <- "Installing Databricks Connect v" + runtimeVersion
pipSpec := "databricks-connect==" + runtimeVersion
err = i.installPythonDependencies(ctx, pipSpec)
if err != nil {
return fmt.Errorf("dbconnect: %w", err)
}
}
feedback <- "Installing Python library dependencies"
return i.installPythonDependencies(ctx, ".")
}
func (i *installer) installPythonDependencies(ctx context.Context, spec string) error {
if !i.IsPythonProject() {
return nil
}
libDir := i.LibDir()
log.Debugf(ctx, "Installing Python dependencies for: %s", libDir)
// maybe we'll need to add call one of the two scripts:
// - python3 -m ensurepip --default-pip
// - curl -o https://bootstrap.pypa.io/get-pip.py | python3
var buf bytes.Buffer
// Ensure latest version(s) is installed with the `--upgrade` and `--upgrade-strategy eager` flags
// https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-U
_, err := process.Background(ctx,
[]string{i.virtualEnvPython(ctx), "-m", "pip", "install", "--upgrade", "--upgrade-strategy", "eager", spec},
process.WithCombinedOutput(&buf),
process.WithDir(libDir))
if err != nil {
i.warningf(buf.String())
return fmt.Errorf("failed to install dependencies of %s", spec)
}
return nil
}
func (i *installer) runInstallHook(ctx context.Context) error {
if i.Installer == nil {
return nil
}
if i.Installer.Script == "" {
return nil
}
log.Debugf(ctx, "Launching installer script %s in %s", i.Installer.Script, i.LibDir())
return i.Installer.runHook(i.cmd)
}