@di-framework/cli-plugin-platform 0.0.1 → 6.0.3

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.
Files changed (169) hide show
  1. package/README.md +408 -0
  2. package/dist/actor-protocol.d.ts +4 -0
  3. package/dist/actor-protocol.js +162 -0
  4. package/dist/actors.d.ts +31 -0
  5. package/dist/actors.js +296 -0
  6. package/dist/args.d.ts +15 -0
  7. package/dist/args.js +66 -0
  8. package/dist/assets/http-adapter.js +876 -0
  9. package/dist/assets/platform/.gitignore.tmpl +6 -0
  10. package/dist/assets/platform/Pulumi.yaml.tmpl +6 -0
  11. package/dist/assets/platform/README.md +208 -0
  12. package/dist/assets/platform/index.ts.tmpl +2 -0
  13. package/dist/assets/platform/package.json.tmpl +18 -0
  14. package/dist/assets/platform/tsconfig.json +11 -0
  15. package/dist/assets/queue-adapter.js +60 -0
  16. package/dist/assets/sqlite/BUILD-INFO.txt +13 -0
  17. package/dist/assets/sqlite/SHA256SUMS +2 -0
  18. package/dist/assets/sqlite/di-framework-sqlite.wasm +0 -0
  19. package/dist/assets/sqlite/di-framework-sqlite.wit +337 -0
  20. package/dist/assets/wit/deps/LICENSE.md +8 -0
  21. package/dist/assets/wit/deps/di-framework-queues/package.wit +18 -0
  22. package/dist/assets/wit/deps/di-framework-sqlite/package.wit +211 -0
  23. package/dist/assets/wit/deps/wasi-cli/package.wit +70 -0
  24. package/dist/assets/wit/deps/wasi-clocks/package.wit +42 -0
  25. package/dist/assets/wit/deps/wasi-config/package.wit +15 -0
  26. package/dist/assets/wit/deps/wasi-filesystem/package.wit +184 -0
  27. package/dist/assets/wit/deps/wasi-http/package.wit +170 -0
  28. package/dist/assets/wit/deps/wasi-random/package.wit +17 -0
  29. package/dist/assets/wit/deps/wasi-sockets/package.wit +105 -0
  30. package/dist/assets/wit/deps/wasi-tls/package.wit +26 -0
  31. package/dist/assets/wit/deps/wasmcloud-blobstore/package.wit +153 -0
  32. package/dist/assets/wit/deps/wasmcloud-keyvalue/package.wit +370 -0
  33. package/dist/assets/wit/deps/wasmcloud-messaging/package.wit +125 -0
  34. package/dist/assets/wit/deps/wasmcloud-postgres/package.wit +435 -0
  35. package/dist/assets/wit/deps/wasmcloud-secrets/package.wit +80 -0
  36. package/dist/async-transform.d.ts +3 -0
  37. package/dist/async-transform.js +60 -0
  38. package/dist/bindings.d.ts +34 -0
  39. package/dist/bindings.js +307 -0
  40. package/dist/build.d.ts +29 -0
  41. package/dist/build.js +325 -0
  42. package/dist/command.d.ts +3 -0
  43. package/dist/command.js +141 -0
  44. package/dist/control/auth.d.ts +18 -0
  45. package/dist/control/auth.js +89 -0
  46. package/dist/control/cron.d.ts +7 -0
  47. package/dist/control/cron.js +86 -0
  48. package/dist/control/index.d.ts +4 -0
  49. package/dist/control/index.js +4 -0
  50. package/dist/control/network.d.ts +12 -0
  51. package/dist/control/network.js +49 -0
  52. package/dist/control/queues.d.ts +15 -0
  53. package/dist/control/queues.js +77 -0
  54. package/dist/cron.d.ts +26 -0
  55. package/dist/cron.js +239 -0
  56. package/dist/deploy.d.ts +29 -0
  57. package/dist/deploy.js +57 -0
  58. package/dist/deps.d.ts +96 -0
  59. package/dist/deps.js +303 -0
  60. package/dist/destroy.d.ts +3 -0
  61. package/dist/destroy.js +24 -0
  62. package/dist/dev-runner.d.ts +24 -0
  63. package/dist/dev-runner.js +83 -0
  64. package/dist/dev.d.ts +8 -0
  65. package/dist/dev.js +76 -0
  66. package/dist/discovery.d.ts +15 -0
  67. package/dist/discovery.js +182 -0
  68. package/dist/doctor.d.ts +8 -0
  69. package/dist/doctor.js +82 -0
  70. package/dist/guests.d.ts +9 -0
  71. package/dist/guests.js +56 -0
  72. package/dist/host-interface.d.ts +29 -0
  73. package/dist/host-interface.js +142 -0
  74. package/dist/index.d.ts +33 -0
  75. package/dist/index.js +39 -0
  76. package/dist/kubernetes.d.ts +9 -0
  77. package/dist/kubernetes.js +17 -0
  78. package/dist/managed-bindings.d.ts +9 -0
  79. package/dist/managed-bindings.js +150 -0
  80. package/dist/manifest.d.ts +36 -0
  81. package/dist/manifest.js +257 -0
  82. package/dist/namespace.d.ts +2 -0
  83. package/dist/namespace.js +4 -0
  84. package/dist/node-compat/async-hooks.d.ts +29 -0
  85. package/dist/node-compat/async-hooks.js +80 -0
  86. package/dist/node-compat/bootstrap.d.ts +1 -0
  87. package/dist/node-compat/bootstrap.js +9 -0
  88. package/dist/node-compat/bytes.d.ts +7 -0
  89. package/dist/node-compat/bytes.js +41 -0
  90. package/dist/node-compat/crypto-hash.d.ts +27 -0
  91. package/dist/node-compat/crypto-hash.js +108 -0
  92. package/dist/node-compat/crypto-subtle.d.ts +59 -0
  93. package/dist/node-compat/crypto-subtle.js +288 -0
  94. package/dist/node-compat/crypto.d.ts +136 -0
  95. package/dist/node-compat/crypto.js +142 -0
  96. package/dist/node-compat/dgram.d.ts +49 -0
  97. package/dist/node-compat/dgram.js +197 -0
  98. package/dist/node-compat/env.d.ts +6 -0
  99. package/dist/node-compat/env.js +88 -0
  100. package/dist/node-compat/fetch-runtime.d.ts +80 -0
  101. package/dist/node-compat/fetch-runtime.js +314 -0
  102. package/dist/node-compat/fs.d.ts +84 -0
  103. package/dist/node-compat/fs.js +285 -0
  104. package/dist/node-compat/http-parser.d.ts +40 -0
  105. package/dist/node-compat/http-parser.js +185 -0
  106. package/dist/node-compat/http.d.ts +130 -0
  107. package/dist/node-compat/http.js +688 -0
  108. package/dist/node-compat/https.d.ts +28 -0
  109. package/dist/node-compat/https.js +88 -0
  110. package/dist/node-compat/module.d.ts +11 -0
  111. package/dist/node-compat/module.js +35 -0
  112. package/dist/node-compat/net.d.ts +119 -0
  113. package/dist/node-compat/net.js +441 -0
  114. package/dist/node-compat/process.d.ts +25 -0
  115. package/dist/node-compat/process.js +63 -0
  116. package/dist/node-compat/runtime-application.d.ts +4 -0
  117. package/dist/node-compat/runtime-application.js +24 -0
  118. package/dist/node-compat/seed-virtual.d.ts +6 -0
  119. package/dist/node-compat/seed-virtual.js +6 -0
  120. package/dist/node-compat/seed.d.ts +20 -0
  121. package/dist/node-compat/seed.js +106 -0
  122. package/dist/node-compat/socket-address.d.ts +35 -0
  123. package/dist/node-compat/socket-address.js +162 -0
  124. package/dist/node-compat/timers.d.ts +32 -0
  125. package/dist/node-compat/timers.js +96 -0
  126. package/dist/node-compat/tls.d.ts +57 -0
  127. package/dist/node-compat/tls.js +290 -0
  128. package/dist/node-compat/wasi-random.d.ts +2 -0
  129. package/dist/node-compat/wasi-random.js +25 -0
  130. package/dist/node-compat/wasi-sockets.d.ts +47 -0
  131. package/dist/node-compat/wasi-sockets.js +134 -0
  132. package/dist/oci.d.ts +9 -0
  133. package/dist/oci.js +9 -0
  134. package/dist/paths.d.ts +7 -0
  135. package/dist/paths.js +13 -0
  136. package/dist/platform-init.d.ts +13 -0
  137. package/dist/platform-init.js +265 -0
  138. package/dist/platform.d.ts +22 -0
  139. package/dist/platform.js +170 -0
  140. package/dist/project.d.ts +39 -0
  141. package/dist/project.js +130 -0
  142. package/dist/publish.d.ts +15 -0
  143. package/dist/publish.js +57 -0
  144. package/dist/pulumi.d.ts +4 -0
  145. package/dist/pulumi.js +27 -0
  146. package/dist/queues-module.d.ts +4 -0
  147. package/dist/queues-module.js +89 -0
  148. package/dist/queues.d.ts +16 -0
  149. package/dist/queues.js +167 -0
  150. package/dist/registry.d.ts +16 -0
  151. package/dist/registry.js +27 -0
  152. package/dist/service.d.ts +162 -0
  153. package/dist/service.js +678 -0
  154. package/dist/support.d.ts +14 -0
  155. package/dist/support.js +37 -0
  156. package/dist/target.d.ts +18 -0
  157. package/dist/target.js +45 -0
  158. package/dist/toml.d.ts +6 -0
  159. package/dist/toml.js +245 -0
  160. package/dist/wash-dev.d.ts +18 -0
  161. package/dist/wash-dev.js +80 -0
  162. package/dist/wit.d.ts +76 -0
  163. package/dist/wit.js +248 -0
  164. package/dist/workload-members.d.ts +12 -0
  165. package/dist/workload-members.js +136 -0
  166. package/dist/workload.d.ts +45 -0
  167. package/dist/workload.js +457 -0
  168. package/package.json +55 -8
  169. package/src/index.ts +0 -2
