4545 *
4646 * ⛔ Not the retired `os publish` id (see `package/publish.ts`'s header), on
4747 * purpose: that id is documented as removed and must stay unresolvable.
48+ *
49+ * ## A self-hosted runtime target
50+ *
51+ * When `os environments use-runtime` made a self-hosted runtime (the Docker
52+ * image, Community or Enterprise) the active deploy target, the same command
53+ * with no flags deploys THERE instead (`--env` still names a cloud environment
54+ * and deploys to it). There is no catalog and no version to mint: the build is
55+ * sent inline to the runtime's install-local door — `os package install` itself,
56+ * run in this process with the remembered url and key, so its refusals and
57+ * answers stay in ONE place:
58+ *
59+ * 1. build (`compileForDeploy`), as above;
60+ * 2. the dry run — the runtime's package upgrade plan of the installed
61+ * version against this build, printed; nothing is installed. A runtime
62+ * that refuses it, or does not serve install-local, stops the deploy here
63+ * with its sentence and exit 1;
64+ * 3. the install, with `--confirm-access-upgrade` passed through; the door's
65+ * refusals (an access upgrade to confirm, …) stop the deploy with exit 1;
66+ * 4. the answer — install-local registers the package in the live kernel
67+ * before it answers, so the answer IS "live": `hotLoaded` ⇒ 0; a package
68+ * the running kernel could not load ⇒ 1, naming the restart it waits for.
69+ *
70+ * Whether an upgrade is refused for a rename or a type change (epic
71+ * cloud#3652 Q3) is install-local's verdict, not this command's: a refusal it
72+ * answers is printed and stops the deploy like any other.
4873 */
4974
5075import { readFile } from 'node:fs/promises' ;
@@ -56,7 +81,10 @@ import { describeEnvironment, resolveInstallTarget } from '../utils/install-targ
5681import { compileForDeploy } from '../utils/deploy-build.js' ;
5782import { devVersion } from '../utils/dev-version.js' ;
5883import { fetchEnvironmentOrigin , waitUntilServed , wakeEnvironment , type ServedVersionVerdict } from '../utils/served-version.js' ;
84+ import { activeRuntimeTarget } from '../utils/runtime-target.js' ;
85+ import type { RuntimeTarget } from '../utils/cloud-config.js' ;
5986import PackagePublish from './package/publish.js' ;
87+ import PackageInstall from './package/install.js' ;
6088
6189/** The artifact the build writes and the publish reads — `os build`'s default output. */
6290export const DEPLOY_ARTIFACT = 'dist/objectstack.json' ;
@@ -66,12 +94,14 @@ export const DEPLOY_POLL_INTERVAL_MS = 3_000;
6694
6795export default class Deploy extends Command {
6896 static override description =
69- 'Deploy this app to your ObjectOS Cloud environment: build it, publish a dev version, install it, and wait until it is live' ;
97+ 'Deploy this app to your ObjectOS Cloud environment (build it, publish a dev version, install it, and wait until '
98+ + 'it is live), or to the self-hosted runtime `os environments use-runtime` made the active target' ;
7099
71100 static override examples = [
72101 '$ os deploy' ,
73102 '$ os deploy --env env_abc123 # your account reaches several environments' ,
74103 '$ os deploy --no-wait # return once the install is recorded' ,
104+ '$ os deploy --confirm-access-upgrade <finding-set> # self-hosted: confirm the findings the runtime printed' ,
75105 ] ;
76106
77107 static override flags = {
@@ -105,13 +135,34 @@ export default class Deploy extends Command {
105135 description : 'API key for ObjectOS Cloud (bearer token; defaults to OS_CLOUD_API_KEY / OS_TOKEN, then `os cloud login`)' ,
106136 env : 'OS_CLOUD_API_KEY' ,
107137 } ) ,
138+ 'confirm-access-upgrade' : Flags . string ( {
139+ description :
140+ 'Self-hosted runtime target: confirm the access findings of this upgrade with the finding-set token the '
141+ + "runtime's refusal printed (as `os package install --confirm-access-upgrade`)" ,
142+ } ) ,
108143 } ;
109144
110145 async run ( ) : Promise < void > {
111146 const { flags } = await this . parse ( Deploy ) ;
112147 printHeader ( 'Deploy' ) ;
113148
114149 try {
150+ // 0. Target: the self-hosted runtime when it is the active one and no
151+ // cloud environment was named.
152+ const runtime = flags . env ? undefined : await activeRuntimeTarget ( ) ;
153+ if ( runtime ) {
154+ await this . deployToRuntime ( runtime , flags [ 'confirm-access-upgrade' ] ) ;
155+ return ;
156+ }
157+ if ( flags [ 'confirm-access-upgrade' ] !== undefined ) {
158+ printError (
159+ '`--confirm-access-upgrade` confirms a self-hosted runtime\'s refusal; the deploy target is ObjectOS Cloud. '
160+ + 'Drop the flag, or record the runtime with `os environments use-runtime <url> --token <key>`.' ,
161+ ) ;
162+ this . exit ( 1 ) ;
163+ return ;
164+ }
165+
115166 // 1. Credential.
116167 const { token, baseUrl } = await resolveCloudCredential ( { token : flags . token , server : flags . server } ) ;
117168 if ( ! token ) {
@@ -229,6 +280,59 @@ export default class Deploy extends Command {
229280 this . exit ( 1 ) ;
230281 }
231282 }
283+
284+ /**
285+ * The self-hosted half (the header's "A self-hosted runtime target"):
286+ * build → dry run (plan printed) → install → the `hotLoaded` answer.
287+ */
288+ private async deployToRuntime ( runtime : RuntimeTarget , confirmAccessUpgrade : string | undefined ) : Promise < void > {
289+ printStep ( `Deploying to the self-hosted runtime ${ runtime . url } (os environments use-runtime)` ) ;
290+
291+ printStep ( 'Building...' ) ;
292+ const artifactPath = resolvePath ( process . cwd ( ) , DEPLOY_ARTIFACT ) ;
293+ if ( compileForDeploy ( artifactPath ) !== 0 ) {
294+ printError ( 'The build failed — fix the errors above, then run `os deploy` again. Nothing was installed.' ) ;
295+ this . exit ( 1 ) ;
296+ return ;
297+ }
298+
299+ const install = async ( extra : string [ ] ) => {
300+ const command = new PackageInstall (
301+ [ artifactPath , '--url' , runtime . url , '--token' , runtime . token , ...extra ] ,
302+ this . config ,
303+ ) ;
304+ command . retryCommand = 'os deploy' ;
305+ command . embedded = true ;
306+ await command . run ( ) ;
307+ return command . outcome ;
308+ } ;
309+
310+ console . log ( '' ) ;
311+ printStep ( `Asking ${ runtime . url } for the upgrade plan (dry run — nothing is installed)...` ) ;
312+ const plan = await install ( [ '--dry-run' ] ) ;
313+ if ( plan ?. kind !== 'dry-run' ) {
314+ printError ( `${ runtime . url } did not answer the dry run with a plan, so nothing more was sent. Upgrade the runtime, then run \`os deploy\` again.` ) ;
315+ this . exit ( 1 ) ;
316+ return ;
317+ }
318+
319+ printStep ( `Installing ${ plan . manifestId } ${ plan . version } into ${ runtime . url } ...` ) ;
320+ const outcome = await install ( confirmAccessUpgrade !== undefined ? [ '--confirm-access-upgrade' , confirmAccessUpgrade ] : [ ] ) ;
321+ if ( outcome ?. kind !== 'installed' ) {
322+ printError ( `${ runtime . url } did not answer the install as a managed package install, so this cannot say ${ plan . manifestId } is live (see above).` ) ;
323+ this . exit ( 1 ) ;
324+ return ;
325+ }
326+ if ( ! outcome . hotLoaded ) {
327+ printError (
328+ `${ runtime . url } recorded ${ outcome . manifestId } ${ outcome . version } , but its running kernel could not load it, so `
329+ + 'it is not live: it loads when the runtime restarts. Fix the cause above, then run `os deploy` again.' ,
330+ ) ;
331+ this . exit ( 1 ) ;
332+ return ;
333+ }
334+ printSuccess ( `Live: ${ runtime . url } now serves ${ outcome . manifestId } ${ outcome . version } .` ) ;
335+ }
232336}
233337
234338/** Print the wait's verdict; return the exit code it means. */
0 commit comments