-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathinstall_plan.go
156 lines (132 loc) · 4.17 KB
/
install_plan.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
package install
import (
"fmt"
"github.com/go-xorm/builder"
"github.com/google/uuid"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/cmd/operate"
"github.com/itchio/butler/database/models"
"github.com/itchio/butler/endpoints/fetch"
"github.com/itchio/butler/installer"
"github.com/itchio/butler/installer/bfs"
"github.com/itchio/butler/manager"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/hades"
"github.com/itchio/ox"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/eos/option"
"github.com/pkg/errors"
"github.com/sourcegraph/jsonrpc2"
)
func InstallPlan(rc *butlerd.RequestContext, params butlerd.InstallPlanParams) (*butlerd.InstallPlanResult, error) {
consumer := rc.Consumer
conn := rc.GetConn()
defer rc.PutConn(conn)
game := fetch.LazyFetchGame(rc, params.GameID)
consumer.Opf("Planning install for %s", operate.GameToString(game))
baseUploads := fetch.LazyFetchGameUploads(rc, params.GameID)
baseUploads = manager.NarrowDownUploads(consumer, game, baseUploads, ox.CurrentRuntime()).Uploads
// exclude already-installed and currently-installing uploads
var uploadIDs []interface{}
for _, u := range baseUploads {
uploadIDs = append(uploadIDs, u.ID)
}
var validUploads []*itchio.Upload
models.MustSelect(conn, &validUploads, builder.And(
builder.In("id", uploadIDs...),
builder.Expr(`not exists (select 1 from caves where upload_id = uploads.id)`),
builder.Expr(`not exists (select 1 from downloads where upload_id = uploads.id and finished_at is null and not discarded)`),
), hades.Search{})
validUploadIDs := make(map[int64]bool)
for _, u := range validUploads {
validUploadIDs[u.ID] = true
}
// do a little dance to keep the ordering proper
var uploads []*itchio.Upload
for _, u := range baseUploads {
if validUploadIDs[u.ID] {
uploads = append(uploads, u)
}
}
res := &butlerd.InstallPlanResult{
Game: game,
Uploads: uploads,
}
if len(uploads) == 0 {
consumer.Statf("No compatible uploads, returning early.")
return res, nil
}
info := &butlerd.InstallPlanInfo{}
res.Info = info
setResError := func(err error) {
consumer.Errorf("Planning failed: %+v", err)
info.Error = fmt.Sprintf("%+v", err)
if be, ok := butlerd.AsButlerdError(err); ok {
info.ErrorCode = be.RpcErrorCode()
info.ErrorMessage = be.RpcErrorMessage()
} else {
info.ErrorCode = int64(jsonrpc2.CodeInternalError)
info.ErrorMessage = err.Error()
}
}
var upload *itchio.Upload
if params.UploadID != 0 {
for _, u := range uploads {
if u.ID == params.UploadID {
consumer.Infof("Using specified upload.")
upload = u
break
}
}
}
if upload == nil {
consumer.Infof("Picking first upload.")
upload = uploads[0]
}
operate.LogUpload(consumer, upload, upload.Build)
info.Upload = upload
info.Build = upload.Build
if upload.Storage == itchio.UploadStorageExternal && operate.IsBadExternalHost(upload.Host) {
setResError(errors.WithStack(butlerd.CodeUnsupportedHost))
return res, nil
}
sessionID := params.DownloadSessionID
if sessionID == "" {
sessionID = uuid.New().String()
consumer.Infof("No download session ID passed, using %s", sessionID)
}
access := operate.AccessForGameID(conn, game.ID)
client := rc.Client(access.APIKey)
installParams := &operate.InstallParams{
Upload: info.Upload,
Build: info.Build,
Access: access,
}
sourceURL := operate.MakeSourceURL(client, consumer, sessionID, installParams, "")
file, err := eos.Open(sourceURL, option.WithConsumer(consumer))
if err != nil {
setResError(errors.WithStack(err))
return res, nil
}
defer file.Close()
installerInfo, err := installer.GetInstallerInfo(consumer, file)
if err != nil {
setResError(errors.WithStack(err))
return res, nil
}
info.Type = string(installerInfo.Type)
// planning is always for a fresh install
receiptIn := (*bfs.Receipt)(nil)
installFolder := ""
dui, err := operate.AssessDiskUsage(file, receiptIn, installFolder, installerInfo)
if err != nil {
setResError(errors.WithStack(err))
return res, nil
}
info.DiskUsage = &butlerd.DiskUsageInfo{
FinalDiskUsage: dui.FinalDiskUsage,
NeededFreeSpace: dui.NeededFreeSpace,
Accuracy: dui.Accuracy.String(),
}
return res, nil
}