示例#1
0
def image_delete(request,id):
	user = request.user
	image = models.Images.objects.get(id=int(id))
	if image:
		if image.image_type:
			try:
				info = subprocess.Popen("curl -v --raw -X DELETE %s:5000/v1/repositories/%s/" % (image.node.node_ip,image.repository) , shell=True,stdout=subprocess.PIPE).stdout.readlines()
				str_info = ''
				for i in info:
					str_info += i;
				if re.findall('200 OK',str_info):
					image.delete()
					Refresh_image()
					return HttpResponseRedirect(reverse('listimageurl'))
				else:
					err = "删除%s失败" % image.repository
					return render_to_response('docker/listimages.html',{'user':user,'err':err})
				
			except:
				print traceback.format_exc()
				err = "删除%s失败" % image.repository
				return HttpResponseRedirect(reverse('listimageurl'))
				# return render_to_response('auto_docker/listimages.html',{'user':user,'err':err})
		else:
			try:
				docker_cli = DockerApi(image.node.node_ip)
				docker_cli.remove_image(image.repository)
				Refresh_image()
				return HttpResponseRedirect(reverse('listimageurl'))
			except:
				print traceback.format_exc()
				return HttpResponseRedirect(reverse('listimageurl'))
示例#2
0
def image_build(request):
	user = request.user
	registry_node = models.Node.objects.get(node_type="registory")
	try:
		if request.method == 'POST':
			data = ''
			dockerfile = request.POST.get('dockerfile').encode("utf-8")
			# datapath = os.getcwd() + '/auto_docker/views/dockerfile1'
			# f = open(datapath,'r+')
			# data=f.write(dockerfile)
			# f.close()
			# print dockerfile
			# print type(dockerfile)
			image_name = request.POST.get('image_name').encode("utf-8")
			tag = "%s:5000/%s" % (registry_node.node_ip,image_name)
			# print tag
			docker_cli = DockerApi(registry_node.node_ip)
			# print docker_cli.build(dockerfile,tag)
			# print "+++++++++++++++++++++++++++++"
			# print docker_cli.push(tag)
			for i in docker_cli.build(dockerfile,tag):
				data = data + i
			data +="Push image: %s \n\n" % tag
			for i in docker_cli.push(tag):
				data = data + i 
			pull_images()
			Refresh_image()
			return render_to_response('docker/buildresult.html',{'user':user,'data':data})
		else:
			return render_to_response('docker/buildimage.html',{'user':user})
	except:
		print traceback.format_exc()
		return HttpResponseRedirect(reverse('listimageurl'))
示例#3
0
def container_stop(request, id):
    user = request.user
    container = models.Containers.objects.get(id=int(id))
    try:
        if container:
            docker_cli = DockerApi(container.node.node_ip)
            docker_cli.stop(container.container_name)
            container.status = False
            return HttpResponseRedirect(reverse('listcontainerurl'))
    except:
        print traceback.format_exc()
        return HttpResponseRedirect(reverse('listcontainerurl'))
示例#4
0
def container_stop(request,id):
	user = request.user
	container = models.Containers.objects.get(id=int(id))
	try:
		if container:
			docker_cli = DockerApi(container.node.node_ip)
			docker_cli.stop(container.container_name)
			container.status = False
			return HttpResponseRedirect(reverse('listcontainerurl'))
	except:
		print traceback.format_exc()
		return HttpResponseRedirect(reverse('listcontainerurl'))
示例#5
0
def pull_images():
	try:
		registry_images = models.Images.objects.filter(image_type=True)
		nodes=models.Node.objects.filter(node_type="node")
		registry_node = models.Node.objects.get(node_type="registory")
		for i in registry_images:
			# print i.repository
			for j in nodes:
				repository="%s:5000/%s:latest" % (registry_node.node_ip,i.repository)
				if not models.Images.objects.filter(node__node_ip=j.node_ip).filter(repository=repository):
					docker_cli = DockerApi(j.node_ip)
					# print j.node_ip
					images = docker_cli.pull("%s:5000/%s" % (registry_node.node_ip,i.repository))
	except:
		print traceback.format_exc()
