@serve.zone/interfaces 32.72.0 → 32.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,259 @@
1
+ import { canonicalizeStrictJson, compareWireTokens, createCanonicalJsonSha256Hex } from '../private/canonicaljson.js';
2
+ import type { ICluster } from './cluster.js';
3
+ import type { TSha256Digest } from './immutableimage.js';
4
+
5
+ /**
6
+ * Locations: the rooms Cloudly's physical objects stand in.
7
+ *
8
+ * A location is a record of its own, referenced by its `id` and named by one unique, renamable
9
+ * `name`. Objects carry the id: a cluster node in `IClusterNode.data.locationId`, the records later
10
+ * lanes add (gateway, edge, Onebox host) in a required `locationId` of their own. A cluster has no
11
+ * stored location: it stands where its nodes stand (`getClusterLocationId`), and an empty cluster
12
+ * stands nowhere. A BareMetal server stands where the node whose `data.baremetalId` names it
13
+ * stands; its `data.location` stays the provider's datacenter.
14
+ *
15
+ * Every Cloudly database holds exactly one initial location (`initialLocationId`, named
16
+ * `initialLocationName` until an administrator renames it). It receives every create that names no
17
+ * location for a cluster without nodes; a create for a cluster with nodes takes the cluster's.
18
+ */
19
+ export const locationContract = Object.freeze({
20
+ /** One lowercase RFC 1123 label. */
21
+ maximumNameLength: 63,
22
+ digestDomain: 'serve.zone/location-membership',
23
+ } as const);
24
+
25
+ /**
26
+ * The id of the one location every Cloudly database holds; it never changes. Cloudly generates
27
+ * every other location's id with a generator that can never produce this one.
28
+ */
29
+ export const initialLocationId = 'location-initial';
30
+
31
+ /** The name the initial location is created with; an administrator may rename it. */
32
+ export const initialLocationName = 'initial';
33
+
34
+ const locationNameLabel = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
35
+
36
+ /** True for a location name: one lowercase RFC 1123 label of 1 to 63 characters. */
37
+ export const isLocationName = (valueArg: unknown): valueArg is string => (
38
+ typeof valueArg === 'string'
39
+ && valueArg.length <= locationContract.maximumNameLength
40
+ && locationNameLabel.test(valueArg)
41
+ );
42
+
43
+ /**
44
+ * Every kind of stored object a location can hold, in canonical (`compareWireTokens`) order. A
45
+ * Cloudly records only some of them (`IReq_Any_Cloudly_GetLocations` answers which); a kind it does
46
+ * not record has no members anywhere. A cluster is no member: it stores no location and stands
47
+ * where its nodes stand (`getClusterLocationId`); `getClusters` lists a location's clusters through
48
+ * its `locationScope`.
49
+ */
50
+ export const locationMemberKinds = Object.freeze([
51
+ 'edge', 'gateway', 'node', 'oneboxHost',
52
+ ] as const);
53
+
54
+ export type TLocationMemberKind = typeof locationMemberKinds[number];
55
+
56
+ /** True for one of `locationMemberKinds`. */
57
+ export const isLocationMemberKind = (valueArg: unknown): valueArg is TLocationMemberKind => (
58
+ (locationMemberKinds as readonly unknown[]).includes(valueArg)
59
+ );
60
+
61
+ /** One object a location holds: its kind and its id within that kind. */
62
+ export interface ILocationMember {
63
+ kind: TLocationMemberKind;
64
+ id: string;
65
+ }
66
+
67
+ /**
68
+ * The membership an administrator confirmed complete for one location.
69
+ *
70
+ * Cloudly never rewrites it when membership changes: the location's membership is confirmed while
71
+ * `membershipDigest` equals the digest of its current membership (`isLocationMembershipConfirmed`).
72
+ */
73
+ export interface ILocationMembershipConfirmation {
74
+ /** `computeLocationMembershipDigest` of the membership the administrator saw. */
75
+ membershipDigest: TSha256Digest;
76
+ /** Unix milliseconds on Cloudly's clock when Cloudly accepted the confirmation. */
77
+ confirmedAt: number;
78
+ /** The confirming administrator's user id. */
79
+ confirmedBy: string;
80
+ }
81
+
82
+ /** A location as Cloudly stores it. */
83
+ export interface ILocation {
84
+ /** Fixed for the location's life; every reference uses it. */
85
+ id: string;
86
+ data: {
87
+ /** Unique among locations; `isLocationName`. */
88
+ name: string;
89
+ /** Unix milliseconds on Cloudly's clock. */
90
+ createdAt: number;
91
+ /** The creating administrator's user id; absent for the initial location Cloudly created itself. */
92
+ createdBy?: string;
93
+ /** Absent until an administrator first confirms the location's membership. */
94
+ membershipConfirmation?: ILocationMembershipConfirmation;
95
+ };
96
+ }
97
+
98
+ /** A location with what it holds now, as `getLocations` answers it. */
99
+ export interface ILocationView {
100
+ location: ILocation;
101
+ /** Every stored member of a kind this Cloudly records, sorted by kind and then id. */
102
+ members: ILocationMember[];
103
+ /** `computeLocationMembershipDigest(members, <the kinds this Cloudly records>)`. */
104
+ membershipDigest: TSha256Digest;
105
+ /** `isLocationMembershipConfirmed(location, membershipDigest)`. */
106
+ membershipConfirmed: boolean;
107
+ /**
108
+ * True for exactly one location: the one a create naming no location places the first node of
109
+ * an empty cluster in (the initial location).
110
+ */
111
+ receivesUnnamedCreates: boolean;
112
+ }
113
+
114
+ /**
115
+ * Narrows a list request to the rows of one location. A Cloudly that applied it echoes it as the
116
+ * response's `appliedLocationScope`; a response without the echo comes from a Cloudly that ignored
117
+ * it, and its list must not be shown as scoped.
118
+ */
119
+ export interface ILocationScope {
120
+ locationId: string;
121
+ }
122
+
123
+ /**
124
+ * Every reason Cloudly answers instead of a location request or a create that names a location.
125
+ * Each travels by name as a `TypedResponseError`, and a refused request changes nothing.
126
+ */
127
+ export const locationRefusals = Object.freeze([
128
+ /**
129
+ * A create named a location but creates no node to stand in it (`createCluster` with `setupMode`
130
+ * `manual` or absent); name the location on `createNodeJumpCommand` instead. Checked before
131
+ * `location-not-found`.
132
+ */
133
+ 'location-needs-node',
134
+ /** No location of that id exists. */
135
+ 'location-not-found',
136
+ /** The name is not one lowercase RFC 1123 label (`isLocationName`). */
137
+ 'location-name-invalid',
138
+ /** Another location carries that name. */
139
+ 'location-name-taken',
140
+ /** No cluster of that id exists (`setClusterLocation`). */
141
+ 'cluster-not-found',
142
+ /**
143
+ * A create for a cluster with nodes named a location other than the cluster's:
144
+ * `createNodeJumpCommand` when it is asked, and the jump-code claim when the cluster gained nodes
145
+ * in another location after the code was made.
146
+ */
147
+ 'location-mismatch',
148
+ /**
149
+ * A create or a jump-code claim for a cluster whose nodes already stand in more than one
150
+ * location: a state Cloudly never writes, which `getClusterLocationId` reports by throwing
151
+ * `ClusterSpansLocationsError`. `setClusterLocation` never answers it: it writes every node of the
152
+ * cluster to the target location in one transaction, which also repairs a spanning cluster.
153
+ */
154
+ 'cluster-spans-locations',
155
+ /**
156
+ * The digest a confirmation states is not the location's current membership digest: read the
157
+ * location again and confirm what it holds now.
158
+ */
159
+ 'membership-changed',
160
+ /** A concurrent change to the same records won; nothing changed. Read again and retry. */
161
+ 'conflict',
162
+ ] as const);
163
+
164
+ /** One of the reasons Cloudly answers instead of a location request. */
165
+ export type TLocationRefusal = typeof locationRefusals[number];
166
+
167
+ /** What a `setClusterLocation` did; every outcome is a success. */
168
+ export const setClusterLocationOutcomes = Object.freeze([
169
+ /** Every node of the cluster, and the cluster's copy of each, now names the location. */
170
+ 'moved',
171
+ /** Every node already stood in the location; nothing was written. */
172
+ 'unchanged',
173
+ /** The cluster has no nodes, so it stands nowhere and nothing was moved. */
174
+ 'cluster-empty',
175
+ ] as const);
176
+
177
+ export type TSetClusterLocationOutcome = typeof setClusterLocationOutcomes[number];
178
+
179
+ /** Thrown by `getClusterLocationId` for a cluster whose node copies disagree. */
180
+ export class ClusterSpansLocationsError extends Error {
181
+ public readonly refusal: TLocationRefusal = 'cluster-spans-locations';
182
+ public readonly clusterId: string;
183
+
184
+ public constructor(clusterIdArg: string) {
185
+ super(`Cluster ${clusterIdArg} spans locations.`);
186
+ this.name = 'ClusterSpansLocationsError';
187
+ this.clusterId = clusterIdArg;
188
+ }
189
+ }
190
+
191
+ /**
192
+ * The location a cluster stands in, read from its own `data.nodes` copies.
193
+ *
194
+ * `undefined` when the cluster has no nodes, or when no copy names a location (the cluster came
195
+ * from a controller without locations); such a cluster drops out of every location scope. Throws
196
+ * `ClusterSpansLocationsError` when the copies name different locations, or some name one and
197
+ * others none: Cloudly never writes that state, and no single answer would be true for it.
198
+ */
199
+ export const getClusterLocationId = (clusterArg: ICluster): string | undefined => {
200
+ const locationIds = new Set(clusterArg.data.nodes.map((nodeArg) => nodeArg.data.locationId));
201
+ if (locationIds.size > 1) {
202
+ throw new ClusterSpansLocationsError(clusterArg.id);
203
+ }
204
+ const [locationId] = locationIds;
205
+ return locationId;
206
+ };
207
+
208
+ /** True while the location's confirmed digest equals its current membership digest. */
209
+ export const isLocationMembershipConfirmed = (
210
+ locationArg: ILocation,
211
+ membershipDigestArg: TSha256Digest,
212
+ ): boolean => locationArg.data.membershipConfirmation?.membershipDigest === membershipDigestArg;
213
+
214
+ const fail = (reasonArg: string): never => {
215
+ throw new Error(`Location membership digest input rejected: ${reasonArg}.`);
216
+ };
217
+
218
+ const compareMembers = (leftArg: ILocationMember, rightArg: ILocationMember): number => (
219
+ compareWireTokens(leftArg.kind, rightArg.kind) || compareWireTokens(leftArg.id, rightArg.id)
220
+ );
221
+
222
+ /**
223
+ * The canonical JSON a membership digest hashes: the digest domain, the recorded kinds in
224
+ * canonical order and the members as `[kind, id]` pairs sorted by kind and then id. The order the
225
+ * caller lists either in does not matter. Throws for an unknown or repeated kind, a member of a
226
+ * kind not recorded, an empty id or a repeated member.
227
+ */
228
+ export const createLocationMembershipDigestInput = (
229
+ membersArg: readonly ILocationMember[],
230
+ recordedKindsArg: readonly TLocationMemberKind[],
231
+ ): string => {
232
+ const recordedKinds = [...recordedKindsArg].sort(compareWireTokens);
233
+ recordedKinds.forEach((kindArg, indexArg) => {
234
+ if (!isLocationMemberKind(kindArg)) fail('unknown member kind');
235
+ if (indexArg > 0 && kindArg === recordedKinds[indexArg - 1]) fail('repeated member kind');
236
+ });
237
+ const members = [...membersArg].sort(compareMembers);
238
+ members.forEach((memberArg, indexArg) => {
239
+ if (!recordedKinds.includes(memberArg.kind)) fail('member of a kind not recorded');
240
+ if (typeof memberArg.id !== 'string' || memberArg.id.length === 0) fail('member id empty');
241
+ if (indexArg > 0 && compareMembers(memberArg, members[indexArg - 1]) === 0) fail('repeated member');
242
+ });
243
+ return canonicalizeStrictJson({
244
+ domain: locationContract.digestDomain,
245
+ recordedKinds,
246
+ members: members.map((memberArg) => [memberArg.kind, memberArg.id]),
247
+ }, fail, 'location membership');
248
+ };
249
+
250
+ /**
251
+ * `sha256:<hex>` of `createLocationMembershipDigestInput`, through Web Crypto, so Cloudly and a
252
+ * browser client compute the same value.
253
+ */
254
+ export const computeLocationMembershipDigest = async (
255
+ membersArg: readonly ILocationMember[],
256
+ recordedKindsArg: readonly TLocationMemberKind[],
257
+ ): Promise<TSha256Digest> => `sha256:${await createCanonicalJsonSha256Hex(
258
+ createLocationMembershipDigestInput(membersArg, recordedKindsArg), fail,
259
+ )}`;
@@ -1,5 +1,6 @@
1
1
  import * as plugins from '../plugins.js';
