带字符串比较的while循环无法正常工作

时间:2019-05-04 14:32:48

标签: python python-3.x bash fabric

我必须编写程序的一部分以从kvm中删除虚拟机。这是为python http://www.fabfile.org/用fabric编写的。因此,我需要连接到kvm主机,检查vm是否正在运行,将其关闭并删除。

当我尝试放入包含if语句的while循环时,出现了问题。似乎无法正常工作,即使机器状态为“ shut”,程序也无法继续执行,只要重复5次相同的if语句即可。

我尝试了不同类型的字符串比较,例如: -如果“关闭”不在 -==“关闭” -!=“闭嘴” -我也尝试将“关机”作为变量

它将由詹金斯·乔布斯(Jenkins job)运行,我可以用这段代码看到的是while循环,当它计数到5时,就可以继续下去。

running
Domain test-test01.test is being shutdown

running
shut
shut
shut
shut
>>> VM not found on this host, but this is fine 33333

这似乎是可以的代码,但是不起作用。 Connection只是结构特征之一,可以正常工作。它通过ssh作为与某些主机的连接。您可以在上方看到以下代码在终端中得到的答复。

removed = False
    if vmhosttype == "KVM":
        with Connection(vmhostname) as c:
            if c.sudo("virsh list --all | grep {0}".format(shortfqdn), warn=True, hide="stdout").failed:
                print(">>> VM not found on {0}.".format(vmhostname))
            else:
                print(">>> VM found on {0}.".format(vmhostname))
                if c.sudo("virsh list --all | grep {0} | awk '{{ print $3 }}'".format(shortfqdn)) == "shut":
                    c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
                    removed = True
                else:
                    c.sudo("virsh shutdown {0}".format(shortfqdn), hide=hide)
                    i = 0
                    while (i < 5):
                        if c.sudo("virsh list --all | grep {0} | awk '{{ print $3 }}'".format(shortfqdn)) != "shut":
                            time.sleep(30)
                            i += 1
                        else:
                            c.sudo("virsh undefine {0} --remove-all-storage".format(shortfqdn), hide=hide)
                            removed = True
                            i = 5

1 个答案:

答案 0 :(得分:0)

问题出在stdout中,为空。

相关问题