3333from fabric import api as fab
3434from fabric .api import run , env , prefix , output , settings
3535
36+
3637class StorageHelper ():
37-
38+
3839 def __init__ (self , remote_ssh_key = '~/.ssh/id_rsa.pub' , remote_ssh_user = 'root' , remote_ssh_password = 'password' , remote_timeout = 10 , debug = 0 ):
3940
4041 self .debug = debug
41-
42+
4243 # fabric settings
4344 env .user = remote_ssh_user
4445 env .password = remote_ssh_password
@@ -48,30 +49,38 @@ def __init__(self, remote_ssh_key='~/.ssh/id_rsa.pub', remote_ssh_user='root', r
4849 env .pool_size = 1
4950 env .timeout = remote_timeout
5051 env .abort_on_prompts = True
51-
52+
5253 # prevent warnings on remote host
5354 os .environ ['LC_CTYPE' ] = 'C'
54-
55- # Supress Fabric output by default, we will enable when needed
56- output ['debug' ] = False
57- output ['running' ] = False
58- output ['stdout' ] = False
59- output ['stdin' ] = False
60- output ['output' ] = False
61- output ['warnings' ] = False
55+
56+ # Supress Fabric output by default, unless debug level has been set
57+ if self .debug > 0 :
58+ output ['debug' ] = True
59+ output ['running' ] = True
60+ output ['stdout' ] = True
61+ output ['stdin' ] = True
62+ output ['output' ] = True
63+ output ['warnings' ] = True
64+ else :
65+ output ['debug' ] = False
66+ output ['running' ] = False
67+ output ['stdout' ] = False
68+ output ['stdin' ] = False
69+ output ['output' ] = False
70+ output ['warnings' ] = False
6271
6372 # generic method to run remote commands via fabric
6473 def _remote_cmd (self , hostname , cmd ):
65-
74+
6675 returncode = '0'
6776 result = ''
6877 output = ''
6978 errormsg = ''
70-
79+
7180 try :
7281 if self .debug > 0 :
73- print "[DEBUG]: Running remote command: " , cmd , " on" , env .user + "@" + hostname
74-
82+ print "[DEBUG]: Running remote command: " , cmd , " on" , env .user + "@" + hostname
83+
7584 with settings (host_string = env .user + "@" + hostname , warn_only = True , capture = False ):
7685 result = fab .run (command = cmd )
7786
@@ -80,86 +89,86 @@ def _remote_cmd(self, hostname, cmd):
8089 returncode = '-1'
8190
8291 finally :
83-
92+
8493 if result :
85- if self .debug > 0 :
94+ if self .debug > 0 :
8695 print "[DEBUG]: command success:" , result .succeeded , "command failed:" , result .failed , "command returncode:" , result .return_code , "command error:" , result .stderr
87-
96+
8897 returncode = result .return_code
8998 output = result .stdout
90-
99+
91100 if result .failed :
92101 errormsg = result .stdout
93-
102+
94103 return (returncode , output , errormsg )
95-
104+
96105 # returns a dict of mounts on remote host
97- # dict is structured <mountpoint> : <device/export>
106+ # dict is structured <mountpoint> : <device/export>
98107 def list_mounts (self , hostname ):
99108 mount_file = '/proc/mounts'
100109 remote_cmd = "cat " + mount_file
101110 mount_list = {}
102-
111+
103112 returncode , output , errmsg = self ._remote_cmd (hostname , remote_cmd )
104-
113+
105114 if returncode == 0 :
106115
107- for mount in output .split ('\r \n ' ):
116+ for mount in output .split ('\r \n ' ):
108117 mount = mount .split (' ' )
109-
118+
110119 mount_device = mount [0 ]
111120 mount_path = mount [1 ]
112- mount_list [mount_path ] = mount_device
121+ mount_list [mount_path ] = mount_device
113122
114123 else :
115124 print "[ERROR]: Failed to retrieve list of mounts on " + hostname + " due to: " , errmsg
116-
125+
117126 return mount_list
118-
127+
119128 # returns a remote mountpoint for a given devicepath
120129 def get_mountpoint (self , hostname , device_path ):
121-
130+
122131 mount_list = self .list_mounts (hostname )
123132 mountpoint = None
124-
133+
125134 if device_path .endswith ('/' ):
126- #strip the slash
135+ # strip the slash
127136 device_path = device_path [:- 1 ]
128137
129138 if len (mount_list ) > 0 :
130139 for path , device in mount_list .iteritems ():
131-
140+
132141 if device .endswith ('/' ):
133142 # strip the slash
134143 device = device [:- 1 ]
135144
136145 if device == device_path :
137146
138147 mountpoint = path
139-
148+
140149 return mountpoint
141-
142- # returns a dict of remote files and size (mb) for a given hostname and path
150+
151+ # returns a dict of remote files and size (mb) for a given hostname and
152+ # path
143153 def list_files (self , hostname , path ):
144-
154+
145155 file_list = {}
146-
156+
147157 if path is not '' :
148158 remote_cmd = "find -H " + path + " -type f -exec du -sm {} \;"
149159 returncode , output , errmsg = self ._remote_cmd (hostname , remote_cmd )
150160
151161 if returncode == 0 :
152-
162+
153163 for line in output .split ('\r \n ' ):
154164 line = line .split ('\t ' )
155-
165+
156166 file_size = line [0 ]
157167 file_path = line [1 ]
158-
168+
159169 file_list [file_path ] = file_size
160-
170+
161171 else :
162- print "[ERROR]: Failed to retrieve list from " + hostname + "of file due to: " , errmsg
163-
164- return file_list
172+ print "[ERROR]: Failed to retrieve list from " + hostname + "of file due to: " , output , errmsg
165173
174+ return file_list
0 commit comments