Skip to content

Commit 53d49cb

Browse files
author
Michael Andersen
committed
PEP8 adjustments
1 parent b9aca02 commit 53d49cb

2 files changed

Lines changed: 71 additions & 60 deletions

File tree

cloudstackops/cloudstackstorage.py

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
from fabric import api as fab
3434
from fabric.api import run, env, prefix, output, settings
3535

36+
3637
class 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

listOrphanedDisks.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def handle_arguments(argv):
142142

143143

144144
# get a list of storage pools for each cluster
145-
t_storagepool = PrettyTable(["Cluster", "Storage Pool", "Number of Orphaned disks", "Real Space used (GB)"])
145+
t_storagepool = PrettyTable(
146+
["Cluster", "Storage Pool", "Number of Orphaned disks", "Real Space used (GB)"])
146147

147148
for cluster in clusters:
148149
storagepools = []
@@ -156,7 +157,6 @@ def handle_arguments(argv):
156157

157158
storagehelper = StorageHelper(debug=DEBUG)
158159

159-
160160
for storagepool in storagepools:
161161
used_space = 0
162162

@@ -175,31 +175,33 @@ def handle_arguments(argv):
175175
if primary_mountpoint is None:
176176
print "[DEBUG]: no physical volume list retrieved for " + storagepool.name + " skipping"
177177
storagepool_filelist = None
178-
178+
179179
else:
180-
storagepool_filelist = storagehelper.list_files(random_hypervisor.ipaddress, primary_mountpoint)
181-
182-
183-
t = PrettyTable(["Domain", "Account", "Name", "Cluster","Storagepool","Path",
180+
storagepool_filelist = storagehelper.list_files(
181+
random_hypervisor.ipaddress, primary_mountpoint)
182+
183+
t = PrettyTable(["Domain", "Account", "Name", "Cluster", "Storagepool", "Path",
184184
"Allocated Size (GB)", "Real Size (GB)", "Orphaned"])
185185

186186
for orphan in orphans:
187187
isorphaned = ''
188188

189189
orphan_allocated_sizeGB = (orphan.size / math.pow(1024, 3))
190-
190+
191191
if storagepool_filelist is None:
192192
orphan_real_sizeGB = 'n/a'
193-
isorphaned = '?'
194-
193+
isorphaned = '?'
194+
195195
else:
196-
orphan_real_sizeGB = get_volume_filesize(orphan.path, storagepool_filelist)
197-
196+
orphan_real_sizeGB = get_volume_filesize(
197+
orphan.path, storagepool_filelist)
198+
198199
if orphan_real_sizeGB is not None:
199200
used_space += (orphan_real_sizeGB / 1024)
200-
orphan_real_sizeGB = format((orphan_real_sizeGB / 1024), '.2f')
201+
orphan_real_sizeGB = format(
202+
(orphan_real_sizeGB / 1024), '.2f')
201203
isorphaned = 'Y'
202-
204+
203205
else:
204206
orphan_real_sizeGB = 0
205207
isorphaned = 'N'
@@ -212,6 +214,6 @@ def handle_arguments(argv):
212214
print t.get_string()
213215
t_storagepool.add_row(
214216
[cluster.name, storagepool.name, len(orphans), format(used_space, '.2f')])
215-
217+
216218
print "Storagepool Totals"
217219
print t_storagepool.get_string()

0 commit comments

Comments
 (0)