@geek-fun/serverlessinsight 0.9.3 → 0.9.5
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.
- package/AGENTS.md +5 -2
- package/CHANGELOG.md +56 -0
- package/README.md +266 -179
- package/README.zh-CN.md +264 -147
- package/codecov +0 -0
- package/codecov.SHA256SUM +1 -1
- package/codecov.SHA256SUM.sig +13 -13
- package/dist/package.json +22 -19
- package/dist/src/commands/deploy.js +7 -2
- package/dist/src/commands/destroy.js +6 -3
- package/dist/src/commands/forceUnlock.js +16 -21
- package/dist/src/commands/index.js +26 -63
- package/dist/src/commands/migrate.js +105 -132
- package/dist/src/commands/plan.js +27 -3
- package/dist/src/commands/show.js +17 -6
- package/dist/src/common/aggregateState.js +185 -0
- package/dist/src/common/aggregateStore.js +247 -0
- package/dist/src/common/aliyunClient/apigwOperations.js +32 -9
- package/dist/src/common/apiClient.js +35 -12
- package/dist/src/common/constants.js +2 -1
- package/dist/src/common/context.js +22 -15
- package/dist/src/common/effectiveConfig.js +48 -0
- package/dist/src/common/index.js +5 -0
- package/dist/src/common/jsonOutput.js +2 -13
- package/dist/src/common/migrationMarker.js +0 -7
- package/dist/src/common/nameBuilder.js +2 -3
- package/dist/src/common/regionalMigration.js +261 -0
- package/dist/src/common/saasProtocol.js +41 -0
- package/dist/src/common/stateBackend/cosStateBackend.js +88 -8
- package/dist/src/common/stateBackend/index.js +42 -18
- package/dist/src/common/stateBackend/localStateBackend.js +61 -26
- package/dist/src/common/stateBackend/ossStateBackend.js +62 -9
- package/dist/src/common/stateBackend/remoteStateBackend.js +94 -94
- package/dist/src/common/stateBackend/saasStateBackend.js +327 -92
- package/dist/src/common/stateBackend/types.js +9 -0
- package/dist/src/common/stateManager.js +38 -11
- package/dist/src/common/stateScope.js +34 -0
- package/dist/src/common/stateStorageIO.js +31 -0
- package/dist/src/lang/en.js +48 -6
- package/dist/src/lang/index.js +28 -14
- package/dist/src/lang/zh-CN.js +48 -6
- package/dist/src/parser/index.js +25 -4
- package/dist/src/stack/aliyunStack/apigwDomainPlan.js +82 -0
- package/dist/src/stack/aliyunStack/apigwExecutor.js +4 -1
- package/dist/src/stack/aliyunStack/apigwPlanner.js +115 -55
- package/dist/src/stack/aliyunStack/apigwResource.js +169 -124
- package/dist/src/stack/aliyunStack/deployer.js +5 -5
- package/dist/src/stack/aliyunStack/destroyer.js +5 -5
- package/dist/src/stack/aliyunStack/fc3Planner.js +1 -1
- package/dist/src/stack/aliyunStack/fc3Resource.js +2 -2
- package/dist/src/stack/aliyunStack/planner.js +1 -1
- package/dist/src/stack/aliyunStack/sharedLogProject.js +2 -2
- package/dist/src/stack/ownershipTag.js +34 -26
- package/dist/src/stack/scfStack/deployer.js +5 -5
- package/dist/src/stack/scfStack/destroyer.js +5 -6
- package/dist/src/stack/scfStack/planner.js +1 -1
- package/dist/src/stack/scfStack/scfResource.js +1 -1
- package/dist/src/stack/scfStack/sharedLogset.js +5 -2
- package/dist/src/stack/volcengineStack/deployer.js +3 -3
- package/dist/src/stack/volcengineStack/destroyer.js +2 -3
- package/dist/src/stack/volcengineStack/planner.js +1 -1
- package/dist/src/stack/volcengineStack/sharedLogProject.js +4 -4
- package/dist/src/stack/volcengineStack/vefaasResource.js +1 -1
- package/dist/src/types/domains/state.js +28 -2
- package/dist/src/validator/rootSchema.js +8 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -19
- package/release-notes.md +27 -56
- package/samples/README_TENCENT_COS.md +68 -26
- package/samples/README_TENCENT_SCF.md +47 -29
- package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +0 -14
- package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +0 -32
- package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +0 -90
- package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +0 -118
- package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +0 -215
- package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +0 -14
- package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +0 -132
- package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +0 -55
- package/.serverlessinsight/state-insight-volc-destroy-app-insight-volc-destroy.json.backup +0 -1
- package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +0 -93
- package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +0 -163
- package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +0 -148
|
@@ -8,8 +8,9 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
9
|
const credentials_1 = require("./credentials");
|
|
10
10
|
const imsClient_1 = require("./imsClient");
|
|
11
|
-
const providerEnum_1 = require("./providerEnum");
|
|
12
11
|
const refreshCache_1 = require("./refreshCache");
|
|
12
|
+
const effectiveConfig_1 = require("./effectiveConfig");
|
|
13
|
+
const logger_1 = require("./logger");
|
|
13
14
|
const lang_1 = require("../lang");
|
|
14
15
|
let context;
|
|
15
16
|
const DEFAULT_IAC_FILES = [
|
|
@@ -55,31 +56,37 @@ const getIacLocation = (location) => {
|
|
|
55
56
|
};
|
|
56
57
|
exports.getIacLocation = getIacLocation;
|
|
57
58
|
const setContext = async (config, reaValToken = false) => {
|
|
58
|
-
const
|
|
59
|
-
config.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
const effective = (0, effectiveConfig_1.resolveEffectiveConfig)({
|
|
60
|
+
cli: { stage: config.stage, region: config.region, provider: config.provider },
|
|
61
|
+
iacProvider: {
|
|
62
|
+
name: config.iacProvider?.name,
|
|
63
|
+
region: config.iacProvider?.region,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
if (!config.region &&
|
|
67
|
+
!config.iacProvider?.region &&
|
|
68
|
+
process.env.ROS_REGION_ID &&
|
|
69
|
+
!process.env.SI_REGION) {
|
|
70
|
+
logger_1.logger.warn(lang_1.lang.__('CONTEXT_REGION_DEPRECATED'));
|
|
65
71
|
}
|
|
66
|
-
const provider = (config.provider ??
|
|
67
|
-
config.iacProvider?.name ??
|
|
68
|
-
providerEnum_1.ProviderEnum.ALIYUN);
|
|
69
72
|
const credentials = (0, credentials_1.getCredentials)({
|
|
70
73
|
accessKeyId: config.accessKeyId,
|
|
71
74
|
accessKeySecret: config.accessKeySecret,
|
|
72
75
|
securityToken: config.securityToken,
|
|
73
|
-
}, provider);
|
|
76
|
+
}, effective.provider);
|
|
74
77
|
const newContext = {
|
|
75
|
-
stage:
|
|
78
|
+
stage: effective.stage,
|
|
76
79
|
app: config.app,
|
|
77
80
|
service: config.service,
|
|
78
|
-
provider,
|
|
79
|
-
region,
|
|
81
|
+
provider: effective.provider,
|
|
82
|
+
region: effective.region,
|
|
80
83
|
accessKeyId: credentials.accessKeyId,
|
|
81
84
|
accessKeySecret: credentials.accessKeySecret,
|
|
82
85
|
securityToken: credentials.securityToken,
|
|
86
|
+
storage: (() => {
|
|
87
|
+
const defaults = (0, effectiveConfig_1.resolveEffectiveConfig)({ iacProvider: config.iacProvider });
|
|
88
|
+
return { provider: defaults.provider, region: defaults.region };
|
|
89
|
+
})(),
|
|
83
90
|
iacLocation: (0, exports.getIacLocation)(config.location),
|
|
84
91
|
parameters: Object.entries(config.parameters ?? {}).map(([key, value]) => ({ key, value })),
|
|
85
92
|
stages: Object.entries(config.stages ?? {}).reduce((acc, [stage, parameters]) => ({
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveEffectiveConfig = void 0;
|
|
4
|
+
const providerEnum_1 = require("./providerEnum");
|
|
5
|
+
const stateScope_1 = require("./stateScope");
|
|
6
|
+
const lang_1 = require("../lang");
|
|
7
|
+
const DEFAULT_STAGE = 'default';
|
|
8
|
+
const DEFAULT_REGION = 'cn-hangzhou';
|
|
9
|
+
const parseCliProvider = (raw) => {
|
|
10
|
+
if (!raw)
|
|
11
|
+
return undefined;
|
|
12
|
+
const normalized = raw.trim().toLowerCase();
|
|
13
|
+
if (!Object.values(providerEnum_1.ProviderEnum).includes(normalized)) {
|
|
14
|
+
throw new Error(lang_1.lang.__('EFFECTIVE_CONFIG_UNKNOWN_PROVIDER', { provider: raw }));
|
|
15
|
+
}
|
|
16
|
+
return normalized;
|
|
17
|
+
};
|
|
18
|
+
const resolveEffectiveConfig = (input) => {
|
|
19
|
+
const cli = input.cli ?? {};
|
|
20
|
+
const iacProvider = input.iacProvider ?? {};
|
|
21
|
+
const cliRegion = cli.region?.trim() || undefined;
|
|
22
|
+
const cliProvider = parseCliProvider(cli.provider);
|
|
23
|
+
const region = cliRegion ??
|
|
24
|
+
iacProvider.region?.trim() ??
|
|
25
|
+
process.env.SI_REGION ??
|
|
26
|
+
process.env.ALIYUN_REGION ??
|
|
27
|
+
DEFAULT_REGION;
|
|
28
|
+
const provider = cliProvider ?? iacProvider.name ?? providerEnum_1.ProviderEnum.ALIYUN;
|
|
29
|
+
const stage = cli.stage?.trim() || DEFAULT_STAGE;
|
|
30
|
+
return {
|
|
31
|
+
provider,
|
|
32
|
+
region: canonicalRegion(region),
|
|
33
|
+
stage: (0, stateScope_1.validateScopeKeyPart)(stage, 'stage'),
|
|
34
|
+
overrides: {
|
|
35
|
+
region: !!cliRegion,
|
|
36
|
+
provider: !!cliProvider,
|
|
37
|
+
stage: !!cli.stage?.trim(),
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
exports.resolveEffectiveConfig = resolveEffectiveConfig;
|
|
42
|
+
const canonicalRegion = (region) => {
|
|
43
|
+
const canonical = region.trim().toLowerCase();
|
|
44
|
+
if (!canonical) {
|
|
45
|
+
throw new Error(lang_1.lang.__('EFFECTIVE_CONFIG_REGION_REQUIRED'));
|
|
46
|
+
}
|
|
47
|
+
return canonical;
|
|
48
|
+
};
|
package/dist/src/common/index.js
CHANGED
|
@@ -47,3 +47,8 @@ __exportStar(require("./planCapabilities"), exports);
|
|
|
47
47
|
__exportStar(require("./bucketPolicy"), exports);
|
|
48
48
|
__exportStar(require("./functionDefinition"), exports);
|
|
49
49
|
__exportStar(require("./planRedaction"), exports);
|
|
50
|
+
__exportStar(require("./stateScope"), exports);
|
|
51
|
+
__exportStar(require("./effectiveConfig"), exports);
|
|
52
|
+
__exportStar(require("./regionalMigration"), exports);
|
|
53
|
+
__exportStar(require("./stateStorageIO"), exports);
|
|
54
|
+
__exportStar(require("./saasProtocol"), exports);
|
|
@@ -3,20 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.buildPlanJson = exports.buildPlanChange = exports.buildPlanSummary = exports.writeJson = exports.isJsonMode = exports.setJsonMode = exports.SHOW_JSON_VERSION = exports.VALIDATE_JSON_VERSION = exports.PLAN_JSON_VERSION = void 0;
|
|
4
4
|
const planRedaction_1 = require("./planRedaction");
|
|
5
5
|
const planFormatter_1 = require("./planFormatter");
|
|
6
|
-
|
|
7
|
-
* Machine-readable output channel (issue #250): the `--json` surface of the CLI.
|
|
8
|
-
*
|
|
9
|
-
* Contract with agents:
|
|
10
|
-
* - logs (pino) go to stderr, JSON results go to stdout — a pipe of stdout is
|
|
11
|
-
* always a single JSON document (jq-parseable);
|
|
12
|
-
* - every envelope carries a version field (`planVersion` / `validateVersion` /
|
|
13
|
-
* `showVersion`) so consumers can pin a shape;
|
|
14
|
-
* - failures emit `{ error: { code, message } }` where `code` is the stable
|
|
15
|
-
* i18n key behind the message — locale changes never change the code.
|
|
16
|
-
*/
|
|
17
|
-
exports.PLAN_JSON_VERSION = 1;
|
|
6
|
+
exports.PLAN_JSON_VERSION = 2;
|
|
18
7
|
exports.VALIDATE_JSON_VERSION = 1;
|
|
19
|
-
exports.SHOW_JSON_VERSION =
|
|
8
|
+
exports.SHOW_JSON_VERSION = 2;
|
|
20
9
|
let jsonMode = false;
|
|
21
10
|
const setJsonMode = (enabled) => {
|
|
22
11
|
jsonMode = enabled;
|
|
@@ -15,13 +15,6 @@ const isMigratedToSaas = (state) => {
|
|
|
15
15
|
return state?.managedBy === exports.MANAGED_BY_SAAS;
|
|
16
16
|
};
|
|
17
17
|
exports.isMigratedToSaas = isMigratedToSaas;
|
|
18
|
-
/**
|
|
19
|
-
* Ownership handover check (decision D-4): after migration the Console is the
|
|
20
|
-
* single source of truth. Mutating commands (deploy/destroy) pass 'refuse' so
|
|
21
|
-
* a legacy-backend write attempt fails closed; read-only commands (plan/diff/
|
|
22
|
-
* show) pass 'warn'; `si migrate` itself passes 'off' (it must be able to read
|
|
23
|
-
* marked state for idempotent re-runs and --rollback).
|
|
24
|
-
*/
|
|
25
18
|
const enforceMigrationMarker = (state, action) => {
|
|
26
19
|
if (action === 'off' || !(0, exports.isMigratedToSaas)(state)) {
|
|
27
20
|
return;
|
|
@@ -79,9 +79,8 @@ exports.CONSTRAINT_NAME_LIMITS = {
|
|
|
79
79
|
VOLCENGINE_ROUTE_NAME: 63,
|
|
80
80
|
FUNCTION_ROLE_NAME: 64,
|
|
81
81
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
parts: [serviceName, stage, fnKey, 'role'],
|
|
82
|
+
const buildFunctionRoleName = (serviceName, stage, region, fnKey) => (0, exports.buildConstrainedName)({
|
|
83
|
+
parts: [serviceName, stage, region, fnKey, 'role'],
|
|
85
84
|
maxLength: exports.CONSTRAINT_NAME_LIMITS.FUNCTION_ROLE_NAME,
|
|
86
85
|
charset: 'hyphen',
|
|
87
86
|
});
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.applyRegionalMigration = exports.readRegionalMigrationJournal = exports.buildScopedDocument = exports.planRegionalMigration = exports.hashDocument = exports.collectRegionEvidence = void 0;
|
|
7
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
8
|
+
const aggregateState_1 = require("./aggregateState");
|
|
9
|
+
const types_1 = require("../types");
|
|
10
|
+
const stateScope_1 = require("./stateScope");
|
|
11
|
+
const migrationMarker_1 = require("./migrationMarker");
|
|
12
|
+
const logger_1 = require("./logger");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
const lang_1 = require("../lang");
|
|
15
|
+
const emptyRegion = (region) => !region || String(region).trim().length === 0;
|
|
16
|
+
const collectRegionEvidence = (legacy) => {
|
|
17
|
+
return Object.entries(legacy.stages ?? {}).flatMap(([stage, stageState]) => ['resource', 'shared'].flatMap((kind) => Object.entries(kind === 'resource' ? (stageState.resources ?? {}) : (stageState.shared ?? {})).map(([logicalId, resource]) => ({
|
|
18
|
+
stage,
|
|
19
|
+
evidence: {
|
|
20
|
+
logicalId,
|
|
21
|
+
kind,
|
|
22
|
+
region: emptyRegion(resource.region) ? null : String(resource.region).toLowerCase(),
|
|
23
|
+
},
|
|
24
|
+
}))));
|
|
25
|
+
};
|
|
26
|
+
exports.collectRegionEvidence = collectRegionEvidence;
|
|
27
|
+
const hashDocument = (doc) => node_crypto_1.default.createHash('sha256').update(JSON.stringify(doc)).digest('hex');
|
|
28
|
+
exports.hashDocument = hashDocument;
|
|
29
|
+
const planRegionalMigration = (legacy, options) => {
|
|
30
|
+
if (legacy.managedBy === migrationMarker_1.MANAGED_BY_SAAS) {
|
|
31
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_MANAGED_BY_SAAS'));
|
|
32
|
+
}
|
|
33
|
+
if (legacy.version && legacy.version !== types_1.LEGACY_STATE_VERSION) {
|
|
34
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_UNSUPPORTED_SOURCE', {
|
|
35
|
+
version: legacy.version,
|
|
36
|
+
expected: types_1.LEGACY_STATE_VERSION,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
const evidence = (0, exports.collectRegionEvidence)(legacy).filter(({ stage }) => !options.stage || stage === options.stage);
|
|
40
|
+
const unknown = evidence
|
|
41
|
+
.filter(({ evidence: item }) => item.region === null)
|
|
42
|
+
.map(({ stage, evidence: item }) => ({ stage, logicalId: item.logicalId, kind: item.kind }));
|
|
43
|
+
// A local Map keeps grouping linear without mutating the source state.
|
|
44
|
+
const regionGroups = evidence.reduce((groups, { stage, evidence: item }) => {
|
|
45
|
+
if (item.region === null)
|
|
46
|
+
return groups;
|
|
47
|
+
const key = `${stage}::${item.region}`;
|
|
48
|
+
const previous = groups.get(key) ?? { resources: 0, shared: 0 };
|
|
49
|
+
return groups.set(key, {
|
|
50
|
+
resources: previous.resources + Number(item.kind === 'resource'),
|
|
51
|
+
shared: previous.shared + Number(item.kind === 'shared'),
|
|
52
|
+
});
|
|
53
|
+
}, new Map());
|
|
54
|
+
const warnings = legacy.provider && legacy.provider !== options.provider
|
|
55
|
+
? [
|
|
56
|
+
lang_1.lang.__('REGIONAL_MIGRATION_PROVIDER_MISMATCH', {
|
|
57
|
+
source: legacy.provider,
|
|
58
|
+
effective: options.provider,
|
|
59
|
+
}),
|
|
60
|
+
]
|
|
61
|
+
: [];
|
|
62
|
+
return {
|
|
63
|
+
opId: node_crypto_1.default.randomUUID(),
|
|
64
|
+
sourceVersion: legacy.version ?? types_1.LEGACY_STATE_VERSION,
|
|
65
|
+
sourceHash: (0, exports.hashDocument)(legacy),
|
|
66
|
+
documents: Array.from(regionGroups.entries()).map(([key, group]) => {
|
|
67
|
+
const [stage, region] = key.split('::');
|
|
68
|
+
return { stage, region, ...group };
|
|
69
|
+
}),
|
|
70
|
+
unknown,
|
|
71
|
+
warnings,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
exports.planRegionalMigration = planRegionalMigration;
|
|
75
|
+
const buildStageStateForRegion = (legacy, stage, region) => {
|
|
76
|
+
const stageState = legacy.stages?.[stage];
|
|
77
|
+
const selectRegion = (resources = {}) => Object.fromEntries(Object.entries(resources).filter(([, resource]) => !emptyRegion(resource.region) && String(resource.region).toLowerCase() === region));
|
|
78
|
+
return {
|
|
79
|
+
resources: selectRegion(stageState?.resources),
|
|
80
|
+
shared: selectRegion(stageState?.shared),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const buildScopedDocument = (legacy, plan, scope, target) => {
|
|
84
|
+
(0, stateScope_1.validateScopeKeyPart)(scope.stage, 'stage');
|
|
85
|
+
(0, stateScope_1.canonicalizeRegion)(scope.region);
|
|
86
|
+
const { resources, shared } = buildStageStateForRegion(legacy, scope.stage, scope.region);
|
|
87
|
+
const migration = {
|
|
88
|
+
opId: plan.opId,
|
|
89
|
+
sourceVersion: plan.sourceVersion,
|
|
90
|
+
sourceHash: plan.sourceHash,
|
|
91
|
+
migratedAt: new Date().toISOString(),
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
version: types_1.CURRENT_STATE_VERSION,
|
|
95
|
+
provider: target.provider,
|
|
96
|
+
app: target.app,
|
|
97
|
+
service: target.service,
|
|
98
|
+
...(legacy.orgId ? { orgId: legacy.orgId } : {}),
|
|
99
|
+
...(legacy.appId ? { appId: legacy.appId } : {}),
|
|
100
|
+
...(legacy.serviceId ? { serviceId: legacy.serviceId } : {}),
|
|
101
|
+
scope: { stage: scope.stage, region: scope.region },
|
|
102
|
+
lineage: node_crypto_1.default.randomUUID(),
|
|
103
|
+
serial: 1,
|
|
104
|
+
migration,
|
|
105
|
+
stages: {
|
|
106
|
+
[scope.stage]: {
|
|
107
|
+
resources,
|
|
108
|
+
...(Object.keys(shared).length > 0 ? { shared } : {}),
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
resources,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
exports.buildScopedDocument = buildScopedDocument;
|
|
115
|
+
const journalKey = (paths) => `${paths.legacyKey}${constants_1.REGIONAL_MIGRATION_SUFFIX}`;
|
|
116
|
+
const readRegionalMigrationJournal = (io, paths) => io.read(journalKey(paths));
|
|
117
|
+
exports.readRegionalMigrationJournal = readRegionalMigrationJournal;
|
|
118
|
+
const stripRuntimeProjection = (state) => {
|
|
119
|
+
const { resources: _projection, ...persisted } = state;
|
|
120
|
+
return persisted;
|
|
121
|
+
};
|
|
122
|
+
const buildMigrationTargets = (legacy, plan, options) => {
|
|
123
|
+
const bound = options.bindUnknownRegion
|
|
124
|
+
? (0, stateScope_1.canonicalizeRegion)(options.bindUnknownRegion)
|
|
125
|
+
: undefined;
|
|
126
|
+
const boundScopes = bound
|
|
127
|
+
? [...new Set(plan.unknown.map((item) => item.stage))]
|
|
128
|
+
.filter((stage) => !plan.documents.some((scope) => scope.stage === stage && scope.region === bound))
|
|
129
|
+
.map((stage) => ({ stage, region: bound, resources: 0, shared: 0 }))
|
|
130
|
+
: [];
|
|
131
|
+
return [...plan.documents, ...boundScopes].map(({ stage, region }) => {
|
|
132
|
+
const state = (0, exports.buildScopedDocument)(legacy, plan, { stage, region }, {
|
|
133
|
+
provider: legacy.provider || options.provider,
|
|
134
|
+
app: options.app,
|
|
135
|
+
service: options.service,
|
|
136
|
+
});
|
|
137
|
+
if (bound !== region)
|
|
138
|
+
return { stage, region, state: stripRuntimeProjection(state) };
|
|
139
|
+
const unknownFor = (kind) => Object.fromEntries(plan.unknown
|
|
140
|
+
.filter((item) => item.stage === stage && item.kind === kind)
|
|
141
|
+
.flatMap((item) => {
|
|
142
|
+
const store = kind === 'shared' ? 'shared' : 'resources';
|
|
143
|
+
const resource = legacy.stages[stage]?.[store]?.[item.logicalId];
|
|
144
|
+
return resource ? [[item.logicalId, { ...resource, region }]] : [];
|
|
145
|
+
}));
|
|
146
|
+
const shared = unknownFor('shared');
|
|
147
|
+
return {
|
|
148
|
+
stage,
|
|
149
|
+
region,
|
|
150
|
+
state: stripRuntimeProjection({
|
|
151
|
+
...state,
|
|
152
|
+
stages: {
|
|
153
|
+
...state.stages,
|
|
154
|
+
[stage]: {
|
|
155
|
+
...state.stages[stage],
|
|
156
|
+
resources: { ...state.stages[stage].resources, ...unknownFor('resource') },
|
|
157
|
+
...(Object.keys(shared).length
|
|
158
|
+
? { shared: { ...state.stages[stage].shared, ...shared } }
|
|
159
|
+
: {}),
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
}),
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
const aggregateMigrationTargets = (targets, options) => targets.reduce((document, { stage, region, state }) => (0, aggregateState_1.mergeAggregateScope)(document, { app: options.app, service: options.service, provider: state.provider, stage, region }, { ...state, resources: state.stages[stage].resources }), { version: aggregateState_1.AGGREGATE_STATE_VERSION, app: options.app, service: options.service, stages: {} });
|
|
167
|
+
const migrationReport = (journal) => ({
|
|
168
|
+
...journal.plan,
|
|
169
|
+
documents: journal.targets.map(({ stage, region, state }) => ({
|
|
170
|
+
stage,
|
|
171
|
+
region,
|
|
172
|
+
resources: Object.keys(state.stages[stage].resources).length,
|
|
173
|
+
shared: Object.keys(state.stages[stage].shared ?? {}).length,
|
|
174
|
+
})),
|
|
175
|
+
migratedAt: new Date().toISOString(),
|
|
176
|
+
barrierWritten: true,
|
|
177
|
+
});
|
|
178
|
+
/** The caller holds the original file/key lock throughout the in-place upgrade. */
|
|
179
|
+
const applyRegionalMigration = async (io, legacy, paths, plan, options) => {
|
|
180
|
+
if (plan.unknown.length > 0 && !options.bindUnknownRegion) {
|
|
181
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_UNKNOWN_REGION_REQUIRED'));
|
|
182
|
+
}
|
|
183
|
+
if ((0, exports.hashDocument)(legacy) !== plan.sourceHash) {
|
|
184
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
|
|
185
|
+
}
|
|
186
|
+
const previous = await (0, exports.readRegionalMigrationJournal)(io, paths);
|
|
187
|
+
if (previous &&
|
|
188
|
+
(previous.plan.opId !== plan.opId ||
|
|
189
|
+
!previous.aggregate ||
|
|
190
|
+
!['provider', 'app', 'service', 'stage', 'bindUnknownRegion'].every((key) => previous.options[key] === options[key]))) {
|
|
191
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_PENDING', { opId: previous.plan.opId }));
|
|
192
|
+
}
|
|
193
|
+
const targets = previous?.targets ?? buildMigrationTargets(legacy, plan, options);
|
|
194
|
+
const completePlan = {
|
|
195
|
+
...(0, exports.planRegionalMigration)(legacy, { ...options, stage: undefined }),
|
|
196
|
+
opId: plan.opId,
|
|
197
|
+
};
|
|
198
|
+
const expectedTargets = buildMigrationTargets(legacy, completePlan, options);
|
|
199
|
+
const uncovered = Object.entries(legacy.stages).filter(([stage, value]) => (Object.keys(value.resources ?? {}).length || Object.keys(value.shared ?? {}).length) &&
|
|
200
|
+
!targets.some((target) => target.stage === stage));
|
|
201
|
+
const coverage = (items) => items
|
|
202
|
+
.map(({ stage, region, state }) => ({
|
|
203
|
+
stage,
|
|
204
|
+
region,
|
|
205
|
+
provider: state.provider,
|
|
206
|
+
resources: state.stages[stage].resources,
|
|
207
|
+
shared: state.stages[stage].shared ?? {},
|
|
208
|
+
}))
|
|
209
|
+
.sort((left, right) => `${left.stage}/${left.region}`.localeCompare(`${right.stage}/${right.region}`));
|
|
210
|
+
if (uncovered.length ||
|
|
211
|
+
(0, exports.hashDocument)(coverage(targets)) !== (0, exports.hashDocument)(coverage(expectedTargets))) {
|
|
212
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_INCOMPLETE_STAGE_COVERAGE', {
|
|
213
|
+
stages: Object.keys(legacy.stages).join(', '),
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
const journal = previous ?? {
|
|
217
|
+
plan,
|
|
218
|
+
options,
|
|
219
|
+
targets,
|
|
220
|
+
phase: 'applying',
|
|
221
|
+
aggregate: aggregateMigrationTargets(targets, options),
|
|
222
|
+
};
|
|
223
|
+
if (journal.phase === 'complete') {
|
|
224
|
+
const report = await io.read(paths.reportKey(plan.opId));
|
|
225
|
+
if (report) {
|
|
226
|
+
(0, aggregateState_1.readAggregateDocument)(await io.read(paths.legacyKey), options, paths.legacyKey);
|
|
227
|
+
return report;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const current = await io.read(paths.legacyKey);
|
|
231
|
+
const sourceMatches = current !== null && (0, exports.hashDocument)(current) === plan.sourceHash;
|
|
232
|
+
const aggregateMatches = current !== null && (0, exports.hashDocument)(current) === (0, exports.hashDocument)(journal.aggregate);
|
|
233
|
+
if (!sourceMatches && !aggregateMatches) {
|
|
234
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
|
|
235
|
+
}
|
|
236
|
+
const backup = await io.read(paths.backupKey(plan.opId));
|
|
237
|
+
if (backup && (0, exports.hashDocument)(backup) !== plan.sourceHash) {
|
|
238
|
+
throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
|
|
239
|
+
}
|
|
240
|
+
if (!backup)
|
|
241
|
+
await io.write(paths.backupKey(plan.opId), legacy);
|
|
242
|
+
if (!previous)
|
|
243
|
+
await io.write(journalKey(paths), journal);
|
|
244
|
+
try {
|
|
245
|
+
if (sourceMatches)
|
|
246
|
+
await io.write(paths.legacyKey, journal.aggregate);
|
|
247
|
+
const report = migrationReport(journal);
|
|
248
|
+
await io.write(paths.reportKey(plan.opId), report);
|
|
249
|
+
await io.write(journalKey(paths), { ...journal, phase: 'complete' });
|
|
250
|
+
logger_1.logger.info(lang_1.lang.__('REGIONAL_MIGRATION_APPLIED', { opId: plan.opId }));
|
|
251
|
+
return report;
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
logger_1.logger.error(lang_1.lang.__('REGIONAL_MIGRATION_PARTIAL_FAILURE', {
|
|
255
|
+
opId: plan.opId,
|
|
256
|
+
error: error instanceof Error ? error.message : String(error),
|
|
257
|
+
}));
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
exports.applyRegionalMigration = applyRegionalMigration;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isRegionalSaasSnapshot = exports.supportsRegionalSaas = exports.hashSaasState = exports.SCOPE_PROTOCOL_VERSION = void 0;
|
|
7
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
8
|
+
/** Regional identity and content-hash CAS shared by all SaaS commands. */
|
|
9
|
+
exports.SCOPE_PROTOCOL_VERSION = 3;
|
|
10
|
+
/** JSONB can reorder keys; content identity must survive a server round trip. */
|
|
11
|
+
const hashSaasState = (state) => node_crypto_1.default
|
|
12
|
+
.createHash('sha256')
|
|
13
|
+
.update(JSON.stringify(state, (_key, value) => value && typeof value === 'object' && !Array.isArray(value)
|
|
14
|
+
? Object.fromEntries(Object.keys(value)
|
|
15
|
+
.sort()
|
|
16
|
+
.map((key) => [key, value[key]]))
|
|
17
|
+
: value))
|
|
18
|
+
.digest('hex');
|
|
19
|
+
exports.hashSaasState = hashSaasState;
|
|
20
|
+
const supportsRegionalSaas = (value) => {
|
|
21
|
+
const caps = value;
|
|
22
|
+
return (caps?.regionalScope === true &&
|
|
23
|
+
caps.providerScope === true &&
|
|
24
|
+
caps.scopeProtocol === exports.SCOPE_PROTOCOL_VERSION &&
|
|
25
|
+
caps.stateFormat === '4.0');
|
|
26
|
+
};
|
|
27
|
+
exports.supportsRegionalSaas = supportsRegionalSaas;
|
|
28
|
+
/** Scope authority is server metadata, independent of historical JSON format. */
|
|
29
|
+
const isRegionalSaasSnapshot = (snapshot, scope) => snapshot?.scopeProtocol === exports.SCOPE_PROTOCOL_VERSION &&
|
|
30
|
+
typeof snapshot.stageId === 'string' &&
|
|
31
|
+
snapshot.stageId.length > 0 &&
|
|
32
|
+
snapshot.stage === scope.stage &&
|
|
33
|
+
snapshot.provider === scope.provider &&
|
|
34
|
+
snapshot.region === scope.region &&
|
|
35
|
+
typeof snapshot.id === 'string' &&
|
|
36
|
+
snapshot.id.length > 0 &&
|
|
37
|
+
typeof snapshot.contentHash === 'string' &&
|
|
38
|
+
snapshot.contentHash.length > 0 &&
|
|
39
|
+
Number.isInteger(snapshot.versionNumber) &&
|
|
40
|
+
snapshot.versionNumber > 0;
|
|
41
|
+
exports.isRegionalSaasSnapshot = isRegionalSaasSnapshot;
|
|
@@ -3,14 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createCosStateBackend = void 0;
|
|
6
|
+
exports.createCosStateBackend = exports.createCosStorageAdapter = void 0;
|
|
7
7
|
const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
|
|
8
|
+
const types_1 = require("./types");
|
|
8
9
|
const remoteStateBackend_1 = require("./remoteStateBackend");
|
|
9
10
|
const constants_1 = require("../constants");
|
|
11
|
+
const lang_1 = require("../../lang");
|
|
10
12
|
const promisifyGet = (cosClient, bucket, region, key) => {
|
|
11
13
|
return new Promise((resolve, reject) => {
|
|
12
14
|
cosClient.getObject({ Bucket: bucket, Region: region, Key: key }, (err, data) => {
|
|
13
15
|
if (err) {
|
|
16
|
+
try {
|
|
17
|
+
classifyCosReadError(err);
|
|
18
|
+
}
|
|
19
|
+
catch (classified) {
|
|
20
|
+
reject(classified);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
14
23
|
if (err.statusCode === 404) {
|
|
15
24
|
resolve(null);
|
|
16
25
|
}
|
|
@@ -34,6 +43,36 @@ const promisifyPut = (cosClient, bucket, region, key, body) => {
|
|
|
34
43
|
});
|
|
35
44
|
});
|
|
36
45
|
};
|
|
46
|
+
const promisifyPutIfAbsent = (cosClient, bucket, region, key, body) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
cosClient.putObject({
|
|
49
|
+
Bucket: bucket,
|
|
50
|
+
Region: region,
|
|
51
|
+
Key: key,
|
|
52
|
+
Body: body,
|
|
53
|
+
Headers: { 'x-cos-forbid-overwrite': 'true' },
|
|
54
|
+
}, (err) => {
|
|
55
|
+
if (err) {
|
|
56
|
+
const code = err.code ?? '';
|
|
57
|
+
if (err.statusCode === 412 ||
|
|
58
|
+
code === 'PreconditionFailed' ||
|
|
59
|
+
code === 'KeyConflict' ||
|
|
60
|
+
err.statusCode === 409) {
|
|
61
|
+
resolve(false);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (err.statusCode === 400 || err.statusCode === 501) {
|
|
65
|
+
reject(new types_1.ConditionalPutUnsupportedError(err));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
reject(err);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
resolve(true);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
};
|
|
37
76
|
const promisifyDelete = (cosClient, bucket, region, key) => {
|
|
38
77
|
return new Promise((resolve, reject) => {
|
|
39
78
|
cosClient.deleteObject({ Bucket: bucket, Region: region, Key: key }, (err) => {
|
|
@@ -44,6 +83,14 @@ const promisifyDelete = (cosClient, bucket, region, key) => {
|
|
|
44
83
|
});
|
|
45
84
|
});
|
|
46
85
|
};
|
|
86
|
+
const classifyCosReadError = (err) => {
|
|
87
|
+
if (err.code === 'NoSuchBucket') {
|
|
88
|
+
throw new Error(lang_1.lang.__('STATE_BUCKET_NOT_FOUND', { code: err.code }));
|
|
89
|
+
}
|
|
90
|
+
if (err.statusCode === 403 || err.code === 'AccessDenied') {
|
|
91
|
+
throw new Error(lang_1.lang.__('STATE_BUCKET_ACCESS_DENIED', { code: err.code ?? 'AccessDenied' }));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
47
94
|
const createCosStorageAdapter = (config) => {
|
|
48
95
|
const cosClient = new cos_nodejs_sdk_v5_1.default({
|
|
49
96
|
SecretId: config.accessKeyId,
|
|
@@ -53,22 +100,55 @@ const createCosStorageAdapter = (config) => {
|
|
|
53
100
|
});
|
|
54
101
|
return {
|
|
55
102
|
read: async (key) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
103
|
+
try {
|
|
104
|
+
const body = await promisifyGet(cosClient, config.bucket, config.region, key);
|
|
105
|
+
if (!body)
|
|
106
|
+
return null;
|
|
107
|
+
return JSON.parse(body.toString('utf-8'));
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
classifyCosReadError(err);
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
60
113
|
},
|
|
61
114
|
write: async (key, data) => {
|
|
62
115
|
await promisifyPut(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
|
|
63
116
|
},
|
|
117
|
+
/** COS only honors forbid-overwrite on buckets that never enabled versioning. */
|
|
118
|
+
putIfAbsent: async (key, data) => {
|
|
119
|
+
const versioning = await new Promise((resolve, reject) => {
|
|
120
|
+
cosClient.getBucketVersioning({ Bucket: config.bucket, Region: config.region }, (error, data) => {
|
|
121
|
+
if (error)
|
|
122
|
+
reject(error);
|
|
123
|
+
else
|
|
124
|
+
resolve(data);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
if (versioning.VersioningConfiguration.Status) {
|
|
128
|
+
throw new types_1.ConditionalPutUnsupportedError('bucket versioning disables exclusive creation');
|
|
129
|
+
}
|
|
130
|
+
return promisifyPutIfAbsent(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
|
|
131
|
+
},
|
|
64
132
|
delete: async (key) => {
|
|
65
133
|
await promisifyDelete(cosClient, config.bucket, config.region, key);
|
|
66
134
|
},
|
|
67
|
-
lockPath:
|
|
135
|
+
lockPath: config.lockPath,
|
|
68
136
|
};
|
|
69
137
|
};
|
|
138
|
+
exports.createCosStorageAdapter = createCosStorageAdapter;
|
|
70
139
|
const createCosStateBackend = (config) => {
|
|
71
|
-
const adapter = createCosStorageAdapter(
|
|
72
|
-
|
|
140
|
+
const adapter = (0, exports.createCosStorageAdapter)({
|
|
141
|
+
bucket: config.bucket,
|
|
142
|
+
region: config.region,
|
|
143
|
+
accessKeyId: config.accessKeyId,
|
|
144
|
+
accessKeySecret: config.accessKeySecret,
|
|
145
|
+
securityToken: config.securityToken,
|
|
146
|
+
lockPath: `cos://${config.bucket}/${config.key}`,
|
|
147
|
+
});
|
|
148
|
+
return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, {
|
|
149
|
+
scope: config.scope,
|
|
150
|
+
key: config.key,
|
|
151
|
+
markerAction: config.markerAction,
|
|
152
|
+
});
|
|
73
153
|
};
|
|
74
154
|
exports.createCosStateBackend = createCosStateBackend;
|