@@ -0,0 +1,457 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { mkdirSync, writeFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { CommandFailure } from '@di-framework/cli-extension';
5
+ import { discoverActors } from './actors.js';
6
+ import { discoverBindings, requirementsFromBindings } from './bindings.js';
7
+ import { discoverScheduledJobs } from './cron.js';
8
+ import { hostInterfacesFromRequirements, renderHostInterfacesYaml } from './host-interface.js';
9
+ import { captureKubectl, runKubectl } from './kubernetes.js';
10
+ import { applyManagedBindings, cleanupManagedBindings } from './managed-bindings.js';
11
+ import { asWitIdentifier } from './project.js';
12
+ import { discoverQueueHandlers, isQueueWorkerProject } from './queues.js';
13
+ import { defaultProjectRequirements, queueProjectRequirements } from './wit.js';
14
+ export const MANAGED_BY_LABEL = 'di-framework';
15
+ export const WAIT_ATTEMPTS = 30;
16
+ export const WAIT_INTERVAL_MS = 2_000;
17
+ export const WORKLOAD_DEPLOYMENT_RESOURCE = 'workloaddeployment.runtime.wasmcloud.dev';
18
+ export const WORKLOAD_REPLICA_SET_RESOURCE = 'workloadreplicaset.runtime.wasmcloud.dev';
19
+ /** Host directory where the storage hostgroup mounts the shared PVC. */
20
+ export const HOST_STORAGE_ROOT = '/var/lib/di-framework/storage';
21
+ /** Dedicated single-replica hostgroup that mounts persistent application storage. */
22
+ export const STORAGE_HOSTGROUP = 'storage';
23
+ /** Guest mount path for application SQLite storage. */
24
+ export const DEFAULT_STORAGE_MOUNT = '/data';
25
+ /** Pinned CronJob invoker image that POSTs to the Wasm HTTP control API. */
26
+ export const CRON_INVOKER_IMAGE = 'curlimages/curl:8.11.1';
27
+ export function deploymentResourceName(project) {
28
+ return project.witName;
29
+ }
30
+ export function generatedManifestPath(project) {
31
+ return join(project.projectRoot, '.di-framework', 'deploy', 'workload.yaml');
32
+ }
33
+ export function hostStoragePath(applicationName) {
34
+ return `${HOST_STORAGE_ROOT}/${asWitIdentifier(applicationName)}`;
35
+ }
36
+ /** Kubernetes Secret that holds DI_CONTROL_TOKEN for a workload. */
37
+ export function controlSecretResourceName(workloadName) {
38
+ return `${workloadName}-control`;
39
+ }
40
+ export function renderWorkloadManifest(project, connection, image, requirements = defaultProjectRequirements(), bindings = [], options, cronJobs = [], queueHandlers = []) {
41
+ const opts = typeof options === 'boolean' ? { hasActors: options } : (options ?? {});
42
+ const hasActors = opts.hasActors ?? false;
43
+ const resolvedHandlers = queueHandlers.length > 0 ? queueHandlers : discoverQueueHandlers(project);
44
+ const isWorker = isQueueWorkerProject(project, resolvedHandlers);
45
+ const hasQueues = resolvedHandlers.length > 0;
46
+ const needsPersistentStorage = opts.hasPersistentStorage ??
47
+ (hasActors || hasQueues || isWorker || project.persistentStorage === true);
48
+ const needsControlHttp = hasActors || cronJobs.length > 0 || hasQueues || isWorker;
49
+ const publicIngress = project.ingress !== false && !isWorker;
50
+ // Cluster Service is required for cron invokers and queue/actor control even when
51
+ // public ingress is disabled.
52
+ const hasHttp = publicIngress || needsControlHttp || needsPersistentStorage;
53
+ if (needsPersistentStorage) {
54
+ if (opts.replicas !== undefined && opts.replicas !== 1) {
55
+ throw new CommandFailure('WASMCLOUD_STORAGE_REPLICA_CONSTRAINT', 'SQLite-backed workloads require replicas: 1 because the WASI VFS lacks file locking', 2, { replicas: opts.replicas });
56
+ }
57
+ }
58
+ const name = deploymentResourceName(project);
59
+ const labels = [
60
+ ` app.kubernetes.io/managed-by: ${MANAGED_BY_LABEL}`,
61
+ ` app.kubernetes.io/name: ${name}`,
62
+ ` di-framework.dev/application: ${yamlQuote(project.applicationName)}`,
63
+ ...(project.workload ? [` di-framework.dev/workload: ${yamlQuote(project.workload)}`] : []),
64
+ ].join('\n');
65
+ const volumeName = opts.storageVolume?.volumeName ?? 'app-storage';
66
+ const mountPath = opts.storageVolume?.mountPath ??
67
+ (hasActors ? `${DEFAULT_STORAGE_MOUNT}/actors` : DEFAULT_STORAGE_MOUNT);
68
+ const hostPath = opts.storageVolume?.hostPath ?? hostStoragePath(project.applicationName);
69
+ const environment = { ...(opts.environment ?? {}) };
70
+ if (needsPersistentStorage) {
71
+ environment.DI_SQLITE_BACKEND = 'wasm';
72
+ environment.DI_STORAGE_DIR = mountPath;
73
+ }
74
+ if (hasActors)
75
+ environment.ACTOR_STORAGE_DIR = mountPath;
76
+ if (needsPersistentStorage && !hasActors) {
77
+ environment.QUEUE_DB_PATH = `${mountPath}/queue.db`;
78
+ environment.MIGRATION_DB_PATH = `${mountPath}/migrations.db`;
79
+ }
80
+ const controlSecretName = opts.controlSecretName ?? (hasHttp ? controlSecretResourceName(name) : undefined);
81
+ const clusterHttpHost = `${name}.${connection.namespace}.svc.cluster.local`;
82
+ const advertisedHttpHost = publicIngress ? project.applicationName : clusterHttpHost;
83
+ if (hasHttp) {
84
+ environment.DI_CONTROL_REJECT_FORWARDED = '1';
85
+ environment.DI_CONTROL_HTTP_HOST = [name, clusterHttpHost].join(',');
86
+ }
87
+ if (cronJobs.length > 0)
88
+ environment.DI_CRON_MODE = 'external';
89
+ if (hasQueues) {
90
+ environment.DI_QUEUE_MODE = 'sqlite';
91
+ for (const handler of resolvedHandlers) {
92
+ const prefix = `DI_QUEUE_${handler.queueName.replace(/[^A-Za-z0-9]/g, '_').toUpperCase()}`;
93
+ if (handler.options.concurrency !== undefined) {
94
+ environment[`${prefix}_CONCURRENCY`] = String(handler.options.concurrency);
95
+ }
96
+ if (handler.options.maxRetries !== undefined) {
97
+ environment[`${prefix}_MAX_RETRIES`] = String(handler.options.maxRetries);
98
+ }
99
+ if (handler.options.backoffMs !== undefined) {
100
+ environment[`${prefix}_BACKOFF_MS`] = String(handler.options.backoffMs);
101
+ }
102
+ if (handler.options.timeoutMs !== undefined) {
103
+ environment[`${prefix}_TIMEOUT_MS`] = String(handler.options.timeoutMs);
104
+ }
105
+ }
106
+ }
107
+ const sections = [];
108
+ if (hasHttp) {
109
+ sections.push(`apiVersion: v1
110
+ kind: Service
111
+ metadata:
112
+ name: ${name}
113
+ namespace: ${connection.namespace}
114
+ labels:
115
+ ${labels}
116
+ spec:
117
+ type: ClusterIP
118
+ ports:
119
+ - name: http
120
+ port: 80
121
+ targetPort: 80
122
+ protocol: TCP`);
123
+ }
124
+ const localResourcesLines = [];
125
+ const envKeys = Object.keys(environment).sort();
126
+ if (envKeys.length > 0 || controlSecretName !== undefined) {
127
+ localResourcesLines.push(' localResources:');
128
+ localResourcesLines.push(' environment:');
129
+ if (envKeys.length > 0) {
130
+ localResourcesLines.push(' config:');
131
+ for (const key of envKeys) {
132
+ localResourcesLines.push(` ${key}: ${yamlQuote(environment[key])}`);
133
+ }
134
+ }
135
+ if (controlSecretName !== undefined) {
136
+ localResourcesLines.push(' secretFrom:');
137
+ localResourcesLines.push(` - name: ${controlSecretName}`);
138
+ }
139
+ }
140
+ else if (project.allowedIpNameLookups !== undefined) {
141
+ localResourcesLines.push(' localResources:');
142
+ }
143
+ if (needsPersistentStorage) {
144
+ localResourcesLines.push(' volumeMounts:');
145
+ localResourcesLines.push(` - name: ${volumeName}`);
146
+ localResourcesLines.push(` mountPath: ${mountPath}`);
147
+ }
148
+ if (project.allowedIpNameLookups !== undefined) {
149
+ localResourcesLines.push(` allowedIpNameLookups: ${JSON.stringify(project.allowedIpNameLookups)}`);
150
+ }
151
+ const hostInterfaces = renderHostInterfacesYaml(hostInterfacesFromRequirements(hasHttp && !requirements.some((r) => r.package === 'wasi:http' && r.direction === 'export')
152
+ ? [
153
+ ...requirements,
154
+ {
155
+ package: 'wasi:http',
156
+ version: '0.3.0',
157
+ interfaces: ['handler'],
158
+ direction: 'export',
159
+ source: 'control-http',
160
+ },
161
+ ]
162
+ : requirements, {
163
+ ...(hasHttp ? { httpHost: advertisedHttpHost } : {}),
164
+ subscriptions: project.workloadEntry?.subscriptions,
165
+ }, bindings.map((binding) => ({
166
+ name: binding.name,
167
+ className: binding.className,
168
+ config: binding.config,
169
+ configFrom: binding.configFrom,
170
+ secretFrom: binding.secretFrom,
171
+ }))));
172
+ const workloadDeployment = `apiVersion: runtime.wasmcloud.dev/v1alpha1
173
+ kind: WorkloadDeployment
174
+ metadata:
175
+ name: ${name}
176
+ namespace: ${connection.namespace}
177
+ labels:
178
+ ${labels}
179
+ spec:
180
+ replicas: 1
181
+ ${needsPersistentStorage ? ' deployPolicy: Recreate\n' : ''} template:
182
+ spec:
183
+ environment: ${yamlQuote(connection.namespace)}
184
+ hostSelector:
185
+ hostgroup: ${needsPersistentStorage ? (connection.storageHostgroup ?? STORAGE_HOSTGROUP) : (connection.hostgroup ?? 'default')}
186
+ ${needsPersistentStorage
187
+ ? ` volumes:
188
+ - name: ${volumeName}
189
+ hostPath:
190
+ path: ${yamlQuote(hostPath)}
191
+ `
192
+ : ''}${hasHttp
193
+ ? ` kubernetes:
194
+ service:
195
+ name: ${name}
196
+ `
197
+ : ''} components:
198
+ - name: ${name}
199
+ image: ${yamlQuote(image)}
200
+ ${localResourcesLines.length > 0 ? `${localResourcesLines.join('\n')}\n` : ''}${hostInterfaces}`;
201
+ sections.push(workloadDeployment);
202
+ for (const job of cronJobs) {
203
+ const jobKebab = job.kebabId || asWitIdentifier(job.name || `${job.className}-${job.methodName}`);
204
+ const jobResourceName = `${name}-${jobKebab}`;
205
+ const timeoutSeconds = Math.max(1, Math.ceil((job.timeoutMs ?? 30_000) / 1000));
206
+ // Job deadline must cover image pull + invoke; curl --max-time enforces the app timeout.
207
+ const activeDeadlineSeconds = Math.max(timeoutSeconds + 60, 90);
208
+ const invokeUrl = `http://${name}.${connection.namespace}.svc.cluster.local/_di/cron/${encodeURIComponent(job.jobId)}/invoke`;
209
+ sections.push(`apiVersion: batch/v1
210
+ kind: CronJob
211
+ metadata:
212
+ name: ${jobResourceName}
213
+ namespace: ${connection.namespace}
214
+ labels:
215
+ ${labels}
216
+ di-framework.dev/cron-job: ${yamlQuote(job.jobId)}
217
+ spec:
218
+ schedule: ${yamlQuote(job.cronExpression)}
219
+ concurrencyPolicy: ${job.allowConcurrent ? 'Allow' : 'Forbid'}
220
+ jobTemplate:
221
+ spec:
222
+ activeDeadlineSeconds: ${activeDeadlineSeconds}
223
+ template:
224
+ metadata:
225
+ labels:
226
+ ${labels.replace(/^/gm, ' ')}
227
+ di-framework.dev/cron-job: ${yamlQuote(job.jobId)}
228
+ spec:
229
+ restartPolicy: OnFailure
230
+ containers:
231
+ - name: cron-invoker
232
+ image: ${yamlQuote(CRON_INVOKER_IMAGE)}
233
+ env:
234
+ - name: DI_CRON_INVOKE_URL
235
+ value: ${yamlQuote(invokeUrl)}
236
+ - name: DI_CRON_INVOKE_JOB
237
+ value: ${yamlQuote(job.jobId)}
238
+ - name: DI_CONTROL_TOKEN
239
+ valueFrom:
240
+ secretKeyRef:
241
+ name: ${controlSecretName ?? `${name}-control`}
242
+ key: DI_CONTROL_TOKEN
243
+ command:
244
+ - /bin/sh
245
+ - -ec
246
+ - |
247
+ set -eu
248
+ response="$(curl -sS -f -X POST \\
249
+ -H "Host: ${advertisedHttpHost}" \\
250
+ -H "content-type: application/json" \\
251
+ -H "Authorization: Bearer \${DI_CONTROL_TOKEN}" \\
252
+ --max-time ${timeoutSeconds} \\
253
+ -d '{}' \\
254
+ "$DI_CRON_INVOKE_URL")"
255
+ echo "$response"
256
+ echo "$response" | grep -q '"completed":true\\|\\"ok\\":true'`);
257
+ }
258
+ return sections.join('\n---\n') + '\n';
259
+ }
260
+ async function ensureControlSecret(project, connection, deps) {
261
+ const name = deploymentResourceName(project);
262
+ const secretName = controlSecretResourceName(name);
263
+ const existing = await captureKubectl(deps, connection, ['get', 'secret', secretName], project.projectRoot);
264
+ if (existing.exitCode !== 0) {
265
+ const token = randomBytes(32).toString('base64url');
266
+ await runKubectl(deps, connection, [
267
+ 'create',
268
+ 'secret',
269
+ 'generic',
270
+ secretName,
271
+ `--from-literal=DI_CONTROL_TOKEN=${token}`,
272
+ `--from-literal=DI_CONTROL_IDENTITY=${name}`,
273
+ ], project.projectRoot);
274
+ }
275
+ await runKubectl(deps, connection, [
276
+ 'label',
277
+ 'secret',
278
+ secretName,
279
+ `app.kubernetes.io/managed-by=${MANAGED_BY_LABEL}`,
280
+ `app.kubernetes.io/name=${name}`,
281
+ '--overwrite',
282
+ ], project.projectRoot);
283
+ return secretName;
284
+ }
285
+ export async function applyWorkload(project, connection, image, io, deps) {
286
+ const bindings = discoverBindings(project, deps);
287
+ const queueHandlers = discoverQueueHandlers(project);
288
+ const isWorker = isQueueWorkerProject(project, queueHandlers);
289
+ const hasActors = discoverActors(project).length > 0 || project.actors === true;
290
+ const cronJobs = discoverScheduledJobs(project.projectRoot);
291
+ const hasPersistentStorage = hasActors || queueHandlers.length > 0 || project.persistentStorage === true;
292
+ const needsHttp = (project.ingress !== false && !isWorker) ||
293
+ hasActors ||
294
+ cronJobs.length > 0 ||
295
+ queueHandlers.length > 0 ||
296
+ hasPersistentStorage;
297
+ const baseRequirements = needsHttp
298
+ ? defaultProjectRequirements()
299
+ : isWorker
300
+ ? queueProjectRequirements()
301
+ : [];
302
+ const requirements = [
303
+ ...baseRequirements,
304
+ ...requirementsFromBindings(bindings),
305
+ ];
306
+ if (project.workloadEntry?.subscriptions)
307
+ requirements.push({
308
+ package: 'wasmcloud:messaging',
309
+ version: '0.3.0',
310
+ interfaces: ['handler'],
311
+ direction: 'export',
312
+ source: 'workload-service',
313
+ });
314
+ const associations = await applyManagedBindings(project, connection, bindings, deps);
315
+ await assertStorageOwnership(project, connection, deps, {
316
+ hasActors,
317
+ hasQueues: queueHandlers.length > 0,
318
+ hasPersistentStorage,
319
+ });
320
+ const controlSecretName = needsHttp
321
+ ? await ensureControlSecret(project, connection, deps)
322
+ : undefined;
323
+ const manifest = renderWorkloadManifest(project, connection, image, requirements, bindings, { hasActors, hasPersistentStorage, controlSecretName }, cronJobs, queueHandlers);
324
+ const path = generatedManifestPath(project);
325
+ mkdirSync(join(project.projectRoot, '.di-framework', 'deploy'), { recursive: true });
326
+ writeFileSync(path, manifest);
327
+ const name = deploymentResourceName(project);
328
+ io.stdout.write(`Applying WorkloadDeployment ${name} in ${connection.namespace}...\n`);
329
+ await runKubectl(deps, connection, ['apply', '-f', path], project.projectRoot);
330
+ await waitForReady(project, connection, deps, io);
331
+ await cleanupManagedBindings(project, connection, associations, deps);
332
+ return path;
333
+ }
334
+ async function assertStorageOwnership(project, connection, deps, flags) {
335
+ if (!flags.hasActors && !flags.hasQueues && !flags.hasPersistentStorage)
336
+ return;
337
+ const name = deploymentResourceName(project);
338
+ const hostPath = hostStoragePath(project.applicationName);
339
+ const result = await captureKubectl(deps, connection, [
340
+ 'get',
341
+ WORKLOAD_DEPLOYMENT_RESOURCE,
342
+ '-o',
343
+ 'json',
344
+ '-l',
345
+ `di-framework.dev/application!=${project.applicationName}`,
346
+ ], project.projectRoot);
347
+ if (result.exitCode !== 0)
348
+ return;
349
+ try {
350
+ const list = JSON.parse(result.stdout);
351
+ let conflict;
352
+ for (const item of list.items ?? []) {
353
+ for (const volume of item.spec?.template?.spec?.volumes ?? []) {
354
+ if (volume.hostPath?.path === hostPath) {
355
+ conflict = new CommandFailure('WASMCLOUD_STORAGE_OWNERSHIP_CONFLICT', `Storage path ${hostPath} is already claimed by WorkloadDeployment ${item.metadata?.name ?? 'unknown'}`, 2, { application: project.applicationName, path: hostPath, owner: item.metadata?.name });
356
+ }
357
+ }
358
+ }
359
+ if (conflict)
360
+ throw conflict;
361
+ }
362
+ catch (error) {
363
+ if (error instanceof CommandFailure)
364
+ throw error;
365
+ }
366
+ void name;
367
+ }
368
+ export async function deleteWorkload(project, connection, io, deps) {
369
+ const name = deploymentResourceName(project);
370
+ io.stdout.write(`Removing WorkloadDeployment ${name} from ${connection.namespace}...\n`);
371
+ await runKubectl(deps, connection, [
372
+ 'delete',
373
+ `${WORKLOAD_DEPLOYMENT_RESOURCE},service,cronjob,secret`,
374
+ '-l',
375
+ `app.kubernetes.io/name=${name}`,
376
+ '--ignore-not-found',
377
+ '--wait=true',
378
+ '--timeout=180s',
379
+ ], project.projectRoot);
380
+ await cleanupManagedBindings(project, connection, new Set(), deps);
381
+ }
382
+ export async function waitForReady(project, connection, deps, io) {
383
+ const name = deploymentResourceName(project);
384
+ for (let attempt = 0; attempt < WAIT_ATTEMPTS; attempt++) {
385
+ const result = await captureKubectl(deps, connection, ['get', WORKLOAD_DEPLOYMENT_RESOURCE, name, '-o', 'json'], project.projectRoot);
386
+ if (result.exitCode === 0 && isReady(result.stdout))
387
+ return;
388
+ await deps.wait(WAIT_INTERVAL_MS);
389
+ }
390
+ const diagnostics = await deploymentDiagnostics(project, connection, deps);
391
+ if (io !== undefined) {
392
+ io.stderr.write(`WorkloadDeployment ${name} did not become ready. Kubernetes diagnostics follow:\n${diagnostics}\n`);
393
+ }
394
+ throw new CommandFailure('WASMCLOUD_DEPLOYMENT_NOT_READY', `WorkloadDeployment ${name} in ${connection.namespace} did not become ready`, 3, {
395
+ application: project.applicationName,
396
+ namespace: connection.namespace,
397
+ name,
398
+ diagnostics,
399
+ });
400
+ }
401
+ export function isReady(stdout) {
402
+ try {
403
+ const document = JSON.parse(stdout);
404
+ const replicas = document.spec?.replicas ?? 1;
405
+ if ((document.status?.readyReplicas ?? 0) >= replicas)
406
+ return true;
407
+ if ((document.status?.replicas?.ready ?? 0) >= replicas)
408
+ return true;
409
+ return (document.status?.conditions ?? []).some((condition) => (condition.type === 'Ready' || condition.type === 'Available') &&
410
+ condition.status === 'True');
411
+ }
412
+ catch {
413
+ return false;
414
+ }
415
+ }
416
+ async function deploymentDiagnostics(project, connection, deps) {
417
+ const name = deploymentResourceName(project);
418
+ const commands = [
419
+ {
420
+ title: 'WorkloadDeployment',
421
+ args: ['get', WORKLOAD_DEPLOYMENT_RESOURCE, name, '-o', 'yaml'],
422
+ },
423
+ {
424
+ title: 'WorkloadReplicaSets',
425
+ args: [
426
+ 'get',
427
+ WORKLOAD_REPLICA_SET_RESOURCE,
428
+ '-l',
429
+ `runtime.wasmcloud.dev/workload-deployment=${name}`,
430
+ '-o',
431
+ 'wide',
432
+ ],
433
+ },
434
+ {
435
+ title: 'wasmCloud host pods',
436
+ args: ['get', 'pods', '-l', 'wasmcloud.com/hostgroup', '-o', 'wide'],
437
+ },
438
+ {
439
+ title: 'wasmCloud storage host logs',
440
+ args: [
441
+ 'logs',
442
+ `deployment/hostgroup-${connection.storageHostgroup ?? STORAGE_HOSTGROUP}`,
443
+ '--tail=100',
444
+ ],
445
+ },
446
+ ];
447
+ const sections = [];
448
+ for (const command of commands) {
449
+ const result = await captureKubectl(deps, connection, command.args, project.projectRoot);
450
+ const output = result.stdout.trim() || result.stderr.trim() || `(kubectl exited ${result.exitCode})`;
451
+ sections.push(`--- ${command.title} ---\n${output}`);
452
+ }
453
+ return sections.join('\n');
454
+ }
455
+ function yamlQuote(value) {
456
+ return JSON.stringify(value);
457
+ }
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@di-framework/cli-plugin-platform",
3
- "version": "0.0.1",
4
- "description": "Placeholder for the di-framework cli platform plugin.",
3
+ "version": "6.0.3",
4
+ "description": "di-framework CLI extension that builds, serves, and deploys DI Framework apps as platform WebAssembly components.",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "module": "./dist/index.js",
5
8
  "type": "module",
6
9
  "license": "(MIT OR Apache-2.0)",
7
10
  "author": "@di-framework Contributors",
@@ -10,13 +13,57 @@
10
13
  "url": "https://github.com/di-framework/cli-extensions",
11
14
  "directory": "packages/cli-plugin-platform"
12
15
  },
