1
1
import {
2
+ assertExists ,
2
3
captureExec ,
3
4
cyan ,
4
5
delay ,
@@ -14,7 +15,7 @@ import {
14
15
validate ,
15
16
} from "./deps.ts" ;
16
17
import { InstanceConfig , InstanceState , MultipassInfo } from "./types.ts" ;
17
- import { getSshIp , log , print } from "./utils.ts" ;
18
+ import { getSshIp , log , ok , print } from "./utils.ts" ;
18
19
19
20
export const multipassBin = memoizePromise ( ( ) => locateMultipassBin ( ) ) ;
20
21
@@ -293,15 +294,45 @@ export async function multipassPostStart(
293
294
instance : InstanceConfig ,
294
295
) : Promise < string > {
295
296
const { name, sshDirectoryPath } = instance ;
296
- const { state, ipv4 } = await multipassInfo ( { name } ) ;
297
+ const { state } = await multipassInfo ( { name } ) ;
297
298
298
299
if ( state !== InstanceState . Running ) {
299
300
throw new Error (
300
301
`Instance '${ instance . name } ' is not in 'Running' state. Current state is '${ state } '` ,
301
302
) ;
302
303
}
303
304
304
- const ip = getSshIp ( ipv4 , instance . filterSshIpByCidr ) ;
305
+ let ip : string | undefined = undefined ;
306
+
307
+ if ( instance . filterSshIpByCidr ) {
308
+ const maxAttempts = 30 ;
309
+ for ( let i = 0 ; i < maxAttempts ; i ++ ) {
310
+ let ipv4 : string [ ] = [ ] ;
311
+
312
+ try {
313
+ ipv4 = ( await multipassInfo ( { name } ) ) . ipv4 ;
314
+ ip = getSshIp ( ipv4 , instance . filterSshIpByCidr ) ;
315
+ } catch ( e ) {
316
+ if ( i === maxAttempts - 1 ) {
317
+ throw e ;
318
+ }
319
+
320
+ log (
321
+ "Waiting for the instance's IP that matches the CIDR filter:" ,
322
+ instance . filterSshIpByCidr ,
323
+ "So far got:" ,
324
+ ipv4 . length > 0 ? ipv4 . join ( ", " ) : "none" ,
325
+ ) ;
326
+ await delay ( 1000 ) ;
327
+ }
328
+ }
329
+ } else {
330
+ ip = getSshIp ( ( await multipassInfo ( { name } ) ) . ipv4 ) ;
331
+ }
332
+
333
+ assertExists ( ip ) ;
334
+
335
+ ok ( "Got instance IP" , ip ) ;
305
336
306
337
await multipassResolveClusterLocalDns ( { ip, instance } ) ;
307
338
@@ -444,6 +475,8 @@ export async function multipassRoute(
444
475
`'New-NetRoute -DestinationPrefix ${ cidr } -InterfaceAlias "vEthernet (Default Switch)" -NextHop ${ ip } '` ,
445
476
] ,
446
477
stdin : { inherit : true } ,
478
+ stdout : { read : printOutLines ( ( line ) => `${ gray ( "[$ New-NetRoute ]" ) } ${ line } ` ) } ,
479
+ stderr : { read : printErrLines ( ( line ) => `${ gray ( "[$ New-NetRoute ]" ) } ${ line } ` ) } ,
447
480
} ) ;
448
481
}
449
482
@@ -454,25 +487,33 @@ export async function multipassRoute(
454
487
`'Add-DnsClientNrptRule -Namespace ".svc.${ clusterDomain } " -DnsSecEnable -NameServers "${ clusterDnsIp } "'` ,
455
488
] ,
456
489
stdin : { inherit : true } ,
490
+ stdout : { read : printOutLines ( ( line ) => `${ gray ( "[$ Add-DnsClientNrptRule ]" ) } ${ line } ` ) } ,
491
+ stderr : { read : printErrLines ( ( line ) => `${ gray ( "[$ Add-DnsClientNrptRule ]" ) } ${ line } ` ) } ,
457
492
} ) ;
458
493
} else {
459
494
log ( "Adding routes, will require root permissions..." ) ;
460
495
for ( const cidr of [ clusterCidr , serviceCidr ] ) {
461
496
await inheritExec ( {
462
497
cmd : [ "sudo" , "/sbin/route" , "add" , "-net" , cidr , ip ] ,
463
498
stdin : { inherit : true } ,
499
+ stdout : { read : printOutLines ( ( line ) => `${ gray ( "[$ route ]" ) } ${ line } ` ) } ,
500
+ stderr : { read : printErrLines ( ( line ) => `${ gray ( "[$ route ]" ) } ${ line } ` ) } ,
464
501
} ) ;
465
502
}
466
503
467
504
await inheritExec ( {
468
505
cmd : [ "sudo" , "mkdir" , "-p" , "/etc/resolver" ] ,
506
+ stdout : { read : printOutLines ( ( line ) => `${ gray ( "[$ resolver ]" ) } ${ line } ` ) } ,
507
+ stderr : { read : printErrLines ( ( line ) => `${ gray ( "[$ resolver ]" ) } ${ line } ` ) } ,
469
508
} ) ;
470
509
471
510
await inheritExec ( {
472
511
cmd : [ "sudo" , "tee" , `/etc/resolver/svc.${ clusterDomain } ` ] ,
473
512
stdin : {
474
513
pipe : [ `domain svc.${ clusterDomain } ` , `nameserver ${ clusterDnsIp } ` , "search_order 1" , "" ] . join ( "\n" ) ,
475
514
} ,
515
+ stdout : { read : printOutLines ( ( line ) => `${ gray ( "[$ resolver ]" ) } ${ line } ` ) } ,
516
+ stderr : { read : printErrLines ( ( line ) => `${ gray ( "[$ resolver ]" ) } ${ line } ` ) } ,
476
517
} ) ;
477
518
}
478
519
}
0 commit comments