[203] | 1 | #!/bin/bash |
---|
| 2 | # Program: |
---|
| 3 | # Deploy PXE PV VM to DRBL client |
---|
| 4 | # Author: |
---|
| 5 | # Jazz, Rock {jazz, rock}@nchc.org.tw |
---|
| 6 | # Version: |
---|
| 7 | # 1.0 |
---|
| 8 | # History: |
---|
| 9 | # 2010/08/27 Rock First release (1.0) |
---|
| 10 | |
---|
| 11 | # [Source] |
---|
| 12 | source /opt/drbl-virt/conf/drbl-virt.conf |
---|
| 13 | source $Work_Path/functions_drbl_virt |
---|
| 14 | #source ./functions_drbl_PXE_PV-VM_create |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | # [Declation] |
---|
| 18 | # = 1. Varibales declation = |
---|
| 19 | #vm_dir="/home/domains" |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | # = 2. Functions declation = |
---|
| 23 | Usage(){ |
---|
[222] | 24 | echo "Usage: drbl_xen_cmd options" |
---|
[203] | 25 | echo "Options:" |
---|
[211] | 26 | echo "-c|--client client name" |
---|
| 27 | echo "-x|--execute execute command" |
---|
[203] | 28 | echo "Example:" |
---|
[222] | 29 | echo "drbl_xen_cmd -c drbl101 -x 'shutdown'" |
---|
[203] | 30 | } |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | # [Main] |
---|
| 34 | check_root |
---|
| 35 | # = 1. Parse parameters = |
---|
| 36 | if [ $# -eq 0 ]; then |
---|
| 37 | Usage && exit |
---|
| 38 | fi |
---|
| 39 | |
---|
| 40 | while [ $# -gt 0 ]; do |
---|
| 41 | case "$1" in |
---|
[211] | 42 | -c|--client) |
---|
[203] | 43 | shift |
---|
| 44 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
| 45 | if [ -n "$(echo $1)" ]; then |
---|
| 46 | client_name=$1 |
---|
| 47 | else |
---|
[222] | 48 | echo "-h client value is null" |
---|
[203] | 49 | Usage && exit 2 |
---|
| 50 | fi |
---|
| 51 | shift |
---|
| 52 | fi |
---|
| 53 | ;; |
---|
[211] | 54 | -x|--command) |
---|
[203] | 55 | shift |
---|
| 56 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
| 57 | if [ -n "$(echo $1)" ]; then |
---|
[211] | 58 | command="$1" |
---|
[203] | 59 | else |
---|
[211] | 60 | echo "-x command is null" |
---|
[203] | 61 | Usage && exit 2 |
---|
| 62 | fi |
---|
| 63 | shift |
---|
| 64 | fi |
---|
| 65 | ;; |
---|
| 66 | -*) |
---|
| 67 | echo "$0 $1 invalid option" >&2 |
---|
| 68 | echo "" |
---|
| 69 | Usage >&2 |
---|
| 70 | exit 2 |
---|
| 71 | ;; |
---|
| 72 | *) Usage >&2 |
---|
| 73 | exit 2 |
---|
| 74 | ;; |
---|
| 75 | esac |
---|
| 76 | done |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | # = 2. Check null value = |
---|
| 80 | [ -z $client_name ] && echo "[Error] no host" && Usage && exit 2 |
---|
[211] | 81 | [ -z "$command" ] && echo "[Error] no command" && Usage && exit 2 |
---|
[203] | 82 | |
---|
| 83 | |
---|
[212] | 84 | # = 3. ssh client to execute command = |
---|
[211] | 85 | echo "[$client_name]" |
---|
[203] | 86 | |
---|
[212] | 87 | # check network |
---|
| 88 | IP_status="online" |
---|
| 89 | ping -c1 -w1 $client_name > /dev/null 2>&1 || IP_status="offline" |
---|
[203] | 90 | |
---|
[212] | 91 | if [ $IP_status == "offline" ]; then |
---|
| 92 | echo "[Error] $client_name's network is offline !!!" |
---|
| 93 | else |
---|
| 94 | ssh -o StrictHostKeyChecking=no $client_name "$command" |
---|
| 95 | fi |
---|