changeset 3:6ebce132ee89

ie-docker for cloud
author taira
date Sun, 15 Feb 2015 19:20:32 +0900
parents 8de3ca550e8f
children 939072d0981b
files Makefile create.py ie-cloud.c portops.py
diffstat 4 files changed, 52 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Thu Feb 12 10:38:14 2015 +0900
+++ b/Makefile	Sun Feb 15 19:20:32 2015 +0900
@@ -16,9 +16,11 @@
 	install $(TARGET) $(INSTALL_DIR)
 	install create.py $(INSTALL_DIR)
 	install portops.py $(INSTALL_DIR)
+	install remove.py $(INSTALL_DIR)
 	chmod 4711 $(INSTALL_DIR)/$(TARGET)
 	chmod 755 $(INSTALL_DIR)/create.py
 	chmod 755 $(INSTALL_DIR)/portops.py
+	chmod 755 $(INSTALL_DIR)/remove.py
 	-mkdir $(IEDOCKERDIR)
 	python numberfile.py $(PORTRANGE1) $(PORTRANGE2)
 	-cp iecloudport.list $(IEDOCKERDIR)
--- a/create.py	Thu Feb 12 10:38:14 2015 +0900
+++ b/create.py	Sun Feb 15 19:20:32 2015 +0900
@@ -5,6 +5,7 @@
 import portops
 
 base_path = "/home/k138582/docker-hg/"
+CONTAINER_NUM_LIMIT = 8
 
 def ie_mkdir(projectname):
     username = os.getlogin()
@@ -15,7 +16,7 @@
         mkdir1(dir)
         os.system("/bin/chown "+os.getlogin()+" "+ dir)
 
-        if (len(os.listdir(dir)) > 4):
+        if (len(os.listdir(dir)) > CONTAINER_NUM_LIMIT):
             print("[!] Too many project.")
             exit()
 
--- a/ie-cloud.c	Thu Feb 12 10:38:14 2015 +0900
+++ b/ie-cloud.c	Sun Feb 15 19:20:32 2015 +0900
@@ -291,6 +291,12 @@
     setegid(getgid());
     seteuid(getuid());
 
+    FILE *fp = NULL;
+    if ((fp = fopen("output", "w")) == NULL) {
+        fputs("test\n", fp);
+    }
+    fclose(fp);
+
     int account_type = check_user_name(name);
     if (account_type < 0) {
         fprintf(stderr, "[!] Permission denied. :%s\n", name);
@@ -355,10 +361,18 @@
             strncpy(ps_name, run_opt->ps_name, 64);
             run_opt->ps_name[0] = '\0';
             make_ps_name(run_opt->ps_name, account_type, name, ps_name);
-        } else if (strncmp(argv[1], exec_command, 4)){
+        } else if (strncmp(argv[1], exec_command, 4) == 0){
             parse_exec_command(argc, argv, exec_opt);
             strncpy(ps_name, exec_opt->ps_name, 64);
             make_ps_name(exec_opt->ps_name, account_type, name, ps_name);
+        } else if (strncmp(argv[1], rm_command, 4) == 0) {
+            char exec[512];
+            sprintf(exec, "/usr/local/bin/remove.py %s", argv[2]);
+            system(exec);
+
+            make_ps_name(ps_name, account_type, name, argv[2]);
+            free(run_opt);
+            free(exec_opt);
         } else {
             make_ps_name(ps_name, account_type, name, argv[2]);
             free(run_opt);
@@ -377,11 +391,12 @@
         print_pslist(pslist);
     } else if (strncmp(argv[1], run_command, 5) == 0) {
 
-        char *run_args[16];
+        char *run_args[17];
         int i = 0;
 
         run_args[i++] = command;
         run_args[i++] = run_command;
+        run_args[i++] = "--privileged";
         if (run_opt->dettach) run_args[i++] = "-d";
         if (run_opt->tty) run_args[i++] = "-t";
         if (run_opt->interactive) run_args[i++] = "-i";
--- a/portops.py	Thu Feb 12 10:38:14 2015 +0900
+++ b/portops.py	Sun Feb 15 19:20:32 2015 +0900
@@ -1,14 +1,19 @@
 #!/usr/bin/python
 
 portlist_file = "/etc/iecloud/iecloudport.list"
-CONTAINER_NUM_LIMIT = 4
+CONTAINER_NUM_LIMIT = 8
 testuser = "testuser"
 DELIMITER = ","
+HOST = "localhost"
+
+import sys,os
+import socket
 
 def remove_port_list(user, projectname):
     portlist = read_port_list()
     delete_line = ""
     release_port = ""
+    print(user + projectname)
     for port in portlist:
         portline = port.split(DELIMITER)
         if (len(portline) < 3):
@@ -20,10 +25,11 @@
 
     if release_port == "":
         print("[!] No remove port.")
-        return
+        return False
     portlist.remove(delete_line)
     portlist.append(release_port)
     write_port_list(portlist)
+    return True
 
 def read_port_list():
     f = open(portlist_file, "r")
@@ -54,6 +60,24 @@
     else:
         return False
 
+def connect_to(host, port):
+    try:
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        sock.connect((host,port))
+        return sock
+    except:
+        sock.close()
+        return None
+
+def is_used_port(port):
+    sock = connect_to(HOST, port)
+    socket.setdefaulttimeout(5)
+    if sock:
+        sock.close()
+        return True
+    else:
+        return False
+
 def mark_use_port(user, projectname):
     portlist = read_port_list()
     port_num = ""
@@ -62,6 +86,11 @@
         if len(portline) == 1:
             port_num = portline[0]
             break
+#    if is_used_port(port_num):
+#        print("[!] This port is already used.")
+#        sys.exit()
+        
+    print("This port is not already used.")
     portlist.remove(port_num)
     portlist.append(port_num + DELIMITER + user + DELIMITER + projectname)
     write_port_list(portlist)