13
- "main": "./src/index.ts",
14
- "types": "./src/index.ts",
16
+ "bugs": {
17
+ "url": "https://github.com/di-framework/cli-extensions/issues"
18
+ },
19
+ "homepage": "https://github.com/di-framework/cli-extensions",
20
+ "keywords": [
21
+ "cli-plugin-platform",
22
+ "wasmcloud",
23
+ "webassembly",
24
+ "wasi",
25
+ "component-model",
26
+ "cli-extension",
27
+ "di-framework"
28
+ ],
29
+ "scripts": {
30
+ "test": "bun test",
31
+ "build": "rm -rf dist && bun x tsc -p tsconfig.dist.json && bun build assets/http-adapter.ts --outfile dist/assets/http-adapter.js --packages bundle --external 'wasi:*' --external 'wasmcloud:*' --external 'di-framework:*' --external '@di-framework/actors' --external '@di-framework/actors/portable' --external '@di-framework/repo' --external '@di-framework/repo/portable' --external '@di-framework/queues' --external '@di-framework/queues/portable' --external 'virtual:di-framework-application' --external 'virtual:di-framework-wasmcloud-guests' --external 'virtual:di-framework-wasmcloud-actors' --external 'virtual:di-framework-wasmcloud-cron' --external 'virtual:di-framework-wasmcloud-queues' --external 'virtual:di-framework-wasmcloud-runtime' && bun build assets/queue-adapter.ts --outfile dist/assets/queue-adapter.js --packages bundle --external 'wasi:*' --external 'wasmcloud:*' --external 'di-framework:*' --external '@di-framework/actors' --external '@di-framework/actors/portable' --external 'virtual:di-framework-application' --external 'virtual:di-framework-wasmcloud-guests' --external 'virtual:di-framework-wasmcloud-actors' && cp -R assets/wit dist/assets/wit && cp -R assets/platform dist/assets/platform && bun ../../scripts/copy-sqlite-asset.ts && bun ../../scripts/fix-esm-imports.ts dist"
32
+ },
15
33
  "exports": {
16
- ".": "./src/index.ts"
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "bun": "./dist/index.js",
37
+ "import": "./dist/index.js",
38
+ "default": "./dist/index.js"
39
+ }
40
+ },
41
+ "dependencies": {
42
+ "@bytecodealliance/jco": "1.33.0",
43
+ "@di-framework/cli-extension": "^5",
44
+ "@di-framework/componentize-qjs": "0.4.4-di.3",
45
+ "@noble/ciphers": "2.4.0",
46
+ "@noble/curves": "2.4.0",
47
+ "@noble/hashes": "2.4.0",
48
+ "rolldown": "1.2.8",
49
+ "typescript": "^5",
50
+ "unenv": "2.0.0-rc.24",
51
+ "@babel/core": "7.29.7",
52
+ "@babel/plugin-transform-async-to-generator": "7.29.7",
53
+ "@di-framework/actors": "^5",
54
+ "@di-framework/platform": "^6"
55
+ },
56
+ "peerDependencies": {
57
+ "typescript": "^5"
17
58
  },
18
- "files": ["src"],
19
- "publishConfig": {
20
- "access": "public"
59
+ "files": [
60
+ "dist",
61
+ "README.md"
62
+ ],
63
+ "devDependencies": {
64
+ "@di-framework/config": "^5",
65
+ "@di-framework/core": "^5",
66
+ "@di-framework/bindings": "^6",
67
+ "@types/babel__core": "^7.20.5"
21
68
  }
22
69
  }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- /** Placeholder. Implementation ships from di-framework/platform. */
2
- export const name = "@di-framework/cli-plugin-platform" as const;