summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/common/ssh.py
blob: 1f7eddc3f7388dbc336d7b8ea9c5362e5b3eaee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'''
Created on 2015-7-23

@author: y00228926
'''
import os
import logging
from stat import S_ISDIR
import Queue
import shutil
import paramiko
from paramiko.ssh_exception import AuthenticationException

LOG = logging.getLogger(__name__)


class SSHClientContext(paramiko.SSHClient):
    def __init__(self, ip, user, passwd, port=22):
        self.host = ip
        self.user = user
        self.passwd = passwd
        self.port = port
        super(SSHClientContext, self).__init__()

    def sync_exec_command(self, cmd):
        _, stdout, stderr = self.exec_command(cmd)
        ret = stdout.channel.recv_exit_status()
        out = stdout.read().strip()
        err = stderr.read().strip()
        LOG.info("in %s,%s,return:%s,output:%s:error:%s" % (self.host, cmd, ret, out, err))
        return ret, out, err

    def connect(self):
        super(SSHClientContext, self).connect(self.host, self.port, self.user, self.passwd, timeout=10)

    def __enter__(self):
        self.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()
        if exc_type == AuthenticationException:
            return False


class SFTPClientContext(object):
    def __init__(self, ip, user, passwd, port=22):
        self.host = ip
        self.passwd = passwd
        self.user = user
        self.port = port

    def connect(self):
        self.t = paramiko.Transport((self.host, self.port))
        self.t.connect(username=self.user, password=self.passwd)
        self.sftp = paramiko.SFTPClient.from_transport(self.t)

    def get(self, remote, local):
        self.sftp.get(remote, local)

    def put(self, local, remote):
        self.sftp.put(local, remote)

    def mkdir(self, path):
        self.sftp.mkdir(path)

    def rmdir(self, path):
        self.sftp.rmdir(path)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type == TypeError:
            return False
        return False


def upload_conf_file(host, user, passwd, src, dst):
    with SFTPClientContext(host, user, passwd) as ftp:
        ftp.connect()
        LOG.info('putting file:%s to %s:%s' % (src, host, dst))
        ftp.put(src, dst)


def upload_dir(host, user, passwd, local_dir, remote_dir):
    assert remote_dir.startswith('/')
    assert local_dir != '/'
    while local_dir.endswith('/'):
        local_dir = local_dir[:-1]
    while remote_dir.endswith('/'):
        remote_dir = remote_dir[:-1]
    remote_dir = os.path.join(remote_dir, os.path.basename(local_dir))
    ret, _, _ = run_cmd(host, user, passwd, "sudo rm -rf %s" % remote_dir)
    if ret != 0 and ret != 1:
        LOG.error("somehow failed in rm -rf %s on host:%s,return:%s" % (remote_dir, host, ret))
        exit(1)
    with SFTPClientContext(host, user, passwd) as sftp:
        sftp.connect()
        for root, dirs, files in os.walk(local_dir):
            for filename in files:
                local_file = os.path.join(root, filename)
                remote_file = local_file.replace(local_dir, remote_dir)
                try:
                    sftp.put(local_file, remote_file)
                except IOError:
                    sftp.mkdir(os.path.split(remote_file)[0])
                    sftp.put(local_file, remote_file)
                LOG.info("upload %s to remote %s" % (local_file, remote_file))
            for name in dirs:
                local_path = os.path.join(root, name)
                remote_path = local_path.replace(local_dir, remote_dir)
                try:
                    sftp.mkdir(remote_path)
                    LOG.info("mkdir path %s" % remote_path)
                except Exception, e:
                    raise
    return remote_dir


def isdir(path, sftp):
    exists = True
    is_dir = False
    file_stat = None
    try:
        file_stat = sftp.stat(path).st_mode
        is_dir = S_ISDIR(file_stat)
    except IOError:
        exists = False
    return exists, is_dir, file_stat


def download_file(host, user, passwd, remote_path, local_path):
    assert not remote_path.endswith('/')
    remote_file_name = os.path.basename(remote_path)
    if local_path.endswith('/'):
        if not os.path.exists(local_path):
            raise Exception('path:%s not exist.' % local_path)
        dest = os.path.join(local_path, remote_file_name)
    else:
        if os.path.isdir(local_path):
            dest = os.path.join(local_path, remote_file_name)
        else:
            dir_path = os.path.dirname(local_path)
            if not os.path.exists(dir_path):
                raise Exception('path:%s not exist' % dir_path)
            dest = local_path
    transport = paramiko.Transport((host, 22))
    transport.connect(username=user, password=passwd)
    sftp = paramiko.SFTPClient.from_transport(transport)
    exists, is_dir, st = isdir(remote_path, sftp)
    if exists and not is_dir:
        sftp.get(remote_path, dest)
        os.chmod(dest, st)
    else:
        raise Exception('error:cannot find the file or file is dir')
    return True


def download_dir(host, user, passwd, remote_path, local_path):
    while remote_path.endswith('/'):
        remote_path = remote_path[:-1]
    if local_path.endswith('/'):
        if not os.path.exists(local_path):
            raise Exception('path:%s not exist.' % local_path)
        dest_path = os.path.join(local_path, os.path.basename(remote_path))
    else:
        if os.path.isdir(local_path):
            dest_path = os.path.join(local_path, os.path.basename(remote_path))
        else:
            dir_name = os.path.dirname(local_path)
            if os.path.exists(dir_name):
                dest_path = local_path
            else:
                raise Exception('path:%s is not exists' % dir_name)
    LOG.info("download_dir from host:%s:%s to dest:%s" % (host, remote_path, dest_path))
    transport = paramiko.Transport((host, 22))
    transport.connect(username=user, password=passwd)
    sftp = paramiko.SFTPClient.from_transport(transport)
    exists, is_dir, _ = isdir(remote_path, sftp)
    if exists and is_dir:
        q = Queue.Queue(0)
        q.put(remote_path)
        while not q.empty():
            path = q.get()
            st = sftp.lstat(path).st_mode
            relative_path = path[len(remote_path):]
            if relative_path.startswith('/'): relative_path = relative_path[1:]
            local = os.path.join(dest_path, relative_path)
            if os.path.exists(local):
                shutil.rmtree(local)
            os.mkdir(local)
            os.chmod(local, st)
            file_list = sftp.listdir(path)
            for item in file_list:
                fullpath = os.path.join(path, item)
                _, is_dir, st = isdir(fullpath, sftp)
                if is_dir:
                    q.put(fullpath)
                else:
                    dest = os.path.join(local, item)
                    sftp.get(fullpath, dest)
                    os.chmod(dest, st)
    else:
        raise Exception('path:%s:%s not exists or is not a dir' % (host, remote_path))
    return dest_path


def run_cmd(host, user, passwd, cmd):
    with SSHClientContext(host, user, passwd) as ssh:
        ssh.connect()
        ret, stdout, stderr = ssh.sync_exec_command(cmd)
    return ret, stdout, stderr


class SshFileTransfer(object):
    def __init__(self, ip, user, passwd):
        self.ip, self.user, self.passwd = ip, user, passwd

    def upload_dir(self, src, dst):
        return upload_dir(self.ip, self.user, self.passwd, src, dst)

    def download_dir(self, src, dst):
        download_dir(self.ip, self.user, self.passwd, src, dst)

    def upload_file(self, src, dst):
        upload_conf_file(self.ip, self.user, self.passwd, src, dst)

    def download_file(self, src, dst):
        download_file(self.ip, self.user, self.passwd, src, dst)