summaryrefslogtreecommitdiffstats
path: root/os-plugins/plugins/xen/files/xen.examples/scripts/xend-relocation.sh
blob: 1f1cd061ffc5c2789eeb34d5d2802b8882692fe5 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
#============================================================================
# xend-relocation
#
# Version = 1.0.3
# Date = 2007-09-14
#
# Maintainer(s) = Ron Terry - ron (at) pronetworkconsulting (dot) com
#
# The latest version can be found at:
#
#    http://pronetworkconsulting.com/linux/scripts/xend-relocation.html
#
# Description:
#
# This script is used to enable or disable the VM relocation (migration)
#  feature of xend.  It can be used to manage the local instance of xend
#  or both the local instance and instances of xend on the other machines
#  to/from which VMs can be relocated.
# To manage the instances of xend on other machines this script communicates
#  using ssh so it is recomended that if you use this feature you 
#  pre-distribute ssh keys between the servers.
#
# Depends on:
#
# Can use:           /etc/sysconfig/xend
#
# Usage:             xend-relocation (start|stop|status)
#                 or
#                    xend-relocation (on|off|status)
#
# Vars:
#
#  XEN_CONFIG_FILE
#
#  RELOCATION_NODELIST
#
#  MANAGE_ALL_RELOCATION_NODES
#
#  XEN_RELOCATION_PORT
#
#============================================================================

####  Read config files and set variables  ##################################

# If you source the /etc/sysconfig/xend file comment out the variables
#  being set in this script.

. /etc/sysconfig/xend

XEN_CONFIG_FILE="/etc/xen/xend-config.sxp"

####  Script Functions  #####################################################

usage(){
  echo ""
  echo "Usage:      xend-relocation {start|stop|status}" 
  echo "         or"
  echo "            xend-relocation {on|off|status}"
  echo ""
}

relocate_on() {
  for NODE in $RELOCATION_NODELIST
  do
    case $NODE in
      any)
        SSHCMD=""
        RELOCATION_NODELIST=""
      ;;
      *)
        if [ "$MANAGE_ALL_RELOCATION_NODES" = "true" ]
        then
          SSHCMD="ssh root@$NODE "
        else
          SSHCMD=""
        fi
      ;;
    esac

    $SSHCMD sed -i "s/^#(xend-relocation-server yes)/(xend-relocation-server yes)/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^#(xend-relocation-server no)/(xend-relocation-server yes)/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^#(xend-relocation-port [^)]*)/(xend-relocation-port $XEN_RELOCATION_PORT)/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^(xend-relocation-hosts-allow \(.*\)/###(xend-relocation-hosts-allow \1/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^#(xend-relocation-hosts-allow .*/(xend-relocation-hosts-allow \'$RELOCATION_NODELIST')/g" $XEN_CONFIG_FILE
    $SSHCMD rcxend restart

    if [ "$NODE" = "any" ] || [ "$MANAGE_ALL_RELOCATION_NODES" = "false" ]
    then
      exit 0
    fi
  done
}

relocate_off() {
  for NODE in $RELOCATION_NODELIST
  do
    case $NODE in
      any)
        SSHCMD=""
        RELOCATION_NODELIST=""
      ;;
      *)
        SSHCMD="ssh root@$NODE "
      ;;
    esac

    $SSHCMD sed -i "s/^(xend-relocation-server yes)/#(xend-relocation-server yes)/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^(xend-relocation-port [^)]*)/#(xend-relocation-port $XEN_RELOCATION_PORT)/g" $XEN_CONFIG_FILE
    $SSHCMD sed -i "s/^(xend-relocation-hosts-allow .*/#(xend-relocation-hosts-allow \'$RELOCATION_NODELIST')/g" $XEN_CONFIG_FILE
    $SSHCMD rcxend restart

    if [ "$NODE" = "any" ] || [ "$MANAGE_ALL_RELOCATION_NODES" = "false" ]
    then
      exit 0
    fi
  done
}

relocate_status() {
  if grep -q "^(xend-relocation-server .*yes)" $XEN_CONFIG_FILE
  then
     ENABLED="yes"
  elif egrep -q "(^\(xend-relocation-server .*no\)|^#\(xend-relocation-server .*no\)|^#\(xend-relocation-server .*yes\))" $XEN_CONFIG_FILE
  then
    ENABLED="no"
  fi

  echo ""
  echo "Xend Relocation Server Enabled:                $ENABLED"
  echo ""
}

####  Script Body  ##########################################################

case $1 in
  on|ON|On|start)
    case $ENABLE_RELOCATION in
      true)
        relocate_on
      ;;
      false)
      ;;
    esac
    exit 0
  ;;
  off|OFF|Off|stop)
    relocate_off
    exit 0
  ;;
  status|STATUS|Status)
    relocate_status
    exit 0
  ;;
  *)
    usage
    exit 1
  ;;
esac