可视化代码:
import docker
import subprocess
import tkinter
import tkinter.messagebox
import random
import paramiko
import tkinter.font as tkFont
from PIL import Image, ImageTk
from tkinter import ttk
import os
host_share = '/mpi'
docker_file = '/mpi_config'
volumes = {}
temp_volumes = {}
temp_volumes['bind'] = docker_file
temp_volumes['mode'] = 'rw'
volumes[host_share] = temp_volumes
temp_volumes2 = {}
temp_volumes2['bind'] = '/data'
temp_volumes2['mode'] = 'rw'
volumes['/data/'] = temp_volumes2
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="192.168.149.49", port=22, username="aph", password="aph")
ip1 = '192.168.149.152'
ip2 = '192.168.149.49'
docker_images = 'yjs:v1'
connect1 = ip1 + ':2375'
connect2 = ip2 + ':2375'
client1 = docker.DockerClient(base_url=connect1)
client2 = docker.DockerClient(base_url=connect2)
network = client1.networks.get('multihost')
CPU_liu = 8
CPU_miao = 8
def show_state():
create_Five = tkinter.Toplevel()
frame_main = tkinter.Frame(create_Five, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
create_Five.title('state')
create_Five.geometry('800x600') # 窗体大小
os.system("docker stats --no-stream >x")
command = "docker stats --no-stream"
stdin, stdout, stderr = ssh.exec_command(command)
for i in stdout.readlines():
with open("x", "a") as fp:
fp.write(i)
with open("x","r") as fp:
state=fp.read()
stae_label=ttk.Label(frame_main,text=state,font=tkFont.Font(family="微软雅黑", size=12, weight=tkFont.NORMAL))
stae_label.pack()
def write_config():
file_address = host_share + '/config'
containers1 = client1.containers.list()
containers2 = client2.containers.list()
containers_ip = network.attrs['Containers']
ips = []
with open(file_address, mode='w', encoding='utf-8') as file_obj:
for container in containers1:
# stats = container.stats(decode=True)
# stats_value = next(stats)
# pids_num = stats_value['pids_stats']['current']
for container_ip in containers_ip:
key = container_ip
value = containers_ip[key]
if value['Name'] == container.name:
ip = value['IPv4Address'][:-3]
if value['Name'] == 'master_liu':
file_obj.write(ip + ':' + str(1) + '\n')
else:
ips.append(ip)
for container in containers2:
# stats = container.stats(decode=True)
# stats_value = next(stats)
# pids_num = stats_value['pids_stats']['current']
for container_ip in containers_ip:
key = container_ip
value = containers_ip[key]
if value['Name'] == container.name:
ip = value['IPv4Address'][:-3]
if value['Name'] == 'master_miao':
file_obj.write(ip + ':' + str(1) + '\n')
else:
ips.append(ip)
for i in range(len(ips)):
file_obj.write(ips[i] + ':' + str(1) + '\n')
def create():
create_one = tkinter.Toplevel()
frame_main = tkinter.Frame(create_one, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
create_one.title('创建虚拟机')
create_one.geometry('800x600') # 窗体大小
global memory, CPU_number, docker_name, docker_place
number3 = tkinter.StringVar()
memory = ttk.Combobox(create_one, width=12, textvariable=number3)
memory['values'] = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16')
memory.place(width=250, x=500, y=20, height=20)
memory.current(0)
memory_label = tkinter.Label(create_one, text='请输入最大内存(单位GB)(范围1-16)',bg='whitesmoke')
memory_label.place(x=20, y=20, width=400, height=20)
number1 = tkinter.StringVar()
CPU_number = ttk.Combobox(create_one, width=12, textvariable=number1)
CPU_number['values'] = ('1', '2', '3', '4', '5', '6', '7', '8')
CPU_number.place(width=250, x=500, y=70, height=20)
CPU_number.current(0)
CPU_number_label = tkinter.Label(create_one, text='请输入CPU核数目(范围1-8)',bg='whitesmoke')
CPU_number_label.place(x=20, y=70, width=400, height=20)
docker_name = tkinter.Entry(create_one)
docker_name.place(width=250, x=500, y=120, height=20)
docker_name_label = tkinter.Label(create_one, text='请输入虚拟机名字', width=80,bg='whitesmoke')
docker_name_label.place(x=20, y=120, width=400, height=20)
number2 = tkinter.StringVar()
docker_place = ttk.Combobox(create_one, width=12, textvariable=number2)
docker_place['values'] = ('0', '1')
docker_place.place(width=250, x=500, y=170, height=20)
docker_place.current(0)
docker_place_label = tkinter.Label(create_one, text='请选择在那一台物理机上创建docker(0, 1):',bg='whitesmoke')
docker_place_label.place(x=20, y=170, width=400, height=20)
button2 = tkinter.Button(create_one, text='创建虚拟机', command=docker_create, font=('Arial', 12), width=50, height=2)
button2.place(x=170, y=300)
def docker_check_status(container, flag):
result = {}
result['name'] = container.name
containers_ip = network.attrs['Containers']
ip = '----'
result['ipv4'] = ip
for container_ip in containers_ip:
key = container_ip
value = containers_ip[key]
if value['Name'] == container.name:
ip = value['IPv4Address'][:-3]
result['ipv4'] = ip
break
stats = container.stats(decode=True)
stats_value = next(stats)
short_id = container.short_id
status = container.status
result['status'] = status
result['id'] = short_id
# view_time = stats_value['read'][:-11]
# view_time = view_time[0:11] + str((int(view_time[11:13]) + 8) % 24) + view_time[13:]
# result['view_time'] = view_time
try:
pids_num = stats_value['pids_stats']['current']
except:
pids_num = 0
result['pid_num'] = pids_num
if flag == 0:
out = subprocess.Popen('docker stats {}'.format(container.name), shell=True, stdout=subprocess.PIPE)
for n, v in enumerate(out.stdout):
if n == 1:
value = bytes.decode(v.strip()).split(' ')
valuelist = [j for j in value if j != '' and j != '/']
result['cpu_percentage'] = valuelist[2]
result['memory_use'] = valuelist[3]
result['memory_total'] = valuelist[4]
result['memory_percentage'] = valuelist[5]
out.terminate()
break
if flag == 1:
command = "docker stats --no-stream " + container.name
stdin, stdout, stderr = ssh.exec_command(command)
a = (stdout.read().decode()).split('\n')[1]
b = a.split(' ')
list1 = []
for i in range(len(b)):
if len(b[i]) > 1:
list1.append(b[i])
result['cpu_percentage'] = list1[2]
result['memory_use'] = list1[3]
result['memory_total'] = list1[4]
result['memory_percentage'] = list1[5]
return result
def start():
create_three = tkinter.Toplevel()
frame_main = tkinter.Frame(create_three, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
create_three.title('开启虚拟机')
create_three.geometry('800x600') # 窗体大小
global name2, docker_place2
number2 = tkinter.StringVar()
name2 = ttk.Combobox(create_three, width=12, textvariable=number2)
containers1 = client1.containers.list(all=True)
containers2 = client2.containers.list(all=True)
containers1_run = client1.containers.list()
containers2_run = client2.containers.list()
list_temp = []
for container in containers1:
if container not in containers1_run:
list_temp.append(container.name)
for container in containers2:
if container not in containers2_run:
list_temp.append(container.name)
if len(list_temp) == 0:
list_temp.append('所有的docker容器都已经开启')
name2['values'] = tuple(list_temp)
name2.place(width=250, x=500, y=70, height=20)
name2.current(0)
# name2 = tkinter.Entry(create_three)
# name2.place(width=250, x=500, y=70, height=20)
name2_label = tkinter.Label(create_three, text='请输入要开启的虚拟机名字',bg='whitesmoke')
name2_label.place(x=20, y=70, width=400, height=20)
number1 = tkinter.StringVar()
docker_place2 = ttk.Combobox(create_three, width=12, textvariable=number1)
docker_place2['values'] = ('0', '1')
docker_place2.place(width=250, x=500, y=170, height=20)
docker_place2.current(0)
docker_place2_label1 = tkinter.Label(create_three, text='请选择在那一台物理机上开启docker:0,1):',bg='whitesmoke')
docker_place2_label1.place(x=20, y=170, width=400, height=20)
button1 = tkinter.Button(create_three, text='开启虚拟机', command=start_container, font=('Arial', 12), width=50,
height=2)
button1.place(x=170, y=300)
def remove():
create_four = tkinter.Toplevel()
frame_main = tkinter.Frame(create_four, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
create_four.title('删除虚拟机')
create_four.geometry('800x600') # 窗体大小
global name1, docker_place1
number1 = tkinter.StringVar()
name1 = ttk.Combobox(create_four, width=12, textvariable=number1)
containers1 = client1.containers.list(all=True)
containers2 = client2.containers.list(all=True)
list_temp = []
for container in containers1:
list_temp.append(container.name)
for container in containers2:
list_temp.append(container.name)
name1['values'] = tuple(list_temp)
name1.place(width=250, x=500, y=70, height=20)
name1.current(0)
name1_label = tkinter.Label(create_four, text='请输入要删除的虚拟机名字',bg='whitesmoke')
name1_label.place(x=20, y=70, width=400, height=20)
number2 = tkinter.StringVar()
docker_place1 = ttk.Combobox(create_four, width=12, textvariable=number2)
docker_place1['values'] = ('0', '1')
docker_place1.place(width=250, x=500, y=170, height=20)
docker_place1.current(0)
docker_place_label1 = tkinter.Label(create_four, text='请选择在那一台物理机上删除docker(0, 1):',bg='whitesmoke')
docker_place_label1.place(x=20, y=170, width=400, height=20)
button1 = tkinter.Button(create_four, text='删除虚拟机', command=remove_container, font=('Arial', 12), width=50, height=2)
button1.place(x=170, y=300)
def stop():
create_five = tkinter.Toplevel()
frame_main = tkinter.Frame(create_five, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
create_five.title('停止虚拟机')
create_five.geometry('800x600') # 窗体大小
global name3, docker_place3
number1 = tkinter.StringVar()
name3 = ttk.Combobox(create_five, width=12, textvariable=number1)
containers1 = client1.containers.list()
containers2 = client2.containers.list()
list_temp = []
for container in containers1:
list_temp.append(container.name)
for container in containers2:
list_temp.append(container.name)
name3['values'] = tuple(list_temp)
name3.place(width=250, x=500, y=70, height=20)
name3.current(0)
name3_label = tkinter.Label(create_five, text='请输入要停止的虚拟机名字',bg='whitesmoke')
name3_label.place(x=20, y=70, width=400, height=20)
number2 = tkinter.StringVar()
docker_place3 = ttk.Combobox(create_five, width=12, textvariable=number2)
docker_place3['values'] = ('0', '1')
docker_place3.place(width=250, x=500, y=170, height=20)
docker_place3.current(0)
docker_place_label3 = tkinter.Label(create_five, text='请选择在那一台物理机上停止docker(0, 1):',bg='whitesmoke')
docker_place_label3.place(x=20, y=170, width=400, height=20)
button3 = tkinter.Button(create_five, text='停止虚拟机', command=stop_container, font=('Arial', 12), width=50, height=2)
button3.place(x=170, y=300)
def remove_container():
position = docker_place1.get()
name = name1.get()
if position == "0":
client = client1
elif position == '1':
client = client2
if position!='0' and position!='1':
tkinter.messagebox.showwarning("提示", '虚拟机参数输入错误')
return
name_list = []
containers = client.containers.list(all=True)
for container in containers:
name_list.append(container.name)
if name not in name_list:
tkinter.messagebox.showwarning("提示", '要删除的虚拟机不存在')
return
container = client.containers.get(name)
container.stop()
container.remove()
tkinter.messagebox.showwarning("提示", '成功删除虚拟机')
def start_container():
position = docker_place2.get()
name = name2.get()
if position == "0":
client = client1
elif position == '1':
client = client2
if position != '0' and position != '1':
tkinter.messagebox.showwarning("提示", '虚拟机参数输入错误')
return
name_list = []
containers = client.containers.list(all = True)
for container in containers:
name_list.append(container.name)
if name not in name_list:
tkinter.messagebox.showwarning("提示", '要开启的虚拟机不存在')
return
container = client.containers.get(name)
container.start()
tkinter.messagebox.showwarning("提示", '成功开启虚拟机')
def stop_container():
position = docker_place3.get()
name = name3.get()
if position == "0":
client = client1
elif position == '1':
client = client2
if position != '0' and position != '1':
tkinter.messagebox.showwarning("提示", '虚拟机参数输入错误')
return
name_list = []
containers = client.containers.list()
for container in containers:
name_list.append(container.name)
if name not in name_list:
tkinter.messagebox.showwarning("提示", '要停止的虚拟机不存在')
return
container = client.containers.get(name)
container.stop()
tkinter.messagebox.showwarning("提示", '成功停止虚拟机')
root = tkinter.Tk()
root.title('docker虚拟管理') # 标题
root.geometry('1200x800') #窗体大小
root.title("docker虚拟管理") # 窗体标题
frame_top = tkinter.Frame(root, bg="white")
frame_top.pack(fill=tkinter.X)
lbl_image = tkinter.Label(frame_top, width=100, height=80)
lbl_image.pack(side=tkinter.LEFT)
imgtop = Image.open(r'docker.jpg')
img1 = imgtop.resize((100, 80), Image.ANTIALIAS)
tk_img = ImageTk.PhotoImage(img1)
lbl_image.image = tk_img
lbl_image.config(image=tk_img)
tkinter.Label(frame_top, text="云计算课程设计",bg = 'white',font=tkFont.Font(family="微软雅黑", size=16, weight=tkFont.NORMAL)).pack(side=tkinter.LEFT, padx=20)
tkinter.Label(frame_top, text="", bg="white", height=2, font=tkFont.Font(family="微软雅黑", size=20, weight=tkFont.NORMAL)).pack(side=tkinter.RIGHT, padx=200)
frame_main = tkinter.Frame(root, bg="whitesmoke")
frame_main.propagate(False)
frame_main.pack(expand=tkinter.YES, fill=tkinter.BOTH)
frame_maintop = tkinter.Frame(frame_main, bg="white", height=80)
frame_maintop.pack(fill=tkinter.X, padx=20, pady=10)
imagetop = tkinter.Label(frame_maintop, width=180, height=60)
tkinter.Label(frame_maintop, text="Docker虚拟机管理器", bg="white", fg='black', height=4, font=tkFont.Font(family="微软雅黑", size=20, weight=tkFont.BOLD)).pack(side=tkinter.LEFT, padx=450)
frame_mainleft=tkinter.Frame(frame_main, width=550, bg="white",height=300)
frame_mainleft.pack(side=tkinter.LEFT, fill=tkinter.Y, padx=30)
tkinter.Label(frame_mainleft, text="启动功能", bg="white", fg='black', height=2, font=tkFont.Font(family="微软雅黑", size=20,weight=tkFont.BOLD)).pack(anchor=tkinter.W, padx=210, pady=5)
frame_mainleft.propagate(False)
button1 = tkinter.Button(frame_mainleft, text='创建虚拟机', command=create, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
button2 = tkinter.Button(frame_mainleft, text='查看虚拟机状态', command=show_state, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
button3 = tkinter.Button(frame_mainleft, text='开启虚拟机', command=start, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
#button5 = tkinter.Button(frame_mainleft, text='停止虚拟机', command=stop, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
#button4 = tkinter.Button(frame_mainleft, text='删除虚拟机', command=remove, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
frame_mainright = tkinter.Frame(frame_main, bg="white",width=500)
frame_mainright.pack(side=tkinter.RIGHT, expand=tkinter.YES, fill=tkinter.BOTH)
tkinter.Label(frame_mainright, text="删除功能", bg="white", fg='black', height=2, font=tkFont.Font(family="微软雅黑", size=20,weight=tkFont.BOLD)).pack(anchor=tkinter.W, padx=170, pady=5)
frame_mainleft.propagate(False)
button5 = tkinter.Button(frame_mainright, text='停止虚拟机', command=stop, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
button4 = tkinter.Button(frame_mainright, text='删除虚拟机', command=remove, font=tkFont.Font(family="微软雅黑", size=12), width=50, height=2, bg='white').pack(padx=10,pady=5)
frame_mainbottom = tkinter.Frame(frame_main, bg="white",width=500)
frame_mainbottom.pack(side=tkinter.BOTTOM, expand=tkinter.YES, fill=tkinter.BOTH)
tkinter.Label(frame_mainbottom, text="docker功能", bg="white", fg='black', height=2, font=tkFont.Font(family="微软雅黑", size=20,weight=tkFont.BOLD)).pack(anchor=tkinter.W, padx=170, pady=5)
#show_data = tkinter.Label(frame_mainright,text =longtext,bg="white",justify='left',anchor='w',font=tkFont.Font(family="微软雅黑", size=12,weight=tkFont.NORMAL)).pack(padx=10,pady=100)
# button1 = tkinter.Button(root, text='创建虚拟机', command=create, font=('Arial', 12), width=50, height=2, bg='pink')
# button1.place(x=170, y=200)
# button2 = tkinter.Button(root, text='查看虚拟机状态', command=check, font=('Arial', 12), width=50, height=2, bg='pink')
# button2.place(x=170, y=100)
# button2 = tkinter.Button(root, text='开启虚拟机', command=start, font=('Arial', 12), width=50, height=2, bg='pink')
# button2.place(x=170, y=300)
# button2 = tkinter.Button(root, text='删除虚拟机', command=remove, font=('Arial', 12), width=50, height=2, bg='pink')
# button2.place(x=170, y=400)
def docker_create():
position = docker_place.get()
if position == "0":
client = client1
CPU_number_limit = CPU_liu
elif position == '1':
client = client2
CPU_number_limit = CPU_miao
if position!= '0' and position!='1':
tkinter.messagebox.showwarning("提示", '虚拟机参数输入错误')
return
name = docker_name.get()
memory_number = int(eval(memory.get()))
if memory_number>16:
tkinter.messagebox.showwarning("提示", '虚拟机超过主机最大内存容量')
return
mem_limit = memory.get() + 'g'
number = int(eval(CPU_number.get()))
if number>CPU_number_limit:
tkinter.messagebox.showwarning("提示", '虚拟机超过最大CPU核数目')
return
CPU_list = []
CPU_str = ''
while len(CPU_list) < number:
i = random.randint(0,7)
if i not in CPU_list:
CPU_list.append(i)
CPU_str += str(i)
CPU_str += ','
CPU_str = CPU_str[:-1]
container1 = client.containers.run(image=docker_images, name=name, mem_limit=mem_limit, tty=True, privileged=True, detach=True,stdin_open=True,network='multihost',cpuset_cpus=CPU_str, volumes =volumes)
tkinter.messagebox.showwarning("提示", '虚拟机创建成功')
client1.close()
client2.close()
root.mainloop()
ssh.close()
12.py
import os
from heapq import merge
import subprocess
ip_other = input('please input other machine ipv4:')
def over_write_config():
mpi_config = '/mpi/config'
with open(mpi_config,'r',encoding='utf-8') as f:
ips = f.read().split('\n')
del ips[len(ips)-1]
for i in range(len(ips)):
ips[i] = ips[i][:-2]
new_ips = []
for i,ip in enumerate(ips):
try:
p = subprocess.call('ping -c 1 '+ip,shell=True,timeout=10)
except subprocess.TimeoutExpired:
continue
if p == 0:
new_ips.append(ip)
print(new_ips)
with open(mpi_config,'w',encoding='utf-8') as f:
for ip in new_ips:
f.write(ip+':'+str(1)+'\n')
print('update mpi_config')
try:
p = subprocess.call('ping -c 1 ' + ip_other, shell=True, timeout=1)
except subprocess.TimeoutExpired:
print('the '+ip_other+' loss connect')
return
over_write_config()
cmd = 'mpiexec -n 10 -f /mpi/config /mpi_config/sort'
p = subprocess.Popen(cmd,shell=True)
mpi.py
from mpi4py import MPI
import random
import numpy as np
import sys
import time
# Got the communicator and basic information of the all comm
start = time.process_time()
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
count_time = 10000000
# Not parallel
if size <= 1:
print("*"*50)
print("This is not a parallel program!")
print("*"*50)
cal = 0
for i in range(count_time):
x = random.random()
y = random.random()
dst = x*x + y*y
if dst <= 1:
cal += 1
pi = 4*cal/count_time
print("There are %d points in the square." %
count_time) # print real calculate times
print("There are %d points in the circle." % cal)
print("the pi is %f !" % pi)
end = time.process_time()
print("Time used:", (end-start)) # get the process time
sys.exit(0) # program exit
# Parallel
if rank == 0:
width = int(count_time/(size-1)) # count time for every process
real_count = width*(size-1)
# send count time to other process
for i in range(1, size):
comm.send(width, dest=i, tag=0)
print("send count_time to process%d" % i)
# recieve information from ohter process
part_cal = []
for i in range(1, size):
part_cal.append(comm.recv(source=i, tag=11))
np.array(part_cal)
pi = np.array(np.sum(part_cal)/count_time)*4
# print answer
print("There are %d points in the square." %
real_count) # print real calculate times
print("There are %d points in the circle." % np.sum(part_cal))
print("the pi is %f !" % pi)
end = time.process_time()
print("Time used:", (end-start))
else:
# recieve the width
print("This is rank %d" % rank)
part_ct_time = comm.recv(source=0, tag=0)
cal = 0
# calculate the random points
for i in range(part_ct_time):
if i < 5:
print(rank)
x = random.random()
y = random.random()
dst = x*x + y*y
if dst <= 1:
cal += 1
comm.send(cal, dest=0, tag=11) # send cal to rank 0
|