summaryrefslogtreecommitdiffstats
path: root/com.gluster.storage.management.server.scripts/src/server/RemoteExecute.py
blob: 1800234f6aadbe971ac30339edf677bee812a450 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#  Copyright (C) 2010 Gluster, Inc. <http://www.gluster.com>
#  This file is part of Gluster Storage Platform.
#
#  Gluster Storage Platform is free software; you can redistribute it
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 3 of
#  the License, or (at your option) any later version.
#
#  Gluster Storage Platform is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.

import os
import socket
#import paramiko
import syslog
import sys
import Socket
import Globals
from copy import deepcopy
from ServerUtils import *

SERVER_AGENT_COMMAND = "/usr/sbin/server-agent"
SERVER_AGENT_CLEANUP_COMMAND = SERVER_AGENT_COMMAND + " --cleanup"
SERVER_AGENT_PRE_COMMAND = SERVER_AGENT_COMMAND + " --pre"
SERVER_AGENT_POST_COMMAND = SERVER_AGENT_COMMAND + " --post"
TRANSPORT_USER_NAME = "transport"
TRANSPORT_PRIVATE_KEY_FILE = Globals.TRANSPORT_HOME_DIR + "/.ssh/id_rsa"

def remoteExecute(serverList, command, commandInput=None):
    print "REMOTE:", serverList
    statusDict = {}
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        privateKey = paramiko.RSAKey.from_private_key_file(TRANSPORT_PRIVATE_KEY_FILE)
    except IOError:
        log(syslog.LOG_ERR, "Private key file %s not found" % TRANSPORT_PRIVATE_KEY_FILE)
        return None
    print "STAGE1"
    for serverName in serverList.keys():
        serverStatus = {}
        serverStatus["ConnectionStatus"] = None
        serverStatus["ExecutionStatus"] = None
        serverStatus["StdOutString"] = None
        serverStatus["StdErrString"] = None
        serverStatus["ConnectedIp"] = None
        serverStatus["Error"] = None

        isConnected = False
        for serverIp in serverList[serverName]:
            try:
                ssh.connect(serverIp, username=TRANSPORT_USER_NAME, pkey=privateKey)
                isConnected = True
                break
            except socket.error:
                log(syslog.LOG_ERR, "Server %s:%s is inaccessible" % (serverName, serverIp))
                continue
        if not isConnected:
            serverStatus["ConnectionStatus"] = "inaccessible"
            statusDict[serverName] = serverStatus
            continue

        try:
            transport = ssh.get_transport()
            channel = transport.open_session()
            serverStatus["ConnectionStatus"] = True
            channel.exec_command(command)
            stdin = channel.makefile('wb')
            stdout = channel.makefile('rb')
            stderr = channel.makefile_stderr('rb')
            if commandInput:
                stdin.write(commandInput)
            channel.shutdown_write()
        
            returnValue = channel.recv_exit_status() ## this is blocking call
            serverStatus["ExecutionStatus"] = returnValue
            print "RRRRRRRRRRRRRRRR:", returnValue
            errorString = ""
            if -1 == returnValue:
                errorString = stderr.read()
                serverStatus["StdErrString"] = errorString
                if "bash: " + command.split()[0] + ": command not found\n" == errorString:
                    log(syslog.LOG_ERR, "command %s not found in server %s" % (command, serverName))
                    serverStatus["Error"] = "Command not found"
            else:
                serverStatus["StdErrString"] = stderr.read()
            serverStatus["StdOutString"] = stdout.read()
            ssh.close()
        except paramiko.SSHException:
            # Channel error (channel not open)
            log(syslog.LOG_ERR, "Server %s:%s connection aborted" % (serverName, serverIp))
            serverStatus["ConnectionStatus"] = "aborted"
        except socket.error:
            log(syslog.LOG_ERR, "Server %s:%s is inaccessible" % (serverName, serverIp))
            serverStatus["ConnectionStatus"] = "inaccessible"
        except paramiko.AuthenticationException:
            log(syslog.LOG_ERR, "Authentication error on server %s:%s of user %s" % 
                (serverName, serverIp, TRANSPORT_USER_NAME))
            serverStatus["ConnectionStatus"] = "authentication error"
        serverStatus["ConnectedIp"] = serverIp
        statusDict[serverName] = serverStatus
    return statusDict

