Beispiel #1
0
# encoding: utf-8

import config

from torndb import Connection
import requests
import json
from time import sleep

db = Connection(config.mysql['host'],
                config.mysql['database'],
                user=config.mysql['user'],
                password=config.mysql['password'])

states = {
    '2012': {
        'label': u":speech_balloon: Initialisation",
        'color': '',
        'post': False
    },
    '2021': {
        'label': u":white_check_mark: OK",
        'color': 'good',
        'post': True
    },
    '2032': {
        'label': u":zzz: Sleep",
        'color': '',
        'post': True
    },
    '2042': {
Beispiel #2
0
def update_db(info):
    """Automate server audit into Racktables"""

    # connect to racktables db
    db = Connection(rt_server,rt_dbname,rt_dbuser,rt_dbpass)

    # get object_type_id
    for item in db.query("select * from Dictionary where dict_value='{0}'".format(info['server_type'])):
        object_type_id = item.dict_key
    
    # delete object if already exists
    delete_object(info)   
 
    # create object
    url = """http://{0}/racktables/index.php?module=redirect&page=depot&tab=addmore&op=addObjects""".format(rt_server)
    if info['server_type'] in ["Server","XenServer"]:
        payload = """0_object_type_id={0}&0_object_name={1}&0_object_label=&0_object_asset_no={1}&got_fast_data=Go%21"""\
                  .format(object_type_id,info['hostname'])
    if info['server_type'] in ["VM","EC2"]:
        payload = """virtual_objects=&0_object_type_id={0}&0_object_name={1}&got_fast_data=Go%21"""\
                  .format(object_type_id,info['hostname'])

    req = requests.post(url, data=payload, headers=rt_headers, auth=rt_auth)
    if req.status_code != requests.codes.ok:
        print "Failed to create object: {0}".format(info['hostname'])
        return False
    else:
        print "OK - Created object: {0}".format(info['hostname'])

    object_id = ""
    # get object_id
    for item in db.query("select * from Object where name='{0}'".format(info['hostname'])):
        object_id = item.id
    if not object_id:
        print "Failed to get object_id"
        return False

    # get os_release_id
    os_release_key = ""
    for item in db.query("select * from Dictionary where dict_value='{0}'".format(info['os_release'])):
        os_release_key = item.dict_key
    if not os_release_key:
        print "Failed to get object_type_id, please add '{0}' to 'Configuration - Dictionary - Server OS type'.".format(info['os_release'])
        return False

    # update the informations of object, all post data formats were got by firebug on firefox
    url = """http://{0}/racktables/index.php?module=redirect&page=object&tab=edit&op=update""".format(rt_server)
    if info['server_type'] == "Server":
        payload = """object_id={0}&object_type_id={1}&object_name={2}&object_label=&object_asset_no={2}&0_attr_id=14&0_value=&1_attr_id=10000&1_value={3}\
&2_attr_id=10004&2_value={4}&3_attr_id=3&3_value={5}&4_attr_id=2&4_value=0&5_attr_id=26&5_value=0&6_attr_id=10006&6_value={6}\
&7_attr_id=10003&7_value={7}&8_attr_id=1&8_value=&9_attr_id=28&9_value=&10_attr_id=21&10_value=&11_attr_id=4&11_value={8}\
&12_attr_id=24&12_value=&13_attr_id=10005&13_value={9}&14_attr_id=25&14_value=&num_attrs=15&object_comment=&submit.x=15&submit.y=13"""\
                  .format(object_id,object_type_id,info['hostname'],info['cpu_cores'],quote_plus(info['disk']),info['fqdn'],
                          info['memory'],quote_plus(info['network']),os_release_key,info['swap'])
    if info['server_type'] == "XenServer":
        payload = """object_id={0}&object_type_id={1}&object_name={2}&object_label=&object_asset_no={2}&0_attr_id=14&0_value=&1_attr_id=10000&1_value={3}\
&2_attr_id=10004&2_value={4}&3_attr_id=3&3_value={5}&4_attr_id=26&4_value=0&5_attr_id=10006&5_value={6}&6_attr_id=10003&6_value={7}\
&7_attr_id=1&7_value=&8_attr_id=28&8_value=&9_attr_id=4&9_value={8}&10_attr_id=24&10_value=&11_attr_id=10005&11_value={9}\
&12_attr_id=25&12_value=&13_attr_id=10008&13_value={10}&num_attrs=14&object_comment=&submit.x=18&submit.y=21"""\
                  .format(object_id,object_type_id,info['hostname'],info['cpu_cores'],quote_plus(info['disk']),info['fqdn'],
                          info['memory'],quote_plus(info['network']),os_release_key,info['swap'],quote_plus(info['vm_list']))
    if info['server_type'] == "EC2":
        payload = """object_id={0}&object_type_id={1}&object_name={2}&object_label=&object_asset_no=&0_attr_id=14&0_value=&1_attr_id=10000&1_value={3}\
&2_attr_id=10004&2_value={4}&3_attr_id=3&3_value={5}&4_attr_id=26&4_value=0&5_attr_id=10006&5_value={6}&6_attr_id=10003&6_value={7}\
&7_attr_id=10010&7_value={8}&8_attr_id=4&8_value={9}&9_attr_id=24&9_value=&10_attr_id=10005&10_value={10}&num_attrs=11&object_comment=&submit.x=19&submit.y=27"""\
                  .format(object_id,object_type_id,info['hostname'],info['cpu_cores'],quote_plus(info['disk']),info['fqdn'],
                          info['memory'],quote_plus(info['network']),info['ec2_pubname'],os_release_key,info['swap'])
    if info['server_type'] == "VM":
        payload = """object_id={0}&object_type_id={1}&object_name={2}&object_label=&object_asset_no=&0_attr_id=14&0_value=&1_attr_id=10000&1_value={3}\
&2_attr_id=10004&2_value={4}&3_attr_id=3&3_value={5}&4_attr_id=26&4_value=0&5_attr_id=10006&5_value={6}&6_attr_id=10003&6_value={7}\
&7_attr_id=10007&7_value={8}&8_attr_id=4&8_value={9}&9_attr_id=24&9_value=&10_attr_id=10005&10_value={10}&num_attrs=11&object_comment=&submit.x=25&submit.y=14'"""\
                  .format(object_id,object_type_id,info['hostname'],info['cpu_cores'],quote_plus(info['disk']),info['fqdn'],
                          info['memory'],quote_plus(info['network']),info['resident_on'],os_release_key,info['swap']) 

    req = requests.post(url, data=payload, headers=rt_headers, auth=rt_auth)
    if req.status_code != requests.codes.ok:
        print "Failed to update attributes"
        return False
    print "OK - Updated attributes"

    # ec2 servers don't need to update the ip pool
    if info['server_type'] not in ["EC2"]:
        # update the ip pool
        nics = ("".join(info['network'].split())).split(',')
        for i in nics:
            nic_info = i.split(':')
            nic_name = "".join(nic_info[0:1])
            nic_addr = "".join(nic_info[1:2])
            # check if nic_name is not correct
            if nic_name.isalnum():
                # create nic
                url = """http://{0}/racktables/index.php?module=redirect&page=object&tab=ip&op=add""".format(rt_server)
                payload = """object_id={0}&bond_name={1}&ip={2}&bond_type=regular&submit.x=11&submit.y=6"""\
                          .format(object_id,nic_name,nic_addr)
                req = requests.post(url, data=payload, headers=rt_headers, auth=rt_auth)
                if req.status_code != requests.codes.ok:
                    print "Failed to update ip pool for {0}:{1}".format(nic_name,nic_addr)
                    return False
                print "OK - Updated ip pool for {0}:{1}".format(nic_name,nic_addr)

    # virtual servers don't need to update the rackspace
    if info['server_type'] not in ["EC2","VM"]:
        # update rack info
        update_rack(info,object_id)

    # close db
    db.close()

    # end
    return True
Beispiel #3
0
def create_mysql_engine():
    """ 创建mysql连接 """
    protocol, host, port, user, password, database, charset, timezone = ParseMySQL(MYSQL_URL, callback="tuple")
    return Connection(host="{}:{}".format(host, port), database=database, user=user, password=password, time_zone=timezone, charset=charset)
Beispiel #4
0
 def connect(self):
     """ Establish connection to mysql database """
     return Connection(self.__HOST__,
                       self.__DB__,
                       user=self.__USER__,
                       password=self.__PASSWORD__)
Beispiel #5
0
def db_connect():
    return Connection(options.database_host, options.database_name,
                      options.database_user, options.database_password)