示例#6
0
def check_containers(node_ip):
	if node_ip:
		name_list = []
		try:
			docker_cli = DockerApi(node_ip)
			containers = docker_cli.containers()
			# print containers
		except:
			# pass
			print traceback.format_exc()
		con_info = {}
		for i in containers:
			if  re.match('Exited',i['Status']):
				con_info['status'] = False
			else:
				con_info['status'] = True
			con_info['image'] = i['Image'].encode('utf-8')
			con_info['container_name'] = i['Names'][0].encode('utf-8').replace('/','')
			name_list.append(con_info['container_name'])
			con_info['create_time'] = timestamp_datetime(int(i['Created']))
			con_info['container_id'] = i['Id'].encode('utf-8')
			if con_info['status']:
				con_info['container_ip'] = match_ip(docker_cli.exec_start(docker_cli.exec_create(con_info['container_name'],'ip addr')['Id']))
			else:
				con_info['container_ip'] = ''
			container = models.Containers.objects.filter(node__node_ip=node_ip).filter(container_name=con_info['container_name'])
			if container:
				if container[0].status != con_info['status']:
					container[0].status = con_info['status']
				if container[0].container_ip != con_info['container_ip'] and con_info['container_ip']:
					container[0].container_ip = con_info['container_ip']
			else:
				try:
					models.Containers.objects.create(user=models.User.objects.get(username='******'),container_name=con_info['container_name'],container_id=con_info['container_id'],container_ip=con_info['container_ip'],image=con_info['image'],create_time=con_info['create_time'],node=models.Node.objects.get(node_ip=node_ip),status= con_info['status'])
				except:
					print traceback.format_exc()
		try:
			container_list = models.Containers.objects.filter(node__node_ip=node_ip).filter(node__node_ip=node_ip)
			if container_list:
				for i in container_list:
					if i.container_name not in name_list:
						i.delete()
		except:
			# pass
			print traceback.format_exc()
示例#7
0
def pull_images():
    try:
        registry_images = models.Images.objects.filter(image_type=True)
        nodes = models.Node.objects.filter(node_type="node")
        registry_node = models.Node.objects.get(node_type="registory")
        for i in registry_images:
            # print i.repository
            for j in nodes:
                repository = "%s:5000/%s:latest" % (registry_node.node_ip,
                                                    i.repository)
                if not models.Images.objects.filter(
                        node__node_ip=j.node_ip).filter(repository=repository):
                    docker_cli = DockerApi(j.node_ip)
                    # print j.node_ip
                    images = docker_cli.pull(
                        "%s:5000/%s" % (registry_node.node_ip, i.repository))
    except:
        print traceback.format_exc()
示例#8
0
def image_commit(request,id):
	user = request.user
	container = models.Containers.objects.get(id=int(id))
	registry_node = models.Node.objects.get(node_type="registory")
	try:
		if request.method == 'POST' and request.POST.get('image_name'):
			image_name = request.POST.get('image_name').encode("utf-8")
			repository = "%s:5000/%s" % (registry_node.node_ip,image_name)
			# print image_name
			if container:
				docker_cli = DockerApi(container.node.node_ip)
				docker_cli.commit(container.container_name,repository)
				docker_cli.push(repository)
				pull_images()
				Refresh_image()
			return HttpResponseRedirect(reverse('listimageurl'))
	except:
		print traceback.format_exc()
		return HttpResponseRedirect(reverse('listimageurl'))