def cleanupExecuteSsh(serverList, requestDom):
    return remoteExecute(serverList, SERVER_AGENT_CLEANUP_COMMAND, requestDom.toxml())

def executeRequestCommandSsh(serverList, command, requestDom, cleanupFlag):
    cleanupStatusDict = {}
    successStatusDict = {}
    failureServerList = {}
    cleanupServerList = {}
    serverList = deepcopy(serverList)
    statusDict = remoteExecute(serverList, command, requestDom.toxml())
    for serverName in statusDict.keys():
        statusDict["Response"] = None
        if statusDict[serverName]["ConnectionStatus"] == True:
            setLastAccessedNetwork(serverName, statusDict[serverName]["ConnectedIp"])
        if statusDict[serverName]["ConnectedIp"]:
            ipList = serverList[serverName]
            ipList.remove(statusDict[serverName]["ConnectedIp"])
            cleanupServerList[serverName] = [statusDict[serverName]["ConnectedIp"]] + ipList
        if statusDict[serverName]["ExecutionStatus"] != 0:
            failureServerList[serverName] = statusDict[serverName]
            continue
        responseDom = XDOM()
        if not responseDom.parseString(statusDict[serverName]["StdOutString"]):
            failureServerList[serverName] = statusDict[serverName]
            continue
        statusDict["Response"] = responseDom
        if "OK" != responseDom.getAttribute("response-code"):
            failureServerList[serverName] = statusDict[serverName]
            continue
        successStatusDict[serverName] = statusDict[serverName]
    if cleanupFlag and failureServerList:
        cleanupStatusDict = remoteExecute(cleanupServerList, SERVER_AGENT_CLEANUP_COMMAND, requestDom.toxml())
    return successStatusDict, failureServerList, cleanupStatusDict

def preExecuteSsh(serverList, requestDom, cleanupFlag=True):
    return executeRequestCommandSsh(serverList, SERVER_AGENT_PRE_COMMAND, requestDom, cleanupFlag)

def executeSsh(serverList, requestDom, cleanupFlag=True):
    return executeRequestCommandSsh(serverList, SERVER_AGENT_COMMAND, requestDom, cleanupFlag)

def postExecuteSsh(serverList, requestDom, cleanupFlag=True):
    return executeRequestCommandSsh(serverList, SERVER_AGENT_POST_COMMAND, requestDom, cleanupFlag)

def runPullUpdatesDir(sourceServerIp, destServerIpList):
    command = "/usr/sbin/pull-dir.sh %s %s %s" % (sourceServerIp,
                                                  Globals.UPDATES_DIR[1:],
                                                  Globals.UPDATES_DIR)
    statusDict = remoteExecute(destServerIpList, command)
    status = True
    for serverName in statusDict.keys():
        if statusDict[serverName]["ExecutionStatus"] != 0:
            log(syslog.LOG_ERR, "Failed to execute [%s] in server %s" % (command, serverName))
            status = False
    return status

def runPullGlusterDir(sourceServerIp, destServerIpList):
    command = "/usr/sbin/pull-dir.sh %s %s %s" % (sourceServerIp,
                                                  Globals.GLUSTER_BASE_DIR[1:],
                                                  Globals.GLUSTER_BASE_DIR)
    statusDict = remoteExecute(destServerIpList, command)
    status = True
    for serverName in statusDict.keys():
        if statusDict[serverName]["ExecutionStatus"] != 0:
            log(syslog.LOG_ERR, "Failed to execute [%s] in server %s" % (command, serverName))
            status = False
    return status

def syncConfiguration(syncToInstaller=False, sourceServerIpList=None):
    thisServerName = getCurrentServerName()
    serverList = getAllServerList()
    serverList.remove(thisServerName)
    serverIpList = getExecuteServerList(serverList)
    if syncToInstaller:
        installerIp = getInstallerIp()
        if not installerIp:
            log(syslog.LOG_ERR, "Installer IP address is not found")
            return False
        serverIpList[Globals.INSTALLER_SERVER_NAME] = [installerIp]

    if not serverIpList:
        log(syslog.LOG_ERR, "No servers found for sync configuration")
        return False

    signature = generateSignature()
    if not storeSignature(signature, Globals.SIGNATURE_FILE):
        log(syslog.LOG_ERR, "failed to store signature %s to %s file" % 
            (signature, Globals.SIGNATURE_FILE))
        return False

    thisServerIpList = getExecuteServerList([thisServerName])
    if sourceServerIpList:
        thisServerIpList = sourceServerIpList
    return runPullGlusterDir(thisServerIpList[thisServerName][0], serverIpList)

