@@ -23,12 +23,12 @@ export interface SpawnedProcessOutput {
2323}
2424
2525export interface ProcessOutput {
26- type : 'data' | 'exit' | 'container' ;
26+ type : 'data' | 'exit' | 'container' | 'exposedPort' ;
2727 data : string ;
2828}
2929
3030export function startBuildProcess ( buildId : number , jobId : number ,
31- commands : string [ ] , image : string ) : Observable < ProcessOutput > {
31+ commands : string [ ] , image : string , ssh = false ) : Observable < ProcessOutput > {
3232 return new Observable ( observer => {
3333 const name = 'abstruse_' + buildId + '_' + jobId ;
3434 const vars = commands . filter ( cmd => cmd . startsWith ( 'export' ) )
@@ -39,6 +39,8 @@ export function startBuildProcess(buildId: number, jobId: number,
3939 commands = commands . filter ( cmd => ! cmd . startsWith ( 'export' ) ) ;
4040
4141 startContainer ( name , image , vars )
42+ . concat ( ssh ? executeInContainer ( name , 'sudo /etc/init.d/ssh start' ) : null )
43+ . concat ( ssh ? getContainerExposedPort ( name , 22 ) : null )
4244 . concat ( ...commands . map ( command => executeInContainer ( name , command ) ) )
4345 . subscribe ( ( event : ProcessOutput ) => {
4446 observer . next ( event ) ;
@@ -100,7 +102,7 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
100102
101103function startContainer ( name : string , image : string , vars = [ ] ) : Observable < ProcessOutput > {
102104 return new Observable ( observer => {
103- const args = [ 'run' , '--privileged' , '-dit' ] . concat ( vars ) . concat ( '--name' , name , image ) ;
105+ const args = [ 'run' , '--privileged' , '-dit' , '-P' ] . concat ( vars ) . concat ( '--name' , name , image ) ;
104106 const process = nodePty . spawn ( 'docker' , args ) ;
105107
106108 process . on ( 'exit' , exitCode => {
@@ -137,6 +139,19 @@ function stopContainer(name: string): Observable<ProcessOutput> {
137139 } ) ;
138140}
139141
142+ function getContainerExposedPort ( name : string , port : number ) : Observable < ProcessOutput > {
143+ return new Observable ( observer => {
144+ const process = nodePty . spawn ( 'docker' , [
145+ 'port' ,
146+ name ,
147+ port
148+ ] ) ;
149+
150+ process . on ( 'data' , data => observer . next ( { type : 'exposedPort' , data : data . split ( ':' ) [ 1 ] } ) ) ;
151+ process . on ( 'exit' , ( ) => observer . complete ( ) ) ;
152+ } ) ;
153+ }
154+
140155export function startDockerImageSetupJob ( name : string ) : Job {
141156 let pty = new PtyInstance ( ) ;
142157 let job : Job = {
0 commit comments