示例#9
0
def image_delete(request, id):
    user = request.user
    image = models.Images.objects.get(id=int(id))
    if image:
        if image.image_type:
            try:
                info = subprocess.Popen(
                    "curl -v --raw -X DELETE %s:5000/v1/repositories/%s/" %
                    (image.node.node_ip, image.repository),
                    shell=True,
                    stdout=subprocess.PIPE).stdout.readlines()
                str_info = ''
                for i in info:
                    str_info += i
                if re.findall('200 OK', str_info):
                    image.delete()
                    Refresh_image()
                    return HttpResponseRedirect(reverse('listimageurl'))
                else:
                    err = "删除%s失败" % image.repository
                    return render_to_response('docker/listimages.html', {
                        'user': user,
                        'err': err
                    })

            except:
                print traceback.format_exc()
                err = "删除%s失败" % image.repository
                return HttpResponseRedirect(reverse('listimageurl'))
                # return render_to_response('auto_docker/listimages.html',{'user':user,'err':err})
        else:
            try:
                docker_cli = DockerApi(image.node.node_ip)
                docker_cli.remove_image(image.repository)
                Refresh_image()
                return HttpResponseRedirect(reverse('listimageurl'))
            except:
                print traceback.format_exc()
                return HttpResponseRedirect(reverse('listimageurl'))
示例#10
0
def image_build(request):
    user = request.user
    registry_node = models.Node.objects.get(node_type="registory")
    try:
        if request.method == 'POST':
            data = ''
            dockerfile = request.POST.get('dockerfile').encode("utf-8")
            # datapath = os.getcwd() + '/auto_docker/views/dockerfile1'
            # f = open(datapath,'r+')
            # data=f.write(dockerfile)
            # f.close()
            # print dockerfile
            # print type(dockerfile)
            image_name = request.POST.get('image_name').encode("utf-8")
            tag = "%s:5000/%s" % (registry_node.node_ip, image_name)
            # print tag
            docker_cli = DockerApi(registry_node.node_ip)
            # print docker_cli.build(dockerfile,tag)
            # print "+++++++++++++++++++++++++++++"
            # print docker_cli.push(tag)
            for i in docker_cli.build(dockerfile, tag):
                data = data + i
            data += "Push image: %s \n\n" % tag
            for i in docker_cli.push(tag):
                data = data + i
            pull_images()
            Refresh_image()
            return render_to_response('docker/buildresult.html', {
                'user': user,
                'data': data
            })
        else:
            return render_to_response('docker/buildimage.html', {'user': user})
    except:
        print traceback.format_exc()
        return HttpResponseRedirect(reverse('listimageurl'))
示例#11
0
def image_commit(request, id):
    user = request.user
    container = models.Containers.objects.get(id=int(id))
    registry_node = models.Node.objects.get(node_type="registory")
    try:
        if request.method == 'POST' and request.POST.get('image_name'):
            image_name = request.POST.get('image_name').encode("utf-8")
            repository = "%s:5000/%s" % (registry_node.node_ip, image_name)
            # print image_name
            if container:
                docker_cli = DockerApi(container.node.node_ip)
                docker_cli.commit(container.container_name, repository)
                docker_cli.push(repository)
                pull_images()
                Refresh_image()
            return HttpResponseRedirect(reverse('listimageurl'))
    except:
        print traceback.format_exc()
        return HttpResponseRedirect(reverse('listimageurl'))
