@@ -5,7 +5,6 @@ require('../social/monkey/process');
55import arraybuffers = require( '../../arraybuffers/arraybuffers' ) ;
66import linefeeder = require( '../../net/linefeeder' ) ;
77import logging = require( '../../logging/logging' ) ;
8- import promises = require( '../../promises/promises' ) ;
98import queue = require( '../../handler/queue' ) ;
109
1110// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
@@ -63,92 +62,88 @@ class CloudInstaller {
6362 debug : undefined
6463 } ;
6564
66- let numAttempts = 0 ;
67- return promises . retryWithExponentialBackoff ( ( ) => {
68- log . debug ( 'connection attempt %1...' , ( ++ numAttempts ) ) ;
69- return new Promise < string > ( ( F , R ) => {
70- const connection = new Client ( ) ;
71- connection . on ( 'ready' , ( ) => {
72- log . debug ( 'logged into server' ) ;
73-
74- const stdoutRaw = new queue . Queue < ArrayBuffer , void > ( ) ;
75- const stdout = new linefeeder . LineFeeder ( stdoutRaw ) ;
76- stdout . setSyncHandler ( ( line : string ) => {
77- log . debug ( 'STDOUT: %1' , line ) ;
78- // Search for the URL anywhere in the line so we will
79- // continue to work in the face of minor changes
80- // to the install script.
81- if ( line . indexOf ( INVITATION_PREFIX ) === 0 ) {
82- const inviteJson = line . substring ( INVITATION_PREFIX . length ) ;
83- try {
84- F ( JSON . parse ( inviteJson ) ) ;
85- } catch ( e ) {
86- R ( {
87- message : 'could not parse invite: ' + inviteJson
88- } ) ;
89- }
90- } else if ( line . indexOf ( PROGRESS_PREFIX ) === 0 ) {
91- const tokens = line . split ( ' ' ) ;
92- if ( tokens . length < 2 ) {
93- log . warn ( 'could not parse progress update' ) ;
94- } else {
95- const progress = parseInt ( tokens [ 1 ] , 10 ) ;
96- this . dispatchEvent_ ( 'progress' , progress ) ;
97- }
98- } else if ( line . indexOf ( STATUS_PREFIX ) === 0 ) {
99- this . dispatchEvent_ ( 'status' , line ) ;
100- }
101- } ) ;
102-
103- const stderrRaw = new queue . Queue < ArrayBuffer , void > ( ) ;
104- const stderr = new linefeeder . LineFeeder ( stderrRaw ) ;
105- stderr . setSyncHandler ( ( line : string ) => {
106- log . error ( 'STDERR: %1' , line ) ;
107- } ) ;
108-
109- connection . exec ( INSTALL_COMMAND , ( e : Error , stream : ssh2 . Channel ) => {
110- if ( e ) {
111- connection . end ( ) ;
65+ return new Promise < string > ( ( F , R ) => {
66+ const connection = new Client ( ) ;
67+ connection . on ( 'ready' , ( ) => {
68+ log . debug ( 'logged into server' ) ;
69+
70+ const stdoutRaw = new queue . Queue < ArrayBuffer , void > ( ) ;
71+ const stdout = new linefeeder . LineFeeder ( stdoutRaw ) ;
72+ stdout . setSyncHandler ( ( line : string ) => {
73+ log . debug ( 'STDOUT: %1' , line ) ;
74+ // Search for the URL anywhere in the line so we will
75+ // continue to work in the face of minor changes
76+ // to the install script.
77+ if ( line . indexOf ( INVITATION_PREFIX ) === 0 ) {
78+ const inviteJson = line . substring ( INVITATION_PREFIX . length ) ;
79+ try {
80+ F ( JSON . parse ( inviteJson ) ) ;
81+ } catch ( e ) {
11282 R ( {
113- message : 'could not execute command : ' + e . message
83+ message : 'could not parse invite : ' + inviteJson
11484 } ) ;
115- return ;
11685 }
117- stream . on ( 'end' , ( ) => {
118- stdout . flush ( ) ;
119- stderr . flush ( ) ;
120- connection . end ( ) ;
121- R ( {
122- message : 'invitation URL not found'
123- } ) ;
124- } ) . on ( 'data' , ( data : Buffer ) => {
125- // Make a copy before passing to the async queue.
126- stdoutRaw . handle ( arraybuffers . bufferToArrayBuffer ( new Buffer ( data ) ) ) ;
127- } ) . stderr . on ( 'data' , ( data : Buffer ) => {
128- // Make a copy before passing to the async queue.
129- stderrRaw . handle ( arraybuffers . bufferToArrayBuffer ( new Buffer ( data ) ) ) ;
86+ } else if ( line . indexOf ( PROGRESS_PREFIX ) === 0 ) {
87+ const tokens = line . split ( ' ' ) ;
88+ if ( tokens . length < 2 ) {
89+ log . warn ( 'could not parse progress update' ) ;
90+ } else {
91+ const progress = parseInt ( tokens [ 1 ] , 10 ) ;
92+ this . dispatchEvent_ ( 'progress' , progress ) ;
93+ }
94+ } else if ( line . indexOf ( STATUS_PREFIX ) === 0 ) {
95+ this . dispatchEvent_ ( 'status' , line ) ;
96+ }
97+ } ) ;
98+
99+ const stderrRaw = new queue . Queue < ArrayBuffer , void > ( ) ;
100+ const stderr = new linefeeder . LineFeeder ( stderrRaw ) ;
101+ stderr . setSyncHandler ( ( line : string ) => {
102+ log . error ( 'STDERR: %1' , line ) ;
103+ } ) ;
104+
105+ connection . exec ( INSTALL_COMMAND , ( e : Error , stream : ssh2 . Channel ) => {
106+ if ( e ) {
107+ connection . end ( ) ;
108+ R ( {
109+ message : 'could not execute command: ' + e . message
130110 } ) ;
111+ return ;
112+ }
113+ stream . on ( 'end' , ( ) => {
114+ stdout . flush ( ) ;
115+ stderr . flush ( ) ;
116+ connection . end ( ) ;
117+ R ( {
118+ message : 'invitation URL not found'
119+ } ) ;
120+ } ) . on ( 'data' , ( data : Buffer ) => {
121+ // Make a copy before passing to the async queue.
122+ stdoutRaw . handle ( arraybuffers . bufferToArrayBuffer ( new Buffer ( data ) ) ) ;
123+ } ) . stderr . on ( 'data' , ( data : Buffer ) => {
124+ // Make a copy before passing to the async queue.
125+ stderrRaw . handle ( arraybuffers . bufferToArrayBuffer ( new Buffer ( data ) ) ) ;
131126 } ) ;
132- } ) . on ( 'error' , ( e : Error ) => {
133- // This occurs when:
134- // - user supplies the wrong username or password
135- // - host cannot be reached, e.g. non-existant hostname
136- R ( {
137- message : 'could not login: ' + e . message
138- } ) ;
139- } ) . on ( 'end' , ( ) => {
140- log . debug ( 'connection end') ;
141- R ( {
142- message : 'connection end without invitation URL'
143- } ) ;
144- } ) . on ( 'close' , ( hadError : boolean ) => {
145- log . debug ( 'connection close, with%1 error ', ( hadError ? '' : 'out' ) ) ;
146- R ( {
147- message : 'connection close without invitation URL'
148- } ) ;
149- } ) . connect ( connectConfig ) ;
150- } ) ;
151- } , MAX_CONNECTION_INTERVAL_MS , INITIAL_CONNECTION_INTERVAL_MS ) ;
127+ } ) ;
128+ } ) . on ( 'error' , ( e : Error ) => {
129+ // This occurs when:
130+ // - user supplies the wrong username or password
131+ // - host cannot be reached, e.g. non-existant hostname
132+ R ( {
133+ message : 'could not login: ' + e . message
134+ } ) ;
135+ } ) . on ( ' end', ( ) => {
136+ log . debug ( 'connection end' ) ;
137+ R ( {
138+ message : 'connection end without invitation URL'
139+ } ) ;
140+ } ) . on ( ' close', ( hadError : boolean ) => {
141+ log . debug ( 'connection close, with%1 error' , ( hadError ? '' : 'out' ) ) ;
142+ R ( {
143+ message : 'connection close without invitation URL'
144+ } ) ;
145+ } ) . connect ( connectConfig ) ;
146+ } ) ;
152147 }
153148}
154149
0 commit comments