summaryrefslogtreecommitdiffstats
path: root/Libraries/GlusterCommands/ATFGlusterVolume.py
blob: 20bfd4f8f87dac7ead564481da39f4efbf3c10e6 (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
#!/usr/bin/env python
import ATFUtils
import ATFTestEnv
import re

def create_volume(volumekey, *exportdirs, **arguments):
    """
    Description:
        Create a new Volume of specified type with mentioned bricks

    Parameters:
        volumekey: Name of the Volume

        exportdirs:     List of exportdirs to be used to create Volume

        arguments:  Key=Value pair for "Volume Type" and "Transport Type".
                    'voltype' = stripe|replica|distribute
                    'transport' = tcp|rdma|tcp,rdma
                    'host' = Server_IP
                    'user' = User

    Return:
        Success: 0
        Failure: 1
        
    Example:
        create('Replicate',
        'host1:export1',
        'host2:export1,
        voltype='replica 2',
        transport='transport tcp'
        host = 'host(1..n)'
        server = 'server(1..n)'
       )
    """

    command = "gluster volume create"

    volumename = ATFUtils.TestEnvObj.get_volume(volumekey)
    if ( volumename == ''):
        ATFUtils.Logger.error("Volume %s Not defined in GlobalParam File" %
                                volumekey)
        return 1
    else:
        command = command + " " + volumename

    if (arguments.has_key('voltype')):
        command = command + " " + arguments.pop('voltype') 

    if (arguments.has_key('transport')):
        command = command + " " + arguments.pop('transport') 

    exportdirlist = []
    for exportdir in exportdirs:
        hostkey, exportkey = re.split(':', exportdir)

        host = ATFUtils.TestEnvObj.get_host(hostkey)
        if ( host == '' ):
            ATFUtils.Logger.error("Host %s Not defined in GlobalParam File" %
                                    hostkey)
            return 1

        export = ATFUtils.TestEnvObj.get_exportdir(exportkey)
        if (export == '' ):
            ATFUtils.Logger.error(
                "ExportDir %s Not defined in GlobalParam File" % exportkey)
            return 1

        exportdirlist.append(host + ":" +export)
            
    for exportdir in exportdirlist:
        command = command + " " + exportdir

    arguments['user'] = 'root' 
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if (status == 1):
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)
            
def start_volume(volumekey, force='', **arguments):
    """
    Description:
        Start volume specified by volumename

    Parameters:
        volumekey: Name of the Volume

    Returns:
        Success: 0
        Failure: 1
    """

    command = "gluster volume start"

    volumename = ATFUtils.TestEnvObj.get_volume(volumekey)
    if ( volumename == ''):
        ATFUtils.Logger.error("Volume %s Not defined in GlobalParam File" %
                                volumekey)
        return 1
    else:
        command = command + " " + volumename + " " + force

    arguments['user'] = 'root' 
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if (status == 1):
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)
    
def stop_volume(volumekey, force='', **arguments):
    """
    Description:
        Stop volume specified by volumename

    Parameters:
        volumekey: Name of the Volume
        force: force stop Gluster Volume

    Returns:
        Success: 0
        Failure: 1
    """

    command = "gluster volume stop"

    volumename = ATFUtils.TestEnvObj.get_volume(volumekey)
    if ( volumename == ''):
        ATFUtils.Logger.error("Volume %s Not defined in GlobalParam File" %
                                volumekey)
        return 1
    else:
        command = command + " " + volumename + " " + force

    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if (status == 1):
        return 1
    else:
        stdin.write("y\n")
        return ATFUtils.parse_output(stdout, stderr)
    

def delete_volume(volumekey, **arguments):
    """
    Description:
        Delete volume specified by volumename

    Parameters:
        volumekey: Name of the Volume

    Returns:
        Success: 0
        Failure: 1
    """

    command = "gluster volume delete"

    volumename = ATFUtils.TestEnvObj.get_volume(volumekey)
    if ( volumename == ''):
        ATFUtils.Logger.error("Volume %s Not defined in GlobalParam File" %
                                volumekey)
        return 1
    else:
        command = command + " " + volumename
    
    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if (status == 1):
        return 1
    else:
        stdin.write("y\n")
        return ATFUtils.parse_output(stdout, stderr)

def create_exportdirs(*exportdirs, **arguments):
    """
    Description:
        Create Bricks on the Server 'server'

    Parameters:
        server: IP Address of Server where bricks has to be created
        username: User on Server 'server'
        bricks: List of Bricks to be created

    Return:
        Success: 0
        Failure: 1
    """

    command = "mkdir -p"
    cleanup_command = "rm -rf" 
    exportdirlist = []
    
    for exportkey in exportdirs:
        exportdir = ATFUtils.TestEnvObj.get_exportdir(exportkey)
        if (exportdir == '' ):
            ATFUtils.Logger.error(
                "ExportDir %s Not defined in GlobalParam File" % exportkey)
            return 1
        else:
            exportdirlist.append(exportdir)
            
    for exportdir in exportdirlist:
        command = command + " " + exportdir
        cleanup_command = cleanup_command + " " + exportdir

    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(cleanup_command,
                                                            **arguments)
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if (status == 1):
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)