summaryrefslogtreecommitdiffstats
path: root/libs/utils/clientutils.py
blob: 9272e55fe66cb438ab87d82077ebf593524cf216 (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
288
289
290
291
292
"""clientutils module contains functions required for performing
certain operations on client

Supported Wrappers :-
-----------
*) umount
*) umountall
*) mount
*) mountall
"""

import atfutils
import hostutils
from atfglobals import GlobalObj
import pdb

def umount(mountkey):
    """unmounts a mountpoint

    Parameters:
        mountkey : name given to a mount as specified in testenv.cfg file.
        Ex:-"mount1"
        
    Returns:
        Success : 0
        Failure : 1`  
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "umount"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    
    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. '%s' not defined in TestEnvironment"
                     % mountkey)
        return 1
    
    clientkey = mount_obj.client
    client_connection = cm.getConnection(clientkey)
    if not client_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % clientkey)
        return 1

    command = ' '.join([base_command, mount_obj.dir])
    logger.debug('%s: Executing Command: %s' % (clientkey, command))
    output = client_connection.executecommand(command)
    return_status = atfutils.assert_success(**output)
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    if return_status:
        stdoutdata = str(output["stdoutdata"])
        if ((stdoutdata.rfind("not found")) or (stdoutdata.rfind("not mount"))):
            return_status = 0

        else:
            logger.error("Unable to umount %s" % mountkey)
            
    return return_status

def umountall():
    """unmounts all mount specified in testenv.cfg file.
        Ex:- mount1, mount2 etc

    Parameters:
        None
        
    Returns:
        Success : 0
        Failure : 1`  
    """
    env = GlobalObj.getTestenvObj()
    failure_flag = False

    mounts_keys = env.getMountsKeys()
    for mountkey in mounts_keys:
        return_status = umount(mountkey)
        if return_status:
            failure_flag = True

    if failure_flag:
        return 1
    else:
        return 0

def mount(mountkey):
    """mounts a filesystem

    Parameters:
        mountkey : name given to a mount as specified in testenv.cfg file.
        Ex:-"mount1"
        
    Returns:
        Success : 0
        Failure : 1`  
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "mount"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]
    options = []
    
    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. %s not defined in TestEnvironment"
                     % mountkey)
        return 1
    
    clientkey = mount_obj.client
    client_connection = cm.getConnection(clientkey)
    if not client_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % clientkey)
        return 1

    return_status = hostutils.mkdir(clientkey, mount_obj.dir)
    if return_status:
        return return_status

    mountdevice_obj = mount_obj.device
    device = mountdevice_obj.hostname + ":/" + mountdevice_obj.volumename
    options.extend(["-t", mount_obj.type])
    if mount_obj.logfile:
        options.extend(["-o", ("log-file="+mount_obj.logfile),
                        "log-level=INFO"])
    if mount_obj.options:
        options.extend([mount_obj.option])
    options.extend([device, mount_obj.dir])
    
    command.extend(options)
    command = ' '.join(command)
        
    logger.debug('%s: Executing Command: %s' % (clientkey, command))
    output = client_connection.executecommand(command)
    return_status = atfutils.assert_success(**output)
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def mountall():
    """mounts a filesystem for all mounts specified in testenv.cfg file. 

    Parameters:
        None
        
    Returns:
        Success : 0
        Failure : 1`  
    """
    env = GlobalObj.getTestenvObj()

    mounts_keys = env.getMountsKeys()
    for mountkey in mounts_keys:
        return_status = mount(mountkey)
        if return_status:
            return return_status

    return 0


def execute_on_mount(mountkey, command, commandInput=None):
    """
    """
    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()
    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. %s not defined in TestEnvironment"
                     % mountkey)
        return 1
    
    
    clientkey = mount_obj.client
    mountdir = mount_obj.dir
    command = "cd " + mountdir + " ;" + command
    output = hostutils.execute_command(clientkey, command, commandInput)
    return output['exitstatus']
    
__all__ = ['execute_on_mount',
           'umount',
           'umountall',
           'mount',
           'mountall']

##def umountall(clientkey):
##    """
##    """
##    base_command = "umount "
##    env = GlobalObj.get_testenv_obj()
##    cm = GlobalObj.get_connectionsmanager_obj()
##    client_obj = env.getclient(clientkey)
##    mountdir = client_obj.mountdir
##    volume = client_obj.device 
##    client = cm.getconnection(clientkey)
##    
##    mountpoints = []
##    success_flag = False
##    failure_flag = False
##    command = "mount | grep " + mountdir
##    output = client.executecommand(command)
##    if not output["exitstatus"]:
##        for data in output["stdoutdata"]:
##            mountpoints.append(data.split(" ")[2])
##
##        for mountpoint in mountpoints:
##            command = base_command + mountpoint
##            output = client.executecommand(command)
##            return_code = utils.assert_success(**output)
##            if return_code:
##                failure_flag = True
##            else:
##                success_flag = True
##                continue
##
##        if failure_flag:
##            return 1
##
##    mountpoints = []
##    success_flag = False
##    failure_flag = False
##    command = "mount | grep " + volume
##    output = client.executecommand(command)
##    if not output["exitstatus"]:
##        for data in output["stdoutdata"]:
##            mountpoints.append(data.split(" ")[2])
##
##        for mountpoint in mountpoints:
##            command = base_command + mountpoint
##            output = client.executecommand(command)
##            return_code = utils.assert_success(**output)
##            if return_code:
##                failure_flag = True
##            else:
##                success_flag = True
##                continue
##
##        if failure_flag:
##            return 1
##        
##    return 0

##def cd_mount(mountkey):
##    """
##    """
##    env = GlobalObj.getTestenvObj()
##    mount_obj = env.getMount(mountkey)
##    if not mount_obj:
##        print "InValid Mount. %s not defined in TestEnvironment" % mountkey
##        return 1
##    
##    clientkey = mount_obj.client
##    dirpath = mount_obj.dir
##    return_status = hostutils.cd(clientkey, dirpath)
##    return return_status
##
##def cd_allmounts():
##    """
##    """
##    env = GlobalObj.getTestenvObj()
##    mounts_keys = env.getMountsKeys()
##    for mountkey in mounts_keys:
##        return_status = cd_mount(mountkey)
##        if return_status:
##            return return_status
##
##    return 0