Bump version to 0.3.5next
[archipelago] / xseg / tools / vlmc-filed.py
1 # Copyright 2013 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 #!/usr/bin/env python2.7
35 #
36 # vlmc tool for filed
37
38 from vlmc_shared import *
39 import os, sys, subprocess, argparse
40
41 def vlmc_map(args):
42     xsegbd_loaded()
43     name = args.name[0]
44     prev = 0
45     try:
46         result = [int(open(XSEGBD_SYSFS + "devices/" + f + "/srcport").read().strip()) for f in os.listdir(XSEGBD_SYSFS + "devices/")]
47         result.sort()
48
49         for p in result:
50             if p - prev > 1:
51                break
52             else:
53                prev = p
54
55         port = prev + 1
56         fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
57         os.write(fd, "%s %d:%d:%d" % (name, port, FILED_PORT, REQS))
58         os.close(fd)
59     except Exception, reason:
60         print >> sys.stderr, reason
61         sys.exit(-1)
62
63 def vlmc_unmap(args):
64     xsegbd_loaded()
65     device = args.name[0]
66     try:
67         for f in os.listdir(XSEGBD_SYSFS + "devices/"):
68             d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
69             name = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
70             if device == DEVICE_PREFIX + d_id:
71                 fd = os.open(XSEGBD_SYSFS + "remove", os.O_WRONLY)
72                 os.write(fd, d_id)
73                 os.close(fd)
74
75                 sys.exit(0)
76         print >> sys.stderr, "Device %s doesn't exist" % device
77         sys.exit(-1)
78     except Exception, reason:
79         print >> sys.stderr, reason
80         sys.exit(-1)
81
82 def vlmc_showmapped(args):
83     xsegbd_loaded()
84     print "id\tpool\timage\tsnap\tdevice"
85     try:
86         for f in os.listdir(XSEGBD_SYSFS + "devices/"):
87             d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
88             target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
89
90             print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
91             d_id)
92     except Exception, reason:
93         print >> sys.stderr, reason
94         sys.exit(-1)
95
96 if __name__ == "__main__":
97     # parse arguments and discpatch to the correct func
98     parser = argparse.ArgumentParser(description='vlmc tool')
99     parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
100     subparsers = parser.add_subparsers()
101
102     create_parser = subparsers.add_parser('create', help='Create volume')
103     #group = create_parser.add_mutually_exclusive_group(required=True)
104     create_parser.add_argument('-s', '--size', type=int, nargs='?', help='requested size in MB for create')
105     create_parser.add_argument('--snap', type=str, nargs='?', help='create from snapshot')
106     create_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
107     create_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
108     create_parser.set_defaults(func=vlmc_create)
109
110     remove_parser = subparsers.add_parser('remove', help='Delete volume')
111     remove_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
112     remove_parser.set_defaults(func=vlmc_remove)
113     remove_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
114
115     rm_parser = subparsers.add_parser('rm', help='Delete volume')
116     rm_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
117     rm_parser.set_defaults(func=vlmc_remove)
118     rm_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
119
120     map_parser = subparsers.add_parser('map', help='Map volume')
121     map_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
122     map_parser.set_defaults(func=vlmc_map)
123     map_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
124
125     unmap_parser = subparsers.add_parser('unmap', help='Unmap volume')
126     unmap_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
127     unmap_parser.set_defaults(func=vlmc_unmap)
128     unmap_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
129
130     showmapped_parser = subparsers.add_parser('showmapped', help='Show mapped volumes')
131     showmapped_parser.set_defaults(func=vlmc_showmapped)
132     showmapped_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
133
134     list_parser = subparsers.add_parser('list', help='List volumes')
135     list_parser.set_defaults(func=vlmc_list)
136     list_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
137
138     ls_parser = subparsers.add_parser('ls', help='List volumes')
139     ls_parser.set_defaults(func=vlmc_list)
140     ls_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
141
142     resize_parser = subparsers.add_parser('resize', help='Resize volume')
143     resize_parser.add_argument('-s', '--size', type=int, nargs=1, help='requested size in MB for resize')
144     resize_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
145     resize_parser.set_defaults(func=vlmc_resize)
146     resize_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
147
148     args = parser.parse_args()
149     loadrc(args.config)
150     args.func(args)