@@ -31,29 +31,35 @@ public SshTool(UserOptions opts, LaunchBuilder launch)
3131
3232 public SshClient Ssh => _ssh ;
3333
34- public string Bash ( string command , bool createCommand = false )
34+ public async Task < string > BashAsync ( string command , bool createCommand = false )
3535 {
3636 try
3737 {
3838 if ( ! _isConnected )
3939 {
4040 // attempt to reconnect
41- if ( ! Connect ( ) )
41+ if ( ! await ConnectAsync ( ) )
4242 return "Cannot connect to remote host to execute Bash command." ;
4343 }
4444
4545 Logger . Output ( $ "BASH> { command } ") ;
4646
4747 SshCommand cmd ;
48- if ( ! createCommand )
49- {
50- cmd = _ssh . RunCommand ( command ) ;
51- }
52- else
48+
49+ cmd = await Task . Run ( ( ) =>
5350 {
54- cmd = _ssh . CreateCommand ( command ) ;
55- cmd . Execute ( ) ;
56- }
51+ if ( ! createCommand )
52+ {
53+ cmd = _ssh . RunCommand ( command ) ;
54+ }
55+ else
56+ {
57+ cmd = _ssh . CreateCommand ( command ) ;
58+ cmd . Execute ( ) ;
59+ }
60+
61+ return cmd ;
62+ } ) ;
5763
5864 return cmd . Result ;
5965 }
@@ -79,6 +85,7 @@ public string BashStream(string command, bool isSudo = false)
7985 var modes = new Dictionary < TerminalModes , uint > ( ) ;
8086 // modes.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53);
8187
88+ // TODO: Make async
8289 using ( var stream = _ssh . CreateShellStream ( "xterm" , 255 , 50 , 800 , 600 , 1024 , modes ) )
8390 {
8491 stream . Write ( command + "\n " ) ;
@@ -89,13 +96,13 @@ public string BashStream(string command, bool isSudo = false)
8996 }
9097 else
9198 {
92- Logger . Output ( $ "BASH-SUDO: TRUE") ;
99+ Logger . Debug ( $ "BASH-SUDO: TRUE") ;
93100 result = stream . Expect ( "password" ) ;
94- Logger . Output ( $ "BASH-SUDO: { result } ") ;
101+ Logger . Debug ( $ "BASH-SUDO: { result } ") ;
95102
96103 stream . Write ( _opts . UserPass + "\n " ) ;
97104 result = stream . Expect ( prompt ) ;
98- Logger . Output ( $ "BASH-SUDO: { result } ") ;
105+ Logger . Debug ( $ "BASH-SUDO: { result } ") ;
99106 }
100107 }
101108
@@ -124,34 +131,30 @@ public string BashStream(string command, string searchText)
124131
125132 result = stream . ReadLine ( ) ;
126133
127- Logger . Output ( $ "BashEx: Waiting for search text, '{ searchText } '") ;
134+ Logger . Debug ( $ "BashEx: Waiting for search text, '{ searchText } '") ;
128135 result = stream . Expect ( searchText ) ;
129- Logger . Output ( $ "BashEx: { result } ") ;
136+ Logger . Debug ( $ "BashEx: { result } ") ;
130137 }
131138
132139 return result ;
133140 }
134141
135142 /// <summary>Cleans the contents of the deployment path.</summary>
136143 /// <param name="fullScrub">Clear entire base deployment folder (TRUE) or just our project.</param>
137- public void CleanDeploymentFolder ( bool fullScrub = false )
144+ public async Task CleanDeploymentFolderAsync ( bool fullScrub = false )
138145 {
139146 // Whole deployment folder and hidden files
140147 // rm -rf xxx/* == Contents of the folder but not the folder itself
141148 // rm -rf xxx/{*,.*} == All hidden files and folders
142149 var filesAndFolders = "{*,.*}" ;
143150
144151 if ( fullScrub )
145- {
146- Bash ( $ "rm -rf \" { _opts . RemoteDeployBasePath } /{ filesAndFolders } \" ") ;
147- }
152+ await BashAsync ( $ "rm -rf \" { _opts . RemoteDeployBasePath } /{ filesAndFolders } \" ") ;
148153 else
149- {
150- Bash ( $ "rm -rf { _launch . RemoteDeployProjectFolder } /{ filesAndFolders } ") ;
151- }
154+ await BashAsync ( $ "rm -rf { _launch . RemoteDeployProjectFolder } /{ filesAndFolders } ") ;
152155 }
153156
154- public bool Connect ( )
157+ public async Task < bool > ConnectAsync ( )
155158 {
156159 PrivateKeyFile keyFile = null ;
157160
@@ -178,7 +181,7 @@ public bool Connect()
178181 ? new SshClient ( _opts . HostIp , _opts . HostPort , _opts . UserName , keyFile )
179182 : new SshClient ( _opts . HostIp , _opts . HostPort , _opts . UserName , _opts . UserPass ) ;
180183
181- _ssh . Connect ( ) ;
184+ await Task . Run ( ( ) => _ssh . Connect ( ) ) ;
182185 }
183186 catch ( Exception ex )
184187 {
@@ -222,10 +225,10 @@ public void Dispose()
222225 _scp ? . Dispose ( ) ;
223226 }
224227
225- public void MakeDeploymentFolder ( )
228+ public async Task MakeDeploymentFolderAsync ( )
226229 {
227230 // do we need SUDO?
228- Bash ( $ "mkdir -p { _opts . RemoteDeployBasePath } ") ;
231+ await BashAsync ( $ "mkdir -p { _opts . RemoteDeployBasePath } ") ;
229232 //// Bash($"mkdir -p {_opts.RemoteDeployDebugPath}");
230233 //// Bash($"mkdir -p {_opts.RemoteDeployReleasePath}");
231234
@@ -234,17 +237,17 @@ public void MakeDeploymentFolder()
234237 : $ ":{ _opts . UserGroupName } ";
235238
236239 // Update ownership so it's not "root"
237- Bash ( $ "sudo chown -R { _opts . UserName } { group } { _opts . RemoteDeployBasePath } ") ;
240+ await BashAsync ( $ "sudo chown -R { _opts . UserName } { group } { _opts . RemoteDeployBasePath } ") ;
238241 }
239242
240243 /// <summary>
241- /// Instals VS Debugger if it doesn't exist already
244+ /// Install cURL and VS Debugger if it doesn't exist already.
242245 /// </summary>
243- public void TryInstallVsDbg ( )
246+ public async Task TryInstallVsDbgAsync ( )
244247 {
245- string arch = Bash ( "uname -m" ) . Trim ( '\n ' ) ;
248+ string arch = ( await BashAsync ( "uname -m" ) ) . Trim ( '\n ' ) ;
246249
247- var curlExists = Bash ( "which curl ; echo $?" ) ;
250+ var curlExists = await BashAsync ( "which curl ; echo $?" ) ;
248251 if ( curlExists . Equals ( "1\n " ) )
249252 {
250253 // TODO: Need to pass in password.
@@ -253,7 +256,7 @@ public void TryInstallVsDbg()
253256 }
254257
255258 //// OLD: var ret = Bash("[ -d ~/.vsdbg ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/.vsdbg");
256- var ret = Bash ( $ "[ -d { _opts . RemoteVsDbgBasePath } ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l { _opts . RemoteVsDbgBasePath } ") ;
259+ var ret = await BashAsync ( $ "[ -d { _opts . RemoteVsDbgBasePath } ] || curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l { _opts . RemoteVsDbgBasePath } ") ;
257260 Logger . Output ( $ "Returned: { ret } ") ;
258261 }
259262
@@ -282,7 +285,7 @@ public async Task<string> UploadFilesAsync()
282285 // TODO: Rev3 - Allow for both SFTP and SCP as a backup. This separating connection to a new disposable class.
283286 //// Logger.Output($"Connected to {_connectionInfo.Username}@{_connectionInfo.Host}:{_connectionInfo.Port} via SSH and {(_sftpClient != null ? "SFTP" : "SCP")}");
284287
285- Bash ( $@ "mkdir -p { _launch . RemoteDeployProjectFolder } ") ;
288+ BashAsync ( $@ "mkdir -p { _launch . RemoteDeployProjectFolder } ") ;
286289
287290 var srcDirInfo = new DirectoryInfo ( _launch . OutputDirFullPath ) ;
288291 if ( ! srcDirInfo . Exists )
@@ -497,9 +500,10 @@ await Task.Run(() =>
497500 /// <returns>Returns true on success.</returns>
498501 private async Task < bool > PayloadDecompressAsync ( string pathBuildTarGz , bool removeTarGz = true )
499502 {
503+ var decompressOutput = string . Empty ;
504+
500505 try
501506 {
502- var decompressOutput = string . Empty ;
503507
504508 var cmd = "set -e" ;
505509 cmd += $ ";cd \" { _launch . RemoteDeployProjectFolder } \" ";
@@ -509,19 +513,16 @@ private async Task<bool> PayloadDecompressAsync(string pathBuildTarGz, bool remo
509513 if ( removeTarGz )
510514 cmd += $ ";rm \" { pathBuildTarGz } \" ";
511515
512- await Task . Run ( ( ) =>
513- {
514- decompressOutput = Bash ( cmd ) ;
515- } ) ;
516+ decompressOutput = await BashAsync ( cmd ) ;
516517
517518 Logger . Output ( $ "Payload Decompress results: '{ decompressOutput } ' (blank=OK)") ;
518-
519- return true ;
520519 }
521520 catch ( Exception )
522521 {
523522 return false ;
524523 }
524+
525+ return true ;
525526 }
526527 }
527528}
0 commit comments