def _start_service(self, hosts): def _start_srv_warp(h): cmd = "python2 %s start" % (self.config.uss_node) #dmsg(cmd) exec_remote(h, cmd) mutil_exec(_start_srv_warp, [[h] for h in hosts])
def _init_env(self, hosts): def _init_env_warp(h): cmd = "python2 %s env_init" % (self.config.uss_node) (out, err) = exec_remote(h, cmd) #print out #print err mutil_exec(_init_env_warp, [[h] for h in hosts])
def logclean(self): def _warp(h): cmd = "python2 %s logclean" % (self.config.uss_node) x, y = exec_remote(h, cmd) print "logclean host: %s \n%s" % (h, x) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
def ucarpstatus(self): def _warp(h): cmd = "python2 %s ucarpstatus" % (self.config.uss_node) x, y = exec_remote(h, cmd) print "host: %s, status : %s" % (h, x) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
def dns_del(self, name): def _warp(h): cmd = "python2 %s dnsdel --name %s" % (self.config.uss_node, name) x, y = exec_remote(h, cmd) print "host: %s \n%s" % (h, x) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
def ucarpdestroy(self): def _warp(h): cmd = "python2 %s ucarpdestroy" % (self.config.uss_node) x, y = exec_remote(h, cmd) print "destroy ucarp host: %s \n%s" % (h, x) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
def vipdestroy(self): self.vipstop() def _warp(h): cmd = "rm -rf %s" % (self.config.vip_conf) x, y = exec_remote(h, cmd) print "host: %s vip service destroy ok\n" % (h) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
def stop(self): def _cluster_stop(module_py, host): cmd = "python2 %s stop" % (module_py) x, y = exec_remote(host, cmd) print "stop host: %s \n%s" % (host, x) if y: print y #stop all node minio service first #args = [[self.config.uss_minio, x] for x in self.config.cluster.keys()] #mutil_exec(_cluster_stop, args) args = [[self.config.uss_node, x] for x in self.config.cluster.keys()] mutil_exec(_cluster_stop, args)
def _init_etcd(self, hosts): def _init_etcd_(self, host, statue, lst): s = '' for i in lst: s = s + "%s," % (i) cmd = "%s etcd --state %s --hosts %s" % (self.config.uss_node, statue, s[:-1]) #dmsg(cmd) (out, err) = exec_remote(host, cmd) """ if (out): dmsg(host + ":\n" + out) if (err): dwarn(host + ":\n" + err) """ args = [[self, k, 'new', hosts] for (k) in hosts] mutil_exec(_init_etcd_, args)
def create_cluster(self, hosts, proxy_nodes=None): cluster_list = hosts.split(',') proxy_list = [] cmd = "python %s/app/admin/node.py etcd --state %s --hosts %s" % (self.config.home, "new", hosts) args = [[x, cmd] for x in cluster_list] mutil_exec(self._exec_node, args) if proxy_nodes is not None: proxy_list = proxy_nodes.split(',') for node in proxy_list: if node in cluster_list: _dwarn("host:%s is in etcd cluster, not permit run as proxy mode!" % (node)) proxy_list.remove(node) if len(proxy_list): args = [[x, cmd] for x in proxy_list] mutil_exec(self._exec_node, args)
def _ucarp_conf_group(self, hosts, vip, master_vip): master_skew = 5 slave_skew = 10 print "hosts %s, vip:%s, master_vip:%s" % (hosts, vip, master_vip) def _warp(h): if h == master_vip: cmd = "python2 %s ucarpconf --srcip %s --addr %s --skew %s" % ( self.config.uss_node, h.strip(), vip.strip(), master_skew) else: cmd = "python2 %s ucarpconf --srcip %s --addr %s --skew %s" % ( self.config.uss_node, h.strip(), vip.strip(), slave_skew) x, y = exec_remote(h, cmd) print "ucarp conf host: %s \n%s" % (h, x) if y: print y args = [[x] for x in hosts] mutil_exec(_warp, args)
def _init_mdctl(self, hosts): def _init_warp(h, index): cmd = "python2 %s mdctl_init --service %s" % (self.config.uss_node, index) exec_remote(h, cmd) init_args = [] for h in hosts: for i in self.config.cluster[h]['mdctl']: print 'init mdctl', h, i _init_warp(h, i) #init_args.append([h, i]) #mutil_exec(_init_warp, init_args) def _start_warp(h): cmd = "python2 %s start --op simple" % (self.config.uss_node) exec_remote(h, cmd) mutil_exec(_start_warp, [[h] for h in hosts])
def viplist(self, json_show=False): if not os.path.isfile(self.config.vip_conf): dwarn("cluster vip service not configured, please check it !") sys.exit(errno.EPERM) group_dic = {} data = vip_loadconf(self.config.vip_conf) for group in data.keys(): host_dic = {} if not json_show: print "-----------------------------------------------------" print "%s(%s):" % (group, data[group]["type"]) else: host_dic["type"] = data[group]["type"] def _warp(h): try: cmd = "python2 %s getvip --group %s" % ( self.config.uss_node, group) x, y = exec_remote(h, cmd) vips = x.strip() if vips == "": vips = "None" if not json_show: print " host:%s --> vip:%s\n" % (h, vips) else: host_dic[h] = vips except Exp, e: if not json_show: print " host:%s --> vip:None\n" % (h) else: host_dic[h] = "None" group_dic[group] = host_dic args = [[x] for x in data[group]["nodes"].split(',')] mutil_exec(_warp, args)
def vipdel(self, group, host, vip): if host == None and vip == None: derror("vip and host can not be null at the same time.") sys.exit(1) cmd = "python2 %s del -g %s" % (self.config.uss_vip, group) if vip is not None: cmd = cmd + " -v %s" % (vip) if host is not None: cmd = cmd + " -H %s" % (host) #print 'cmd',cmd def _warp(h): x, y = exec_remote(h, cmd) #print "stat host: %s \n%s" % (h, x) if y: print y args = [[x] for x in self.config.cluster.keys()] mutil_exec(_warp, args)
if dic.has_key(hosts[i%node_num]): dic[str(hosts[i%node_num])] = dic[str(hosts[i%node_num])] + ',' + vip_list[i] else: dic[str(hosts[i%node_num])] = vip_list[i] def _warp(h): try: dmsg("try set vip:%s on host:%s..." % (dic[h], h)) cmd = "%s setvip --vips %s --mask %s" % (config.uss_node, dic[h], mask) exec_remote(h, cmd) dmsg("set vip:%s on host:%s ok..." % (dic[h], h)) except Exp, e: derror("%s : %s" % (h, e)) args = [[h] for (h) in dic.keys()] mutil_exec(_warp, args) def _get_host_vipnum_min(host2vip_dic, valid_hosts): _num = 0 _min = 999999 _min_host = "" for host in valid_hosts: _num = len(host2vip_dic[host]) if _num < _min: _min = _num _min_host = host return _min_host def set_vip_by_priority(config, hosts, mask, vip_list, host2vip_dic):
return host_list # all host in cluster should be updated def meta_update(version, hosts=None): def _update_version(k): update_cmd = "echo %s > /opt/sdfs/%s/%s/status/version " % (version, k[1], k[2]) try: dmsg('update meta %s %s.%s version to %s' % (k[0], k[1], k[2], version)) exec_remote(k[0], update_cmd) except Exp, e: derror("%s : %s" % (k, e)) if hosts != []: args = [[k] for (k) in hosts] mutil_exec(_update_version, args) if __name__ == "__main__": hosts = [] version = "meta \(2017Q3\)" try: verify = raw_input(darkyellow("Are you sure to update version from apple to %s? (y/n) " % (version))).lower() if verify == 'y': hosts = get_hosts() meta_update(version, hosts) else: print "not update version." sys.exit(1) except KeyboardInterrupt:
_dmsg("host:%s permit to remove, begin remove..." % (host)) try: cmd = "etcdctl member list | grep %s | awk '{print $1}' | awk -F':' '{print $1}'| awk -F'[' '{print $1}'| xargs etcdctl member remove" % (host) self._exec_node(new_cluster_list[0], cmd) _sysinfo("etcd member remove %s ok !" % (host)) new_del_list.append(host) except Exp, e: _syserror("etcd member remove %s fail ! errmsg:%s" % (host, e.err)) raise Exp(errno.EPERM, "etcd member remove %s fail, %s" % (host, e.err)) if proxy and len(new_del_list) > 0: #set hosts run as proxy mode cmd = "python %s/app/admin/node.py etcd --state %s --hosts %s" % (self.config.home, "existing", ','.join(new_cluster_list)) args = [[x, cmd] for x in del_list] mutil_exec(self._exec_node, args) _sysinfo("etcd member remove %s ok , new_cluster_list:%s!" % (del_list, new_cluster_list)) print "member del hosts:%s ok !" % (new_del_list) def member_list(self): _etcd_cluster = self._etcd_get_init_cluster() for host in _etcd_cluster.keys(): if _etcd_cluster[host] == "true": print "%s : leader" % (host) else: print "%s" % (host) def create_cluster(self, hosts, proxy_nodes=None): cluster_list = hosts.split(',') proxy_list = []
class Cluster(object): def __init__(self, config=None): self.config = config if self.config is None: self.config = Config() def _get_src(self, op): src_tar = None if op not in ["etc", "app", "samba"]: raise Exp(errno.EINVAL, "not support %s" % (op)) if op == "samba": src_file = "/usr/local/samba" else: src_file = os.path.join(self.config.home, op) src_tar = "/tmp/uss_%s.tar.gz" % (op) cmd = "rm -rf %s" % (src_tar) cmd2 = "cd %s" % (src_file) cmd3 = "tar czf %s *" % (src_tar) if op == "samba": cmd3 = cmd3 + " --exclude=./private/* --exclude=./var/* >/dev/null 2>&1" else: cmd3 = cmd3 cmd = " && ".join([cmd, cmd2, cmd3]) exec_shell(cmd) return src_tar def sshkey(self, hosts, password): print 'sshkey', hosts ssh_set_nopassword(hosts, password) def _update(self, src, hosts=None): now = time.strftime('%H:%M:%S') src_tar = self._get_src(src) if src == "samba": dist = "/usr/local/samba" else: dist = os.path.join(self.config.home, src) backup_dir = os.path.join(self.config.home, "backup") backup = os.path.join(backup_dir, "%s-%s.tar.gz" % (src, now)) cmd_backup = "mkdir -p %s" % (backup_dir) cmd_backup = cmd_backup + " && " + \ "mkdir -p %s && cd %s && tar czf %s *" % (dist, dist, backup) tmp_update = '/tmp/%s-tmp-%s.tar.gz' % (src, now) cmd_update = "mkdir -p %s && tar xf %s -C %s > /dev/null && rm %s " % ( dist, tmp_update, dist, tmp_update) def _put_remote_warp(k): # backup and update """ try: dmsg('backup %s to %s' % (dist, backup)) exec_remote(k, cmd_backup) except Exp, e: derror("%s : %s" % (k, e)) """ try: #dmsg('update %s to %s' % (src, k)) put_remote(k, src_tar, tmp_update) exec_remote(k, cmd_update) except Exp, e: derror("%s : %s" % (k, e)) args = [[k] for (k) in hosts] mutil_exec(_put_remote_warp, args)
file_name = os.path.split(src_tar)[1] remote_tmp_path = os.path.join('/tmp', file_name) cmd_update = "mkdir -p %s && tar xf %s -C %s > /dev/null && rm %s " % ( abs_path, remote_tmp_path, abs_path, remote_tmp_path) def _put_remote_warp(k): try: #dmsg('update %s to %s' % (src_tar, k)) put_remote(k, src_tar, remote_tmp_path) #dmsg('install %s to %s' % (src_tar, k)) exec_remote(k, cmd_update) except Exp, e: derror("%s : %s" % (k, e)) args = [[k] for (k) in hosts] mutil_exec(_put_remote_warp, args) def update(self, src=None, tar=None, nfs=None, force=False, hosts=None): if hosts is None: hosts = self.hosts() #print (src, tar, nfs, force, hosts) if tar is not None: self._update_tar(tar, hosts) elif src is not None: self._update(src, hosts) else: self._update("etc", hosts) self._update("app", hosts) def deploy(self, src, dist, hosts):