Skip to content

Commit 4890dbb

Browse files
author
gitlab
committed
Merge branch 'fix-4312' into 'master'
Fixes ZSTAC-4312 Closes ZSTAC-4312 See merge request !856
2 parents 65eba85 + f69c4c4 commit 4890dbb

File tree

7 files changed

+69
-61
lines changed

7 files changed

+69
-61
lines changed

cephbackupstorage/cephbackupstorage/cephagent.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def _progress_consumer(total):
235235

236236
@thread.AsyncThread
237237
def _do_import(task, fpath):
238-
shell.call("cat %s | rbd import --image-format 2 - %s" % (fpath, task.tmpPath))
238+
shell.check_run("cat %s | rbd import --image-format 2 - %s" % (fpath, task.tmpPath))
239239

240240
while True:
241241
headers = cherrypy._cpreqbody.Part.read_headers(entity.fp)
@@ -257,7 +257,7 @@ def _do_import(task, fpath):
257257

258258
if task.downloadedSize != task.expectedSize:
259259
task.fail('incomplete upload, got %d, expect %d' % (task.downloadedSize, task.expectedSize))
260-
shell.call('rbd rm %s' % task.tmpPath)
260+
shell.run('rbd rm %s' % task.tmpPath)
261261
return
262262

263263
file_format = None
@@ -271,7 +271,7 @@ def _do_import(task, fpath):
271271
if file_format == 'qcow2':
272272
if linux.qcow2_get_backing_file('rbd:'+task.tmpPath):
273273
task.fail('Qcow2 image %s has backing file' % task.imageUuid)
274-
shell.call('rbd rm %s' % task.tmpPath)
274+
shell.run('rbd rm %s' % task.tmpPath)
275275
return
276276

277277
conf_path = None
@@ -281,13 +281,13 @@ def _do_import(task, fpath):
281281
conf = '%s\n%s\n' % (conf, 'rbd default format = 2')
282282
conf_path = linux.write_to_temp_file(conf)
283283

284-
shell.call('qemu-img convert -f qcow2 -O rbd rbd:%s rbd:%s:conf=%s' % (task.tmpPath, task.dstPath, conf_path))
285-
shell.call('rbd rm %s' % task.tmpPath)
284+
shell.check_run('qemu-img convert -f qcow2 -O rbd rbd:%s rbd:%s:conf=%s' % (task.tmpPath, task.dstPath, conf_path))
285+
shell.check_run('rbd rm %s' % task.tmpPath)
286286
finally:
287287
if conf_path:
288288
os.remove(conf_path)
289289
else:
290-
shell.call('rbd mv %s %s' % (task.tmpPath, task.dstPath))
290+
shell.check_run('rbd mv %s %s' % (task.tmpPath, task.dstPath))
291291

292292
task.success()
293293

@@ -544,7 +544,7 @@ def init(self, req):
544544
if pool.predefined and pool.name not in existing_pools:
545545
raise Exception('cannot find pool[%s] in the ceph cluster, you must create it manually' % pool.name)
546546
elif pool.name not in existing_pools:
547-
shell.call('ceph osd pool create %s 128' % pool.name)
547+
shell.check_run('ceph osd pool create %s 128' % pool.name)
548548

549549
rsp = InitRsp()
550550
rsp.fsid = fsid
@@ -668,7 +668,7 @@ def fail_if_has_backing_file(fpath):
668668

669669
@rollbackable
670670
def _1():
671-
shell.call('rbd rm %s/%s' % (pool, tmp_image_name))
671+
shell.check_run('rbd rm %s/%s' % (pool, tmp_image_name))
672672

