sshClientUtility.ts 6.23 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5

'use strict';

6
import * as assert from 'assert';
Deshui Yu's avatar
Deshui Yu committed
7
import * as cpp from 'child-process-promise';
8
import * as os from 'os';
9
import * as path from 'path';
Deshui Yu's avatar
Deshui Yu committed
10
import { Client, ClientChannel, SFTPWrapper } from 'ssh2';
11
import * as stream from 'stream';
Deshui Yu's avatar
Deshui Yu committed
12
13
import { Deferred } from 'ts-deferred';
import { NNIError, NNIErrorNames } from '../../common/errors';
chicm-ms's avatar
chicm-ms committed
14
import { getLogger, Logger } from '../../common/log';
15
import { getRemoteTmpDir, uniqueString, unixPathJoin } from '../../common/utils';
16
import { execRemove, tarAdd } from '../common/util';
17
import { RemoteCommandResult } from './remoteMachineData';
Deshui Yu's avatar
Deshui Yu committed
18
19
20
21
22
23
24
25
26
27
28
29
30

/**
 *
 * Utility for frequent operations towards SSH client
 *
 */
export namespace SSHClientUtility {
    /**
     * Copy files and directories in local directory recursively to remote directory
     * @param localDirectory local diretory
     * @param remoteDirectory remote directory
     * @param sshClient SSH client
     */
31
32
    export async function copyDirectoryToRemote(localDirectory : string, remoteDirectory : string, sshClient : Client, remoteOS: string)
     : Promise<void> {
Deshui Yu's avatar
Deshui Yu committed
33
        const deferred: Deferred<void> = new Deferred<void>();
34
35
        const tmpTarName: string = `${uniqueString(10)}.tar.gz`;
        const localTarPath: string = path.join(os.tmpdir(), tmpTarName);
36
        const remoteTarPath: string = unixPathJoin(getRemoteTmpDir(remoteOS), tmpTarName);
Deshui Yu's avatar
Deshui Yu committed
37
38

        // Compress files in local directory to experiment root directory
39
        await tarAdd(localTarPath, localDirectory);
Deshui Yu's avatar
Deshui Yu committed
40
        // Copy the compressed file to remoteDirectory and delete it
41
        await copyFileToRemote(localTarPath, remoteTarPath, sshClient);
42
        await execRemove(localTarPath);
Deshui Yu's avatar
Deshui Yu committed
43
        // Decompress the remote compressed file in and delete it
44
45
        await remoteExeCommand(`tar -oxzf ${remoteTarPath} -C ${remoteDirectory}`, sshClient);
        await remoteExeCommand(`rm ${remoteTarPath}`, sshClient);
Deshui Yu's avatar
Deshui Yu committed
46
47
48
49
50
51
52
53
54
55
56
        deferred.resolve();

        return deferred.promise;
    }

    /**
     * Copy local file to remote path
     * @param localFilePath the path of local file
     * @param remoteFilePath the target path in remote machine
     * @param sshClient SSH Client
     */
57
    export function copyFileToRemote(localFilePath : string, remoteFilePath : string, sshClient : Client) : Promise<boolean> {
chicm-ms's avatar
chicm-ms committed
58
59
        const log: Logger = getLogger();
        log.debug(`copyFileToRemote: localFilePath: ${localFilePath}, remoteFilePath: ${remoteFilePath}`);
60
61
        assert(sshClient !== undefined);
        const deferred: Deferred<boolean> = new Deferred<boolean>();
Deshui Yu's avatar
Deshui Yu committed
62
        sshClient.sftp((err : Error, sftp : SFTPWrapper) => {
63
            if (err !== undefined && err !== null) {
chicm-ms's avatar
chicm-ms committed
64
                log.error(`copyFileToRemote: ${err.message}, ${localFilePath}, ${remoteFilePath}`);
65
66
67
                deferred.reject(err);

                return;
Deshui Yu's avatar
Deshui Yu committed
68
            }
69
            assert(sftp !== undefined);
Deshui Yu's avatar
Deshui Yu committed
70
71
            sftp.fastPut(localFilePath, remoteFilePath, (fastPutErr : Error) => {
                sftp.end();
72
                if (fastPutErr !== undefined && fastPutErr !== null) {
73
                    deferred.reject(fastPutErr);
Deshui Yu's avatar
Deshui Yu committed
74
                } else {
75
                    deferred.resolve(true);
Deshui Yu's avatar
Deshui Yu committed
76
77
78
79
80
81
82
83
84
85
86
87
                }
            });
        });

        return deferred.promise;
    }

