xen-drbd

annotate xen-drbd.py @ 0:b6dc3480caae

xen-drbd merge
author igor
date Thu Sep 27 18:56:29 2007 +0300 (2007-09-27)
parents
children d6f7c53b085d
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3 network='eb'
igor@0 4
igor@0 5 import sys,os
igor@0 6 from commands import mkarg
igor@0 7
igor@0 8 sys.path.append('/etc/xen')
igor@0 9 try:
igor@0 10 exec 'from %s import *' % (network)
igor@0 11 except:
igor@0 12 print "Can't find or interpret module %s with topology description" %(network)
igor@0 13 sys.exit(1)
igor@0 14
igor@0 15 domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
igor@0 16
igor@0 17 def the_peer_of(node):
igor@0 18 if node == node1:
igor@0 19 another_node=node2
igor@0 20 else:
igor@0 21 another_node=node1
igor@0 22 return another_node
igor@0 23
igor@0 24 he_is=the_peer_of(i_am)
igor@0 25
igor@0 26 def log_error(error):
igor@0 27 print error
igor@0 28
igor@0 29 def run_now(command,node=i_am):
igor@0 30 if node == i_am:
igor@0 31 line=command+" > /dev/stderr"
igor@0 32 else:
igor@0 33 line="ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
igor@0 34 (p, child_stdout, child_stderr) = os.popen3(line)
igor@0 35 output = child_stderr.read()
igor@0 36 #p = os.popen(line)
igor@0 37 #output = p.read()
igor@0 38 p.close()
igor@0 39 return output
igor@0 40
igor@0 41 def run(command,node=i_am):
igor@0 42 run_now(command,node)
igor@0 43 # if node == i_am:
igor@0 44 # print command
igor@0 45 # else:
igor@0 46 # print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
igor@0 47
igor@0 48
igor@0 49 def get_drbd_resources(domain):
igor@0 50 disk=[]
igor@0 51 for disk_description in disk_table[domain]:
igor@0 52 if disk_description.find(":") == -1:
igor@0 53 disk.append(domain)
igor@0 54 else:
igor@0 55 disk.append((disk_description.split(':'))[0])
igor@0 56 return disk
igor@0 57
igor@0 58
igor@0 59 def set_drbd_primary(domain, node=i_am):
igor@0 60 drbd_resources=get_drbd_resources(domain)
igor@0 61 for drbd in drbd_resources:
igor@0 62 run("drbdadm primary %s"%(drbd),node)
igor@0 63
igor@0 64 def set_drbd_secondary(domain, node=i_am):
igor@0 65 drbd_resources=get_drbd_resources(domain)
igor@0 66 for drbd in drbd_resources:
igor@0 67 run("drbdadm secondary %s"%(drbd),node)
igor@0 68
igor@0 69 def get_domain_id(domain,node=i_am):
igor@0 70 """
igor@0 71 Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
igor@0 72 """
igor@0 73 res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
igor@0 74 if not res:
igor@0 75 res = -1
igor@0 76 else:
igor@0 77 res = int(res)
igor@0 78 return res
igor@0 79
igor@0 80 def get_drbd_id(resource,node=i_am):
igor@0 81 res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
igor@0 82 if not res:
igor@0 83 res = -1
igor@0 84 else:
igor@0 85 res = int(res)
igor@0 86 return res
igor@0 87
igor@0 88 def get_drbd_state(resource,node=i_am):
igor@0 89 res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
igor@0 90 if not res:
igor@0 91 res = -1
igor@0 92 return res
igor@0 93
igor@0 94 def get_drbd_cstate(resource,node=i_am):
igor@0 95 res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
igor@0 96 if not res:
igor@0 97 res = -1
igor@0 98 return res
igor@0 99
igor@0 100
igor@0 101 def start_domain(domain,node=i_am):
igor@0 102 if (get_domain_id(domain,i_am) != -1):
igor@0 103 log_error("Domain %s is running already on the node %s" % (domain,i_am))
igor@0 104 return -1
igor@0 105 if (get_domain_id(domain,he_is) != -1):
igor@0 106 log_error("Domain %s is running already on the node %s" % (domain,he_is))
igor@0 107 return -1
igor@0 108 another_node=the_peer_of(node)
igor@0 109 set_drbd_secondary(domain,another_node)
igor@0 110 set_drbd_primary(domain,node)
igor@0 111 run(domain_create_line % domain)
igor@0 112
igor@0 113 def migrate_domain_out(domain,node=i_am):
igor@0 114 if (get_domain_id(domain,node) == -1):
igor@0 115 log_error("Domain %s is not running on the node %s" % (domain,node))
igor@0 116 return -1
igor@0 117 another_node=the_peer_of(node)
igor@0 118 print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
igor@0 119 set_drbd_primary(domain,another_node)
igor@0 120 run("xm migrate %s %s --live" % (domain,another_node),node)
igor@0 121 run("sleep 2")
igor@0 122 set_drbd_secondary(domain,node)
igor@0 123 print "+ Done"
igor@0 124
igor@0 125 def migrate_domain_in(domain,node=i_am):
igor@0 126 migrate_domain_out(domain,the_peer_of(node))
igor@0 127
igor@0 128 def migrate_domain(domain,node):
igor@0 129 migrate_domain_in(domain,node)
igor@0 130
igor@0 131 def running_domains(node=i_am):
igor@0 132 xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
igor@0 133 return filter(lambda x: x in xm_domains, domains)
igor@0 134
igor@0 135 def migrate_all_out(node=i_am):
igor@0 136 for domain in running_domains(node):
igor@0 137 migrate_domain_out(domain,node)
igor@0 138
igor@0 139 def migrate_all_in(node=i_am):
igor@0 140 for domain in running_domains(the_peer_of(node)):
igor@0 141 migrate_domain_in(domain,node)
igor@0 142
igor@0 143 def start_all(node=i_am):
igor@0 144 for domain in domains:
igor@0 145 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
igor@0 146 start_domain(domain,node)
igor@0 147
igor@0 148 def start_my_domains(node=i_am):
igor@0 149 for domain in domain_home[node]:
igor@0 150 if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
igor@0 151 start_domain(domain,node)
igor@0 152
igor@0 153 def migrate_my_domains_home(node=i_am):
igor@0 154 for domain in domain_home[node]:
igor@0 155 if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
igor@0 156 migrate_domain_in(domain,node)
igor@0 157
igor@0 158 def migrate_and_start_all(node=i_am):
igor@0 159 migrate_my_domains_home(node)
igor@0 160 start_my_domains(node)
igor@0 161
igor@0 162 def show_usage():
igor@0 163 print """
igor@0 164 Usage:
igor@0 165 xen-drbd command [domain]
igor@0 166
igor@0 167 Commands:
igor@0 168 start domain
igor@0 169 start-all
igor@0 170 start-my-domains
igor@0 171
igor@0 172 migrate-out domain
igor@0 173 migrate-in domain
igor@0 174 migrate-all-out
igor@0 175 migrate-all-in
igor@0 176 migrate-my-domains-home
igor@0 177
igor@0 178 migrate-and-start-all
igor@0 179 """
igor@0 180
igor@0 181 def test():
igor@0 182 print "get_drbd_resources(samba)=",get_drbd_resources("samba")
igor@0 183 print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
igor@0 184 print "get_domain_id(samba)=",get_domain_id("samba")
igor@0 185 print "get_drbd_id(samba)=",get_drbd_id("samba")
igor@0 186 print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
igor@0 187 print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
igor@0 188 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
igor@0 189 print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
igor@0 190 print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
igor@0 191 print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
igor@0 192 print "start_domain(samba)"
igor@0 193 start_domain("samba")
igor@0 194 print "migrate_domain_out(samba)"
igor@0 195 migrate_domain_in("samba")
igor@0 196
igor@0 197
igor@0 198 #print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
igor@0 199 #sys.exit(0)
igor@0 200
igor@0 201 if len(sys.argv) == 1:
igor@0 202 show_usage()
igor@0 203 sys.exit(0)
igor@0 204
igor@0 205 command=sys.argv[1]
igor@0 206 if len(sys.argv) == 3:
igor@0 207 domain=sys.argv[2]
igor@0 208 if command == 'start':
igor@0 209 start_domain(domain)
igor@0 210 elif command == 'migrate-out':
igor@0 211 migrate_domain_out(domain)
igor@0 212 elif command == 'migrate-in':
igor@0 213 migrate_domain_in(domain)
igor@0 214 else:
igor@0 215 show_usage()
igor@0 216 sys.exit(0)
igor@0 217 elif len(sys.argv) == 2:
igor@0 218 if command == 'start-all':
igor@0 219 start_all()
igor@0 220 elif command == 'start-my-domains':
igor@0 221 start_my_domains()
igor@0 222 elif command == 'migrate-all-out':
igor@0 223 migrate_all_out()
igor@0 224 elif command == 'migrate-all-in':
igor@0 225 migrate_all_in()
igor@0 226 elif command == 'migrate-my-domains-home':
igor@0 227 migrate_my_domains_home()
igor@0 228 elif command == 'migrate-and-start-all':
igor@0 229 migrate_and_start_all()
igor@0 230 elif command == 'list':
igor@0 231 print running_domains()
igor@0 232 else:
igor@0 233 show_usage()
igor@0 234 sys.exit(0)
igor@0 235
igor@0 236
igor@0 237
igor@0 238