2
2
  import type { IBareMetal } from '../data/baremetal.js';
3
+ import type { ILocationScope } from '../data/location.js';
3
4
  import type { IIdentity } from '../data/user.js';
4
5
 
5
6
  export interface IRequest_Any_Cloudly_GetBaremetalServers
@@ -10,9 +11,20 @@ extends plugins.typedrequestInterfaces.implementsTR<
10
11
  method: 'getBaremetalServers';
11
12
  request: {
12
13
  identity: IIdentity;
14
+ /**
15
+ * Only the servers standing in this location: those a node of the location names in its
16
+ * `data.baremetalId`. A server no node names stands nowhere and is never in a scoped answer;
17
+ * `IBareMetal.data.location` (the provider's datacenter) plays no part.
18
+ */
19
+ locationScope?: ILocationScope;
13
20
  };
14
21
  response: {
15
22
  baremetals: IBareMetal[];
23
+ /**
24
+ * The scope Cloudly applied, echoing `locationScope`. Absent from a Cloudly that ignored the
25
+ * scope (one without locations): its list is unscoped and must not be shown as scoped.
26
+ */
27
+ appliedLocationScope?: ILocationScope;
16
28
  };
17
29
  }
18
30
 
@@ -1,5 +1,6 @@
1
1
  import * as userInterfaces from '../data/user.js';
