Bootstrap

Docker从文件中获取交换机IP段

线上本来是通过dhcp获取IP 启动容器,但是后来发现dhcp分配ip会存在重复分配,解决这个问题是从文件中获取未分配的ip,然后写进已分配的文件中

#!/usr/bin/env python #coding:utf-8 # arnolix 本脚本是用于解决docker dhcp冲突改为 从配置文件中读取IP 分配, 本脚本需要使用root权限执行 import os, sys import time import commands from random import Random import string import getopt class Docker(object): def usage(self): print 'usage:' print '-h,--help: print help message.' print '--run : start a container; EX: python xxx.py --run 172.xxx.xxx.250:5000/xxxx' print '--delete : delete a container; EX: python xxx.py --delete 容器名 or 容器ID' def version(self): print "V0.0.2" def getIp(self): ## 获取IP if os.path.exists('/data/docker/unallocated.conf') and os.path.isfile('/data/docker/unallocated.conf') \ and os.path.exists('/data/docker/allocated.conf') and os.path.isfile('/data/docker/allocated.conf'): file = open('/data/docker/unallocated.conf','r') ip = file.readline() file.close() return ip else: print "Err, docker ip 配置文件不存在 /data/docker/unallowcated.conf,allowcated.conf" sys.exit() def random_str(self,randomlength=20): #获取docker命名中的随即字符 str = '' chars = 'abcdefghijklmnopqrstuvwxyz0123456789' length = len(chars) - 1 random = Random() for i in range(randomlength): str+=chars[random.randint(0, length)] return str def run(self): ## 启动容器 # if len(sys.argv) < 2: # print "\n Usage: python xxx.py 172.xx.xx.250:5000/xxxx \n" # sys.exit() ip = self.getIp() list_del = [] letters = self.random_str() name = commands.getoutput("echo %s |awk -F / '{print $2}'"%(sys.argv[2])) dockername = name + '_' + letters print dockername,sys.argv[2] cmd = 'docker run -itd -e KERISY_ENV=production --net=none --name=%s -v /data/web/%s.xxx.com:/data/web/%s.xxx.com:ro -v /data/logs/%s:/data/logs/nginx:rw -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro %s'%(dockername,name,name,name,sys.argv[2]) cmd1 = "pipework br0 %s %s/[email protected]"%(dockername, ip.strip()) print commands.getoutput('docker pull %s'%(sys.argv[2])) ##获取最新镜像 time.sleep(2) print os.popen(cmd).read() ### 容器启动开始 10秒内 未up状态,IP则不加载进去 超时退出 for i in range(9): checkstats = commands.getoutput("docker ps -a |grep %s |awk '{print $9}'" % (dockername)) if checkstats == 'Up': print os.popen(cmd1).read() print "容器名:%s ,IP : %s 加载成功"%(dockername,ip.strip()) stats = 1 break else: time.sleep(1) stats = 0 if stats == 0: print "容器启动失败,IP加载超时" sys.exit() else: files_del = open('/data/docker/unallocated.conf','r+') files_add = open('/data/docker/allocated.conf','a+') files_del_list = files_del.readlines() for j in files_del_list[0]: files_add.write(j) del files_del_list[0] files_del.close() files_del_sec = open('/data/docker/unallocated.conf','w+') for i in files_del_list: files_del_sec.write(i) files_del_sec.flush() files_add.flush() files_del_sec.close() files_add.close() def delete(self): cmd = '''docker exec -it %s ifconfig |grep "inet addr" |awk '{print $2}' |awk -F ':' '{print $2}'|grep -v "127.0.0.1" '''%(sys.argv[2]) cmd2 = 'docker stop %s && docker rm %s'%(sys.argv[2],sys.argv[2]) del_ip = os.popen(cmd).read() cmd3 = 'sed -i "/%s/d" /data/docker/allocated.conf' %(del_ip.strip()) print os.popen(cmd2).read(),"该名容器已删除" os.popen(cmd3).read() os.popen("sed -i '$a\%s' /data/docker/unallocated.conf" %(del_ip)) ## 判断重复行IP 报警未设置 def main(self,argv): try: opts, args = getopt.getopt(argv[1:], 'hvr:d::', ['help', 'version', 'run=', 'delete=']) except getopt.GetoptError, err: print str(err) self.usage() sys.exit(2) for o, a in opts: if o in ('-h', '--help'): self.usage() sys.exit(1) elif o in ('-v', '--version'): self.version() elif o in ('-r','--run'): self.run() elif o in ('-d','--delete'): self.delete() else: print 'unhandled option' sys.exit(2) if __name__ == '__main__': go = Docker() go.main(sys.argv)

  

转载于:https://www.cnblogs.com/arnolix/p/5587846.html

;