22// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
33// SPDX-License-Identifier: Apache-2.0
44
5- const { execSync , spawnSync } = require ( "child_process" ) ;
5+ const { execFileSync , spawnSync } = require ( "child_process" ) ;
66const path = require ( "path" ) ;
77const fs = require ( "fs" ) ;
88const os = require ( "os" ) ;
99
10- const { ROOT , SCRIPTS , run, runCapture, runInteractive } = require ( "./lib/runner" ) ;
10+ const { ROOT , SCRIPTS , run, runCapture, runInteractive, shellQuote } = require ( "./lib/runner" ) ;
1111const {
1212 ensureApiKey,
1313 ensureGithubToken,
@@ -28,10 +28,6 @@ const GLOBAL_COMMANDS = new Set([
2828
2929const REMOTE_UNINSTALL_URL = "https://raw.githubusercontent.com/NVIDIA/NemoClaw/refs/heads/main/uninstall.sh" ;
3030
31- function shellQuote ( value ) {
32- return `'${ String ( value ) . replace ( / ' / g, `'\\''` ) } '` ;
33- }
34-
3531function resolveUninstallScript ( ) {
3632 const candidates = [
3733 path . join ( ROOT , "uninstall.sh" ) ,
@@ -107,14 +103,15 @@ async function deploy(instanceName) {
107103 }
108104 const name = instanceName . trim ( ) ;
109105
110- // Validate: same RFC 1123 subdomain rules used for sandbox names — lowercase
111- // alphanumeric and hyphens, must start and end with alphanumeric.
112- if ( ! / ^ [ a - z 0 - 9 ] ( [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? $ / . test ( name ) || name . length > 253 ) {
106+ // Validate: RFC 1123 label — lowercase alphanumeric and hyphens,
107+ // must start and end with alphanumeric, max 63 chars .
108+ if ( ! name || name . length > 63 || ! / ^ [ a - z 0 - 9 ] ( [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? $ / . test ( name ) ) {
113109 console . error ( ` Invalid instance name: '${ name } '` ) ;
114110 console . error ( " Names must be lowercase, contain only letters, numbers, and hyphens," ) ;
115- console . error ( " and must start and end with a letter or number (max 253 chars)." ) ;
111+ console . error ( " and must start and end with a letter or number (max 63 chars)." ) ;
116112 process . exit ( 1 ) ;
117113 }
114+ const qname = shellQuote ( name ) ;
118115
119116 const gpu = process . env . NEMOCLAW_GPU || "a2-highgpu-1g:nvidia-tesla-a100:1" ;
120117
@@ -123,21 +120,24 @@ async function deploy(instanceName) {
123120 console . log ( "" ) ;
124121
125122 try {
126- execSync ( "which brev" , { stdio : "ignore" } ) ;
123+ execFileSync ( "which" , [ " brev"] , { stdio : "ignore" } ) ;
127124 } catch {
128125 console . error ( "brev CLI not found. Install: https://brev.nvidia.com" ) ;
129126 process . exit ( 1 ) ;
130127 }
131128
132129 let exists = false ;
133130 try {
134- const out = execSync ( "brev ls 2>&1" , { encoding : "utf-8" } ) ;
131+ const out = execFileSync ( "brev" , [ "ls" ] , { encoding : "utf-8" } ) ;
135132 exists = out . includes ( name ) ;
136- } catch { }
133+ } catch ( err ) {
134+ if ( err . stdout && err . stdout . includes ( name ) ) exists = true ;
135+ if ( err . stderr && err . stderr . includes ( name ) ) exists = true ;
136+ }
137137
138138 if ( ! exists ) {
139139 console . log ( ` Creating Brev instance '${ name } ' (${ gpu } )...` ) ;
140- run ( `brev create " ${ name } " --gpu " ${ gpu } " ` ) ;
140+ run ( `brev create ${ qname } --gpu ${ shellQuote ( gpu ) } ` ) ;
141141 } else {
142142 console . log ( ` Brev instance '${ name } ' already exists.` ) ;
143143 }
@@ -147,7 +147,7 @@ async function deploy(instanceName) {
147147 console . log ( " Waiting for SSH..." ) ;
148148 for ( let i = 0 ; i < 60 ; i ++ ) {
149149 try {
150- execSync ( ` ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no " ${ name } " ' echo ok' 2>/dev/null` , { encoding : "utf-8" , stdio : "pipe " } ) ;
150+ execFileSync ( " ssh" , [ "-o" , " ConnectTimeout=5" , "-o" , " StrictHostKeyChecking=no" , name , " echo" , "ok" ] , { encoding : "utf-8" , stdio : "ignore " } ) ;
151151 break ;
152152 } catch {
153153 if ( i === 59 ) {
@@ -159,35 +159,36 @@ async function deploy(instanceName) {
159159 }
160160
161161 console . log ( " Syncing NemoClaw to VM..." ) ;
162- run ( `ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR " ${ name } " 'mkdir -p /home/ubuntu/nemoclaw'` ) ;
163- run ( `rsync -az --delete --exclude node_modules --exclude .git --exclude src -e "ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR" "${ ROOT } /scripts" "${ ROOT } /Dockerfile" "${ ROOT } /nemoclaw" "${ ROOT } /nemoclaw-blueprint" "${ ROOT } /bin" "${ ROOT } /package.json" " ${ name } " :/home/ubuntu/nemoclaw/` ) ;
162+ run ( `ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR ${ qname } 'mkdir -p /home/ubuntu/nemoclaw'` ) ;
163+ run ( `rsync -az --delete --exclude node_modules --exclude .git --exclude src -e "ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR" "${ ROOT } /scripts" "${ ROOT } /Dockerfile" "${ ROOT } /nemoclaw" "${ ROOT } /nemoclaw-blueprint" "${ ROOT } /bin" "${ ROOT } /package.json" ${ qname } :/home/ubuntu/nemoclaw/` ) ;
164164
165- const envLines = [ `NVIDIA_API_KEY=${ process . env . NVIDIA_API_KEY } ` ] ;
165+ const envLines = [ `NVIDIA_API_KEY=${ shellQuote ( process . env . NVIDIA_API_KEY || "" ) } ` ] ;
166166 const ghToken = process . env . GITHUB_TOKEN ;
167- if ( ghToken ) envLines . push ( `GITHUB_TOKEN=${ ghToken } ` ) ;
167+ if ( ghToken ) envLines . push ( `GITHUB_TOKEN=${ shellQuote ( ghToken ) } ` ) ;
168168 const tgToken = getCredential ( "TELEGRAM_BOT_TOKEN" ) ;
169- if ( tgToken ) envLines . push ( `TELEGRAM_BOT_TOKEN=${ tgToken } ` ) ;
169+ if ( tgToken ) envLines . push ( `TELEGRAM_BOT_TOKEN=${ shellQuote ( tgToken ) } ` ) ;
170170 const envDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "nemoclaw-env-" ) ) ;
171- const envTmp = path . join ( envDir , ".env" ) ;
171+ const envTmp = path . join ( envDir , "env" ) ;
172+ fs . writeFileSync ( envTmp , envLines . join ( "\n" ) + "\n" , { mode : 0o600 } ) ;
172173 try {
173- fs . writeFileSync ( envTmp , envLines . join ( "\n" ) + "\n" , { mode : 0o600 , flag : "wx" } ) ;
174- run ( `scp -q -o StrictHostKeyChecking=no -o LogLevel=ERROR "${ envTmp } " "${ name } ":/home/ubuntu/nemoclaw/.env` ) ;
174+ run ( `scp -q -o StrictHostKeyChecking=no -o LogLevel=ERROR ${ shellQuote ( envTmp ) } ${ qname } :/home/ubuntu/nemoclaw/.env` ) ;
175175 } finally {
176- fs . rmSync ( envDir , { recursive : true , force : true } ) ;
176+ try { fs . unlinkSync ( envTmp ) ; } catch { }
177+ try { fs . rmdirSync ( envDir ) ; } catch { }
177178 }
178179
179180 console . log ( " Running setup..." ) ;
180- runInteractive ( `ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR " ${ name } " 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/brev-setup.sh'` ) ;
181+ runInteractive ( `ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR ${ qname } 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/brev-setup.sh'` ) ;
181182
182183 if ( tgToken ) {
183184 console . log ( " Starting services..." ) ;
184- run ( `ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR " ${ name } " 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/start-services.sh'` ) ;
185+ run ( `ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR ${ qname } 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/start-services.sh'` ) ;
185186 }
186187
187188 console . log ( "" ) ;
188189 console . log ( " Connecting to sandbox..." ) ;
189190 console . log ( "" ) ;
190- runInteractive ( `ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR " ${ name } " 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && openshell sandbox connect nemoclaw'` ) ;
191+ runInteractive ( `ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR ${ qname } 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && openshell sandbox connect nemoclaw'` ) ;
191192}
192193
193194async function start ( ) {
0 commit comments