variable change value

This is the place for queries that don't fit in any of the other categories.

variable change value

Postby ihamouda » Sun Aug 03, 2014 12:15 pm

Hello
I am new to this forum and almost new to python.
I have this simple script to shutdown virtual machines. the variable "down" should change to "1" when it is down, but it doesn't.
Code: Select all
#!/usr/bin/python

import subprocess
import time
down = 0

def shutdown_vm(vm):
        print "Shutting down %s" % vm
        subprocess.call(["crm","resource","stop","%s" % vm])

def check_vm(vm):
        crm_output = subprocess.check_output(["crm_mon","-1"])
        if not vm in crm_output:
                print "%s down" % vm
                down = 1
        else:
                print "%s still up" % vm


vm_list = []
vm_list.append("cis-apps-vd")
vm_list.append("cis-ps-vd")
vm_list.append("cis-sql-vd")
vm_list.append("cis-wcis-vd")
vm_list.append("cis-dc-vd")

for v in vm_list:
        down = 0
        shutdown_vm(v)
        while down < 1:
                print "%s" % down
                check_vm(v)
                time.sleep(10)

here is the output
Code: Select all
Shutting down cis-apps-vd
0
cis-apps-vd still up
0
cis-apps-vd still up
0
cis-apps-vd still up
0
cis-apps-vd down
0
cis-apps-vd down


As you see even I set "down" = "1" when the machine is down, the variable doesn't get picked up in the loop.
Could some one put me in the right direction please?

Thanks in advance
Last edited by Yoriz on Sun Aug 03, 2014 2:38 pm, edited 1 time in total.
Reason: First post lock.
ihamouda
 
Posts: 1
Joined: Sun Aug 03, 2014 12:10 pm

Re: variable change value

Postby Yoriz » Sun Aug 03, 2014 2:41 pm

Welcome to the forum, please read the new users read this link in my signature.

Your variable down is local to the function check_vm
Return a value from the function check_vm and assign the returned value to down in your while loop.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1030
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests