servyx-cli 0.1.8 → 0.1.10
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/README.md +1 -1
- package/package.json +1 -1
- package/src/commands/deploy.js +18 -58
- package/src/lib/api.js +0 -7
- package/src/lib/deploy-stream.js +0 -99
package/README.md
CHANGED
|
@@ -72,4 +72,4 @@ Environment variables:
|
|
|
72
72
|
3. Live dotfiles from `public` are copied into staging so they are not removed on deploy (`.env` is skipped when using `--with-env`)
|
|
73
73
|
4. cli-service calls server-service to queue a CLI deployment (`with_env` tells the agent to sync uploaded env files)
|
|
74
74
|
5. The agent syncs staging → public, preserving `.htaccess` and (by default) `.env` on the server
|
|
75
|
-
6. The CLI
|
|
75
|
+
6. The CLI polls until the deployment succeeds or fails
|
package/package.json
CHANGED
package/src/commands/deploy.js
CHANGED
|
@@ -1,27 +1,15 @@
|
|
|
1
1
|
import pc from 'picocolors';
|
|
2
2
|
import { getDeployTarget, getDeployment, startDeployment } from '../lib/api.js';
|
|
3
3
|
import { getToken } from '../lib/config.js';
|
|
4
|
-
import { watchDeploymentStream } from '../lib/deploy-stream.js';
|
|
5
4
|
import { loadProjectConfig, loadServyxIgnore } from '../lib/project.js';
|
|
6
5
|
import { rsyncDeploy, sshPreflight, DEFAULT_RSYNC_EXCLUDES, preserveServerDotfiles, ensureRemoteDir, verifyRemoteStagingDir } from '../lib/rsync.js';
|
|
7
6
|
|
|
8
7
|
const TERMINAL_STATUSES = new Set(['success', 'failure', 'cancelled']);
|
|
9
|
-
const FALLBACK_POLL_MS = 15000;
|
|
10
8
|
|
|
11
9
|
function sleep(ms) {
|
|
12
10
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
function deploymentSnapshot(deployment) {
|
|
16
|
-
return [
|
|
17
|
-
deployment.status,
|
|
18
|
-
deployment.phase,
|
|
19
|
-
deployment.percent,
|
|
20
|
-
deployment.message,
|
|
21
|
-
deployment.error_message,
|
|
22
|
-
].join('|');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
13
|
function formatDeploymentLine(deployment) {
|
|
26
14
|
const pct = deployment.percent != null ? `${deployment.percent}%` : '';
|
|
27
15
|
const phase = deployment.phase || deployment.status || 'unknown';
|
|
@@ -161,57 +149,29 @@ export async function runDeploy(options = {}) {
|
|
|
161
149
|
|
|
162
150
|
console.log(pc.dim(`Deployment ID: ${deploymentId}`));
|
|
163
151
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (snapshot !== lastSnapshot) {
|
|
171
|
-
console.log(formatDeploymentLine(current));
|
|
172
|
-
lastSnapshot = snapshot;
|
|
152
|
+
while (true) {
|
|
153
|
+
const statusResult = await getDeployment(deploymentId);
|
|
154
|
+
const current = statusResult?.data?.deployment;
|
|
155
|
+
if (!current) {
|
|
156
|
+
console.error(pc.red('Deployment not found while polling.'));
|
|
157
|
+
process.exit(1);
|
|
173
158
|
}
|
|
174
|
-
};
|
|
175
159
|
|
|
176
|
-
|
|
177
|
-
await watchDeploymentStream(deploymentId, {
|
|
178
|
-
onUpdate: (next) => handleDeploymentUpdate(next),
|
|
179
|
-
});
|
|
180
|
-
} catch (streamError) {
|
|
181
|
-
if (streamError.code !== 'MQTT_UNAVAILABLE' && streamError.status !== 404) {
|
|
182
|
-
console.log(pc.yellow(`Live deploy stream unavailable (${streamError.message}). Falling back to slow status checks...`));
|
|
183
|
-
} else if (streamError.code === 'MQTT_UNAVAILABLE') {
|
|
184
|
-
console.log(pc.yellow('MQTT deploy stream unavailable. Falling back to slow status checks...'));
|
|
185
|
-
} else {
|
|
186
|
-
throw streamError;
|
|
187
|
-
}
|
|
160
|
+
console.log(formatDeploymentLine(current));
|
|
188
161
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
console.error(pc.red('Deployment not found while checking status.'));
|
|
194
|
-
process.exit(1);
|
|
162
|
+
if (TERMINAL_STATUSES.has(current.status)) {
|
|
163
|
+
if (current.status === 'success') {
|
|
164
|
+
console.log(pc.green('Deployment succeeded.'));
|
|
165
|
+
process.exit(0);
|
|
195
166
|
}
|
|
196
|
-
handleDeploymentUpdate(current);
|
|
197
|
-
if (TERMINAL_STATUSES.has(current.status)) break;
|
|
198
|
-
await sleep(FALLBACK_POLL_MS);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
167
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
console.log(pc.green('Deployment succeeded.'));
|
|
209
|
-
process.exit(0);
|
|
210
|
-
}
|
|
168
|
+
console.error(pc.red(`Deployment ${current.status}.`));
|
|
169
|
+
if (current.error_message) {
|
|
170
|
+
console.error(pc.red(current.error_message));
|
|
171
|
+
}
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
211
174
|
|
|
212
|
-
|
|
213
|
-
if (current.error_message) {
|
|
214
|
-
console.error(pc.red(current.error_message));
|
|
175
|
+
await sleep(2000);
|
|
215
176
|
}
|
|
216
|
-
process.exit(1);
|
|
217
177
|
}
|
package/src/lib/api.js
CHANGED
|
@@ -34,13 +34,6 @@ function unwrap(response) {
|
|
|
34
34
|
err.status = response.status;
|
|
35
35
|
err.code = response.data?.code;
|
|
36
36
|
err.data = response.data;
|
|
37
|
-
const retryAfter = response.headers?.['retry-after'];
|
|
38
|
-
if (retryAfter) {
|
|
39
|
-
const seconds = Number.parseInt(retryAfter, 10);
|
|
40
|
-
if (!Number.isNaN(seconds)) {
|
|
41
|
-
err.retryAfterSeconds = seconds;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
37
|
throw err;
|
|
45
38
|
}
|
|
46
39
|
|
package/src/lib/deploy-stream.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { getApiBaseUrl, getToken } from './config.js';
|
|
2
|
-
|
|
3
|
-
const TERMINAL_STATUSES = new Set(['success', 'failure', 'cancelled']);
|
|
4
|
-
|
|
5
|
-
function parseSseChunk(buffer, onEvent) {
|
|
6
|
-
const blocks = buffer.split('\n\n');
|
|
7
|
-
const remainder = blocks.pop() || '';
|
|
8
|
-
|
|
9
|
-
for (const block of blocks) {
|
|
10
|
-
if (!block.trim()) continue;
|
|
11
|
-
|
|
12
|
-
let event = 'message';
|
|
13
|
-
let data = '';
|
|
14
|
-
|
|
15
|
-
for (const line of block.split('\n')) {
|
|
16
|
-
if (line.startsWith('event:')) {
|
|
17
|
-
event = line.slice(6).trim();
|
|
18
|
-
} else if (line.startsWith('data:')) {
|
|
19
|
-
data += line.slice(5).trim();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (!data) continue;
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
onEvent(event, JSON.parse(data));
|
|
27
|
-
} catch {
|
|
28
|
-
// Ignore malformed SSE frames
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return remainder;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Watch deployment progress over a single long-lived SSE stream backed by MQTT.
|
|
37
|
-
*/
|
|
38
|
-
export async function watchDeploymentStream(deploymentId, { onUpdate, signal } = {}) {
|
|
39
|
-
const token = getToken();
|
|
40
|
-
if (!token) {
|
|
41
|
-
throw new Error('Not logged in');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const url = `${getApiBaseUrl()}/api/v1/cli/deployments/${deploymentId}/stream`;
|
|
45
|
-
const response = await fetch(url, {
|
|
46
|
-
headers: {
|
|
47
|
-
Authorization: `Bearer ${token}`,
|
|
48
|
-
Accept: 'text/event-stream',
|
|
49
|
-
},
|
|
50
|
-
signal,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
if (!response.ok) {
|
|
54
|
-
const err = new Error(`Deploy stream failed (${response.status})`);
|
|
55
|
-
err.status = response.status;
|
|
56
|
-
throw err;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!response.body) {
|
|
60
|
-
throw new Error('Deploy stream response has no body');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const reader = response.body.getReader();
|
|
64
|
-
const decoder = new TextDecoder();
|
|
65
|
-
let buffer = '';
|
|
66
|
-
let latestDeployment = null;
|
|
67
|
-
let streamError = null;
|
|
68
|
-
|
|
69
|
-
while (true) {
|
|
70
|
-
const { done, value } = await reader.read();
|
|
71
|
-
if (done) break;
|
|
72
|
-
|
|
73
|
-
buffer += decoder.decode(value, { stream: true });
|
|
74
|
-
buffer = parseSseChunk(buffer, (event, payload) => {
|
|
75
|
-
if (event === 'snapshot' || event === 'progress' || event === 'done') {
|
|
76
|
-
latestDeployment = payload?.deployment || latestDeployment;
|
|
77
|
-
if (latestDeployment) {
|
|
78
|
-
onUpdate?.(latestDeployment, { event, payload });
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (event === 'error') {
|
|
83
|
-
const err = new Error(payload?.message || 'Deploy stream error');
|
|
84
|
-
err.code = payload?.code;
|
|
85
|
-
streamError = err;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
if (streamError) {
|
|
90
|
-
throw streamError;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return latestDeployment;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function isTerminalDeployment(deployment) {
|
|
98
|
-
return TERMINAL_STATUSES.has(deployment?.status);
|
|
99
|
-
}
|