rocks快速添加节点

1.在任何路径下建立一个文件夹如:addhost

[root@cluster ~]# mkdir addhost

2.在addhost目录下创建一个名为AddHost.py脚本文件和一个名为macs的主机mac地址列表

[root@cluster addhost]# touch AddHost.py macs

3.其中AddHost.py脚本内容如下

#!/usr/bin/env python    
    
import sys    
import os    
    
str1 = "rocks add host node%s cpus=%s membership=compute "  
str2 = "os=linux rack=%s rank=%s"  
str3 = "rocks add host interface node%s iface=eth0 "  
str4 = "ip=%s%s subnet=private name=node%s mac=%s"  
str5 = '"The number of nodes" does not match the number of MAC addresses.'
str6 = '"number of nodes" and "cores of per node" both should be an integer.'
  
cmd1 = str1 + str2  
cmd2 = str3 + str4  
  
class AddHost(object):    
    
    def __init__(self, count, cores, baseip="10.1.255.0",
                 ip="254", rack=0, rank=0, node=1):
        self.__count = count    
        self.__cores = cores  
        self.__baseip = baseip  
        self.__ip = int(ip)     
        self.__rack = rack    
        self.__rank = rank    
        self.__node = node    
    
    def add_host(self):    
        try:    
            fobj = open('macs')    
        except IOError:    
            print '''Sorry, but can't find the file "macs". exiting ...'''    
            sys.exit(1)    
        macs = [mac.strip() for mac in fobj if not mac.startswith("#")]    
        fobj.close()    
        if len(macs) != self.__count:
            print str5, "exiting ..."
            sys.exit(1)
        for mac in macs:    
            while self.__node <= self.__count:    
                print "Adding Host node%s" % self.__node    
                os.system(cmd1 % (self.__node, self.__cores,  
                                self.__rack, self.__rank))    
                os.system(cmd2 % (self.__node, self.__baseip[:-1],
                                self.__ip, self.__node, mac))    
                self.__ip -= 1    
                self.__rank += 1    
                self.__node += 1    
                break    
    
        # Set Host Boot Action    
        print "setting host boot action..."    
        os.system("rocks set host boot compute action=install")    
    
        # Sync Config    
        print "syncing config..."    
        os.system("rocks sync config")    

def _main():    
    import optparse
    Usage = '%prog [options]  '
    parser = optparse.OptionParser(usage=Usage, version="1.0")
    parser.add_option('--baseip', default='10.1.255.0',
                      help='the default segment [default: %default]')
    (opts, args) = parser.parse_args()
    if opts.baseip[0] == '-':
        parser.error('--baseip option requires an argument')
    elif len(opts.baseip.split('.')) != 4:
        print '--baseip option requires an segment, [default: 10.1.255.0]'
        return
    if len(args) != 2:
        parser.print_help()
        return
    #
    try:
        nums = int(args[0])
        cores = int(args[1])
    except ValueError, e:
        print str6
        return
    ah = AddHost(nums, cores, baseip=opts.baseip)    
    ah.add_host()    

if __name__ == "__main__":    
    _main()


例如:要添加10个节点,每个节点8个核心,步骤如下

1.macs里添加好这10台节点对应的管理网口的mac地址,一行一个(从node1到node10),比如

[root@cluster addhost]# cat macs 
00:50:56:33:B5:C0
00:50:56:33:B5:C1
00:50:56:33:B5:C2
00:50:56:33:B5:C3
00:50:56:33:B5:C4
00:50:56:33:B5:C5
00:50:56:33:B5:C6
00:50:56:33:B5:C7
00:50:56:33:B5:C8
00:50:56:33:B5:C9

3.执行脚本

[root@cluster addhost]# python AddHost.py 10 8 
Adding Host node1
Adding Host node2
Adding Host node3
Adding Host node4
Adding Host node5
Adding Host node6
Adding Host node7
Adding Host node8
Adding Host node9
Adding Host node10
setting host boot action...
syncing config...

4.查看已添加的主机列表

[root@cluster addhost]# rocks list host
HOST     MEMBERSHIP CPUS RACK RANK RUNACTION INSTALLACTION
cluster: Frontend   1    0    0    os        install      
node1:   Compute    8    0    0    os        install      
node2:   Compute    8    0    1    os        install      
node3:   Compute    8    0    2    os        install      
node4:   Compute    8    0    3    os        install      
node5:   Compute    8    0    4    os        install      
node6:   Compute    8    0    5    os        install      
node7:   Compute    8    0    6    os        install      
node8:   Compute    8    0    7    os        install      
node9:   Compute    8    0    8    os        install      
node10:  Compute    8    0    9    os        install    