2
2
  import * as clusterInterfaces from '../data/cluster.js';
3
+ import type * as locationInterfaces from '../data/location.js';
3
4
  import type * as relayCredentialInterfaces from '../data/clusterrelaycredential.js';
4
5
  import type * as clusterVpnInterfaces from '../data/clustervpn.js';
5
6
  import type { IClusterRuntime, TClusterRuntimeBlocker } from '../data/clusterruntime.js';
@@ -17,9 +18,19 @@ export interface IReq_Any_Cloudly_GetClusters extends plugins.typedrequestInterf
17
18
  method: 'getClusters';
18
19
  request: {
19
20
  identity: userInterfaces.IIdentity;
21
+ /**
22
+ * Only the clusters standing in this location (`data.getClusterLocationId`); a cluster without
23
+ * nodes stands nowhere and is never in a scoped answer.
24
+ */
25
+ locationScope?: locationInterfaces.ILocationScope;
20
26
  };
21
27
  response: {
22
28
  clusters: clusterInterfaces.ICluster[];
29
+ /**
30
+ * The scope Cloudly applied, echoing `locationScope`. Absent from a Cloudly that ignored the
31
+ * scope (one without locations): its list is unscoped and must not be shown as scoped.
32
+ */
33
+ appliedLocationScope?: locationInterfaces.ILocationScope;
23
34
  };
