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

/**
 *
 * Utility for frequent operations towards SSH client
 *
 */
export namespace SSHClientUtility {
    /**
     * 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
     */
chicm-ms's avatar
chicm-ms committed
30
    export function copyFileToRemote(localFilePath: string, remoteFilePath: string, sshClient: Client): Promise<boolean> {
chicm-ms's avatar
chicm-ms committed
31
32
        const log: Logger = getLogger();
        log.debug(`copyFileToRemote: localFilePath: ${localFilePath}, remoteFilePath: ${remoteFilePath}`);
33
34
        assert(sshClient !== undefined);
        const deferred: Deferred<boolean> = new Deferred<boolean>();
chicm-ms's avatar
chicm-ms committed
35
        sshClient.sftp((err: Error, sftp: SFTPWrapper) => {
36
            if (err !== undefined && err !== null) {
chicm-ms's avatar
chicm-ms committed
37
                log.error(`copyFileToRemote: ${err.message}, ${localFilePath}, ${remoteFilePath}`);
38
39
40
                deferred.reject(err);

                return;
Deshui Yu's avatar
Deshui Yu committed
41
            }
42
            assert(sftp !== undefined);
chicm-ms's avatar
chicm-ms committed
43
            sftp.fastPut(localFilePath, remoteFilePath, (fastPutErr: Error) => {
Deshui Yu's avatar
Deshui Yu committed
44
                sftp.end();
45
                if (fastPutErr !== undefined && fastPutErr !== null) {
46
                    deferred.reject(fastPutErr);
Deshui Yu's avatar
Deshui Yu committed
47
                } else {
48
                    deferred.resolve(true);
Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55
56
57
58
59
60
                }
            });
        });

        return deferred.promise;
    }

    /**
     * Execute command on remote machine
     * @param command the command to execute remotely
     * @param client SSH Client
     */
61
    export function remoteExeCommand(command: string, client: Client, useShell: boolean = false): Promise<RemoteCommandResult> {
chicm-ms's avatar
chicm-ms committed
62
63
        const log: Logger = getLogger();
        log.debug(`remoteExeCommand: command: [${command}]`);
chicm-ms's avatar
chicm-ms committed
64
        const deferred: Deferred<RemoteCommandResult> = new Deferred<RemoteCommandResult>();
Deshui Yu's avatar
Deshui Yu committed
65
66
        let stdout: string = '';
        let stderr: string = '';
chicm-ms's avatar
chicm-ms committed
67
        let exitCode: number;
Deshui Yu's avatar
Deshui Yu committed
68

69
        const callback = (err: Error, channel: ClientChannel): void => {
70
            if (err !== undefined && err !== null) {
chicm-ms's avatar
chicm-ms committed
71
                log.error(`remoteExeCommand: ${err.message}`);
Deshui Yu's avatar
Deshui Yu committed
72
                deferred.reject(err);
73
                return;
Deshui Yu's avatar
Deshui Yu committed
74
75
            }

76
77
78
79
            channel.on('data', (data: any) => {
                stdout += data;
            });
            channel.on('exit', (code: any) => {
80
                exitCode = <number>code;
81
                log.debug(`remoteExeCommand exit(${exitCode})\nstdout: ${stdout}\nstderr: ${stderr}`);
Deshui Yu's avatar
Deshui Yu committed
82
                deferred.resolve({
83
84
85
                    stdout: stdout,
                    stderr: stderr,
                    exitCode: exitCode
Deshui Yu's avatar
Deshui Yu committed
86
87
                });
            });
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            channel.stderr.on('data', function (data) {
                stderr += data;
            });

            if (useShell) {
                channel.stdin.write(`${command}\n`);
                channel.end("exit\n");
            }

            return;
        };

        if (useShell) {
            client.shell(callback);
        } else {
            client.exec(command, callback);
        }
Deshui Yu's avatar
Deshui Yu committed
105
106
107
108

        return deferred.promise;
    }

chicm-ms's avatar
chicm-ms committed
109
110
111
112
113
114
115
    /**
     * Copy files and directories in local directory recursively to remote directory
     * @param localDirectory local diretory
     * @param remoteDirectory remote directory
     * @param sshClient SSH client
     */
    export async function copyDirectoryToRemote(localDirectory: string, remoteDirectory: string, sshClient: Client, remoteOS: string): Promise<void> {
116
117
118
        const tmpSuffix: string = uniqueString(5);
        const localTarPath: string = path.join(os.tmpdir(), `nni_tmp_local_${tmpSuffix}.tar.gz`);
        const remoteTarPath: string = unixPathJoin(getRemoteTmpDir(remoteOS), `nni_tmp_remote_${tmpSuffix}.tar.gz`);
chicm-ms's avatar
chicm-ms committed
119
120
121
122
123
124
125
126
127
128
129

        // Compress files in local directory to experiment root directory
        await tarAdd(localTarPath, localDirectory);
        // Copy the compressed file to remoteDirectory and delete it
        await copyFileToRemote(localTarPath, remoteTarPath, sshClient);
        await execRemove(localTarPath);
        // Decompress the remote compressed file in and delete it
        await remoteExeCommand(`tar -oxzf ${remoteTarPath} -C ${remoteDirectory}`, sshClient);
        await remoteExeCommand(`rm ${remoteTarPath}`, sshClient);
    }

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

                return;
Deshui Yu's avatar
Deshui Yu committed
139
140
            }
            try {
chicm-ms's avatar
chicm-ms committed
141
                const sftpStream: stream.Readable = sftp.createReadStream(filePath);
Deshui Yu's avatar
Deshui Yu committed
142
143

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

        return deferred.promise;
    }
}