示例#12
0
def container_create(request):
    user = request.user
    try:
        if request.method == 'POST' and request.POST.get(
                'container') and request.POST.get('image'):
            container = request.POST.get('container').encode("utf-8")
            image = request.POST.get('image').encode("utf-8")
            node = request.POST.get('node').encode("utf-8")

            if node == "random":
                try:
                    create_node = models.Node.objects.get(weight_type="True")
                    docker_cli = DockerApi(create_node.node_ip)
                    registry_node = models.Node.objects.get(
                        node_type="registory")
                    repository = "%s:5000/%s:latest" % (registry_node.node_ip,
                                                        image)
                    if not models.Images.objects.filter(
                            repository=repository).filter(
                                node__node_ip=create_node.node_ip):
                        images = docker_cli.pull(
                            "%s:5000/%s" % (registry_node.node_ip, image))
                        # print images
                    cmd = "/usr/bin/sh /root/create_docker_container_use_dhcp_ip.sh  %s %s:5000/%s" % (
                        container, registry_node.node_ip, image)
                    # cmd ="ifconfig"
                    # print cmd
                    ssh_docker = paramiko.SSHClient()
                    ssh_docker.set_missing_host_key_policy(
                        paramiko.AutoAddPolicy())
                    ssh_docker.connect(create_node.node_ip,
                                       22,
                                       "root",
                                       create_node.root_password,
                                       timeout=5)
                    stdin, stdout, stderr = ssh_docker.exec_command(cmd)
                    # print "#####"
                    # stdin.write("Y")   #简单交互,输入 ‘Y’
                    create_container_info = stdout.readlines()
                    print create_container_info
                    try:
                        for info in create_container_info:
                            # print type(info)
                            # print info
                            if re.search('Container_name', info):
                                tag = 0
                                node_count = models.Node.objects.all().count()
                                tag = int(create_node.weight) % node_count + 1
                                if tag == node_count:
                                    tag = 1
                                # print create_node.node_ip
                                # print create_node.weight_type
                                create_node.weight_type = False
                                create_node.save()
                                # print create_node.node_ip
                                # print create_node.weight_type
                                now_create_node = models.Node.objects.get(
                                    weight=tag)
                                # print now_create_node.node_ip
                                # print now_create_node.weight_type
                                now_create_node.weight_type = True
                                now_create_node.save()
                                # print now_create_node.node_ip
                                # print now_create_node.weight_type
                                create_container = eval(info.strip('\n'))
                                containers = docker_cli.containers()
                                cont_id = ''
                                cont_image = ''
                                for i in containers:
                                    if i['Names'][0].encode('utf-8').replace(
                                            '/', '') == container:
                                        cont_id = i['Id'].encode('utf-8')
                                        cont_image = i['Image'].encode('utf-8')
                                if cont_id:
                                    models.Containers.objects.create(
                                        user=user,
                                        container_name=container,
                                        container_id=cont_id,
                                        container_ip=create_container[
                                            "Container_ip"],
                                        image=cont_image,
                                        create_time=create_container[
                                            "Container_create"],
                                        node=create_node)
                    except:
                        print traceback.format_exc()
                except:
                    print traceback.format_exc()
            else:
                try:
                    create_node = models.Node.objects.get(node_ip=node)
                    docker_cli = DockerApi(create_node.node_ip)
                    registry_node = models.Node.objects.get(
                        node_type="registory")
                    repository = "%s:5000/%s:latest" % (registry_node.node_ip,
                                                        image)
                    if not models.Images.objects.filter(
                            repository=repository).filter(
                                node__node_ip=create_node.node_ip):
                        images = docker_cli.pull(
                            "%s:5000/%s" % (registry_node.node_ip, image))
                        # print images
                    cmd = "/usr/bin/sh /root/create_docker_container_use_dhcp_ip.sh  %s %s:5000/%s" % (
                        container, registry_node.node_ip, image)
                    # cmd ="ifconfig"
                    ssh_docker = paramiko.SSHClient()
                    ssh_docker.set_missing_host_key_policy(
                        paramiko.AutoAddPolicy())
                    ssh_docker.connect(create_node.node_ip,
                                       22,
                                       "root",
                                       create_node.root_password,
                                       timeout=5)
                    stdin, stdout, stderr = ssh_docker.exec_command(cmd)
                    #stdin.write("Y")   #简单交互,输入 ‘Y’
                    create_container_info = stdout.readlines()
                    for info in create_container_info:
                        if re.search('Container_name', info):
                            create_container = eval(info.strip('\n'))
                            containers = docker_cli.containers()
                            cont_id = ''
                            cont_image = ''
                            for i in containers:
                                if i['Names'][0].encode('utf-8').replace(
                                        '/', '') == container:
                                    cont_id = i['Id'].encode('utf-8')
                                    cont_image = i['Image'].encode('utf-8')
                            if cont_id:
                                models.Containers.objects.create(
                                    user=user,
                                    container_name=container,
                                    container_id=cont_id,
                                    container_ip=create_container[
                                        "Container_ip"],
                                    image=cont_image,
                                    create_time=create_container[
                                        "Container_create"],
                                    node=create_node)
                except:
                    print traceback.format_exc()
            return HttpResponseRedirect(reverse('listcontainerurl'))
        else:
            nodes = models.Node.objects.filter(node_type='node')
            images = models.Images.objects.filter(image_type='True')
            return render_to_response('docker/addcontainer.html', {
                'user': user,
                'nodes': nodes,
                'images': images
            })
    except:
        print traceback.format_exc()
        return HttpResponseRedirect(reverse('listcontainerurl'))