673673
def _getRealSize(length):
674674
'''length looks like: 10245K'''
@@ -747,7 +747,7 @@ def _getProgress(synced):
747747
fail_if_has_backing_file(src_path)
748748
# roll back tmp ceph file after import it
749749
_1()
750-
shell.call("rbd import --image-format 2 %s %s/%s" % (src_path, pool, tmp_image_name))
750+
shell.check_run("rbd import --image-format 2 %s %s/%s" % (src_path, pool, tmp_image_name))
751751
actual_size = os.path.getsize(src_path)
752752
else:
753753
raise Exception('unknown url[%s]' % cmd.url)
@@ -766,18 +766,18 @@ def _getProgress(synced):
766766
conf = '%s\n%s\n' % (conf, 'rbd default format = 2')
767767
conf_path = linux.write_to_temp_file(conf)
768768

769-
shell.call('qemu-img convert -f qcow2 -O rbd rbd:%s/%s rbd:%s/%s:conf=%s' % (pool, tmp_image_name, pool, image_name, conf_path))
770-
shell.call('rbd rm %s/%s' % (pool, tmp_image_name))
769+
shell.check_run('qemu-img convert -f qcow2 -O rbd rbd:%s/%s rbd:%s/%s:conf=%s' % (pool, tmp_image_name, pool, image_name, conf_path))
770+
shell.check_run('rbd rm %s/%s' % (pool, tmp_image_name))
771771
finally:
772772
if conf_path:
773773
os.remove(conf_path)
774774
else:
775-
shell.call('rbd mv %s/%s %s/%s' % (pool, tmp_image_name, pool, image_name))
775+
shell.check_run('rbd mv %s/%s %s/%s' % (pool, tmp_image_name, pool, image_name))
776776
report.progress_report("100", "finish")
777777

778778
@rollbackable
779779
def _2():
780-
shell.call('rbd rm %s/%s' % (pool, image_name))
780+
shell.check_run('rbd rm %s/%s' % (pool, image_name))
781781
_2()
782782

783783

@@ -818,8 +818,7 @@ def ping(self, req):
818818
rsp.failure = 'UnableToCreateFile'
819819
rsp.error = "%s %s" % (create_img.stderr, create_img.stdout)
820820
else:
821-
rm_img = shell.ShellCmd('rbd rm %s' % cmd.testImagePath)
822-
rm_img(False)
821+
shell.run('rbd rm %s' % cmd.testImagePath)
823822

824823
return jsonobject.dumps(rsp)
825824

@@ -831,7 +830,7 @@ def delete(self, req):
831830
def delete_image(_):
832831
# in case image is deleted, we don't have to wait for timeout
833832
img = "%s/%s" % (pool, image_name)
834-
shell.call('rbd info %s && rbd rm %s' % (img, img))
833+
shell.check_run('rbd info %s && rbd rm %s' % (img, img))
835834
return True
836835

837836
# 'rbd rm' might fail due to client crash. We wait for 30 seconds as suggested by 'rbd'.

kvmagent/kvmagent/plugins/ha_plugin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def kill_progresses_using_mount_path(mount_path):
103103

104104
logger.warn('kill the progresses, pids:%s with mount path: %s' % (list_ps, mount_path))
105105
for ps_id in list_ps:
106-
shell.ShellCmd("kill -9 %s || true" % ps_id)
106+
linux.kill9_process(ps_id)
107107

108108

109109
def is_need_kill(vmUuid, mountPaths, isFileSystem):
@@ -219,9 +219,8 @@ def create_heartbeat_file():
219219
return False
220220

221221
def delete_heartbeat_file():
222-
delete = shell.ShellCmd("timeout %s rbd rm --id zstack %s -m %s" %
222+
shell.run("timeout %s rbd rm --id zstack %s -m %s" %
223223
(cmd.storageCheckerTimeout, cmd.heartbeatImagePath, mon_url))
224-
delete(False)
225224

226225
@thread.AsyncThread
227226
def heartbeat_on_ceph():
@@ -267,7 +266,7 @@ def setup_self_fencer(self, req):
267266
def heartbeat_file_fencer(mount_path, ps_uuid, mounted_by_zstack):
268267
def try_remount_fs():
269268
if mount_path_is_nfs(mount_path):
270-
shell.ShellCmd("systemctl start nfs-client.target")(False)
269+
shell.run("systemctl start nfs-client.target")
271270

272271
while self.run_filesystem_fencer(ps_uuid, created_time):
273272
if linux.is_mounted(path=mount_path) and touch_heartbeat_file():

kvmagent/kvmagent/plugins/host_plugin.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self):
7272
logger = log.get_logger(__name__)
7373

