powersync 0.7.1 → 0.9.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.
- package/LICENSE +67 -0
- package/README.md +1376 -165
- package/bin/dev.cmd +3 -3
- package/bin/dev.js +20 -7
- package/bin/run.cmd +3 -3
- package/bin/run.js +9 -5
- package/dist/api/cloud/create-cloud-instance.d.ts +15 -0
- package/dist/api/cloud/create-cloud-instance.js +19 -0
- package/dist/api/cloud/fetch-cloud-config.d.ts +12 -0
- package/dist/api/cloud/fetch-cloud-config.js +18 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.d.ts +7 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.js +26 -0
- package/dist/api/cloud/test-connection.d.ts +16 -0
- package/dist/api/cloud/test-connection.js +36 -0
- package/dist/api/cloud/validate-cloud-link-config.d.ts +17 -0
- package/dist/api/cloud/validate-cloud-link-config.js +47 -0
- package/dist/api/cloud/wait-for-operation.d.ts +16 -0
- package/dist/api/cloud/wait-for-operation.js +33 -0
- package/dist/api/cloud/write-cloud-link.d.ts +10 -0
- package/dist/api/cloud/write-cloud-link.js +21 -0
- package/dist/api/display-status.d.ts +4 -0
- package/dist/api/display-status.js +80 -0
- package/dist/api/ide/configure-vscode-ide.d.ts +14 -0
- package/dist/api/ide/configure-vscode-ide.js +62 -0
- package/dist/api/login-server.d.ts +26 -0
- package/dist/api/login-server.js +137 -0
- package/dist/api/run-validation-tests.d.ts +63 -0
- package/dist/api/run-validation-tests.js +202 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.d.ts +7 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.js +26 -0
- package/dist/commands/configure/ide.d.ts +8 -0
- package/dist/commands/configure/ide.js +58 -0
- package/dist/commands/configure/index.d.ts +8 -0
- package/dist/commands/configure/index.js +11 -0
- package/dist/commands/deploy/index.d.ts +48 -0
- package/dist/commands/deploy/index.js +333 -0
- package/dist/commands/deploy/service-config.d.ts +14 -0
- package/dist/commands/deploy/service-config.js +30 -0
- package/dist/commands/deploy/sync-config.d.ts +24 -0
- package/dist/commands/deploy/sync-config.js +103 -0
- package/dist/commands/destroy.d.ts +14 -0
- package/dist/commands/destroy.js +62 -0
- package/dist/commands/fetch/config.d.ts +14 -0
- package/dist/commands/fetch/config.js +33 -0
- package/dist/commands/fetch/index.d.ts +13 -0
- package/dist/commands/fetch/index.js +16 -0
- package/dist/commands/fetch/instances.d.ts +47 -0
- package/dist/commands/fetch/instances.js +177 -0
- package/dist/commands/fetch/status.d.ts +18 -0
- package/dist/commands/fetch/status.js +60 -0
- package/dist/commands/{instance/schema.d.ts → generate/index.d.ts} +3 -1
- package/dist/commands/generate/index.js +11 -0
- package/dist/commands/generate/schema.d.ts +19 -0
- package/dist/commands/generate/schema.js +72 -0
- package/dist/commands/generate/token.d.ts +25 -0
- package/dist/commands/generate/token.js +124 -0
- package/dist/commands/init/cloud.d.ts +11 -0
- package/dist/commands/init/cloud.js +64 -0
- package/dist/commands/{init.d.ts → init/index.d.ts} +2 -1
- package/dist/commands/init/index.js +11 -0
- package/dist/commands/init/self-hosted.d.ts +11 -0
- package/dist/commands/init/self-hosted.js +60 -0
- package/dist/commands/link/cloud.d.ts +15 -0
- package/dist/commands/link/cloud.js +111 -0
- package/dist/commands/{instance/config.d.ts → link/index.d.ts} +3 -1
- package/dist/commands/link/index.js +11 -0
- package/dist/commands/link/self-hosted.d.ts +12 -0
- package/dist/commands/link/self-hosted.js +54 -0
- package/dist/commands/login.d.ts +8 -0
- package/dist/commands/login.js +107 -0
- package/dist/commands/logout.d.ts +8 -0
- package/dist/commands/logout.js +19 -0
- package/dist/commands/migrate/index.d.ts +7 -0
- package/dist/commands/migrate/index.js +10 -0
- package/dist/commands/migrate/sync-rules.d.ts +15 -0
- package/dist/commands/migrate/sync-rules.js +51 -0
- package/dist/commands/{instances.d.ts → pull/index.d.ts} +3 -1
- package/dist/commands/pull/index.js +11 -0
- package/dist/commands/pull/instance.d.ts +14 -0
- package/dist/commands/pull/instance.js +108 -0
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +2 -0
- package/dist/commands/stop.d.ts +14 -0
- package/dist/commands/stop.js +62 -0
- package/dist/commands/validate.d.ts +16 -0
- package/dist/commands/validate.js +165 -0
- package/dist/help.d.ts +31 -0
- package/dist/help.js +123 -0
- package/oclif.manifest.json +1250 -275
- package/package.json +104 -75
- package/templates/cloud/powersync/cli.yaml +3 -0
- package/templates/cloud/powersync/service.yaml +174 -0
- package/templates/cloud/powersync/sync-config.yaml +22 -0
- package/templates/self-hosted/base/powersync/cli.yaml +3 -0
- package/templates/self-hosted/base/powersync/service.yaml +243 -0
- package/templates/self-hosted/base/powersync/sync-config.yaml +22 -0
- package/templates/vscode/settings.json +3 -0
- package/dist/commands/init.js +0 -66
- package/dist/commands/instance/config.js +0 -23
- package/dist/commands/instance/create.d.ts +0 -20
- package/dist/commands/instance/create.js +0 -121
- package/dist/commands/instance/deploy.d.ts +0 -18
- package/dist/commands/instance/deploy.js +0 -91
- package/dist/commands/instance/destroy.d.ts +0 -9
- package/dist/commands/instance/destroy.js +0 -46
- package/dist/commands/instance/generate-dev-token.d.ts +0 -10
- package/dist/commands/instance/generate-dev-token.js +0 -54
- package/dist/commands/instance/schema.js +0 -20
- package/dist/commands/instance/set.d.ts +0 -9
- package/dist/commands/instance/set.js +0 -31
- package/dist/commands/instance/status.d.ts +0 -12
- package/dist/commands/instance/status.js +0 -50
- package/dist/commands/instance/stop.d.ts +0 -9
- package/dist/commands/instance/stop.js +0 -42
- package/dist/commands/instance/sync-rules/deploy.d.ts +0 -10
- package/dist/commands/instance/sync-rules/deploy.js +0 -49
- package/dist/commands/instance/sync-rules/generate-schema.d.ts +0 -10
- package/dist/commands/instance/sync-rules/generate-schema.js +0 -56
- package/dist/commands/instance/sync-rules/validate.d.ts +0 -9
- package/dist/commands/instance/sync-rules/validate.js +0 -29
- package/dist/commands/instances.js +0 -29
- package/dist/commands.utils.d.ts +0 -2
- package/dist/commands.utils.js +0 -92
- package/dist/constants.d.ts +0 -8
- package/dist/constants.js +0 -10
- package/dist/instance-command-flags.d.ts +0 -12
- package/dist/instance-command-flags.js +0 -13
- package/dist/service/accounts.client.js +0 -11307
- package/dist/service/powersync.client.js +0 -75510
- package/dist/service/types.js +0 -1
- package/dist/service/utils.js +0 -153
- package/dist/utils.d.ts +0 -14
- package/dist/utils.js +0 -68
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { Flags, ux } from '@oclif/core';
|
|
2
|
+
import { CloudInstanceCommand, SERVICE_FILENAME } from '@powersync/cli-core';
|
|
3
|
+
import { routes } from '@powersync/management-types';
|
|
4
|
+
import { ObjectId } from 'bson';
|
|
5
|
+
import ora from 'ora';
|
|
6
|
+
import { formatTestConnectionFailure, testCloudConnections } from '../../api/cloud/test-connection.js';
|
|
7
|
+
import { DEFAULT_DEPLOY_TIMEOUT_MS, waitForOperationStatusChange } from '../../api/cloud/wait-for-operation.js';
|
|
8
|
+
/**
|
|
9
|
+
* Deploys the sync config and service configuration
|
|
10
|
+
*/
|
|
11
|
+
export default class DeployAll extends CloudInstanceCommand {
|
|
12
|
+
static description = [
|
|
13
|
+
'Deploy local config (service.yaml, sync config) to the linked PowerSync Cloud instance.',
|
|
14
|
+
'Validates connections and sync config before deploying.',
|
|
15
|
+
`See also ${ux.colorize('blue', 'powersync deploy sync-config')} to deploy only sync config changes.`,
|
|
16
|
+
`See also ${ux.colorize('blue', 'powersync deploy service-config')} to deploy only service config changes.`
|
|
17
|
+
].join('\n');
|
|
18
|
+
static examples = [
|
|
19
|
+
'<%= config.bin %> <%= command.id %>',
|
|
20
|
+
'<%= config.bin %> <%= command.id %> --instance-id=<id> --project-id=<id>'
|
|
21
|
+
];
|
|
22
|
+
static flags = {
|
|
23
|
+
'deploy-timeout': Flags.integer({
|
|
24
|
+
default: DEFAULT_DEPLOY_TIMEOUT_MS / 1000,
|
|
25
|
+
description: 'Seconds to wait after scheduling a deploy before timing out while polling status (default 300 seconds).',
|
|
26
|
+
async parse(input) {
|
|
27
|
+
const value = Number(input);
|
|
28
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
29
|
+
throw new Error('deploy-timeout must be a positive number of seconds');
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
34
|
+
...CloudInstanceCommand.flags
|
|
35
|
+
};
|
|
36
|
+
static summary = '[Cloud only] Deploy local config to the linked Cloud instance (connections + auth + sync config).';
|
|
37
|
+
async deployAll(params) {
|
|
38
|
+
const { cloudConfigState, deployTimeoutMs, updateSyncConfig } = params;
|
|
39
|
+
return this.withDeploy(deployTimeoutMs, async () => this.deployInstanceConfig({
|
|
40
|
+
cloudConfigState,
|
|
41
|
+
updateSyncConfig
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
async deployInstanceConfig(params) {
|
|
45
|
+
const { cloudConfigState, updateSyncConfig } = params;
|
|
46
|
+
const { client, project } = this;
|
|
47
|
+
const { linked, syncRulesContent } = project;
|
|
48
|
+
const config = this.serviceConfig;
|
|
49
|
+
if (!config) {
|
|
50
|
+
this.styledError({
|
|
51
|
+
message: `Service config not loaded. Ensure ${SERVICE_FILENAME} is present and valid.`
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return client.deployInstance(routes.DeployInstanceRequest.encode({
|
|
55
|
+
// Spread the existing config like name, and program version contraints.
|
|
56
|
+
// Should we allow specifying these in the config file?
|
|
57
|
+
...cloudConfigState,
|
|
58
|
+
app_id: new ObjectId(linked.project_id),
|
|
59
|
+
// The encoding will ensure the correct typing
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
config: config,
|
|
62
|
+
// Allow updating the instance name
|
|
63
|
+
name: config.name,
|
|
64
|
+
org_id: new ObjectId(linked.org_id),
|
|
65
|
+
sync_rules: updateSyncConfig ? syncRulesContent : cloudConfigState.sync_rules
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
async loadCloudConfigState() {
|
|
69
|
+
const { client, project } = this;
|
|
70
|
+
const { linked } = project;
|
|
71
|
+
return client
|
|
72
|
+
.getInstanceConfig({
|
|
73
|
+
app_id: linked.project_id,
|
|
74
|
+
id: linked.instance_id,
|
|
75
|
+
org_id: linked.org_id
|
|
76
|
+
})
|
|
77
|
+
.catch((error) => {
|
|
78
|
+
this.styledError({
|
|
79
|
+
error,
|
|
80
|
+
message: `Failed to get existing config for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}. Ensure the instance has been created before deploying.`
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
parseLocalConfig(projectDirectory) {
|
|
85
|
+
const config = super.parseLocalConfig(projectDirectory);
|
|
86
|
+
/**
|
|
87
|
+
* This is a temporary hack to maintain compatibilty with the PowerSync Dashboard.
|
|
88
|
+
* The dashboard requires replication connections to have a name field set.
|
|
89
|
+
* This is not enforced by the management service.
|
|
90
|
+
*/
|
|
91
|
+
for (const [index, connection] of config.replication?.connections?.entries() ?? []) {
|
|
92
|
+
if (!connection.name) {
|
|
93
|
+
connection.name = `Default${index > 0 ? `_${index}` : ''}`;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return config;
|
|
97
|
+
}
|
|
98
|
+
async provision(params) {
|
|
99
|
+
const { cloudConfigState, deployTimeoutMs, indentationLevel } = params;
|
|
100
|
+
const indentation = '\t'.repeat(Math.max(0, indentationLevel));
|
|
101
|
+
const spinner = ora({
|
|
102
|
+
discardStdin: false,
|
|
103
|
+
prefixText: `\n${indentation}Initial provision: deploying without sync config changes in order to validate sync config before final deploy...\n`,
|
|
104
|
+
spinner: 'moon',
|
|
105
|
+
suffixText: `\n${indentation}This may take a few minutes.\n`
|
|
106
|
+
});
|
|
107
|
+
spinner.start();
|
|
108
|
+
try {
|
|
109
|
+
const deployResult = await this.deployInstanceConfig({
|
|
110
|
+
cloudConfigState,
|
|
111
|
+
updateSyncConfig: false
|
|
112
|
+
});
|
|
113
|
+
await this.waitForDeployCompletion({
|
|
114
|
+
deployResult,
|
|
115
|
+
indentation,
|
|
116
|
+
logCompletionMessage: false,
|
|
117
|
+
logScheduledMessage: false,
|
|
118
|
+
timeoutMs: deployTimeoutMs
|
|
119
|
+
});
|
|
120
|
+
spinner.stop();
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
spinner.stop();
|
|
124
|
+
const { linked } = this.project;
|
|
125
|
+
this.styledError({
|
|
126
|
+
error,
|
|
127
|
+
message: `Failed to deploy changes to instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}`,
|
|
128
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async run() {
|
|
133
|
+
const { flags } = await this.parse(DeployAll);
|
|
134
|
+
const project = await this.loadProject(flags, {
|
|
135
|
+
// local config state is required when deploying all
|
|
136
|
+
configFileRequired: true
|
|
137
|
+
});
|
|
138
|
+
const deployTimeoutMs = (flags['deploy-timeout'] ?? DEFAULT_DEPLOY_TIMEOUT_MS / 1000) * 1000;
|
|
139
|
+
// Parse and store for later
|
|
140
|
+
this.parseLocalConfig(project.projectDirectory);
|
|
141
|
+
// The existing config is required to deploy changes. The instance should have been created already.
|
|
142
|
+
const cloudConfigState = await this.loadCloudConfigState();
|
|
143
|
+
// Start of validations
|
|
144
|
+
this.log('Performing validations before deploy...');
|
|
145
|
+
const instanceStatus = await this.client
|
|
146
|
+
.getInstanceStatus({
|
|
147
|
+
app_id: project.linked.project_id,
|
|
148
|
+
id: project.linked.instance_id,
|
|
149
|
+
org_id: project.linked.org_id
|
|
150
|
+
})
|
|
151
|
+
.catch((error) => {
|
|
152
|
+
this.styledError({
|
|
153
|
+
error,
|
|
154
|
+
message: `Failed to get status for instance ${project.linked.instance_id} in project ${project.linked.project_id} in org ${project.linked.org_id}.`,
|
|
155
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
const requiresReprovision = instanceStatus.provisioned === false;
|
|
159
|
+
const syncConfigHasChanges = project.syncRulesContent !== cloudConfigState.sync_rules;
|
|
160
|
+
await this.validateServiceConfig({ cloudConfigState });
|
|
161
|
+
await this.testConnections();
|
|
162
|
+
this.log('\tValidating sync config...');
|
|
163
|
+
/**
|
|
164
|
+
* At this point we know the instances is deprovisioned, and the current config is valid.
|
|
165
|
+
* We can't verify the sync config yet - since that requires a provisioned instance.
|
|
166
|
+
* We will attempt to deploy the config without the sync config first,
|
|
167
|
+
* if that succeeds, then we will validate the sync config and deploy again with the sync config.
|
|
168
|
+
*/
|
|
169
|
+
if (requiresReprovision) {
|
|
170
|
+
this.log([
|
|
171
|
+
`The instance is ${ux.colorize('yellow', 'not currently provisioned')} and we have detected changes to the sync config.`,
|
|
172
|
+
`To ensure the instance is successfully provisioned with a valid config, we will first deploy without the sync config, then validate and deploy again with the sync config.`
|
|
173
|
+
].join('\n'));
|
|
174
|
+
await this.provision({
|
|
175
|
+
cloudConfigState,
|
|
176
|
+
deployTimeoutMs,
|
|
177
|
+
indentationLevel: 1
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
await this.validateSyncConfig();
|
|
181
|
+
this.log('Validations completed successfully.\n');
|
|
182
|
+
await this.deployAll({ cloudConfigState, deployTimeoutMs, updateSyncConfig: syncConfigHasChanges });
|
|
183
|
+
}
|
|
184
|
+
async testConnections() {
|
|
185
|
+
const { client, project } = this;
|
|
186
|
+
const { linked } = project;
|
|
187
|
+
const { serviceConfig } = this;
|
|
188
|
+
if (!serviceConfig) {
|
|
189
|
+
this.styledError({
|
|
190
|
+
message: `Service config not loaded. Ensure ${SERVICE_FILENAME} is present and valid.`
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
this.log('\tTesting connections...');
|
|
194
|
+
if ((serviceConfig.replication?.connections?.length ?? 0) <= 0) {
|
|
195
|
+
this.styledError({
|
|
196
|
+
message: 'No connection found in config.',
|
|
197
|
+
suggestions: ['Add a connection to the config in replication->connections before deploying.']
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const connectionResults = await testCloudConnections(client, linked, serviceConfig.replication?.connections ?? []).catch((error) => {
|
|
201
|
+
this.styledError({
|
|
202
|
+
error,
|
|
203
|
+
message: 'Failed to test connections',
|
|
204
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
for (const { connectionName, response } of connectionResults) {
|
|
208
|
+
if (response.success !== true) {
|
|
209
|
+
this.styledError({ message: formatTestConnectionFailure(response, connectionName) });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async validateServiceConfig(params) {
|
|
214
|
+
const { cloudConfigState } = params;
|
|
215
|
+
const { linked } = this.project;
|
|
216
|
+
const { serviceConfig } = this;
|
|
217
|
+
if (!serviceConfig) {
|
|
218
|
+
this.styledError({
|
|
219
|
+
message: `Service config not loaded. Ensure ${SERVICE_FILENAME} is present and valid.`
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
this.log('\tValidating configuration...');
|
|
223
|
+
const existingRegion = cloudConfigState.config?.region;
|
|
224
|
+
if (existingRegion && existingRegion !== serviceConfig.region) {
|
|
225
|
+
this.styledError({
|
|
226
|
+
message: `The existing config for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id} has a different region than the config being deployed. Existing region: ${existingRegion}. New region: ${serviceConfig.region}. The region cannot be changed after the initial deployment.`,
|
|
227
|
+
suggestions: ['Check your config and try again.']
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
// Validate region against list of regions obtained from client.listRegions()
|
|
231
|
+
const regions = await this.client.listRegions().catch((error) => {
|
|
232
|
+
this.styledError({
|
|
233
|
+
error,
|
|
234
|
+
message: 'Could not validate region against list of regions: Failed to list regions',
|
|
235
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
const foundRegion = regions.regions.find((region) => region.name === serviceConfig.region);
|
|
239
|
+
if (!foundRegion) {
|
|
240
|
+
this.styledError({
|
|
241
|
+
message: `The region ${serviceConfig.region} is not supported. Please choose a region from the list of supported regions: ${regions.regions.map((region) => region.name).join(', ')}.`
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async validateSyncConfig() {
|
|
246
|
+
const { client, project } = this;
|
|
247
|
+
// It might take a while for the instance to be fully provisioned after the deploy, so we retry the validation until it succeeds or we hit the timeout
|
|
248
|
+
for (let retry = 0; retry < 100; retry++) {
|
|
249
|
+
const validation = await client
|
|
250
|
+
.validateSyncRules({
|
|
251
|
+
app_id: project.linked.project_id,
|
|
252
|
+
id: project.linked.instance_id,
|
|
253
|
+
org_id: project.linked.org_id,
|
|
254
|
+
sync_rules: project.syncRulesContent ?? ''
|
|
255
|
+
})
|
|
256
|
+
.catch((error) => {
|
|
257
|
+
if (retry === 99) {
|
|
258
|
+
this.styledError({
|
|
259
|
+
error,
|
|
260
|
+
message: `Failed to validate sync config for instance ${project.linked.instance_id} in project ${project.linked.project_id} in org ${project.linked.org_id}. Ensure the sync config is valid before deploying.`,
|
|
261
|
+
suggestions: ['Check your sync config and try again.']
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
// signal a retry
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
if (!validation) {
|
|
270
|
+
await new Promise((resolve) => {
|
|
271
|
+
setTimeout(resolve, 1000);
|
|
272
|
+
});
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (validation.errors.length > 0) {
|
|
276
|
+
this.styledError({
|
|
277
|
+
message: `Sync config validation failed for instance. Validation errors:\n${validation.errors.map((error) => error.message).join('\n')}`,
|
|
278
|
+
suggestions: ['Check your sync config and try again.']
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
// Validation succeeded with no errors
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async waitForDeployCompletion(params) {
|
|
286
|
+
const { deployResult, indentation = '', logCompletionMessage = true, logScheduledMessage = true, timeoutMs } = params;
|
|
287
|
+
const { client, project } = this;
|
|
288
|
+
if (logScheduledMessage) {
|
|
289
|
+
this.log(`${indentation}Deploy operation has been scheduled. Waiting for completion...`);
|
|
290
|
+
}
|
|
291
|
+
const status = await waitForOperationStatusChange({
|
|
292
|
+
client,
|
|
293
|
+
instanceId: deployResult.id,
|
|
294
|
+
linked: project.linked,
|
|
295
|
+
operationId: deployResult.operation_id,
|
|
296
|
+
timeoutMs
|
|
297
|
+
});
|
|
298
|
+
if (status === 'completed') {
|
|
299
|
+
if (!logCompletionMessage) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
this.log(ux.colorize('green', 'Deployment operation completed successfully!'));
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
this.styledError({
|
|
306
|
+
message: `Deploy failed. Check instance diagnostics for details, for example: ${ux.colorize('blue', 'powersync status')}`
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async withDeploy(timeoutMs, fn) {
|
|
311
|
+
const { project } = this;
|
|
312
|
+
const spinner = ora({
|
|
313
|
+
discardStdin: false,
|
|
314
|
+
prefixText: '\nDeploying instance...\n',
|
|
315
|
+
spinner: 'moon',
|
|
316
|
+
suffixText: '\nThis may take a few minutes.\n'
|
|
317
|
+
});
|
|
318
|
+
spinner.start();
|
|
319
|
+
try {
|
|
320
|
+
const deployResult = await fn();
|
|
321
|
+
await this.waitForDeployCompletion({ deployResult, timeoutMs });
|
|
322
|
+
spinner.stop();
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
spinner.stop();
|
|
326
|
+
this.styledError({
|
|
327
|
+
error,
|
|
328
|
+
message: `Failed to deploy changes to instance ${project.linked.instance_id} in project ${project.linked.project_id} in org ${project.linked.org_id}`,
|
|
329
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import DeployAll from './index.js';
|
|
2
|
+
export default class DeployServiceConfig extends DeployAll {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
static flags: {
|
|
6
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
'deploy-timeout': import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
};
|
|
12
|
+
static summary: string;
|
|
13
|
+
run(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DEFAULT_DEPLOY_TIMEOUT_MS } from '../../api/cloud/wait-for-operation.js';
|
|
2
|
+
import DeployAll from './index.js';
|
|
3
|
+
export default class DeployServiceConfig extends DeployAll {
|
|
4
|
+
static description = 'Deploy only service config changes (without sync config updates).';
|
|
5
|
+
static examples = [
|
|
6
|
+
'<%= config.bin %> <%= command.id %>',
|
|
7
|
+
'<%= config.bin %> <%= command.id %> --instance-id=<id> --project-id=<id>'
|
|
8
|
+
];
|
|
9
|
+
static flags = {
|
|
10
|
+
...DeployAll.flags
|
|
11
|
+
};
|
|
12
|
+
static summary = '[Cloud only] Deploy only local service config to the linked Cloud instance.';
|
|
13
|
+
async run() {
|
|
14
|
+
const { flags } = await this.parse(DeployServiceConfig);
|
|
15
|
+
const project = await this.loadProject(flags, {
|
|
16
|
+
// local service config is required for this command
|
|
17
|
+
configFileRequired: true
|
|
18
|
+
});
|
|
19
|
+
const deployTimeoutMs = (flags['deploy-timeout'] ?? DEFAULT_DEPLOY_TIMEOUT_MS / 1000) * 1000;
|
|
20
|
+
// Parse and store for later
|
|
21
|
+
this.parseLocalConfig(project.projectDirectory);
|
|
22
|
+
// The existing config is required to deploy changes. The instance should have been created already.
|
|
23
|
+
const cloudConfigState = await this.loadCloudConfigState();
|
|
24
|
+
this.log('Performing validations before deploy...');
|
|
25
|
+
await this.validateServiceConfig({ cloudConfigState });
|
|
26
|
+
await this.testConnections();
|
|
27
|
+
this.log('Validations completed successfully.\n');
|
|
28
|
+
await this.deployAll({ cloudConfigState, deployTimeoutMs, updateSyncConfig: false });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { routes } from '@powersync/management-types';
|
|
2
|
+
import DeployAll from './index.js';
|
|
3
|
+
export default class DeploySyncConfig extends DeployAll {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
'sync-config-file-path': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
'deploy-timeout': import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
};
|
|
14
|
+
static summary: string;
|
|
15
|
+
/**
|
|
16
|
+
* Deploys only the sync config.
|
|
17
|
+
* Uses existing cloud config state for other fields.
|
|
18
|
+
*/
|
|
19
|
+
protected deploySyncConfig(params: {
|
|
20
|
+
cloudConfigState: routes.InstanceConfigResponse;
|
|
21
|
+
timeout: number;
|
|
22
|
+
}): Promise<void>;
|
|
23
|
+
run(): Promise<void>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { ux } from '@oclif/core/ux';
|
|
3
|
+
import { routes } from '@powersync/management-types';
|
|
4
|
+
import { ObjectId } from 'bson';
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { DEFAULT_DEPLOY_TIMEOUT_MS } from '../../api/cloud/wait-for-operation.js';
|
|
7
|
+
import DeployAll from './index.js';
|
|
8
|
+
export default class DeploySyncConfig extends DeployAll {
|
|
9
|
+
static description = 'Deploy only sync config changes.';
|
|
10
|
+
static examples = [
|
|
11
|
+
'<%= config.bin %> <%= command.id %>',
|
|
12
|
+
'<%= config.bin %> <%= command.id %> --instance-id=<id> --project-id=<id>'
|
|
13
|
+
];
|
|
14
|
+
static flags = {
|
|
15
|
+
...DeployAll.flags,
|
|
16
|
+
'sync-config-file-path': Flags.file({
|
|
17
|
+
description: 'Path to a sync config file. If provided, this file will be validated and deployed instead of the default sync-config.yaml.',
|
|
18
|
+
exists: true
|
|
19
|
+
})
|
|
20
|
+
};
|
|
21
|
+
static summary = '[Cloud only] Deploy only local sync config to the linked Cloud instance.';
|
|
22
|
+
/**
|
|
23
|
+
* Deploys only the sync config.
|
|
24
|
+
* Uses existing cloud config state for other fields.
|
|
25
|
+
*/
|
|
26
|
+
async deploySyncConfig(params) {
|
|
27
|
+
const { cloudConfigState, timeout } = params;
|
|
28
|
+
const { project } = this;
|
|
29
|
+
const { linked } = project;
|
|
30
|
+
const { syncRulesContent } = project;
|
|
31
|
+
if (!syncRulesContent) {
|
|
32
|
+
this.styledError({
|
|
33
|
+
message: `Sync config content not loaded. Ensure sync config is present and valid.`
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (!cloudConfigState.config) {
|
|
37
|
+
this.styledError({
|
|
38
|
+
message: `No existing cloud config found for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}. A config must be deployed before deploying sync config changes.`,
|
|
39
|
+
suggestions: [`Run ${ux.colorize('blue', 'powersync deploy')} to deploy the initial config.`]
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return this.withDeploy(timeout, async () => this.client.deployInstance(routes.DeployInstanceRequest.encode({
|
|
43
|
+
...cloudConfigState,
|
|
44
|
+
app_id: new ObjectId(linked.project_id),
|
|
45
|
+
config: cloudConfigState.config,
|
|
46
|
+
id: linked.instance_id,
|
|
47
|
+
org_id: new ObjectId(linked.org_id),
|
|
48
|
+
sync_rules: syncRulesContent
|
|
49
|
+
})));
|
|
50
|
+
}
|
|
51
|
+
async run() {
|
|
52
|
+
const { flags } = await this.parse(DeploySyncConfig);
|
|
53
|
+
const project = await this.loadProject(flags, {
|
|
54
|
+
// We don't need the config to be managed locally for this
|
|
55
|
+
configFileRequired: false
|
|
56
|
+
});
|
|
57
|
+
const { linked } = project;
|
|
58
|
+
const deployTimeoutMs = (flags['deploy-timeout'] ?? DEFAULT_DEPLOY_TIMEOUT_MS / 1000) * 1000;
|
|
59
|
+
const syncConfigFilePath = flags['sync-config-file-path'];
|
|
60
|
+
if (syncConfigFilePath) {
|
|
61
|
+
project.syncRulesContent = readFileSync(syncConfigFilePath, 'utf8');
|
|
62
|
+
}
|
|
63
|
+
// The existing config is required to deploy changes. The instance should have been created already.
|
|
64
|
+
const cloudConfigState = await this.loadCloudConfigState();
|
|
65
|
+
if (!cloudConfigState.config) {
|
|
66
|
+
this.styledError({
|
|
67
|
+
message: `No existing cloud config found for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}. A config must be deployed before deploying sync config changes.`,
|
|
68
|
+
suggestions: [`Run ${ux.colorize('blue', 'powersync deploy')} to deploy the initial config.`]
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// We use the cloud config as the "local config for this"
|
|
72
|
+
this.serviceConfig = {
|
|
73
|
+
_type: linked.type,
|
|
74
|
+
name: cloudConfigState.name,
|
|
75
|
+
...cloudConfigState.config,
|
|
76
|
+
...linked
|
|
77
|
+
};
|
|
78
|
+
this.log('Performing validations before deploy...');
|
|
79
|
+
const instanceStatus = await this.client
|
|
80
|
+
.getInstanceStatus({
|
|
81
|
+
app_id: project.linked.project_id,
|
|
82
|
+
id: project.linked.instance_id,
|
|
83
|
+
org_id: project.linked.org_id
|
|
84
|
+
})
|
|
85
|
+
.catch((error) => {
|
|
86
|
+
this.styledError({
|
|
87
|
+
error,
|
|
88
|
+
message: `Failed to get status for instance ${project.linked.instance_id} in project ${project.linked.project_id} in org ${project.linked.org_id}.`,
|
|
89
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
if (!instanceStatus.provisioned) {
|
|
93
|
+
this.log(`\nThe instance is not currently provisioned. Triggering a deploy in order to reprovision. This may take a few minutes.\n`);
|
|
94
|
+
// Don't yet update the sync config since the instance is not provisioned, but deploy to trigger provisioning
|
|
95
|
+
await this.deployAll({ cloudConfigState, deployTimeoutMs, updateSyncConfig: false });
|
|
96
|
+
}
|
|
97
|
+
// Validate sync config
|
|
98
|
+
this.log('\tValidating sync config...');
|
|
99
|
+
await this.validateSyncConfig();
|
|
100
|
+
this.log('Validations completed successfully.\n');
|
|
101
|
+
await this.deploySyncConfig({ cloudConfigState, timeout: deployTimeoutMs });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CloudInstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
export default class Destroy extends CloudInstanceCommand {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
static flags: {
|
|
6
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
confirm: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
};
|
|
12
|
+
static summary: string;
|
|
13
|
+
run(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Flags, ux } from '@oclif/core';
|
|
2
|
+
import { CloudInstanceCommand } from '@powersync/cli-core';
|
|
3
|
+
import ora from 'ora';
|
|
4
|
+
import { waitForOperationStatusChange } from '../api/cloud/wait-for-operation.js';
|
|
5
|
+
export default class Destroy extends CloudInstanceCommand {
|
|
6
|
+
static description = 'Permanently delete the linked PowerSync Cloud instance and its data. Requires --confirm=yes.';
|
|
7
|
+
static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --confirm=yes'];
|
|
8
|
+
static flags = {
|
|
9
|
+
confirm: Flags.string({
|
|
10
|
+
description: 'Set to "yes" to confirm destruction of the instance.',
|
|
11
|
+
options: ['yes']
|
|
12
|
+
}),
|
|
13
|
+
...CloudInstanceCommand.flags
|
|
14
|
+
};
|
|
15
|
+
static summary = '[Cloud only] Permanently destroy the linked Cloud instance.';
|
|
16
|
+
async run() {
|
|
17
|
+
const { flags } = await this.parse(Destroy);
|
|
18
|
+
if (flags.confirm !== 'yes') {
|
|
19
|
+
this.styledError({ message: 'Destruction requires confirmation. Run with --confirm=yes to confirm.' });
|
|
20
|
+
}
|
|
21
|
+
const { linked } = await this.loadProject(flags);
|
|
22
|
+
const { client } = this;
|
|
23
|
+
const spinner = ora({
|
|
24
|
+
discardStdin: false,
|
|
25
|
+
prefixText: `\n${ux.colorize('red', 'Destroying')} instance ${ux.colorize('blue', linked.instance_id)} in project ${ux.colorize('blue', linked.project_id)} in org ${ux.colorize('blue', linked.org_id)}\n`,
|
|
26
|
+
spinner: 'moon',
|
|
27
|
+
suffixText: '\nThis may take a few minutes.\n'
|
|
28
|
+
});
|
|
29
|
+
spinner.start();
|
|
30
|
+
try {
|
|
31
|
+
const deactivateResult = await client.destroyInstance({
|
|
32
|
+
app_id: linked.project_id,
|
|
33
|
+
id: linked.instance_id,
|
|
34
|
+
org_id: linked.org_id
|
|
35
|
+
});
|
|
36
|
+
const status = await waitForOperationStatusChange({
|
|
37
|
+
client,
|
|
38
|
+
instanceId: linked.instance_id,
|
|
39
|
+
linked,
|
|
40
|
+
operationId: deactivateResult.operation_id,
|
|
41
|
+
timeoutMs: 10 * 60 * 1000
|
|
42
|
+
});
|
|
43
|
+
spinner.stop();
|
|
44
|
+
if (status === 'completed') {
|
|
45
|
+
this.log(ux.colorize('green', 'Instance destroyed successfully.'));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.styledError({
|
|
49
|
+
message: `Operation failed. Check instance diagnostics for details, for example: ${ux.colorize('blue', 'powersync status')}`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
spinner.stop();
|
|
55
|
+
this.styledError({
|
|
56
|
+
error,
|
|
57
|
+
message: `Failed to destroy instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}`,
|
|
58
|
+
suggestions: ['Check your network connection and try again.', 'If the problem persists, contact support.']
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CloudInstanceCommand } from '@powersync/cli-core';
|
|
2
|
+
export default class FetchConfig extends CloudInstanceCommand {
|
|
3
|
+
static description: string;
|
|
4
|
+
static examples: string[];
|
|
5
|
+
static flags: {
|
|
6
|
+
'instance-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
7
|
+
'org-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
directory: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
};
|
|
12
|
+
static summary: string;
|
|
13
|
+
run(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { CloudInstanceCommand } from '@powersync/cli-core';
|
|
3
|
+
import { Document } from 'yaml';
|
|
4
|
+
import { fetchCloudConfig } from '../../api/cloud/fetch-cloud-config.js';
|
|
5
|
+
export default class FetchConfig extends CloudInstanceCommand {
|
|
6
|
+
static description = 'Retrieve the current instance config from PowerSync Cloud and print as YAML or JSON.';
|
|
7
|
+
static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --output=json'];
|
|
8
|
+
static flags = {
|
|
9
|
+
output: Flags.string({
|
|
10
|
+
default: 'yaml',
|
|
11
|
+
description: 'Output format: yaml or json.',
|
|
12
|
+
options: ['json', 'yaml']
|
|
13
|
+
}),
|
|
14
|
+
...CloudInstanceCommand.flags
|
|
15
|
+
};
|
|
16
|
+
static summary = '[Cloud only] Print linked Cloud instance config (YAML or JSON).';
|
|
17
|
+
async run() {
|
|
18
|
+
const { flags } = await this.parse(FetchConfig);
|
|
19
|
+
const { linked } = await this.loadProject(flags);
|
|
20
|
+
const { client } = this;
|
|
21
|
+
const fetched = await fetchCloudConfig(client, linked).catch((error) => {
|
|
22
|
+
this.styledError({
|
|
23
|
+
error,
|
|
24
|
+
message: `Failed to fetch config for instance ${linked.instance_id} in project ${linked.project_id} in org ${linked.org_id}`
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
if (flags.output === 'yaml') {
|
|
28
|
+
this.log(new Document(fetched).toString());
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.log(JSON.stringify(fetched, null, 2));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
/**
|
|
3
|
+
* This cloud only implementation is exported as index.ts
|
|
4
|
+
* in order to allow plugin commands to extend the command name.
|
|
5
|
+
* For example a plugin could add a `fetch instances self-hosted` command.
|
|
6
|
+
*/
|
|
7
|
+
export default class Fetch extends Command {
|
|
8
|
+
static description: string;
|
|
9
|
+
static examples: string[];
|
|
10
|
+
static hidden: boolean;
|
|
11
|
+
static summary: string;
|
|
12
|
+
run(): Promise<void>;
|
|
13
|
+
}
|