summaryrefslogtreecommitdiff
path: root/revert_instance_from_snapshot.sh
blob: 4031cbf6dc56732aa18bb8c50fafe27d7b42dc5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# 
# following prerequisites are needed
# * all ssh-keys specified with the -k option must 
#   be present to current user (use add_keys script to import)

# Todo: How can we add more than one sshkey with the nova cli ?

# 2 cpus, 10gb disk, 1024 ram

flavor='Medium'
secgroup='Bitmask'

usage()
{
cat << EOF
usage: $0 options

This script reverts an openstack instance to it's snapshot.

mandatory:
 -i <instance>   
 -s <snapshot>
 -k <keyname>   add sshkey

optional:
 -f <flavor>    defaults to "$flavor"
 -g <secgroup>  defaults to "$secgroup"
 -h             Show this message
 -v             verbose mode

example:
   
   ./revert_instance_from_snapshot.sh -i resettest.testing.bitmask.net -s resettest-firstboot -k varac
 
EOF
}

set -- $(getopt hi:s:k:f:g:v "$@")
while [ $# -gt 0 ]
do
    case "$1" in
    (-h) usage; exit 1 ;;
    (-i) instance="$2"; shift;;
    (-s) snapshot="$2"; shift;;
    (-k) key="$2"; shift;;
    (-f) flavor="$2"; shift;;
    (-g) secgroup="$2"; shift;;
    (--) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
    (*)  break;;
    esac
    shift
done

if [[ -z $instance ]] || [[ -z $snapshot ]] || [[ -z $key ]]
then
     usage
     exit 1
fi

echo "Querying $instance..."


ips=`nova show --minimal $instance | grep 'private network'| cut -f 3 -d'|'`
# 192.168.10.2, 192.168.11.7, 192.168.11.9

float_ips=`echo "$ips"| cut -f 2- -d',' | sed 's/,/ /g'`
# 192.168.11.7 192.168.11.9

echo "Floating IPs: $float_ips"

for ip in $float_ips
do
  echo "Removing floating ip $ip from $instance"
  nova floating-ip-delete $ip

  # will re-allocate the removed ip in the pool
  echo "Re-allocating $ip to the floating-ip pool"
  nova floating-ip-create > /dev/null
done

echo
echo "Deleting $instance"
nova delete $instance

echo
echo "Booting $instance from snapshot $snap with boot cmd:"
boot_cmd="nova boot --image $snapshot --flavor $flavor --security-groups $secgroup --key_name $key $instance"
echo $boot_cmd
result=`$boot_cmd`

# unfortunatly unformatted
#echo $result > /tmp/result
echo $result

newid=`echo "$result" | grep '| id ' | cut -d '|' -f 3 | sed 's/ //g' `

echo
echo "New ID for instance $instance: $newid"

for ip in $float_ips
do
  echo "Adding floating ip $ip to $newid"
  nova add-floating-ip $newid $ip
done

nova list

echo
echo "Hang on and wait for the new instance (ID $newid) to finish building before you try to login !"