xen-drbd
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xen-drbd.py Thu Sep 27 18:56:29 2007 +0300 1.3 @@ -0,0 +1,238 @@ 1.4 +#!/usr/bin/python 1.5 + 1.6 +network='eb' 1.7 + 1.8 +import sys,os 1.9 +from commands import mkarg 1.10 + 1.11 +sys.path.append('/etc/xen') 1.12 +try: 1.13 + exec 'from %s import *' % (network) 1.14 +except: 1.15 + print "Can't find or interpret module %s with topology description" %(network) 1.16 + sys.exit(1) 1.17 + 1.18 +domain_create_line="xm create xen-drbd-start network="+network+" domain=%s" 1.19 + 1.20 +def the_peer_of(node): 1.21 + if node == node1: 1.22 + another_node=node2 1.23 + else: 1.24 + another_node=node1 1.25 + return another_node 1.26 + 1.27 +he_is=the_peer_of(i_am) 1.28 + 1.29 +def log_error(error): 1.30 + print error 1.31 + 1.32 +def run_now(command,node=i_am): 1.33 + if node == i_am: 1.34 + line=command+" > /dev/stderr" 1.35 + else: 1.36 + line="ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command)) 1.37 + (p, child_stdout, child_stderr) = os.popen3(line) 1.38 + output = child_stderr.read() 1.39 + #p = os.popen(line) 1.40 + #output = p.read() 1.41 + p.close() 1.42 + return output 1.43 + 1.44 +def run(command,node=i_am): 1.45 + run_now(command,node) 1.46 +# if node == i_am: 1.47 +# print command 1.48 +# else: 1.49 +# print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command)) 1.50 + 1.51 + 1.52 +def get_drbd_resources(domain): 1.53 + disk=[] 1.54 + for disk_description in disk_table[domain]: 1.55 + if disk_description.find(":") == -1: 1.56 + disk.append(domain) 1.57 + else: 1.58 + disk.append((disk_description.split(':'))[0]) 1.59 + return disk 1.60 + 1.61 + 1.62 +def set_drbd_primary(domain, node=i_am): 1.63 + drbd_resources=get_drbd_resources(domain) 1.64 + for drbd in drbd_resources: 1.65 + run("drbdadm primary %s"%(drbd),node) 1.66 + 1.67 +def set_drbd_secondary(domain, node=i_am): 1.68 + drbd_resources=get_drbd_resources(domain) 1.69 + for drbd in drbd_resources: 1.70 + run("drbdadm secondary %s"%(drbd),node) 1.71 + 1.72 +def get_domain_id(domain,node=i_am): 1.73 + """ 1.74 + Returns domain id of the <domain> or -1 if the <domain> is not running on the <node> 1.75 + """ 1.76 + res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n") 1.77 + if not res: 1.78 + res = -1 1.79 + else: 1.80 + res = int(res) 1.81 + return res 1.82 + 1.83 +def get_drbd_id(resource,node=i_am): 1.84 + res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n") 1.85 + if not res: 1.86 + res = -1 1.87 + else: 1.88 + res = int(res) 1.89 + return res 1.90 + 1.91 +def get_drbd_state(resource,node=i_am): 1.92 + res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n") 1.93 + if not res: 1.94 + res = -1 1.95 + return res 1.96 + 1.97 +def get_drbd_cstate(resource,node=i_am): 1.98 + res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n") 1.99 + if not res: 1.100 + res = -1 1.101 + return res 1.102 + 1.103 + 1.104 +def start_domain(domain,node=i_am): 1.105 + if (get_domain_id(domain,i_am) != -1): 1.106 + log_error("Domain %s is running already on the node %s" % (domain,i_am)) 1.107 + return -1 1.108 + if (get_domain_id(domain,he_is) != -1): 1.109 + log_error("Domain %s is running already on the node %s" % (domain,he_is)) 1.110 + return -1 1.111 + another_node=the_peer_of(node) 1.112 + set_drbd_secondary(domain,another_node) 1.113 + set_drbd_primary(domain,node) 1.114 + run(domain_create_line % domain) 1.115 + 1.116 +def migrate_domain_out(domain,node=i_am): 1.117 + if (get_domain_id(domain,node) == -1): 1.118 + log_error("Domain %s is not running on the node %s" % (domain,node)) 1.119 + return -1 1.120 + another_node=the_peer_of(node) 1.121 + print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node) 1.122 + set_drbd_primary(domain,another_node) 1.123 + run("xm migrate %s %s --live" % (domain,another_node),node) 1.124 + run("sleep 2") 1.125 + set_drbd_secondary(domain,node) 1.126 + print "+ Done" 1.127 + 1.128 +def migrate_domain_in(domain,node=i_am): 1.129 + migrate_domain_out(domain,the_peer_of(node)) 1.130 + 1.131 +def migrate_domain(domain,node): 1.132 + migrate_domain_in(domain,node) 1.133 + 1.134 +def running_domains(node=i_am): 1.135 + xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n") 1.136 + return filter(lambda x: x in xm_domains, domains) 1.137 + 1.138 +def migrate_all_out(node=i_am): 1.139 + for domain in running_domains(node): 1.140 + migrate_domain_out(domain,node) 1.141 + 1.142 +def migrate_all_in(node=i_am): 1.143 + for domain in running_domains(the_peer_of(node)): 1.144 + migrate_domain_in(domain,node) 1.145 + 1.146 +def start_all(node=i_am): 1.147 + for domain in domains: 1.148 + if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)): 1.149 + start_domain(domain,node) 1.150 + 1.151 +def start_my_domains(node=i_am): 1.152 + for domain in domain_home[node]: 1.153 + if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)): 1.154 + start_domain(domain,node) 1.155 + 1.156 +def migrate_my_domains_home(node=i_am): 1.157 + for domain in domain_home[node]: 1.158 + if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)): 1.159 + migrate_domain_in(domain,node) 1.160 + 1.161 +def migrate_and_start_all(node=i_am): 1.162 + migrate_my_domains_home(node) 1.163 + start_my_domains(node) 1.164 + 1.165 +def show_usage(): 1.166 + print """ 1.167 +Usage: 1.168 + xen-drbd command [domain] 1.169 + 1.170 +Commands: 1.171 + start domain 1.172 + start-all 1.173 + start-my-domains 1.174 + 1.175 + migrate-out domain 1.176 + migrate-in domain 1.177 + migrate-all-out 1.178 + migrate-all-in 1.179 + migrate-my-domains-home 1.180 + 1.181 + migrate-and-start-all 1.182 + """ 1.183 + 1.184 +def test(): 1.185 + print "get_drbd_resources(samba)=",get_drbd_resources("samba") 1.186 + print "get_domain_id(Domain-0)=",get_domain_id("Domain-0") 1.187 + print "get_domain_id(samba)=",get_domain_id("samba") 1.188 + print "get_drbd_id(samba)=",get_drbd_id("samba") 1.189 + print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is) 1.190 + print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is) 1.191 + print "get_drbd_state(samba, he_is)=",get_drbd_state("samba") 1.192 + print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is) 1.193 + print "get_drbd_cstate(samba)=",get_drbd_cstate("samba") 1.194 + print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is) 1.195 + print "start_domain(samba)" 1.196 + start_domain("samba") 1.197 + print "migrate_domain_out(samba)" 1.198 + migrate_domain_in("samba") 1.199 + 1.200 + 1.201 +#print "get_domain_id(samba,node2)=",get_domain_id("samba",node2) 1.202 +#sys.exit(0) 1.203 + 1.204 +if len(sys.argv) == 1: 1.205 + show_usage() 1.206 + sys.exit(0) 1.207 + 1.208 +command=sys.argv[1] 1.209 +if len(sys.argv) == 3: 1.210 + domain=sys.argv[2] 1.211 + if command == 'start': 1.212 + start_domain(domain) 1.213 + elif command == 'migrate-out': 1.214 + migrate_domain_out(domain) 1.215 + elif command == 'migrate-in': 1.216 + migrate_domain_in(domain) 1.217 + else: 1.218 + show_usage() 1.219 + sys.exit(0) 1.220 +elif len(sys.argv) == 2: 1.221 + if command == 'start-all': 1.222 + start_all() 1.223 + elif command == 'start-my-domains': 1.224 + start_my_domains() 1.225 + elif command == 'migrate-all-out': 1.226 + migrate_all_out() 1.227 + elif command == 'migrate-all-in': 1.228 + migrate_all_in() 1.229 + elif command == 'migrate-my-domains-home': 1.230 + migrate_my_domains_home() 1.231 + elif command == 'migrate-and-start-all': 1.232 + migrate_and_start_all() 1.233 + elif command == 'list': 1.234 + print running_domains() 1.235 + else: 1.236 + show_usage() 1.237 + sys.exit(0) 1.238 + 1.239 + 1.240 + 1.241 +