2 ##############################################################################
3 # Copyright (c) 2016 NEC Corporation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
11 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
13 IMAGE_FILE="${IMAGE_NAME}.img"
17 ALARM_NAME=doctor_alarm1
21 SUPPORTED_INSTALLER_TYPES="apex local"
22 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
23 INSTALLER_IP=${INSTALLER_IP:-none}
24 COMPUTE_HOST=${COMPUTE_HOST:-overcloud-novacompute-0}
25 COMPUTE_IP=${COMPUTE_IP:-none}
26 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
27 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
29 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
30 echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
34 prepare_compute_ssh() {
35 ssh_opts_cpu="$ssh_opts"
37 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
38 if [[ "$INSTALLER_IP" == "none" ]] ; then
39 instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
40 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
43 if [[ "$COMPUTE_IP" == "none" ]] ; then
44 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
46 nova show $COMPUTE_HOST \
47 | awk '/ ctlplane network /{print \$5}'")
50 # get ssh key from installer node
51 sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
52 sudo chown $(whoami):$(whoami) instack_key
54 ssh_opts_cpu+=" -i instack_key"
55 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
56 if [[ "$COMPUTE_IP" == "none" ]] ; then
57 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
58 if [[ -z "$COMPUTE_IP" ]]; then
59 echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
64 echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
67 # verify connectivity to target compute host
68 ping -c 1 "$COMPUTE_IP"
72 [ -e "$IMAGE_FILE" ] && return 0
73 wget "$IMAGE_URL" -o "$IMAGE_FILE"
77 glance image-list | grep -q " $IMAGE_NAME " && return 0
78 glance image-create --name "$IMAGE_NAME" \
80 --disk-format "$IMAGE_FORMAT" \
81 --container-format bare \
86 nova list | grep -q " $VM_NAME " && return 0
87 nova boot --flavor "$VM_FLAVOR" \
88 --image "$IMAGE_NAME" \
94 ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
95 vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
96 ceilometer alarm-event-create --name "$ALARM_NAME" \
97 --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
98 --description "VM failure" \
100 --repeat-actions False \
101 --severity "moderate" \
102 --event-type compute.instance.update \
103 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
107 pgrep -f "python monitor.py" && return 0
108 sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
109 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
113 pgrep -f "python monitor.py" || return 0
114 sudo kill $(pgrep -f "python monitor.py")
119 pgrep -f "python inspector.py" && return 0
120 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
124 pgrep -f "python inspector.py" || return 0
125 kill $(pgrep -f "python inspector.py")
130 pgrep -f "python consumer.py" && return 0
131 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
135 pgrep -f "python consumer.py" || return 0
136 kill $(pgrep -f "python consumer.py")
140 wait_for_vm_launch() {
141 echo "waiting for vm launch..."
144 state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
145 [[ "$state" == "ACTIVE" ]] && return 0
151 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
152 cat > disable_network.sh << 'END_TXT'
154 dev=$(ip route | awk '/^default/{print $5}')
156 sudo ip link set $dev down
158 sudo ip link set $dev up
161 chmod +x disable_network.sh
162 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
163 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
166 calculate_notification_time() {
167 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
168 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
169 echo "$notified $detected" | \
170 awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
180 python ./nova_force_down.py "$COMPUTE_HOST" --unset
182 nova delete "$VM_NAME"
184 alarm_id=$(ceilometer alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
186 [ -n "$alarm_id" ] && ceilometer alarm-delete "$alarm_id"
188 image_id=$(glance image-list | grep " $IMAGE_NAME " | awk '{print $2}')
190 [ -n "$image_id" ] && glance image-delete "$image_id"
191 #TODO: add host status check via nova admin api
192 echo "waiting disabled compute host back to be enabled..."
194 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" \
195 "[ -e disable_network.log ] && cat disable_network.log"
199 echo "Note: doctor/tests/run.sh has been executed."
205 echo "preparing VM image..."
209 echo "starting doctor sample components..."
214 echo "creating VM and alarm..."
220 echo "injecting host failure..."
224 calculate_notification_time