24
35
  }
25
36
 
@@ -47,6 +58,15 @@ export interface IRequest_CreateCluster extends plugins.typedrequestInterfaces.i
47
58
  identity: userInterfaces.IIdentity;
48
59
  clusterName: string;
49
60
  setupMode?: 'manual' | 'hetzner' | 'aws' | 'digitalocean';
61
+ /**
62
+ * The location the node a provisioning setup mode creates stands in. Refused
63
+ * `location-needs-node` when the create makes no node to stand in it (`setupMode` `manual` or
64
+ * absent; name the location on `createNodeJumpCommand` instead), checked before
65
+ * `location-not-found`, which answers a location that does not exist. Absent, the created node
66
+ * stands in the location that receives unnamed creates (the initial location); a create that
67
+ * makes no node leaves the cluster empty, standing nowhere.
68
+ */
69
+ locationId?: string;
50
70
  };
51
71
  response: {
52
72
  cluster: clusterInterfaces.ICluster;
@@ -23,6 +23,7 @@ import * as hostedAppRequests from './hostedapp.js';
23
23
  import * as identityRequests from './identity.js';
24
24
  import * as imageRequests from './image.js';
25
25
  import * as informRequests from './inform.js';
26
+ import * as locationRequests from './location.js';
26
27
  import * as logRequests from './log.js';
27
28
  import * as mailRequests from './mail.js';
28
29
  import * as migrationRequests from './migration.js';
@@ -64,6 +65,7 @@ export {
64
65
  identityRequests as identity,
65
66
  imageRequests as image,
66
67
  informRequests as inform,
68
+ locationRequests as location,
67
69
  logRequests as log,
68
70
  mailRequests as mail,
69
71
  migrationRequests as migration,
@@ -0,0 +1,119 @@
1
+ import * as plugins from '../plugins.js';
2
+ import type { ICluster } from '../data/cluster.js';
3
+ import type { TSha256Digest } from '../data/immutableimage.js';
4
+ import type {
5
+ ILocation,
6
+ ILocationView,
7
+ TLocationMemberKind,
8
+ TSetClusterLocationOutcome,
9
+ } from '../data/location.js';
10
+ import type { IIdentity } from '../data/user.js';
11
+
12
+ /*
13
+ * Location administration. Every request here is an administrator's alone: no delegated-admin
14
+ * scope admits one (`data.delegatedAdminTokenScopeRequests`). Every refusal is one of
15
+ * `data.locationRefusals` and changes nothing.
16
+ */
17
+
18
+ /** An administrator creates a location; refused `location-name-invalid` or `location-name-taken`. */
19
+ export interface IReq_Any_Cloudly_CreateLocation extends plugins.typedrequestInterfaces.implementsTR<
20
+ plugins.typedrequestInterfaces.ITypedRequest,
21
+ IReq_Any_Cloudly_CreateLocation
22
+ > {
23
+ method: 'createLocation';
24
+ request: {
25
+ identity: IIdentity;
26
+ /** `data.isLocationName`, unique among locations. */
27
+ name: string;
28
+ };
29
+ response: {
30
+ location: ILocation;
31
+ };
32
+ }
33
+
34
+ /**
35
+ * An administrator renames a location; its id and every reference stay. Renaming to the name it
36
+ * carries changes nothing. Refused `location-not-found`, `location-name-invalid` or
37
+ * `location-name-taken`.
38
+ */
39
+ export interface IReq_Any_Cloudly_RenameLocation extends plugins.typedrequestInterfaces.implementsTR<
40
+ plugins.typedrequestInterfaces.ITypedRequest,
41
+ IReq_Any_Cloudly_RenameLocation
42
+ > {
43
+ method: 'renameLocation';
44
+ request: {
45
+ identity: IIdentity;
46
+ locationId: string;
47
+ /** `data.isLocationName`, unique among locations. */
48
+ name: string;
49
+ };
50
+ response: {
51
+ location: ILocation;
52
+ };
53
+ }
54
+
55
+ /** Every location with its members, its membership digest and whether that membership is confirmed. */
56
+ export interface IReq_Any_Cloudly_GetLocations extends plugins.typedrequestInterfaces.implementsTR<
57
+ plugins.typedrequestInterfaces.ITypedRequest,
58
+ IReq_Any_Cloudly_GetLocations
59
+ > {
60
+ method: 'getLocations';
61
+ request: {
62
+ identity: IIdentity;
63
+ };
64
+ response: {
65
+ /** Sorted by location id; exactly one has `receivesUnnamedCreates`. */
66
+ locations: ILocationView[];
67
+ /**
68
+ * The member kinds this Cloudly records, in `data.locationMemberKinds` order; every view's
69
+ * digest covers them. A kind missing here has no members anywhere yet, so a confirmation
70
+ * cannot vouch for it.
71
+ */
72
+ recordedMemberKinds: TLocationMemberKind[];
73
+ };
74
+ }
75
+
76
+ /**
77
+ * An administrator moves a whole cluster to a location: every node of the cluster and the
78
+ * cluster's copy of each, in one transaction. A cluster without nodes stands nowhere, so the call
79
+ * moves nothing and answers `cluster-empty`. Refused `cluster-not-found`, `location-not-found` or
80
+ * `conflict`; never `cluster-spans-locations`, since the move also repairs a cluster whose nodes
81
+ * stand in more than one location.
82
+ */
83
+ export interface IReq_Any_Cloudly_SetClusterLocation extends plugins.typedrequestInterfaces.implementsTR<
84
+ plugins.typedrequestInterfaces.ITypedRequest,
85
+ IReq_Any_Cloudly_SetClusterLocation
86
+ > {
87
+ method: 'setClusterLocation';
88
+ request: {
89
+ identity: IIdentity;
90
+ clusterId: string;
91
+ locationId: string;
92
+ };
93
+ response: {
94
+ outcome: TSetClusterLocationOutcome;
95
+ /** The cluster as it now stands. */
96
+ cluster: ICluster;
97
+ };
98
+ }
99
+
100
+ /**
101
+ * An administrator confirms that a location holds everything it should, stating the
102
+ * `membershipDigest` of the view they read. Refused `membership-changed` when the location's
103
+ * membership is no longer that one, and `location-not-found`. The confirmation holds until the
104
+ * membership changes; nothing rewrites it then.
105
+ */
106
+ export interface IReq_Any_Cloudly_ConfirmLocationMembership extends plugins.typedrequestInterfaces.implementsTR<
107
+ plugins.typedrequestInterfaces.ITypedRequest,
108
+ IReq_Any_Cloudly_ConfirmLocationMembership
109
+ > {
110
+ method: 'confirmLocationMembership';
111
+ request: {
112
+ identity: IIdentity;
113
+ locationId: string;
114
+ membershipDigest: TSha256Digest;
115
+ };
116
+ response: {
117
+ location: ILocationView;
118
+ };
119
+ }
@@ -3,6 +3,7 @@ import type { IBareMetal } from '../data/baremetal.js';
3
3
  import type { ICluster } from '../data/cluster.js';
4
4
  import type { IClusterNode, INodeAction, INodeMetricsSample, TNodeActionName } from '../data/clusternode.js';
5
5
  import type { IDeployment } from '../data/deployment.js';
6
+ import type { ILocationScope } from '../data/location.js';
6
7
  import type { INodeActionConfirmation, INodeActionPreview } from '../data/nodeaction.js';
7
8
  import type { ISparkFleetTarget, ISparkFleetTargetChange, ISparkNodeTargetHold } from '../data/sparkfleettarget.js';
8
9
  import type { IIdentity } from '../data/user.js';
@@ -159,6 +160,14 @@ export interface IReq_Any_Cloudly_CreateNodeJumpCommand extends plugins.typedreq
159
160
  * its name is proposed from that (`data.proposeClusterNodeName`).
160
161
  */
161
162
  nodeName?: string;
163
+ /**
164
+ * The location the new node stands in. Named, the location must exist (`location-not-found`)
165
+ * and, when the cluster has nodes, be the cluster's (`location-mismatch`); the jump code keeps
166
+ * it and the claim checks it again against the cluster as it stands then. Absent, the claim
167
+ * places the node in the cluster's location, or for a cluster without nodes in the location
168
+ * that receives unnamed creates (the initial location).
169
+ */
170
+ locationId?: string;
162
171
  };
163
172
  response: {
164
173
  jumpCode: string;
@@ -177,9 +186,16 @@ export interface IReq_Any_Cloudly_GetNodes extends plugins.typedrequestInterface
177
186
  request: {
178
187
  identity: IIdentity;
179
188
  clusterId?: string;
189
+ /** Only the nodes whose `data.locationId` is this location. */
190
+ locationScope?: ILocationScope;
180
191
  };
181
192
  response: {
182
193
  nodes: IClusterNode[];
194
+ /**
195
+ * The scope Cloudly applied, echoing `locationScope`. Absent from a Cloudly that ignored the
196
+ * scope (one without locations): its list is unscoped and must not be shown as scoped.
197
+ */
198
+ appliedLocationScope?: ILocationScope;
183
199
  };
184
200
  }
185
201