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
|
#!/usr/bin/python
# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com>
# This file is part of Gluster Management Gateway (GlusterMG).
#
# GlusterMG 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.
#
# GlusterMG 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 sys
import glob
p1 = os.path.abspath(os.path.dirname(sys.argv[0]))
p2 = "%s/common" % os.path.dirname(p1)
if not p1 in sys.path:
sys.path.append(p1)
if not p2 in sys.path:
sys.path.append(p2)
import Globals
import Utils
import VolumeUtils
def getGlusterVolumeInfo(volumeName=None):
volumeNameList = None
if Utils.isString(volumeName):
volumeNameList = [volumeName]
if type(volumeName) == type([]):
volumeNameList = volumeName
status = Utils.runCommand("gluster volume info", output=True, root=True)
if status["Status"] != 0:
Utils.log("Failed to execute 'gluster volume info' command")
return None
volumeInfoDict = {}
volumeInfo = {}
volumeName = None
brickList = []
for line in status['Stdout'].split("\n"):
if not line:
if volumeName and volumeInfo:
volumeInfo["Bricks"] = brickList
volumeInfoDict[volumeName] = volumeInfo
volumeInfo = {}
volumeName = None
brickList = []
continue
tokens = line.split(":")
if tokens[0].strip().upper() == "BRICKS":
continue
elif tokens[0].strip().upper() == "VOLUME NAME":
volumeName = tokens[1].strip()
volumeInfo["VolumeName"] = volumeName
elif tokens[0].strip().upper() == "TYPE":
volumeInfo["VolumeType"] = tokens[1].strip()
elif tokens[0].strip().upper() == "STATUS":
volumeInfo["VolumeStatus"] = tokens[1].strip()
elif tokens[0].strip().upper() == "TRANSPORT-TYPE":
volumeInfo["TransportType"] = tokens[1].strip()
elif tokens[0].strip().upper().startswith("BRICK"):
brickList.append(":".join(tokens[1:]).strip())
if volumeName and volumeInfo:
volumeInfoDict[volumeName] = volumeInfo
if not volumeNameList:
return volumeInfoDict
# remove unwanted volume info
for volumeName in list(set(volumeInfoDict.keys()) - set(volumeNameList)):
del volumeInfoDict[volumeName]
return volumeInfoDict
def main():
volumeInfo = getGlusterVolumeInfo()
if not volumeInfo:
print "No volume present. Removing CIFS volume configuration if any"
Utils.runCommand("rm -fr %s/*" % Globals.VOLUME_CONF_DIR, root=True, shell=True)
Utils.runCommand("rm -fr %s/*" % Globals.REEXPORT_DIR, root=True, shell=True)
Utils.runCommand("rm -fr %s/*" % Globals.CIFS_EXPORT_DIR, root=True, shell=True)
sys.exit(0)
lines = Utils.readFile(Globals.VOLUME_SMBCONF_FILE)
volumeSmbConfList = [line.strip() for line in lines]
for volumeName in volumeInfo.keys():
if not "include = %s/%s.smbconf" % (Globals.VOLUME_CONF_DIR, volumeName) in volumeSmbConfList:
continue
if 'STOPPED' == volumeInfo[volumeName]['VolumeStatus'].upper():
Utils.runCommand("rmdir %s/%s" % (Globals.CIFS_EXPORT_DIR, volumeName), root=True)
if not VolumeUtils.excludeVolume(volumeName):
Utils.log("Failed to exclude %s volume for CIFS reexport" % volumeName)
continue
if 'STARTED' == volumeInfo[volumeName]['VolumeStatus'].upper():
volumeMountDirName = "%s/%s" % (Globals.REEXPORT_DIR, volumeName)
if Utils.runCommand("mount -t glusterfs 127.0.0.1:%s %s" % (volumeName, volumeMountDirName)) != 0:
Utils.log("Failed to mount volume %s" % (volumeName))
smbConfFileList = glob.glob("%s/*.smbconf" % Globals.VOLUME_CONF_DIR)
volumeList = [smbConfFileName.split(".smbconf")[0].split("/")[-1] for smbConfFileName in smbConfFileList]
danglingVolumeList = list(set(volumeList).difference(set(volumeInfo.keys())))
if not danglingVolumeList:
sys.exit(0)
print "Cleaning up dangling volume(s):", danglingVolumeList
for volumeName in danglingVolumeList:
Utils.runCommand("rmdir %s/%s" % (Globals.REEXPORT_DIR, volumeName), root=True)
Utils.runCommand("rm -f %s/%s" % (Globals.CIFS_EXPORT_DIR, volumeName), root=True)
if not VolumeUtils.excludeVolume(volumeName):
Utils.log("Failed to exclude volume %s for CIFS reexport" % volumeName)
Utils.runCommand("rm -f %s/%s.smbconf" % (Globals.VOLUME_CONF_DIR, volumeName), root=True)
sys.exit(0)
if __name__ == "__main__":
main()
|