comparison portops.py @ 16:c64a640558ba

add remove.py
author taiki
date Tue, 10 Mar 2015 06:57:12 +0900
parents 855a5e399f6e
children
comparison
equal deleted inserted replaced
15:56298c45a7e5 16:c64a640558ba
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 portlist_file = "/home/k138582/docker-wrapper/iedockerport.list" 3 portlist_file = "/etc/iecloud/iedockerport.list"
4 CONTAINER_NUM_LIMIT = 8
4 testuser = "testuser" 5 testuser = "testuser"
5 delimiter = "," 6 DELIMITER = ","
7 HOST = "localhost"
8
9 import sys,os
10 import socket
6 11
7 def remove_port_list(user, projectname): 12 def remove_port_list(user, projectname):
8 portlist = read_port_list() 13 portlist = read_port_list()
9 delete_line = "" 14 delete_line = ""
10 release_port = "" 15 release_port = ""
16 print(user + projectname)
11 for port in portlist: 17 for port in portlist:
12 portline = port.split(delimiter) 18 portline = port.split(DELIMITER)
13 if (len(portline) < 3): 19 if (len(portline) < 3):
14 continue 20 continue
15 if (portline[1] == user and portline[2] == projectname): 21 if (portline[1] == user and portline[2] == projectname):
16 delete_line = port 22 delete_line = port
17 release_port = portline[0] 23 release_port = portline[0]
18 break 24 break
19 25
20 if release_port == "": 26 if release_port == "":
21 print("[!] No remove port.") 27 print("[!] No remove port.")
22 return 28 return False
23 portlist.remove(delete_line) 29 portlist.remove(delete_line)
24 portlist.append(release_port) 30 portlist.append(release_port)
25 write_port_list(portlist) 31 write_port_list(portlist)
32 return True
26 33
27 def read_port_list(): 34 def read_port_list():
28 f = open(portlist_file, "r") 35 f = open(portlist_file, "r")
29 portlist = [] 36 portlist = []
30 for port in f: 37 for port in f:
41 f.close() 48 f.close()
42 49
43 def is_limit(portlist, user): 50 def is_limit(portlist, user):
44 count = 0 51 count = 0
45 for port in portlist: 52 for port in portlist:
46 portline = port.split(delimiter) 53 portline = port.split(DELIMITER)
47 if len(portline) < 2: 54 if len(portline) < 2:
48 continue 55 continue
49 if portline[1] == user: 56 if portline[1] == user:
50 count = count + 1 57 count = count + 1
51 if count < 4: 58 if count < CONTAINER_NUM_LIMIT:
59 return True
60 else:
61 return False
62
63 def connect_to(host, port):
64 try:
65 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
66 sock.connect((host,port))
67 return sock
68 except:
69 sock.close()
70 return None
71
72 def is_used_port(port):
73 sock = connect_to(HOST, port)
74 socket.setdefaulttimeout(5)
75 if sock:
76 sock.close()
52 return True 77 return True
53 else: 78 else:
54 return False 79 return False
55 80
56 def mark_use_port(user, projectname): 81 def mark_use_port(user, projectname):
57 portlist = read_port_list() 82 portlist = read_port_list()
58 port_num = "" 83 port_num = ""
59 for port in portlist: 84 for port in portlist:
60 portline = port.split(delimiter) 85 portline = port.split(DELIMITER)
61 if len(portline) == 1: 86 if len(portline) == 1:
62 port_num = portline[0] 87 port_num = portline[0]
63 break 88 break
89 # if is_used_port(port_num):
90 # print("[!] This port is already used.")
91 # sys.exit()
92
93 print("This port is not already used.")
64 portlist.remove(port_num) 94 portlist.remove(port_num)
65 portlist.append(port_num + delimiter + user + delimiter + projectname) 95 portlist.append(port_num + DELIMITER + user + DELIMITER + projectname)
66 write_port_list(portlist) 96 write_port_list(portlist)
67 97
68 def get_marked_port(user): 98 def get_marked_port(user):
69 ports = read_port_list() 99 ports = read_port_list()
70 for port in ports: 100 for port in ports:
71 portline = port.split(delimiter) 101 portline = port.split(DELIMITER)
72 if len(portline) < 2: 102 if len(portline) < 2:
73 continue 103 continue
74 if portline[1] == user: 104 if portline[1] == user:
75 return portline[0] 105 return portline[0]
76 106