HVS_system CGI scripts.

get_status.cgi

#!/usr/bin/env python
# -*- coding: UTF-8 -*-# enable debugging

import sys, os
import cgi
import MySQLdb
from urllib import *

host = '10.130.120.53'
user = 'xxxxxx'
passwd = 'xxxxxxx'
db = 'asset'

print "Content-Type: text/plain;charset=utf-8"
print

form = cgi.FieldStorage()
request_host = form['request_host'].value 
request_status = form['status'].value

#check request_host
client_host = os.environ['REMOTE_ADDR']

if request_host != client_host:
    print "Error: illegal host request."
    sys.exit(55)
if request_status != '1':
    print "ERROR: Status %s cannot acceptable!" % request_status
    sys.exit(55)


conn=MySQLdb.connect(host,user,passwd,db)
cursor = conn.cursor()
sql_getstatus = 'select domu_name,status from hvs_domu_info where eth0 = "%s"' % request_host
n = cursor.execute(sql_getstatus)

#if not n:
#    print "Error: Bad request from host."
#    sys.exit(55)
rows = cursor.fetchall()
domu_name = rows[0][0]
status = rows[0][1]


print "Request_host : %s\nCheck status : %s" %(request_host,status)

def boot_status(status):
    if status == 0:
        boot = "off"
    elif status == 1:
        boot = "idle"
    elif status == 2:
        boot = "on"
    else:
        boot = "Uknown"
    return boot

if boot_status(status) == 'off':
    sql = 'update hvs_domu_info set status = 1 where eth0 ="%s"' % request_host
    e = cursor.execute(sql)
    conn.commit()
    n = cursor.execute(sql_getstatus)
    rows = cursor.fetchall()
    for row in rows:
        status = row[1]
        domu_id = row[0]
    print "Running status: %s" % boot_status(status)
    print "domu_id: %s" % domu_id
    print "Client host:%s" % client_host
elif boot_status(status) == 'on':
    print "Running status: %s" % boot_status(status)
    print "domu_id: %s" % domu_id
elif boot_status(status) == 'idle':
    print "domu_id: %s" % domu_name
    print "Warnning: Instance: %s already initialized." % domu_name
conn.close()