def remoteExecuteTcp(serverIpList, requestString):
    serverStatus = {}
    serverStatus["ConnectionStatus"] = False
    serverStatus["ExecutionStatus"] = -1
    serverStatus["StdOutString"] = None
    serverStatus["StdErrString"] = None
    serverStatus["ConnectedIp"] = None
    serverStatus["Error"] = None

    for ipAddress in serverIpList.values()[0]:
        try:
            sock, inputStream, outputStream = Socket.connectToServer(ipAddress)
            Socket.writePacket(outputStream, requestString)
            packetString = Socket.readPacket(inputStream)
            log('__DEBUG__ Received: %s' % repr(packetString))
            sock.close()
            serverStatus["ConnectionStatus"] = True
            serverStatus["ExecutionStatus"] = 0
            serverStatus["StdOutString"] = packetString
            serverStatus["StdErrString"] = None
            serverStatus["ConnectedIp"] = ipAddress
            serverStatus["Error"] = None
            return serverStatus
        except socket.error, e:
            log("socket error on [%s:%s]: %s" % (serverIpList.keys()[0], ipAddress, str(e)))
    return serverStatus

def executeRequestCommand(serverList, command, requestDom, cleanupFlag):
    cleanupStatusDict = {}
    successStatusDict = {}
    failureServerList = {}
    cleanupServerList = {}
    serverList = deepcopy(serverList)

    statusDict = {}
    for serverName in serverList.keys():
        serverStatus = remoteExecuteTcp({serverName : serverList[serverName]}, requestDom.toxml())
        statusDict[serverName] = serverStatus
    for serverName in statusDict.keys():
        statusDict["Response"] = None
        if statusDict[serverName]["ConnectionStatus"] == True:
            setLastAccessedNetwork(serverName, statusDict[serverName]["ConnectedIp"])
        if statusDict[serverName]["ConnectedIp"]:
            ipList = serverList[serverName]
            ipList.remove(statusDict[serverName]["ConnectedIp"])
            cleanupServerList[serverName] = [statusDict[serverName]["ConnectedIp"]] + ipList
        if statusDict[serverName]["ExecutionStatus"] != 0:
            failureServerList[serverName] = statusDict[serverName]
            continue
        responseDom = XDOM()
        if not responseDom.parseString(statusDict[serverName]["StdOutString"]):
            failureServerList[serverName] = statusDict[serverName]
            continue
        statusDict["Response"] = responseDom
        if "OK" != responseDom.getResponseCode():
            failureServerList[serverName] = statusDict[serverName]
            continue
        successStatusDict[serverName] = statusDict[serverName]
    if cleanupFlag and failureServerList:
        rq = deepcopy(requestDom)
        rq.setRequestAction("cleanup")
        cleanupStatusDict = {}
        for serverName in cleanupServerList.keys():
            serverStatus = remoteExecuteTcp({serverName : cleanupServerList[serverName]}, rq.toxml())
            cleanupStatusDict[serverName] = serverStatus
    return successStatusDict, failureServerList, cleanupStatusDict

def preExecute(serverList, requestDom, cleanupFlag=True):
    rq = deepcopy(requestDom)
    rq.setRequestAction("pre")
    return executeRequestCommand(serverList, SERVER_AGENT_PRE_COMMAND, rq, cleanupFlag)

def execute(serverList, requestDom, cleanupFlag=True):
    return executeRequestCommand(serverList, SERVER_AGENT_COMMAND, requestDom, cleanupFlag)

def postExecute(serverList, requestDom, cleanupFlag=True):
    rq = deepcopy(requestDom)
    rq.setRequestAction("post")
    return executeRequestCommand(serverList, SERVER_AGENT_POST_COMMAND, rq, cleanupFlag)

def cleanupExecute(serverList, requestDom):
    rq = deepcopy(requestDom)
    rq.setRequestAction("cleanup")
    return executeRequestCommand(serverList, SERVER_AGENT_CLEANUP_COMMAND, rq, False)