示例#13
0
def check_images(node_ip, tag):
    if node_ip:
        name_list = []
        try:
            docker_cli = DockerApi(node_ip)
            images = docker_cli.images()
        except:
            # pass
            print traceback.format_exc()
        image_info = {}
        image_name = []
        for i in images:
            image_info['size'] = str(
                float('%.2f' % (float(i['VirtualSize']) / 1000000)))
            for j in i['RepoTags']:
                image_name.append(j.encode('utf-8'))
                name_list.append(j.encode('utf-8'))
            image_info['image_id'] = i['Id'].encode('utf-8')
            for i in image_name:
                image = models.Images.objects.filter(
                    node__node_ip=node_ip).filter(repository=i).filter(
                        image_type=False)
                if image:
                    if image[0].repository == i and image[
                            0].image_id != image_info['image_id']:
                        image[0].image_id = image_info['image_id']
                        image[0].size = image_info['size']
                else:
                    try:
                        models.Images.objects.create(
                            repository=i,
                            image_id=image_info['image_id'],
                            size=image_info['size'],
                            node=models.Node.objects.get(node_ip=node_ip),
                            image_type=False)
                    except:
                        # pass
                        print traceback.format_exc()
        try:
            image_list = models.Images.objects.filter(
                node__node_ip=node_ip).filter(image_type=False)
            if image_list:
                for i in image_list:
                    if i.repository not in name_list:
                        i.delete()
        except:
            # pass
            print traceback.format_exc()
        if tag == 'registory':
            name_list = []
            try:
                images = subprocess.Popen(
                    "curl -X GET %s:5000/v1/search" % node_ip,
                    shell=True,
                    stdout=subprocess.PIPE).stdout.readlines()
                if images:
                    # print type(images)
                    # print images
                    images = eval(images[0])['results']
                    for i in images:
                        image_info['repository'] = i['name'].encode('utf-8')
                        name_list.append(image_info['repository'])
                        image = models.Images.objects.filter(
                            node__node_ip=node_ip).filter(
                                repository=image_info['repository']).filter(
                                    image_type=True)
                        if not image:
                            try:
                                models.Images.objects.create(
                                    repository=image_info['repository'],
                                    node=models.Node.objects.get(
                                        node_ip=node_ip),
                                    image_type=True)
                            except:
                                # pass
                                print traceback.format_exc()
                    try:
                        image_list = models.Images.objects.filter(
                            node__node_ip=node_ip).filter(
                                node__node_ip=node_ip).filter(image_type=True)
                        if image_list:
                            for i in image_list:
                                if i.repository not in name_list:
                                    i.delete()
                    except:
                        # pass
                        print traceback.format_exc()
            except:
                # pass
                print traceback.format_exc()
    else:
        pass
示例#14
0
def check_containers(node_ip):
    if node_ip:
        name_list = []
        try:
            docker_cli = DockerApi(node_ip)
            containers = docker_cli.containers()
            # print containers
        except:
            # pass
            print traceback.format_exc()
        con_info = {}
        for i in containers:
            if re.match('Exited', i['Status']):
                con_info['status'] = False
            else:
                con_info['status'] = True
            con_info['image'] = i['Image'].encode('utf-8')
            con_info['container_name'] = i['Names'][0].encode('utf-8').replace(
                '/', '')
            name_list.append(con_info['container_name'])
            con_info['create_time'] = timestamp_datetime(int(i['Created']))
            con_info['container_id'] = i['Id'].encode('utf-8')
            if con_info['status']:
                con_info['container_ip'] = match_ip(
                    docker_cli.exec_start(
                        docker_cli.exec_create(con_info['container_name'],
                                               'ip addr')['Id']))
            else:
                con_info['container_ip'] = ''
            container = models.Containers.objects.filter(
                node__node_ip=node_ip).filter(
                    container_name=con_info['container_name'])
            if container:
                if container[0].status != con_info['status']:
                    container[0].status = con_info['status']
                if container[0].container_ip != con_info[
                        'container_ip'] and con_info['container_ip']:
                    container[0].container_ip = con_info['container_ip']
            else:
                try:
                    models.Containers.objects.create(
                        user=models.User.objects.get(username='******'),
                        container_name=con_info['container_name'],
                        container_id=con_info['container_id'],
                        container_ip=con_info['container_ip'],
                        image=con_info['image'],
                        create_time=con_info['create_time'],
                        node=models.Node.objects.get(node_ip=node_ip),
                        status=con_info['status'])
                except:
                    print traceback.format_exc()
        try:
            container_list = models.Containers.objects.filter(
                node__node_ip=node_ip).filter(node__node_ip=node_ip)
            if container_list:
                for i in container_list:
                    if i.container_name not in name_list:
                        i.delete()
        except:
            # pass
            print traceback.format_exc()
