summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py
blob: e7aeeb5f8832596b0d9cff6910f7619cae03ad17 (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
#!/usr/bin/python
#  Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com>
#  This file is part of Gluster Storage Platform.
#

import os
import sys
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 subprocess
import Utils
import DiskUtils
from optparse import OptionParser

def writeStatus(deviceFormatStatusFile, message):
    try:
        fp = open(deviceFormatStatusFile, "w")
        fp.write(message)
        fp.close()
    except IOError, e:
        Utils.log("Failed to update log file %s: %s" % (deviceFormatStatusFile, str(e)))
        return False
    return True


def main():
    parser = OptionParser()
    parser.add_option("-t", "--type", action="store", type="string", dest="fstype")
    (options, args) = parser.parse_args()

    if len(args) != 1:
        sys.stderr.write("usage: %s [-t FSTYPE] DEVICE" % os.path.basename(sys.argv[0]))
        sys.exit(-1)

    device = args[0]
    deviceFormatLockFile = Utils.getDeviceFormatLockFile(device)
    deviceFormatStatusFile = Utils.getDeviceFormatStatusFile(device)
    deviceFormatOutputFile = Utils.getDeviceFormatOutputFile(device)

    if os.path.exists(deviceFormatStatusFile):
        Utils.log("device format status file %s exists" % deviceFormatStatusFile)
        sys.exit(1)

    if os.path.exists(deviceFormatLockFile):
        Utils.log("device format lock file %s exists" % deviceFormatLockFile)
        sys.exit(2)

    try:
        fp = open(deviceFormatLockFile, "w")
        fp.close()
    except OSError, e:
        Utils.log("failed to create lock file %s: %s" % (deviceFormatLockFile, str(e)))
        writeStatus(deviceFormatStatusFile, "Lock file creation failed\n")
        sys.exit(3)

    try:
        fptr = open(deviceFormatOutputFile, 'w')
    except IOError, e:
        Utils.log("failed to create output file %s" % deviceFormatOutputFile)
        writeStatus(deviceFormatStatusFile, "Output file creation failed\n")
        Utils.removeFile(deviceFormatLockFile)
        sys.exit(4)

    if options.fstype:
        command = "gluster-provision-block -t %s %s" % (options.fstype, device)
    else:
        command = "gluster-provision-block %s" % (device)

    process = Utils.runCommandBG(command,
                                 stdinFileObj=subprocess.PIPE,
                                 stdoutFileObj=fptr,
                                 stderrFileObj=subprocess.PIPE)
    if process:
        status = process.wait()
    else:
        Utils.removeFile(deviceFormatOutputFile)
        Utils.removeFile(deviceFormatLockFile)
        writeStatus(deviceFormatStatusFile, "Device format failed\n")
        sys.exit(5)

    if status != 0:
        Utils.removeFile(deviceFormatOutputFile)
        Utils.removeFile(deviceFormatLockFile)
        writeStatus(deviceFormatStatusFile, "Device format failed\n")
        sys.exit(6)

    if Utils.runCommand("/sbin/udevtrigger") != 0:
        Utils.log("failed running /sbin/udevtrigger")

    if Utils.runCommand("/usr/bin/lshal") != 0:
        Utils.log("failed running /usr/bin/lshal")
    writeStatus(deviceFormatStatusFile, "Completed\n")
    Utils.removeFile(deviceFormatOutputFile)
    Utils.removeFile(deviceFormatLockFile)
    sys.exit(0)

if __name__ == "__main__":
    main()