分配虚拟机并更新CMDB的shell脚本一例

用于提取jira描述段信息,根据这些信息去查CMDB库然后分配机器并更新CMDB库,做个备忘:)

shell中调用的desc.py

#!/usr/bin/env python

import json,sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

f=file('json')
s=json.load(f)

print s["fields"]["description"]

shell

#!/bin/bash

JID=$1

if [ -z "${JID}" ]; then
        echo "ERROR: need Jira ID, it's a number usually"
        echo "Usage: $0 JiraID"
        exit 1
fi

if [ -f jid/${JID} ];then
    echo "Notice: JIRA-${JID} had been executed,exit..."
    exit 1
fi

curl -u user:passwd http://jira.yourdomain.cn/issue/JIRA-${JID} > json 2>/dev/null
python desc.py > desc
dos2unix desc > /dev/null 2>&1

APPNAME=`cat desc|grep '应用名称'|cut -d: -f2|sed "s/ //g"`
MASTER=`cat desc|grep '应用负责人'|cut -d: -f2|sed "s/ //g"`
#echo "Appname: ${APPNAME}"
#echo "Master: ${MASTER}"

if [ -z "${APPNAME}" ] || [ -z "${MASTER}" ]; then
    echo "Error: Appname or Master can't Found,PLS check description in jira,exit..."
    exit 1
fi

function get_conf() {
    ENVIR=$1
    CONF=`cat desc|grep ${ENVIR}|cut -d: -f2|sed "s/ //g"|tr '[A-Z]' '[a-z]'|tr '台' 'T'`
    PATTERN="^[0-9]{1,2}TTT[0-9]{1,2}c[0-9]{1,2}g[0-9]{3}g"
        #CONF=`cat desc|grep ${ENVIR}|cut -d: -f2|sed "s/ //g"|tr '[A-Z]' '[a-z]'`
        #PATTERN="^[0-9]{1,2}台[0-9]{1,2}c[0-9]{1,2}g[0-9]{3}g"

    if [[ "$CONF" =~ $PATTERN ]];then
        NUM=`echo ${CONF}|cut -d'T' -f1`
        CORES=`echo ${CONF}|cut -d'c' -f1|cut -d'T' -f4`
        MEMORY=`echo ${CONF}|cut -dc -f2|cut -dg -f1|sed "s/ //g"`
        TMP=`echo ${CONF}|cut -dg -f2|sed "s/ //g"`
        DISK=${TMP:0:3}
        #echo Num\&Conf: ${CONF}
    else
        echo "Error: Invalid Config,PLS check description in jira,exit..."
        exit 1
    fi
}

function select_srv() {
        ENVIR=$1;CORES=$2;MEMORY=$3;DISK=$4;NUM=$5
        SQL="select ip from table where assign="1" and env="${ENVIR}" and cores="${CORES}" and memory="${MEMORY}" and disk="${DISK}" limit ${NUM};"
        #echo Query: "${SQL}"
        mysql -uuser -ppasswd -h1.1.1.1 -Ddbname -e "${SQL}"|grep -v -E 'ip|hostname|cores|memory|disk|assign' |tee ip.list
    RNUM=`cat ip.list |wc -l`
    if [ ${NUM} -gt ${RNUM} ];then
        echo "Notice: Free server is not enough,exit..."
        exit 1
    fi
}

function db_backup() {
        STAMP=`date "+%Y%m%d%H%M%S"`
        echo "Notice: Table backup...."
        mysqldump -uuser -ppasswd -h1.1.1.1 dbname table > backup/dbname_table_backup_${STAMP}.sql
        if [ $? -eq 0 ];then
                echo "Notice: Table already backup successfully"
        else
                echo "Notice: Table already backup failed,exit..."
                exit 1
        fi
}

function update_rows() {
    ENVIR=$1
    for IP in `cat ip.list`;
        do
            UPDATE="update table set assign='0',appname='${APPNAME}',master='${MASTER}',env='${ENVIR}',jira='http://jira.yourdomain.cn/browse/JIRA-${JID}' where ip='${IP}';"
            #echo Update: "${UPDATE}" | tee -a logs/JIRA-${JID}.log
            echo Update: "${UPDATE}" >> logs/JIRA-${JID}.log
            mysql -uuser -ppasswd -h1.1.1.1 -Ddbname -e "${UPDATE}"
        done
}

db_backup

for ENVIR in A环境 B环境 C环境
    do
        echo "${ENVIR} #################################"
        if cat desc | grep -q ${ENVIR};then
                get_conf ${ENVIR}
                if echo ${ENVIR} | grep -q A环境;then
                            select_srv 1 ${CORES} ${MEMORY} ${DISK} ${NUM}
        	        		update_rows 1
                elif echo ${ENVIR} | grep -q B环境;then
                                        select_srv 3 ${CORES} ${MEMORY} ${DISK} ${NUM}
                                        update_rows 2
                else
                    select_srv 3 ${CORES} ${MEMORY} ${DISK} ${NUM}
                                        update_rows 3
                fi
        else
            echo "Notice: ${ENVIR} not found,ignore..."
        fi
    done
touch jid/${JID}

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注