-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
225 lines (201 loc) · 6.48 KB
/
index.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
/*
Copyright 2017 Balena Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import * as bSemver from 'balena-semver';
import { TypedError } from 'typed-error';
import { actionsConfig as defaultActionsConfig } from './config';
import type { ActionName, ActionsConfig } from './types';
export { actionsConfig } from './config';
export * from './types';
type SemVer = NonNullable<ReturnType<typeof bSemver.parse>>;
const getVariant = (semver: SemVer) => {
const semverExtraParts = [...semver.build, ...semver.prerelease];
return ['dev', 'prod'].find((variant) => semverExtraParts.includes(variant));
};
export class HUPActionError extends TypedError {
constructor(err: string) {
super(err);
}
}
export class HUPActionHelper {
constructor(private actionsConfig: ActionsConfig = defaultActionsConfig) {}
/**
* @summary Returns the resinhup type based on device type, current and target balenaOS versions
* @name getHUPActionType
* @public
* @function
* @memberof HUPActionHelper
*
* @description Returns the resinhup type based on device type, current and target balenaOS versions
*
* Currently available types are:
* - resinhup11
* - resinhup12
* - balenahup
*
* For a more detailed list of supported actions per device type check config.ts
*
* Throws error in any of these cases:
* - Current or target versions are invalid
* - Current or target versions do not match in dev/prod type
* - Current and target versions imply a downgrade operation
* - Action is not supported by device type
*
* @param {String} deviceType - device type slug
* @param {String} currentVersion - the current semver balenaOS version on the device
* @param {String} targetVersion - the target semver balenaOS version
*
* @returns {String}
*
* @example
* hupActionHelper.getHUPActionType('raspberrypi3', '2.0.0+rev1.prod', '2.2.0+rev1.prod');
*/
public getHUPActionType(
deviceType: string,
currentVersion: string,
targetVersion: string,
): ActionName | 'takeover' {
const currentVersionParsed = bSemver.parse(currentVersion);
if (currentVersionParsed == null) {
throw new HUPActionError('Invalid current balenaOS version');
}
const targetVersionParsed = bSemver.parse(targetVersion);
if (targetVersionParsed == null) {
throw new HUPActionError('Invalid target balenaOS version');
}
const currentVariant = getVariant(currentVersionParsed);
const targetVariant = getVariant(targetVersionParsed);
if (
targetVariant != null &&
// Prefer checking only for dev
(currentVariant === 'dev') !== (targetVariant === 'dev')
) {
throw new HUPActionError(
'Updates cannot be performed between development and production balenaOS variants',
);
}
if (bSemver.lt(targetVersion, currentVersion)) {
throw new HUPActionError('OS downgrades are not allowed');
}
if (bSemver.compare(currentVersion, targetVersion) === 0) {
throw new HUPActionError('Current OS version matches Target OS version');
}
const fromMajor = currentVersionParsed.major;
const toMajor = targetVersionParsed.major;
let actionName: ActionName;
if (fromMajor === 1) {
switch (toMajor) {
case 1:
actionName = 'resinhup11';
break;
case 2:
actionName = 'resinhup12';
break;
default:
throw new HUPActionError(
`This update request cannot be performed from ${currentVersion} to ${targetVersion}`,
);
}
} else {
// // Takeover overrides the checks below for the device type
// if (this.isTakeoverRequired(deviceType, currentVersion, targetVersion)) {
// return 'takeover';
// }
actionName = 'balenahup';
}
const { actionsConfig } = this;
const defaultActions = actionsConfig.deviceTypesDefaults;
const deviceActions = actionsConfig.deviceTypes[deviceType] || {};
if (
defaultActions[actionName] == null &&
deviceActions[actionName] == null
) {
throw new HUPActionError(
`This update request cannot be performed on '${deviceType}'`,
);
}
const {
minSourceVersion,
targetMajorVersion,
minTargetVersion,
minTakeoverVersion,
maxTargetVersion,
} = {
...actionsConfig.actions[actionName],
...defaultActions[actionName],
...deviceActions[actionName],
};
if (bSemver.lt(currentVersion, minSourceVersion)) {
throw new HUPActionError(
`Current OS version must be >= ${minSourceVersion}`,
);
}
// If there's a major version constraint for the given action, take it into account
if (
targetMajorVersion &&
bSemver.major(targetVersion) !== targetMajorVersion
) {
throw new HUPActionError(
`Target OS version must be of major version ${targetMajorVersion}`,
);
}
if (bSemver.lt(targetVersion, minTargetVersion)) {
throw new HUPActionError(
`Target OS version must be >= ${minTargetVersion}`,
);
}
if (maxTargetVersion && bSemver.gte(targetVersion, maxTargetVersion!)) {
throw new HUPActionError(
`Target OS version must be < ${maxTargetVersion}`,
);
}
if (actionName === 'balenahup' && minTakeoverVersion != null) {
if (
bSemver.lt(currentVersion, minTakeoverVersion) &&
bSemver.gte(targetVersion, minTakeoverVersion)
) {
return 'takeover';
}
}
return actionName;
}
/**
* @summary Returns whether the provided device type supports OS updates between the current and target balenaOS versions
* @name isSupportedOsUpdate
* @public
* @function
* @memberof HUPActionHelper
*
* @param {String} deviceType - device type slug
* @param {String} currentVersion - the current semver balenaOS version on the device
* @param {String} targetVersion - the target semver balenaOS version
*
* @returns {Boolean}
*
* @example
* hupActionHelper.isSupportedOsUpdate('raspberrypi3', '2.0.0+rev1.prod', '2.2.0+rev1.prod');
*/
public isSupportedOsUpdate(
deviceType: string,
currentVersion: string,
targetVersion: string,
) {
try {
return !!this.getHUPActionType(deviceType, currentVersion, targetVersion);
} catch (err) {
if (err instanceof HUPActionError) {
return false;
}
throw err;
}
}
}