5.运行python AddHost.py --help获取命令选项

 [root@cluster ~]# python AddHost.py --help

Usage: AddHost.py [options]

Options:
  --version        show program's version number and exit
  -h, --help       show this help message and exit
  --baseip=BASEIP  the default segment [default: 10.1.255.0]

6.如果管理网口网段不是10.1.255.0而是172.16.255.0网段,那么该脚本运行如下:

[root@cluster ~]# python AddHost.py --baseip=172.16.255.0 10 8

7.等添加完毕后,各个节点从网络启动,即可获得相应的ip,并开始安装系统


第二版脚本:

#!/usr/bin/env python      
      
import sys      
import os      
      
str1 = "rocks add host %s%s cpus=%s membership=compute "    
str2 = "os=linux rack=%s rank=%s"    
str3 = "rocks add host interface %s%s iface=eth0 "    
str4 = "ip=%s%s subnet=private name=%s%s mac=%s"    
str5 = '"The number of nodes" does not match the number of MAC addresses.'  
str6 = '"number of nodes" and "cores of per node" both should be an integer.'  
    
cmd1 = str1 + str2    
cmd2 = str3 + str4    
    
class AddHost(object):      
      
    def __init__(self, count, cores, baseip="10.1.255.0",  
                 ip="254", rack=0, rank=0, first_number=1,
                 basename='node'):  
        self.__count = count      
        self.__cores = cores    
        self.__baseip = baseip    
        self.__ip = int(ip)       
        self.__rack = rack      
        self.__rank = rank      
        self.__first_number = int(first_number)    
        self.__basename=basename  
      
    def _add_host(self):      
        try:      
            fobj = open('macs')      
        except IOError:      
            print '''''Sorry, but can't find the file "macs". exiting ...'''      
            sys.exit(1)      
        macs = [mac.strip() for mac in fobj if not mac.startswith("#")]      
        fobj.close()      
        if len(macs) != self.__count:  
            print str5, "exiting ..."  
            sys.exit(1)  
        for mac in macs:      
            while self.__first_number <= self.__count:      
                print "Adding Host %s%s" % (self.__basename,
                                            self.__first_number)      
                os.system(cmd1 % (self.__basename, self.__first_number,
                                self.__cores, self.__rack, self.__rank))      
                os.system(cmd2 % (self.__basename, self.__first_number,
                                self.__baseip[:-1], self.__ip, 
                                self.__basename, self.__first_number, mac))      
                self.__ip -= 1      
                self.__rank += 1      
                self.__first_number += 1      
                break      
      
        # Set Host Boot Action      
        print "setting host boot action..."      
        os.system("rocks set host boot compute action=install")      
      
        # Sync Config      
        print "syncing config..."      
        os.system("rocks sync config")      
  
def _main():      
    import optparse  
    Usage = '%prog [options]  '  
    parser = optparse.OptionParser(usage=Usage, version="1.0")  
    parser.add_option('--baseip', default='10.1.255.0',  
            help='the default network segment [default: %default]')  
    parser.add_option('--basename', default='node',  
            help='the hostname prefix [default: %default]')  
    parser.add_option('--first_number', default='1',  
            help="the first number of hostname's suffix [default: %default]")  
    (opts, args) = parser.parse_args()  
    if opts.baseip[0] == '-':
        parser.error('--baseip option requires an argument')  
    elif len(opts.baseip.split('.')) != 4:  
        print '''--baseip option requires an network segment.'''
        return  
    elif opts.baseip.split('.')[-1] != "0":  
        print 'the end of the network segment must be a zero.'  
        return  
    if opts.basename[0] == '-':
        parser.error('--basename option requires an argument')  
    if opts.first_number[0] == '-':
        parser.error('--first_number option requires an argument')  
    if len(args) != 2:  
        parser.print_help()  
        return  
    #  
    try:  
        nums = int(args[0])  
        cores = int(args[1])  
    except ValueError, e:  
        print str6  
        return  

    ah = AddHost(nums, cores, baseip=opts.baseip,
                 basename=opts.basename, first_number=opts.first_number)      
    ah._add_host()      
  
if __name__ == "__main__":      
    _main()  



你可能感兴趣的:(python)