示例#15
0
def container_create(request):
	user = request.user		
	try:
		if request.method == 'POST' and request.POST.get('container') and request.POST.get('image'):
			container = request.POST.get('container').encode("utf-8")
			image = request.POST.get('image').encode("utf-8")
			node = request.POST.get('node').encode("utf-8")
			
			if node == "random":
				try:
					create_node = models.Node.objects.get(weight_type="True")
					docker_cli = DockerApi(create_node.node_ip)
					registry_node = models.Node.objects.get(node_type="registory")
					repository="%s:5000/%s:latest" % (registry_node.node_ip,image)
					if not models.Images.objects.filter(repository=repository).filter(node__node_ip=create_node.node_ip):
						images = docker_cli.pull("%s:5000/%s" % (registry_node.node_ip,image))
						# print images
					cmd = "/usr/bin/sh /root/create_docker_container_use_dhcp_ip.sh  %s %s:5000/%s" % (container,registry_node.node_ip,image)
					# cmd ="ifconfig"
					# print cmd
					ssh_docker = paramiko.SSHClient()  
					ssh_docker.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
					ssh_docker.connect(create_node.node_ip,22,"root",create_node.root_password,timeout=5)
					stdin, stdout, stderr = ssh_docker.exec_command(cmd)  
					# print "#####"
					# stdin.write("Y")   #简单交互,输入 ‘Y’   
					create_container_info = stdout.readlines()
					print create_container_info
					try:
						for info in create_container_info:
							# print type(info)
							# print info
							if re.search('Container_name',info):
								tag = 0
								node_count = models.Node.objects.all().count()
								tag = int(create_node.weight)%node_count + 1
								if tag == node_count:
									tag = 1
								# print create_node.node_ip
								# print create_node.weight_type
								create_node.weight_type=False
								create_node.save()
								# print create_node.node_ip
								# print create_node.weight_type
								now_create_node = models.Node.objects.get(weight=tag)
								# print now_create_node.node_ip
								# print now_create_node.weight_type
								now_create_node.weight_type=True
								now_create_node.save()
								# print now_create_node.node_ip
								# print now_create_node.weight_type
								create_container = eval(info.strip('\n'))
								containers = docker_cli.containers()
								cont_id = ''
								cont_image = ''
								for i in containers:
									if i['Names'][0].encode('utf-8').replace('/','') == container:
										cont_id=i['Id'].encode('utf-8')
										cont_image = i['Image'].encode('utf-8')
								if cont_id:
									models.Containers.objects.create(user=user,container_name=container,container_id=cont_id,container_ip = create_container["Container_ip"],image=cont_image,create_time=create_container["Container_create"],node=create_node)
					except:
						print traceback.format_exc()
				except:
					print traceback.format_exc()
			else:
				try:
					create_node = models.Node.objects.get(node_ip=node)
					docker_cli = DockerApi(create_node.node_ip)
					registry_node = models.Node.objects.get(node_type="registory")
					repository="%s:5000/%s:latest" % (registry_node.node_ip,image)
					if not models.Images.objects.filter(repository=repository).filter(node__node_ip=create_node.node_ip):
						images = docker_cli.pull("%s:5000/%s" % (registry_node.node_ip,image))
						# print images
					cmd = "/usr/bin/sh /root/create_docker_container_use_dhcp_ip.sh  %s %s:5000/%s" % (container,registry_node.node_ip,image)
					# cmd ="ifconfig"
					ssh_docker = paramiko.SSHClient()  
					ssh_docker.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
					ssh_docker.connect(create_node.node_ip,22,"root",create_node.root_password,timeout=5)
					stdin, stdout, stderr = ssh_docker.exec_command(cmd)  
					#stdin.write("Y")   #简单交互,输入 ‘Y’   
					create_container_info = stdout.readlines()
					for info in create_container_info:
						if  re.search('Container_name',info): 
							create_container = eval(info.strip('\n'))
							containers = docker_cli.containers()
							cont_id = ''
							cont_image = ''
							for i in containers:
								if i['Names'][0].encode('utf-8').replace('/','') == container:
									cont_id=i['Id'].encode('utf-8')
									cont_image = i['Image'].encode('utf-8')
							if cont_id:
								models.Containers.objects.create(user=user,container_name=container,container_id=cont_id,container_ip = create_container["Container_ip"],image=cont_image,create_time=create_container["Container_create"],node=create_node)
				except:
					print traceback.format_exc()
			return HttpResponseRedirect(reverse('listcontainerurl'))
		else:
			nodes = models.Node.objects.filter(node_type='node')
			images = models.Images.objects.filter(image_type='True')
			return render_to_response('docker/addcontainer.html',{'user':user,'nodes':nodes,'images':images})
	except:
		print traceback.format_exc()
		return HttpResponseRedirect(reverse('listcontainerurl'))
