• 68399-68399官方网站》》》

  • 发布时间:2016-01-13 10:11 | 作者:yc | 来源:互联网 | 浏览:1200 次
  • 68399-68399官方网站》》》

    日期:2015/10/20 - 2015/10/27 time 13:43

    主机:n86

    意图:初探oVirt-测验restful-api

    操作内容:

    一、示例

    1、列出本钱,运用GET法子

    ######### 列出统统vm的本钱(请留意,输出是 XML 格局) #########

    【实例】获取统统的vm的信息

    [root@n86 ~]# curl -s -k -u "admin@internal:TestVM" -H "Content-type: application/xml" -X GET https://e01.test/api/vms -o all.xml

    ######### 列出指定vm名称为“test02”(运用test02的uuid)的本钱 #########

    【实例】获取test02的信息

    [root@n86 ~]# curl -s -k -u "admin@internal:TestVM" -H "Content-type: application/xml" -X GET https://e01.test/api/vms/7f64702f-9b6d-42cf-b899-4a506bd50d57

    初探oVirt测试restfulapi

    2、创立本钱,POST法子

    ######### 新建一个VM #########

    【实例】创立vm:test03,指定集群和模版名称

    [root@n86 ~]# curl -s -k \

    -u "admin@internal:TestVM" \

    -H "Content-type: application/xml" \

    -d '

    test03

    Host-Only

    centos6u5x64-small-01

    ' \

    'https://e01.test/api/vms' -o test03.xml

    3、更新本钱,PUT法子

    【实例】更新test02的VM名称字段

    [root@n86 ~]# echo 'test02-renamed' >/tmp/upload.xml

    [root@n86 ~]# curl -s -k \

    -u "admin@internal:TestVM" \

    -H "Content-type: application/xml" \

    -T /tmp/upload.xml \

    'https://e01.test/api/vms/7f64702f-9b6d-42cf-b899-4a506bd50d57' -o test02.xml

    4、移除本钱,DELETE法子

    【实例】

    [root@n86 ~]# curl -s -k \

    -u "admin@internal:TestVM" \

    -X DELETE \

    'https://e01.test/api/vms/0d261c5d-ea5b-4184-bf7a-a7d18c14a45a'

    二、脚本示例

    ----------------------------------------------------------------------------

    [root@n86 bin]# cat ovirt_api.sh

    #!/bin/bash

    #

    # 2015/10/23

    # __version__='0.2.3'

    #

    DEBUG=0

    ## ovirt engine 信息

    oe_url='https://e01.test/api'

    oe_user='admin@internal'

    oe_password='TestVM'

    ## curl 运转时固有的参数

    curl_opts='curl -s --cacert ca.crt'

    ## 列出统统的 vm

    function vm_list() {

    local s_vm_name=$1

    local f_xml="vms.xml"

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    "${oe_url}/vms" -o ${f_xml}

    return 0

    }

    ## 获取 vm id

    function vm_uuid() {

    local s_vm_name="$1"

    local f_xml="vms.xml"

    vm_list

    s_vm_id=`grep -A 1 "${s_vm_name}" ${f_xml} |grep ' href="#" ' -f2 |cut -d'/' -f4`

    if [ -z ${s_vm_id} ]; then

    echo '[ERROR] Not found: VM id'

    exit 1

    fi

    return 0

    }

    ## 获取 vm 的状况

    function vm_state() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.state.xml"

    local state='unknown'

    echo -e '--------------------------------\n'

    echo -n 'Waiting..'

    while true

    do

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    "${oe_url}/vms/${s_vm_id}" -o ${f_xml}

    state=`sed -nr 's/(.*)(.*)(.*)/\2/p' ${f_xml}`

    case ${state} in

    down)

    echo ' vm is down.'

    break

    ;;

    up)

    echo ' vm is up.'

    break

    ;;

    *)

    [ ${DEBUG} -eq 1 ] && echo " vm state: ${state}" || echo -n '.'

    sleep 1

    esac

    done

    echo -e '-----------------------------www555com---\n'

    echo "vm: ${s_vm_name}, id: ${s_vm_id}"

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    exit 0

    }

    ## 查看 curl 请求回来的成果

    function check_fault() {

    local f_xml=$1

    grep 'fault' ${f_xml} >/dev/null

    local r1=$?

    grep 'Request syntactically incorrect' ${f_xml} >/dev/null

    local r2=$?

    if [ $r1 -eq 0 ]; then

    echo "result: failed"

    echo "reason: `sed -nr 's/(.*)(.*)(.*)/\2/p' ${f_xml}`"

    echo "detail: `sed -nr 's/(.*)(.*)(.*)/\2/p' ${f_xml}`"

    exit 1

    fi

    if [ $r2 -eq 0 ]; then

    echo 'result: Request syntactically incorrect'

    exit 2

    fi

    state=`sed -nr 's/(.*)(.*)(.*)/\2/p' ${f_xml}`

    echo "result: ${state}"

    return 0

    }

    ## 发动 vm

    function vm_start() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.start.xml"

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    -d "

    start

    " \

    "${oe_url}/vms/${s_vm_id}/start" -o ${f_xml}

    check_fault ${f_xml}

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    }

    ## 中止 vm

    function vm_stop() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.stop.xml"

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    -d "

    stop

    " \

    "${oe_url}/vms/${s_vm_id}/stop" -o ${f_xml}

    check_fault ${f_xml}

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    }

    ## 删去 vm

    function vm_delete() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.delete.xml"

    ${curl_opts} \

    -u "${oe_user}:${oe_password}" \

    -X DELETE \

    "${oe_url}/vms/${s_vm_id}" -o ${f_xml}

    check_fault ${f_xml}

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    }

    ## 只运转一次,运用固定的模版设置设备摆设 cloud-init

    function vm_runonce() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.runonce.xml"

    local s_vm_password="$2"

    local s_vm_ip="$3"

    local s_vm_netmask="$4"

    local s_vm_gateway="$5"

    local s_vm_dns="$6"

    local tpl_cloud_init="

    ${s_vm_name}

    root

    ${s_vm_password}

    true

    eth0

    static

    true

    ${s_vm_dns}

    "

    # 仅用作调试,输出 cloud-init 的 xml 文件

    local f_xml_init="${s_vm_name}.cloud-init.xml"

    [ ${DEBUG} -eq 1 ] && echo "${tpl_cloud_init}" >${f_xml_init}

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    -d "${tpl_cloud_init}" \

    "${oe_url}/vms/${s_vm_id}/start" -o ${f_xml}

    check_fault ${f_xml}

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    }

    ## 只运转一次,运用指定的模版

    function vm_runonce_tpl() {

    local s_vm_name="$1"

    vm_uuid ${s_vm_name}

    local f_xml="${s_vm_name}.runonce.xml"

    local tpl_cloud_init="`cat $2`"

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    -d 博彩58娱乐"${tpl_cloud_init}" \

    "${oe_url}/vms/${s_vm_id}/start" -o ${f_xml}

    check_fault ${f_xml}

    }

    ## 从模版创立 VM ,不是(Clone/Independent),而是(Thin/Dependent)

    function vm_create_from_tpl() {

    local s_vm_name="$1"

    local s_tpl_name=$2

    local s_cluster_name=$3

    local f_xml="${s_vm_name}.create.xml"

    ${curl_opts} \

    -H "Content-Type: application/xml" \

    -u "${oe_user}:${oe_password}" \

    -d "

    ${s_vm_name}

    ${s_cluster_name}

    ${s_tpl_name}

    " \

    "${oe_url}/vms" -o ${f_xml}

    check_fault ${f_xml}

    [ ${DEBUG} -eq 0 ] && rm -fv ${f_xml}

    }

    ## Usage

    function usage() {

    echo "

    usage: $0 [list|start|stop|delete|create|init|init-tpl] vm_name

    列出统统的VM:list

    发动VM:start [vm_name]

    中止VM:stop [vm_name]

    删去VM:delete [vm_name]

    创立VM:create [vm_name template cluster]

    只运转一次:init [vm_name root_password vm_ip vm_netmask vm_gateway vm_dns]

    只运转一次(指定模版):init-tpl [vm_name template-file]

    "

    exit 1

    }

    ## Main

    s_action=$1

    s_vm_name=$2

    ## $3 to $7 预留给 vm_runonce_tpl

    case ${s_action} in

    list)

    vm_list

    sed -nr 's/(.*)/\1/p' vms.xml |sed 's/\ //g'

    ;;

    start|stop)

    vm_${s_action} ${s_vm_name}

    vm_state ${s_vm_name}

    ;;

    delete)

    vm_${s_action} ${s_vm_name}

    ;;

    create)

    vm_create_from_tpl ${s_vm_name} 'tpl-s1' 'Host-Only'

    vm_state ${s_vm_name}

    ;;

    init)

    if [ ! $# -eq 7 ]; then

    usage

    fi

    vm_runonce ${s_vm_name} $3 $4 $5 $6 $7

    vm_state ${s_vm_name}

    ;;

    init-tpl)

    if [ ! $# -eq 3 ]; then

    usage

    fi

    vm_runonce_tpl ${s_vm_name} $3

    vm_state ${s_vm_name}

    ;;

    *)

    usage

    ;;

    esac

    ----------------------------------------------------------------------------

    ZYXW、参阅

    1、docs

    http://www.ovirt.org/REST-Api

    http://www.ovirt.org/Features/Cloud-Init_Integration

    https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Virtualization/3.5/html-single/Technical_Guide/index.html#chap-REST_API_Quick_Start_Example

  • 相关内容

友情链接: