Mercurial > hg > Applications > ie-cloud
view portops.py @ 0:61bc8410d480
add files
author | taiki |
---|---|
date | Wed, 11 Feb 2015 20:23:09 +0900 |
parents | |
children | 6ebce132ee89 |
line wrap: on
line source
#!/usr/bin/python portlist_file = "/etc/iecloud/iecloudport.list" CONTAINER_NUM_LIMIT = 4 testuser = "testuser" DELIMITER = "," def remove_port_list(user, projectname): portlist = read_port_list() delete_line = "" release_port = "" for port in portlist: portline = port.split(DELIMITER) if (len(portline) < 3): continue if (portline[1] == user and portline[2] == projectname): delete_line = port release_port = portline[0] break if release_port == "": print("[!] No remove port.") return portlist.remove(delete_line) portlist.append(release_port) write_port_list(portlist) def read_port_list(): f = open(portlist_file, "r") portlist = [] for port in f: portlist.append(port.rstrip()) f.close() return portlist def write_port_list(portlist): portlist_tmp = [] for port in portlist: portlist_tmp.append(port + "\n") f = open(portlist_file, "w") f.writelines(portlist_tmp) f.close() def is_limit(portlist, user): count = 0 for port in portlist: portline = port.split(DELIMITER) if len(portline) < 2: continue if portline[1] == user: count = count + 1 if count < CONTAINER_NUM_LIMIT: return True else: return False def mark_use_port(user, projectname): portlist = read_port_list() port_num = "" for port in portlist: portline = port.split(DELIMITER) if len(portline) == 1: port_num = portline[0] break portlist.remove(port_num) portlist.append(port_num + DELIMITER + user + DELIMITER + projectname) write_port_list(portlist) def get_marked_port(user): ports = read_port_list() for port in ports: portline = port.split(DELIMITER) if len(portline) < 2: continue if portline[1] == user: return portline[0] def reserve_port(user, projectname): portlist = read_port_list() if not is_limit(portlist, user): return False mark_use_port(user,projectname) return True if __name__ == "__main__": print("put test source.") remove_port_list("taira", "sample")