7474
def _get_memory(word):
75-
out = shell.ShellCmd("cat /proc/meminfo | grep '%s'" % word)()
75+
out = shell.call("grep '%s' /proc/meminfo" % word)
7676
(name, capacity) = out.split(':')
7777
capacity = re.sub('[k|K][b|B]', '', capacity).strip()
7878
#capacity = capacity.rstrip('kB').rstrip('KB').rstrip('kb').strip()
@@ -177,15 +177,11 @@ def fact(self, req):
177177
rsp.libvirtVersion = self.libvirt_version
178178
rsp.ipAddresses = ipV4Addrs.splitlines()
179179

180-
cmd = shell.ShellCmd('cat /proc/cpuinfo | grep vmx')
181-
cmd(False)
182-
if cmd.return_code == 0:
180+
if shell.run('grep vmx /proc/cpuinfo') == 0:
183181
rsp.hvmCpuFlag = 'vmx'
184182

185183
if not rsp.hvmCpuFlag:
186-
cmd = shell.ShellCmd('cat /proc/cpuinfo | grep svm')
187-
cmd(False)
188-
if cmd.return_code == 0:
184+
if shell.run('grep svm /proc/cpuinfo') == 0:
189185
rsp.hvmCpuFlag = 'svm'
190186

191187
return jsonobject.dumps(rsp)
@@ -201,7 +197,7 @@ def capacity(self, req):
201197
rsp.totalMemory = _get_total_memory()
202198
rsp.usedMemory = used_memory
203199

204-
sockets = bash_o('cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l').strip('\n')
200+
sockets = bash_o('grep "physical id" /proc/cpuinfo | sort -u | wc -l').strip('\n')
205201
rsp.cpuSockets = int(sockets)
206202

207203
ret = jsonobject.dumps(rsp)

kvmagent/kvmagent/plugins/vm_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3590,7 +3590,7 @@ def get_pci_info(self, req):
35903590
rsp.error = "%s %s" % (e, o)
35913591
return jsonobject.dumps(rsp)
35923592
r_bios, o_bios, e_bios = bash.bash_roe("find /sys -iname dmar*")
3593-
r_kernel, o_kernel, e_kernel = bash.bash_roe("cat /proc/cmdline | grep 'intel_iommu=on'")
3593+
r_kernel, o_kernel, e_kernel = bash.bash_roe("grep 'intel_iommu=on' /proc/cmdline")
35943594
if o_bios != '' and r_kernel == 0:
35953595
rsp.hostIommuStatus = True
35963596
else:

zstacklib/zstacklib/utils/daemon.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import sys, os, time, atexit
44
import traceback
55
from signal import SIGTERM,SIGKILL
6+
from zstacklib.utils import linux
67
from zstacklib.utils import log
7-
from zstacklib.utils import shell
88

99
logger = log.get_logger(__name__)
1010

@@ -61,10 +61,10 @@ def daemonize(self):
6161
pid = os.fork()
6262
if pid > 0:
6363
# exit from second parent
64-
sys.exit(0)
64+
os._exit(0)
6565
except OSError, e:
6666
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
67-
sys.exit(1)
67+
os._exit(1)
6868

6969
# redirect standard file descriptors
7070
sys.stdout.flush()
@@ -79,8 +79,7 @@ def daemonize(self):
7979
# write pidfile
8080
Daemon.register_atexit_hook(self.delpid)
8181
atexit.register(Daemon._atexit)
82-
pid = str(os.getpid())
83-
file(self.pidfile,'w').write("%s\n" % pid)
82+
file(self.pidfile,'w').write("%d\n" % os.getpid())
8483

8584
def delpid(self):
8685
os.remove(self.pidfile)
@@ -99,15 +98,14 @@ def start(self):
9998
pid = None
10099

