-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathinstall_perform.go
357 lines (288 loc) · 8.91 KB
/
install_perform.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
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
package operate
import (
"context"
"fmt"
"os"
"path/filepath"
"crawshaw.io/sqlite"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/database/models"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/eos/option"
"github.com/itchio/butler/installer"
"github.com/pkg/errors"
)
func InstallPerform(ctx context.Context, rc *butlerd.RequestContext, performParams butlerd.InstallPerformParams) error {
if performParams.StagingFolder == "" {
return errors.New("No staging folder specified")
}
oc, err := LoadContext(ctx, rc, performParams.StagingFolder)
if err != nil {
return errors.WithStack(err)
}
defer oc.Release()
meta := NewMetaSubcontext()
oc.Load(meta)
err = doInstallPerform(oc, meta)
if err != nil {
oc.Consumer().Errorf("%+v", err)
return errors.WithStack(err)
}
oc.Retire()
return nil
}
func doForceLocal(file eos.File, oc *OperationContext, meta *MetaSubcontext, isub *InstallSubcontext) (eos.File, error) {
consumer := oc.rc.Consumer
params := meta.Data
istate := isub.Data
stats, err := file.Stat()
if err != nil {
return nil, errors.WithStack(err)
}
destName := filepath.Base(stats.Name())
destPath := filepath.Join(oc.StageFolder(), "install-source", destName)
if istate.IsAvailableLocally {
consumer.Infof("Install source needs to be available locally, re-using previously-downloaded file")
} else {
consumer.Infof("Install source needs to be available locally, copying to disk...")
dlErr := func() error {
err := messages.TaskStarted.Notify(oc.rc, butlerd.TaskStartedNotification{
Reason: butlerd.TaskReasonInstall,
Type: butlerd.TaskTypeDownload,
Game: params.Game,
Upload: params.Upload,
Build: params.Build,
TotalSize: stats.Size(),
})
if err != nil {
return errors.WithStack(err)
}
oc.rc.StartProgress()
err = DownloadInstallSource(oc.Consumer(), oc.StageFolder(), oc.ctx, file, destPath)
oc.rc.EndProgress()
oc.consumer.Progress(0)
if err != nil {
return errors.WithStack(err)
}
err = messages.TaskSucceeded.Notify(oc.rc, butlerd.TaskSucceededNotification{
Type: butlerd.TaskTypeDownload,
})
if err != nil {
return errors.WithStack(err)
}
return nil
}()
if dlErr != nil {
return nil, errors.Wrap(dlErr, "downloading install source")
}
istate.IsAvailableLocally = true
err = oc.Save(isub)
if err != nil {
return nil, err
}
}
ret, err := eos.Open(destPath, option.WithConsumer(consumer))
if err != nil {
return nil, errors.WithStack(err)
}
return ret, nil
}
func doInstallPerform(oc *OperationContext, meta *MetaSubcontext) error {
rc := oc.rc
params := meta.Data
consumer := oc.Consumer()
istate := &InstallSubcontextState{}
isub := &InstallSubcontext{
Data: istate,
}
oc.Load(isub)
if params.Game == nil {
return errors.Errorf("Corrupted download info (missing game), refusing to continue.")
}
consumer.Infof("→ Performing install for %s", GameToString(params.Game))
consumer.Infof(" to (%s)", params.InstallFolder)
consumer.Infof(" via (%s)", oc.StageFolder())
return InstallPrepare(oc, meta, isub, true, func(prepareRes *InstallPrepareResult) error {
if !params.NoCave {
var cave *models.Cave
rc.WithConn(func(conn *sqlite.Conn) {
cave = models.CaveByID(conn, params.CaveID)
})
if cave == nil {
cave = &models.Cave{
ID: params.CaveID,
InstallFolderName: params.InstallFolderName,
InstallLocationID: params.InstallLocationID,
}
}
oc.cave = cave
}
if prepareRes.Strategy == InstallPerformStrategyHeal {
return heal(oc, meta, isub, prepareRes.ReceiptIn)
}
if prepareRes.Strategy == InstallPerformStrategyUpgrade {
return upgrade(oc, meta, isub, prepareRes.ReceiptIn)
}
stats, err := prepareRes.File.Stat()
if err != nil {
return errors.WithStack(err)
}
installerInfo := istate.InstallerInfo
consumer.Infof("Will use installer %s", installerInfo.Type)
manager := installer.GetManager(string(installerInfo.Type))
if manager == nil {
msg := fmt.Sprintf("No manager for installer %s", installerInfo.Type)
return errors.New(msg)
}
managerInstallParams := &installer.InstallParams{
Consumer: consumer,
File: prepareRes.File,
InstallerInfo: istate.InstallerInfo,
StageFolderPath: oc.StageFolder(),
InstallFolderPath: params.InstallFolder,
ReceiptIn: prepareRes.ReceiptIn,
Context: oc.ctx,
}
tryInstall := func() (*installer.InstallResult, error) {
defer managerInstallParams.File.Close()
select {
case <-oc.ctx.Done():
return nil, errors.WithStack(butlerd.CodeOperationCancelled)
default:
// keep going!
}
err = messages.TaskStarted.Notify(oc.rc, butlerd.TaskStartedNotification{
Reason: butlerd.TaskReasonInstall,
Type: butlerd.TaskTypeInstall,
Game: params.Game,
Upload: params.Upload,
Build: params.Build,
TotalSize: stats.Size(),
})
if err != nil {
return nil, errors.WithStack(err)
}
oc.rc.StartProgress()
res, err := manager.Install(managerInstallParams)
oc.rc.EndProgress()
if err != nil {
return nil, errors.WithStack(err)
}
return res, nil
}
var firstInstallResult = istate.FirstInstallResult
if firstInstallResult != nil {
consumer.Infof("First install already completed (%d files)", len(firstInstallResult.Files))
} else {
var err error
firstInstallResult, err = tryInstall()
if err != nil && errors.Cause(err) == installer.ErrNeedLocal {
lf, localErr := doForceLocal(prepareRes.File, oc, meta, isub)
if localErr != nil {
return errors.WithStack(err)
}
consumer.Infof("Re-invoking manager with local file...")
managerInstallParams.File = lf
firstInstallResult, err = tryInstall()
}
if err != nil {
return errors.WithStack(err)
}
consumer.Infof("Install successful")
istate.FirstInstallResult = firstInstallResult
err = oc.Save(isub)
if err != nil {
return err
}
}
select {
case <-oc.ctx.Done():
consumer.Warnf("Asked to cancel, so, cancelling...")
return errors.WithStack(butlerd.CodeOperationCancelled)
default:
// continue!
}
var finalInstallResult = firstInstallResult
var finalInstallerInfo = installerInfo
if len(firstInstallResult.Files) == 1 {
single := firstInstallResult.Files[0]
singlePath := filepath.Join(params.InstallFolder, single)
consumer.Infof("Installed a single file")
err = func() error {
secondInstallerInfo := istate.SecondInstallerInfo
if secondInstallerInfo != nil {
consumer.Infof("Using cached second installer info")
} else {
consumer.Infof("Probing (%s)...", single)
sf, err := os.Open(singlePath)
if err != nil {
return errors.WithStack(err)
}
defer sf.Close()
secondInstallerInfo, err = installer.GetInstallerInfo(consumer, sf)
if err != nil {
consumer.Infof("Could not determine installer info for single file, skipping: %s", err.Error())
return nil
}
sf.Close()
istate.SecondInstallerInfo = secondInstallerInfo
err = oc.Save(isub)
if err != nil {
return err
}
}
if !installer.IsWindowsInstaller(secondInstallerInfo.Type) {
consumer.Infof("Installer type is (%s), ignoring", secondInstallerInfo.Type)
return nil
}
consumer.Infof("Will use nested installer (%s)", secondInstallerInfo.Type)
finalInstallerInfo = secondInstallerInfo
manager = installer.GetManager(string(secondInstallerInfo.Type))
if manager == nil {
return fmt.Errorf("Don't know how to install (%s) packages", secondInstallerInfo.Type)
}
destName := filepath.Base(single)
destPath := filepath.Join(oc.StageFolder(), "nested-install-source", destName)
_, err = os.Stat(destPath)
if err == nil {
// ah, it must already be there then
consumer.Infof("Using (%s) for nested install", destPath)
} else {
consumer.Infof("Moving (%s) to (%s) for nested install", singlePath, destPath)
err = os.MkdirAll(filepath.Dir(destPath), 0755)
if err != nil {
return errors.WithStack(err)
}
err = os.RemoveAll(destPath)
if err != nil {
return errors.WithStack(err)
}
err = os.Rename(singlePath, destPath)
if err != nil {
return errors.WithStack(err)
}
}
lf, err := os.Open(destPath)
if err != nil {
return errors.WithStack(err)
}
managerInstallParams.File = lf
consumer.Infof("Invoking nested install manager, let's go!")
finalInstallResult, err = tryInstall()
return err
}()
if err != nil {
return errors.WithStack(err)
}
}
return commitInstall(oc, &CommitInstallParams{
InstallFolder: params.InstallFolder,
InstallerName: string(finalInstallerInfo.Type),
Game: params.Game,
Upload: params.Upload,
Build: params.Build,
InstallResult: finalInstallResult,
})
})
}