@@ -20,7 +20,7 @@ import { nodePath } from "./mounts";
2020import { isValidUUID } from "@cocalc/util/misc" ;
2121import { ensureConfFilesExists , setupDataPath , writeSecretToken } from "./util" ;
2222import { getEnvironment } from "./env" ;
23- import { mkdir } from "fs/promises" ;
23+ import { mkdir , readFile } from "node: fs/promises" ;
2424import { spawn } from "node:child_process" ;
2525import { getCoCalcMounts , COCALC_SRC } from "./mounts" ;
2626import { setQuota } from "./filesystem" ;
@@ -76,27 +76,31 @@ export async function start({
7676
7777 const home = await localPath ( { project_id } ) ;
7878 logger . debug ( "start: got home" , { project_id, home } ) ;
79+ const mounts = getCoCalcMounts ( ) ;
80+ const image = getImage ( config ) ;
81+ await initSshKeys ( { home, sshServers : await sshServers ?.( { project_id } ) } ) ;
82+
83+ const env = await getEnvironment ( {
84+ project_id,
85+ env : config ?. env ,
86+ HOME : "/root" ,
87+ image,
88+ } ) ;
89+ await startSidecar ( { project_id, home, mounts, env, pod } ) ;
90+
7991 const rootfs = await rootFilesystem . mount ( { project_id, home, config } ) ;
8092 logger . debug ( "start: got rootfs" , { project_id, rootfs } ) ;
8193 await mkdir ( home , { recursive : true } ) ;
8294 logger . debug ( "start: created home" , { project_id } ) ;
8395 await ensureConfFilesExists ( home ) ;
8496 logger . debug ( "start: created conf files" , { project_id } ) ;
85- const image = getImage ( config ) ;
8697
8798 await writeMutagenConfig ( {
8899 home,
89100 sync : config ?. sync ,
90101 forward : config ?. forward ,
91102 } ) ;
92- await initSshKeys ( { home, sshServers : await sshServers ?.( { project_id } ) } ) ;
93103
94- const env = await getEnvironment ( {
95- project_id,
96- env : config ?. env ,
97- HOME : "/root" ,
98- image,
99- } ) ;
100104 await setupDataPath ( home ) ;
101105 logger . debug ( "start: setup data path" , { project_id } ) ;
102106 if ( config ?. secret ) {
@@ -124,7 +128,6 @@ export async function start({
124128 args . push ( "--hostname" , `project-${ project_id } ` ) ;
125129 args . push ( "--name" , `project-${ project_id } ` ) ;
126130
127- const mounts = getCoCalcMounts ( ) ;
128131 for ( const path in mounts ) {
129132 args . push ( "-v" , `${ path } :${ mounts [ path ] } :ro` ) ;
130133 }
@@ -154,10 +157,14 @@ export async function start({
154157 child . stderr . on ( "data" , ( chunk : Buffer ) => {
155158 logger . debug ( `project_id=${ project_id } .stderr: ` , chunk . toString ( ) ) ;
156159 } ) ;
160+ }
157161
162+ async function startSidecar ( { project_id, mounts, env, pod, home } ) {
163+ // sidecar: refactor
164+ const sidecarPodName = `sidecar-${ project_id } ` ;
158165 const args2 = [
159166 "run" ,
160- `--name=sidecar- ${ project_id } ` ,
167+ `--name=${ sidecarPodName } ` ,
161168 "--detach" ,
162169 "--rm" ,
163170 "--pod" ,
@@ -172,11 +179,21 @@ export async function start({
172179 args2 . push ( "-e" , `${ name } =${ env [ name ] } ` ) ;
173180 }
174181 args2 . push ( sidecarImageName , "mutagen" , "daemon" , "run" ) ;
175- // TODO: we don't block the startup for this, but we do
176- // need to check and provide info to the user about what happened.
177- ( async ( ) => {
178- await podman ( args2 ) ;
179- } ) ( ) ;
182+ await podman ( args2 ) ;
183+ try {
184+ await podman ( [
185+ "exec" ,
186+ sidecarPodName ,
187+ "rsync" ,
188+ "-axH" ,
189+ "--exclude=.mutagen" ,
190+ "--delete" ,
191+ "file-server:/root/" ,
192+ "/root/" ,
193+ ] ) ;
194+ } catch ( err ) {
195+ console . log ( err ) ;
196+ }
180197}
181198
182199export async function stop ( {
@@ -265,13 +282,18 @@ async function state(project_id): Promise<ProjectState> {
265282 return status == "running" ? "running" : "opened" ;
266283}
267284
268- export async function status ( { project_id } ) {
285+ export async function status ( { project_id, localPath } ) {
269286 if ( ! isValidUUID ( project_id ) ) {
270287 throw Error ( "status: project_id must be valid" ) ;
271288 }
272289 logger . debug ( "status" , { project_id } ) ;
273- // TODO
274- return { state : await state ( project_id ) , ip : "127.0.0.1" } ;
290+ const s = await state ( project_id ) ;
291+ let publicKey : string | undefined = undefined ;
292+ const home = await localPath ( { project_id } ) ;
293+ try {
294+ publicKey = await readFile ( join ( home , ".ssh" , "id_ed25519.pub" ) , "utf8" ) ;
295+ } catch { }
296+ return { state : s , ip : "127.0.0.1" , publicKey } ;
275297}
276298
277299export async function close ( ) {
0 commit comments