    /**
     * Execute command on remote machine
     * @param command the command to execute remotely
     * @param client SSH Client
     */
88
    // tslint:disable:no-unsafe-any no-any
Deshui Yu's avatar
Deshui Yu committed
89
    export function remoteExeCommand(command : string, client : Client): Promise<RemoteCommandResult> {
chicm-ms's avatar
chicm-ms committed
90
91
        const log: Logger = getLogger();
        log.debug(`remoteExeCommand: command: [${command}]`);
Deshui Yu's avatar
Deshui Yu committed
92
93
94
95
96
97
        const deferred : Deferred<RemoteCommandResult> = new Deferred<RemoteCommandResult>();
        let stdout: string = '';
        let stderr: string = '';
        let exitCode : number;

        client.exec(command, (err : Error, channel : ClientChannel) => {
98
            if (err !== undefined && err !== null) {
chicm-ms's avatar
chicm-ms committed
99
                log.error(`remoteExeCommand: ${err.message}`);
Deshui Yu's avatar
Deshui Yu committed
100
                deferred.reject(err);
101
102

                return;
Deshui Yu's avatar
Deshui Yu committed
103
104
            }

105
            channel.on('data', (data : any, dataStderr : any) => {
106
                if (dataStderr !== undefined && dataStderr !== null) {
Deshui Yu's avatar
Deshui Yu committed
107
                    stderr += data.toString();
108
                } else {
Deshui Yu's avatar
Deshui Yu committed
109
110
                    stdout += data.toString();
                }
111
112
113
            })
              .on('exit', (code : any, signal : any) => {
                exitCode = <number>code;
Deshui Yu's avatar
Deshui Yu committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                deferred.resolve({
                    stdout : stdout,
                    stderr : stderr,
                    exitCode : exitCode
                });
            });
        });

        return deferred.promise;
    }

    export function getRemoteFileContent(filePath: string, sshClient: Client): Promise<string> {
        const deferred: Deferred<string> = new Deferred<string>();
        sshClient.sftp((err: Error, sftp : SFTPWrapper) => {
128
129
130
            if (err !== undefined && err !== null) {
                getLogger()
                  .error(`getRemoteFileContent: ${err.message}`);
Deshui Yu's avatar
Deshui Yu committed
131
                deferred.reject(new Error(`SFTP error: ${err.message}`));
132
133

                return;
Deshui Yu's avatar
Deshui Yu committed
134
135
136
137
138
139
140
            }
            try {
                const sftpStream : stream.Readable = sftp.createReadStream(filePath);

                let dataBuffer: string = '';
                sftpStream.on('data', (data : Buffer | string) => {
                    dataBuffer += data;
141
142
                })
                  .on('error', (streamErr: Error) => {
143
                    sftp.end();
Deshui Yu's avatar
Deshui Yu committed
144
                    deferred.reject(new NNIError(NNIErrorNames.NOT_FOUND, streamErr.message));
145
146
                })
                  .on('end', () => {
147
148
                    // sftp connection need to be released manually once operation is done
                    sftp.end();
Deshui Yu's avatar
Deshui Yu committed
149
150
151
                    deferred.resolve(dataBuffer);
                });
            } catch (error) {
152
153
                getLogger()
                  .error(`getRemoteFileContent: ${error.message}`);
154
                sftp.end();
Deshui Yu's avatar
Deshui Yu committed
155
156
157
158
159
160
                deferred.reject(new Error(`SFTP error: ${error.message}`));
            }
        });

        return deferred.promise;
    }
161
    // tslint:enable:no-unsafe-any no-any
Deshui Yu's avatar
Deshui Yu committed
162
}