reuse the session in plugin

This commit is contained in:
anthony 2011-01-05 17:02:20 -08:00
parent a2ee9ff619
commit a5913439ae
1 changed files with 5 additions and 17 deletions

View File

@ -41,18 +41,13 @@ def echo(fn):
return wrapped
def get_xapi_session():
session = XenAPI.xapi_local();
session.login_with_password("","")
return session
@echo
def gethostvmstats(session, args):
collect_host_stats = args['collectHostStats']
consolidation_function = args['consolidationFunction']
interval = args['interval']
start_time = args['startTime']
session = get_xapi_session()
util.SMlog("session1 %s" % session._session)
result = hostvmstats.get_stats(session, collect_host_stats, consolidation_function, interval, start_time)
return result
@ -1431,8 +1426,7 @@ def networkUsage(session, args):
return txt
def get_private_nic( args):
session = get_xapi_session()
def get_private_nic(session, args):
vms = session.xenapi.VM.get_all()
host_uuid = args.get('host_uuid')
host = session.xenapi.host.get_by_uuid(host_uuid)
@ -1460,7 +1454,7 @@ def can_bridge_firewall(session, args):
util.pread2(['iptables', '-D', 'FORWARD', '-j', 'RH-Firewall-1-INPUT'])
except:
util.SMlog('Chain BRIDGE-FIREWALL already exists')
privnic = get_private_nic(args)
privnic = get_private_nic(session, args)
result = 'true'
try:
util.pread2(['/bin/bash', '-c', 'iptables -n -L FORWARD | grep BRIDGE-FIREWALL'])
@ -1475,7 +1469,7 @@ def can_bridge_firewall(session, args):
if not os.path.exists('/var/run/cloud'):
os.makedirs('/var/run/cloud')
cleanup_rules_for_dead_vms()
cleanup_rules_for_dead_vms(session)
cleanup_rules(session, args)
return result
@ -1889,8 +1883,6 @@ def get_rule_log_for_vm(session, vmName):
def get_rule_logs_for_vms(session, args):
host_uuid = args.pop('host_uuid')
try:
session = get_xapi_session()
thishost = session.xenapi.host.get_by_uuid(host_uuid)
hostrec = session.xenapi.host.get_record(thishost)
vms = hostrec.get('resident_VMs')
@ -1913,9 +1905,8 @@ def get_rule_logs_for_vms(session, args):
return ";".join(result)
@echo
def cleanup_rules_for_dead_vms():
def cleanup_rules_for_dead_vms(session):
try:
session = get_xapi_session()
vms = session.xenapi.VM.get_all()
cleaned = 0
for vm_name in [session.xenapi.VM.get_name_label(x) for x in vms]:
@ -1939,8 +1930,6 @@ def cleanup_rules_for_dead_vms():
@echo
def cleanup_rules(session, args):
try:
session = get_xapi_session()
chainscmd = "iptables-save | grep '^:' | grep -v '.*-def' | awk '{print $1}' | cut -d':' -f2"
chains = util.pread2(['/bin/bash', '-c', chainscmd]).split('\n')
cleaned = 0
@ -2035,7 +2024,6 @@ def remove_rule_log_for_vm(vmName):
@echo
def network_rules(session, args):
try:
session = get_xapi_session()
vm_name = args.get('vmName')
vmName = vm_name
vm_ip = args.get('vmIP')