101100
if pid:
102-
pscmd = shell.ShellCmd('ps -p %s > /dev/null' % pid)
103-
pscmd(is_exception=False)
104-
if pscmd.return_code == 0:
101+
if linux.process_exists(pid):
105102
message = "Daemon already running, pid is %s\n"
106103
sys.stderr.write(message % pid)
107104
sys.exit(0)
108105

109106
# Start the daemon
110107
self.daemonize()
108+
111109
try:
112110
self.run()
113111
except Exception:
@@ -140,7 +138,7 @@ def stop(self):
140138
# Try killing the daemon process
141139
start_time = time.time()
142140
while 1:
143-
if os.path.exists('/proc/' + str(pid)):
141+
if linux.process_exists(pid):
144142
curr_time = time.time()
145143
if (curr_time - start_time) > wait_stop:
146144
os.kill(pid, SIGKILL)

zstacklib/zstacklib/utils/linux.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@author: frank
44
'''
55
import os
6+
import os.path
67
import socket
78
import subprocess
89
import datetime
@@ -76,6 +77,18 @@ def rm_file_force(fpath):
7677
except:
7778
pass
7879

80+
def process_exists(pid):
81+
return os.path.exists("/proc/" + str(pid))
82+
83+
def kill_process(pid, sig):
84+
try:
85+
os.kill(int(pid), int(sig))
86+
except:
87+
pass
88+
89+
def kill9_process(pid):
90+
kill_process(pid, 9)
91+
7992
def cidr_to_netmask(cidr):
8093
cidr = int(cidr)
8194
return socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - cidr)) & 0xffffffff))
@@ -167,7 +180,7 @@ def get_used_disk_size(dir_path):
167180
return get_total_disk_size(dir_path) - get_free_disk_size(dir_path)
168181

169182
def get_used_disk_apparent_size(dir_path):
170-
output = shell.ShellCmd('du --apparent-size --max-depth=1 %s | tail -1' % dir_path)()
183+
output = shell.call('du --apparent-size --max-depth=1 %s | tail -1' % dir_path)
171184
return long(output.split()[0])
172185

173186
def get_disk_capacity_by_df(dir_path):
@@ -196,9 +209,7 @@ def is_mounted(path=None, url=None):
196209
else:
197210
raise Exception('path and url cannot both be None')
198211

199-
cmd = shell.ShellCmd(cmdstr)
200-
cmd(is_exception=False)
201-
return cmd.return_code == 0
212+
return shell.run(cmdstr) == 0
202213

203214
def mount(url, path, options=None):
204215
cmd = shell.ShellCmd("mount | grep '%s'" % path)
@@ -270,7 +281,7 @@ def umount_by_url(url):
270281

271282

272283
def get_file_size_by_http_head(url):
273-
output = shell.ShellCmd('curl --head %s' % url)()
284+
output = shell.call('curl --head %s' % url)
274285
for l in output.split('\n'):
275286
if 'Content-Length' in l:
276287
filesize = l.split(':')[1].strip()
@@ -291,7 +302,7 @@ def get_percentage(filesize, dst):
291302
return None
292303

293304
def get_file_size(url):
294-
output = shell.ShellCmd('curl --head %s' % url)()
305+
output = shell.call('curl --head %s' % url)
295306
for l in output.split('\n'):
296307
if 'Content-Length' in l:
297308
filesize = l.split(':')[1].strip()
@@ -351,7 +362,7 @@ def get_file_size(url):
351362
return 0
352363

353364
def md5sum(file_path):
354-
return 'md5sum is not calculated, because too time cost'
365+
return 'md5sum is not calculated due to time cost'
355366

356367
#cmd = shell.ShellCmd('md5sum %s' % file_path)
357368
#cmd()
@@ -550,25 +561,25 @@ def get_img_fmt(src):
550561

551562
def qcow2_clone(src, dst):
552563
fmt = get_img_fmt(src)
553-
shell.ShellCmd('/usr/bin/qemu-img create -F %s -b %s -f qcow2 %s' % (fmt, src, dst))()
554-
shell.ShellCmd('chmod 666 %s' % dst)()
564+
shell.check_run('/usr/bin/qemu-img create -F %s -b %s -f qcow2 %s' % (fmt, src, dst))
565+
shell.check_run('chmod 666 %s' % dst)
555566

556567
def raw_clone(src, dst):
557-
shell.ShellCmd('/usr/bin/qemu-img create -b %s -f raw %s' % (src, dst))()
558-
shell.ShellCmd('chmod 666 %s' % dst)()
568+
shell.check_run('/usr/bin/qemu-img create -b %s -f raw %s' % (src, dst))
569+
shell.check_run('chmod 666 %s' % dst)
559570

560571
def qcow2_create(dst, size):
561-
shell.ShellCmd('/usr/bin/qemu-img create -f qcow2 %s %s' % (dst, size))()
562-
shell.ShellCmd('chmod 666 %s' % dst)()
572+
shell.check_run('/usr/bin/qemu-img create -f qcow2 %s %s' % (dst, size))
573+
shell.check_run('chmod 666 %s' % dst)
563574

564575
def qcow2_create_with_backing_file(backing_file, dst):
565576
fmt = get_img_fmt(backing_file)
566577
shell.call('/usr/bin/qemu-img create -F %s -f qcow2 -b %s %s' % (fmt, backing_file, dst))
567578
shell.call('chmod 666 %s' % dst)
568579

569580
def raw_create(dst, size):
570-
shell.ShellCmd('/usr/bin/qemu-img create -f raw %s %s' % (dst, size))()
571-
shell.ShellCmd('chmod 666 %s' % dst)()
581+
shell.check_run('/usr/bin/qemu-img create -f raw %s %s' % (dst, size))
582+
shell.check_run('chmod 666 %s' % dst)
572583

573584
def create_template(src, dst):
574585
fmt = get_img_fmt(src)
@@ -692,10 +703,10 @@ def get_all_bridge_interface(bridge_name):
692703
def delete_bridge(bridge_name):
693704
vifs = get_all_bridge_interface(bridge_name)
694705
for vif in vifs:
695-
shell.ShellCmd("brctl delif %s %s" % (bridge_name, vif))()
706+
shell.check_run("brctl delif %s %s" % (bridge_name, vif))
696707

697-
shell.ShellCmd("ip link set %s down" % bridge_name)()
698-
shell.ShellCmd("brctl delbr %s" % bridge_name)()
708+
shell.check_run("ip link set %s down" % bridge_name)
709+
shell.check_run("brctl delbr %s" % bridge_name)
699710

700711
def find_bridge_having_physical_interface(ifname):
701712
output = shell.call("brctl show|sed -n '2,$p'|cut -f 1,6")
@@ -730,7 +741,7 @@ def check_ip_mask():
730741
return True
731742

732743
routes = []
733-
out = shell.ShellCmd('ip route')()
744+
out = shell.call('ip route')
734745
for line in out.split('\n'):
735746
line.strip()
736747
if line:
@@ -861,17 +872,17 @@ def get_process_up_time_in_second(pid):
861872

862873

863874
def get_cpu_num():
864-
out = shell.ShellCmd("cat /proc/cpuinfo | grep 'processor' | wc -l")()
875+
out = shell.call("grep -c processor /proc/cpuinfo")
865876
return int(out)
866877

867878
@retry(times=3, sleep_time=3)
868879
def get_cpu_speed():
869880
max_freq = '/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq'
870881
if os.path.exists(max_freq):
871-
out = shell.ShellCmd('cat %s' % max_freq)()
882+
out = file(max_freq).read()
872883
return int(float(out) / 1000)
873884

874-
cmd = shell.ShellCmd("cat /proc/cpuinfo | grep 'cpu MHz' | tail -n 1")
885+
cmd = shell.ShellCmd("grep 'cpu MHz' /proc/cpuinfo | tail -n 1")
875886
out = cmd(False)
876887
if cmd.return_code == -11:
877888
raise

0 commit comments

Comments
 (0)