示例#16
0
def check_images(node_ip,tag):
	if node_ip:
		name_list = []
		try:
			docker_cli = DockerApi(node_ip)
			images = docker_cli.images()
		except:
			# pass
			print traceback.format_exc()
		image_info = {}
		image_name = []
		for i in images:
			image_info['size']=str(float('%.2f' % (float(i['VirtualSize'])/1000000)))
			for j in i['RepoTags']:
				image_name.append(j.encode('utf-8'))
				name_list.append(j.encode('utf-8'))
			image_info['image_id']=i['Id'].encode('utf-8')
			for i in image_name:
				image = models.Images.objects.filter(node__node_ip=node_ip).filter(repository=i).filter(image_type=False)
				if image:
					if image[0].repository == i and image[0].image_id != image_info['image_id']:
						image[0].image_id = image_info['image_id']
						image[0].size = image_info['size']
				else:
					try:
						models.Images.objects.create(repository=i,image_id=image_info['image_id'],size=image_info['size'],node=models.Node.objects.get(node_ip=node_ip),image_type=False)
					except:
						# pass
						print traceback.format_exc()
		try:
			image_list = models.Images.objects.filter(node__node_ip=node_ip).filter(image_type=False)
			if image_list:
				for i in image_list:
					if i.repository not in name_list:
						i.delete()
		except:
			# pass
			print traceback.format_exc()
		if tag == 'registory':
			name_list = []
			try:
				images = subprocess.Popen("curl -X GET %s:5000/v1/search" % node_ip , shell=True,stdout=subprocess.PIPE).stdout.readlines()
				if images:
					# print type(images)
					# print images
					images = eval(images[0])['results']
					for i in images:
						image_info['repository'] = i['name'].encode('utf-8')
						name_list.append(image_info['repository'])
						image = models.Images.objects.filter(node__node_ip=node_ip).filter(repository=image_info['repository']).filter(image_type=True)
						if not image:
							try:
								models.Images.objects.create(repository=image_info['repository'],node=models.Node.objects.get(node_ip=node_ip),image_type=True)
							except:
								# pass
								print traceback.format_exc()
					try:
						image_list = models.Images.objects.filter(node__node_ip=node_ip).filter(node__node_ip=node_ip).filter(image_type=True)
						if image_list:
							for i in image_list:
								if i.repository not in name_list:
									i.delete()
					except:
						# pass
						print traceback.format_exc()
			except:
				# pass
				print traceback.format_exc()
	else:
		pass