forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgluster.py
122 lines (102 loc) · 3.46 KB
/
gluster.py
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
"""
GlusterFS Support
This file has the functions that helps
* To create/check gluster volume.
* To start/check gluster services.
* To create gluster uri which can be used as disk image file path.
"""
import logging, os, re
from autotest.client.shared import utils, error
@error.context_aware
def glusterd_start():
"""
Check for glusterd status
"""
cmd = "service glusterd status"
output = utils.system_output(cmd, ignore_status=True)
if 'stopped' in output:
cmd = "service glusterd start"
error.context("Starting gluster dameon failed")
output = utils.system_output(cmd)
def is_gluster_vol_started(vol_name):
"""
Returns if the volume is started, if not send false
"""
cmd = "gluster volume info %s" % vol_name
error.context("Gluster volume info failed for volume: %s" % vol_name)
vol_info = utils.system_output(cmd)
volume_status = re.findall(r'Status: (\S+)', vol_info)
if 'Started' in volume_status:
return True
else:
return False
def gluster_vol_start(vol_name):
"""
Starts the volume if it is stopped
"""
# Check if the volume is stopped, if then start
if not is_gluster_vol_started(vol_name):
error.context("Gluster volume start failed for volume; %s" % vol_name)
cmd = "gluster volume start %s" % vol_name
utils.system(cmd)
return True
else:
return True
def is_gluster_vol_avail(vol_name):
"""
Returns if the volume already available
"""
cmd = "gluster volume info"
error.context("Gluster volume info failed")
output = utils.system_output(cmd)
volume_name = re.findall(r'Volume Name: (%s)\n' % vol_name, output)
if volume_name:
return gluster_vol_start(vol_name)
def gluster_brick_create(brick_path):
"""
Creates brick
"""
if not os.path.isdir(brick_path):
try:
os.mkdir(brick_path)
return True
except OSError, details:
logging.error("Not able to create brick folder %s", details)
def gluster_vol_create(vol_name, hostname, brick_path):
"""
Gluster Volume Creation
"""
# Create a brick
gluster_brick_create(brick_path)
cmd = "gluster volume create %s %s:/%s" % (vol_name, hostname,
brick_path)
error.context("Volume creation failed")
utils.system(cmd)
def create_gluster_uri(params):
"""
Find/create gluster volume
"""
vol_name = params.get("gluster_volume_name")
brick_path = params.get("gluster_brick")
error.context("Host name lookup failed")
hostname = utils.system_output("hostname -f")
cmd = "ip addr show|grep -A2 'state UP'|grep inet|awk '{print $2}'|cut -d'/' -f1"
if not hostname:
ip_addr = utils.system_output(cmd).split()
hostname = ip_addr[0]
# Start the gluster dameon, if not started
glusterd_start()
# Check for the volume is already present, if not create one.
if not is_gluster_vol_avail(vol_name):
gluster_vol_create(vol_name, hostname, brick_path)
# Building gluster uri
gluster_uri = "gluster://%s:0/%s/" % (hostname, vol_name)
return gluster_uri
def get_image_filename(params, image_name, image_format):
"""
Form the image file name using gluster uri
"""
img_name = image_name.split('/')[-1]
gluster_uri = create_gluster_uri(params)
image_filename = "%s%s.%s" % (gluster_uri, img_name, image_format)
return image_filename