-
Notifications
You must be signed in to change notification settings - Fork 700
/
installedpackages.ts
350 lines (320 loc) · 11.8 KB
/
installedpackages.ts
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
// Copyright 2018-2022 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0
import { JSONSchemaType } from "ajv";
import {
AvailablePackageDetail,
Context,
InstalledPackageDetail,
InstalledPackageReference,
InstalledPackageStatus,
InstalledPackageSummary,
ReconciliationOptions,
VersionReference,
} from "gen/kubeappsapis/core/packages/v1alpha1/packages";
import { ThunkAction } from "redux-thunk";
import PackagesService from "shared/PackagesService";
import {
CreateError,
DeleteError,
FetchError,
FetchWarning,
IStoreState,
RollbackError,
UnprocessableEntityError,
UpgradeError,
} from "shared/types";
import { getPluginsSupportingRollback } from "shared/utils";
import { ActionType, deprecated } from "typesafe-actions";
import { InstalledPackage } from "../shared/InstalledPackage";
import { validate } from "../shared/schema";
import { handleErrorAction } from "./auth";
const { createAction } = deprecated;
export const requestInstalledPackage = createAction("REQUEST_INSTALLED_PACKAGE");
export const requestInstalledPackageList = createAction("REQUEST_INSTALLED_PACKAGE_LIST");
export const receiveInstalledPackageList = createAction(
"RECEIVE_INSTALLED_PACKAGE_LIST",
resolve => {
return (pkgs: InstalledPackageSummary[]) => resolve(pkgs);
},
);
export const requestDeleteInstalledPackage = createAction("REQUEST_DELETE_INSTALLED_PACKAGE");
export const receiveDeleteInstalledPackage = createAction(
"RECEIVE_DELETE_INSTALLED_PACKAGE_CONFIRMATION",
);
export const requestInstallPackage = createAction("REQUEST_INSTALL_PACKAGE");
export const receiveInstallPackage = createAction("RECEIVE_INSTALL_PACKAGE_CONFIRMATION");
export const requestUpdateInstalledPackage = createAction("REQUEST_UPDATE_INSTALLED_PACKAGE");
export const receiveUpdateInstalledPackage = createAction(
"RECEIVE_UPDATE_INSTALLED_PACKAGE_CONFIRMATION",
);
export const requestRollbackInstalledPackage = createAction("REQUEST_ROLLBACK_INSTALLED_PACKAGE");
export const receiveRollbackInstalledPackage = createAction(
"RECEIVE_ROLLBACK_INSTALLED_PACKAGE_CONFIRMATION",
);
export const requestInstalledPackageStatus = createAction("REQUEST_INSTALLED_PACKAGE_STATUS");
export const receiveInstalledPackageStatus = createAction(
"RECEIVE_INSTALLED_PACKAGE_STATUS",
resolve => {
return (status: InstalledPackageStatus) => resolve(status);
},
);
export const errorInstalledPackage = createAction("ERROR_INSTALLED_PACKAGE", resolve => {
return (err: FetchError | CreateError | UpgradeError | RollbackError | DeleteError) =>
resolve(err);
});
export const clearErrorInstalledPackage = createAction("CLEAR_ERROR_INSTALLED_PACKAGE");
export const selectInstalledPackage = createAction("SELECT_INSTALLED_PACKAGE", resolve => {
return (pkg: InstalledPackageDetail, details?: AvailablePackageDetail) =>
resolve({ pkg, details });
});
const allActions = [
requestInstalledPackageList,
requestInstalledPackage,
receiveInstalledPackageList,
requestInstalledPackageStatus,
receiveInstalledPackageStatus,
requestDeleteInstalledPackage,
receiveDeleteInstalledPackage,
requestInstallPackage,
receiveInstallPackage,
requestUpdateInstalledPackage,
receiveUpdateInstalledPackage,
requestRollbackInstalledPackage,
receiveRollbackInstalledPackage,
errorInstalledPackage,
clearErrorInstalledPackage,
selectInstalledPackage,
];
export type InstalledPackagesAction = ActionType<typeof allActions[number]>;
export function getInstalledPackage(
installedPackageRef?: InstalledPackageReference,
): ThunkAction<Promise<void>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
dispatch(requestInstalledPackage());
try {
// Get the details of an installed package
const { installedPackageDetail } = await InstalledPackage.GetInstalledPackageDetail(
installedPackageRef,
);
// For local packages with no references to any available packages (eg.a local package for development)
// we aren't able to get the details, but still want to display the available data so far
let availablePackageDetail;
try {
// Get the details of the available package that corresponds to the installed package
const resp = await PackagesService.getAvailablePackageDetail(
installedPackageDetail?.availablePackageRef,
installedPackageDetail?.currentVersion?.pkgVersion,
);
availablePackageDetail = resp.availablePackageDetail;
} catch (e: any) {
dispatch(
handleErrorAction(
e,
errorInstalledPackage(
new FetchWarning(
"this package has missing information, some actions might not be available.",
),
),
),
);
}
dispatch(selectInstalledPackage(installedPackageDetail!, availablePackageDetail));
} catch (e: any) {
dispatch(
handleErrorAction(
e,
errorInstalledPackage(new FetchError("Unable to get installed package", [e])),
),
);
}
};
}
export function getInstalledPkgStatus(
installedPackageRef?: InstalledPackageReference,
): ThunkAction<Promise<void>, IStoreState, null, InstalledPackagesAction> {
return async (dispatch, getState) => {
const {
kube: { subscriptions },
} = getState();
const subscriptionId = `${installedPackageRef?.context?.cluster}/${installedPackageRef?.context?.namespace}/${installedPackageRef?.identifier}`;
// only get the status if the subscription is active, otherwise the component would have been unmounted
if (subscriptions[subscriptionId]) {
dispatch(requestInstalledPackageStatus());
try {
// Get the details of an installed package for the status.
const { installedPackageDetail } = await InstalledPackage.GetInstalledPackageDetail(
installedPackageRef,
);
dispatch(receiveInstalledPackageStatus(installedPackageDetail!.status!));
} catch (e: any) {
dispatch(
handleErrorAction(
e,
errorInstalledPackage(new FetchError("Unable to refresh installed package", [e])),
),
);
}
}
};
}
export function deleteInstalledPackage(
installedPackageRef: InstalledPackageReference,
): ThunkAction<Promise<boolean>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
dispatch(requestDeleteInstalledPackage());
try {
await InstalledPackage.DeleteInstalledPackage(installedPackageRef);
dispatch(receiveDeleteInstalledPackage());
return true;
} catch (e: any) {
dispatch(handleErrorAction(e, errorInstalledPackage(new DeleteError(e.message))));
return false;
}
};
}
// fetchInstalledPackages returns a list of apps for other actions to compose on top of it
export function fetchInstalledPackages(
cluster: string,
namespace?: string,
): ThunkAction<Promise<InstalledPackageSummary[]>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
dispatch(requestInstalledPackageList());
let installedPackageSummaries: InstalledPackageSummary[];
try {
const res = await InstalledPackage.GetInstalledPackageSummaries(cluster, namespace);
installedPackageSummaries = res?.installedPackageSummaries;
dispatch(receiveInstalledPackageList(installedPackageSummaries));
return installedPackageSummaries;
} catch (e: any) {
dispatch(
handleErrorAction(e, errorInstalledPackage(new FetchError("Unable to list apps", [e]))),
);
return [];
}
};
}
export function installPackage(
targetCluster: string,
targetNamespace: string,
availablePackageDetail: AvailablePackageDetail,
releaseName: string,
values?: string,
schema?: JSONSchemaType<any>,
reconciliationOptions?: ReconciliationOptions,
): ThunkAction<Promise<boolean>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
dispatch(requestInstallPackage());
try {
if (values && schema) {
const validation = validate(values, schema);
if (!validation.valid) {
const errorText =
validation.errors &&
validation.errors.map(e => ` - ${e.instancePath}: ${e.message}`).join("\n");
throw new UnprocessableEntityError(
`The given values don't match the required format. The following errors were found:\n${errorText}`,
);
}
}
if (
availablePackageDetail?.availablePackageRef &&
availablePackageDetail?.version?.pkgVersion
) {
await InstalledPackage.CreateInstalledPackage(
{ cluster: targetCluster, namespace: targetNamespace } as Context,
releaseName,
availablePackageDetail.availablePackageRef,
{ version: availablePackageDetail.version.pkgVersion } as VersionReference,
values,
reconciliationOptions as ReconciliationOptions,
);
dispatch(receiveInstallPackage());
return true;
} else {
dispatch(
errorInstalledPackage(
new CreateError("This package does not contain enough information to be installed"),
),
);
return false;
}
} catch (e: any) {
dispatch(handleErrorAction(e, errorInstalledPackage(new CreateError(e.message))));
return false;
}
};
}
export function updateInstalledPackage(
installedPackageRef: InstalledPackageReference,
availablePackageDetail: AvailablePackageDetail,
values?: string,
schema?: JSONSchemaType<any>,
): ThunkAction<Promise<boolean>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
dispatch(requestUpdateInstalledPackage());
try {
if (values && schema) {
const validation = validate(values, schema);
if (!validation.valid) {
const errorText =
validation.errors &&
validation.errors.map(e => ` - ${e.instancePath}: ${e.message}`).join("\n");
throw new UnprocessableEntityError(
`The given values don't match the required format. The following errors were found:\n${errorText}`,
);
}
}
if (availablePackageDetail?.version?.pkgVersion) {
await InstalledPackage.UpdateInstalledPackage(
installedPackageRef,
{ version: availablePackageDetail.version.pkgVersion } as VersionReference,
values,
);
dispatch(receiveUpdateInstalledPackage());
return true;
} else {
dispatch(
errorInstalledPackage(
new UpgradeError("This package does not contain enough information to be installed"),
),
);
return false;
}
} catch (e: any) {
dispatch(handleErrorAction(e, errorInstalledPackage(new UpgradeError(e.message))));
return false;
}
};
}
export function rollbackInstalledPackage(
installedPackageRef: InstalledPackageReference,
revision: number,
): ThunkAction<Promise<boolean>, IStoreState, null, InstalledPackagesAction> {
return async dispatch => {
// rollbackInstalledPackage is currently only available for Helm packages
if (
installedPackageRef?.plugin?.name &&
getPluginsSupportingRollback().includes(installedPackageRef.plugin.name)
) {
dispatch(requestRollbackInstalledPackage());
try {
await InstalledPackage.RollbackInstalledPackage(installedPackageRef, revision);
dispatch(receiveRollbackInstalledPackage());
dispatch(getInstalledPackage(installedPackageRef));
return true;
} catch (e: any) {
dispatch(handleErrorAction(e, errorInstalledPackage(new RollbackError(e.message))));
return false;
}
} else {
dispatch(
errorInstalledPackage(
new RollbackError(
"This package cannot be rolled back; this operation is only available for Helm packages",
),
),
);
return false;
}
};
}