changeset 16:c64a640558ba

add remove.py
author taiki
date Tue, 10 Mar 2015 06:57:12 +0900
parents 56298c45a7e5
children cefc6c47d109
files Makefile create.py ie-docker.c ie-docker.h numberfile.py portops.py remove.py
diffstat 7 files changed, 189 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Feb 11 00:38:10 2015 +0900
+++ b/Makefile	Tue Mar 10 06:57:12 2015 +0900
@@ -1,4 +1,7 @@
 TARGET = ie-docker
+IEDOCKERDIR = /etc/iecloud/
+PORTRANGE1 = "10000"
+PORTRANGE2 = "12000"
 CFLAGS = -Wall -O2 -g
 
 INSTALL_DIR = /usr/local/bin
@@ -14,9 +17,15 @@
 	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)
+	
 
 clean:
 	rm -f $(TARGET) $(TARGET2) *.o
--- a/create.py	Wed Feb 11 00:38:10 2015 +0900
+++ b/create.py	Tue Mar 10 06:57:12 2015 +0900
@@ -1,10 +1,12 @@
 #!/usr/bin/python
 
+
 import os, re
 import argparse
 import portops
 
 base_path = "/home/k138582/docker-hg/"
+CONTAINER_NUM_LIMIT = 8
 
 def ie_mkdir(projectname):
     username = os.getlogin()
@@ -15,7 +17,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-docker.c	Wed Feb 11 00:38:10 2015 +0900
+++ b/ie-docker.c	Tue Mar 10 06:57:12 2015 +0900
@@ -18,7 +18,7 @@
 { 
     FILE *fp = NULL;
     if ((fp = fopen(portlist_file, "r")) == NULL) {
-        printf("file open error\n");
+        printf("[!] file open error\n");
     }
 
     // file format: portnumber,username,projectname
@@ -38,6 +38,7 @@
         ret = strtok(NULL, ",");
         if (ret == NULL) continue;
         project_name_flag = strncmp(project_name, ret, BUFF_SIZE); 
+        printf("project :%s\n", project_name);
 
         if (user_name_flag == 0 && project_name_flag == 0) {
             printf("port :%s\n", port);
@@ -47,11 +48,33 @@
     }
     fclose(fp);
     if (user_name_flag == 1 || project_name_flag == 1) {
-        printf("[!] can't get port number for %s", user_name);
+        printf("[!] can't get port number for %s\n", user_name);
         exit(1);
     }
 }
 
+void
+parse_exec_command(const int argc, char **argv, exec_command_opt *opt)
+{
+    int i = 2;
+    int parse_ps_flag = 0;
+    for (i = 2; i < argc; i++) {
+        if (argv[i][0] == '-') {
+            if (argv[i][1] == 't') {
+                opt->tty = TRUE;
+            } else if (argv[i][1] == 'i') {
+                opt->interactive = TRUE;
+            } else if (argv[i][1] == 'd') {
+                opt->dettach = TRUE;
+            }
+        } else if (parse_ps_flag) {
+            strncpy(opt->exec_ps_command, argv[i], 64);
+        } else { // image name
+            parse_ps_flag = 1;
+            strncpy(opt->ps_name, argv[i], 16);
+        }
+    }
+}
 
 void
 parse_run_command(const int argc, char **argv, run_command_opt *opt)
@@ -252,7 +275,8 @@
 
 /* main(int argc, char **argv) - main process loop */
 
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
 {
     int gid;
     int uid;
@@ -266,6 +290,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);
@@ -300,33 +330,60 @@
     }
 
     char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH);
+    if (ps_name == NULL) {
+        printf("[!] malloc error.");
+    }
     ps_name[0] = '\0';
 
-    run_command_opt *opt = (run_command_opt *)malloc(sizeof(run_command_opt));
-    opt->tty = FALSE;
-    opt->dettach = FALSE;
-    opt->interactive = FALSE;
+    run_command_opt *run_opt = (run_command_opt *)malloc(sizeof(run_command_opt));
+    exec_command_opt *exec_opt = (exec_command_opt *)malloc(sizeof(exec_command_opt));
+    if (run_opt == NULL && exec_opt == NULL) {
+        printf("[!] malloc error.");
+        exit(1);
+    }
+    run_opt->tty = FALSE;
+    run_opt->dettach = FALSE;
+    run_opt->interactive = FALSE;
+
+    exec_opt->tty = FALSE;
+    exec_opt->dettach = FALSE;
+    exec_opt->interactive = FALSE;
 
     if (strncmp(argv[1], "ps", 4) != 0) {
-        if (strncmp(argv[1], "run", 3) == 0) {
-            parse_run_command(argc, argv, opt);
-            if (check_name(opt->ps_name)) {
+        if (strncmp(argv[1], run_command, 3) == 0) {
+            parse_run_command(argc, argv, run_opt);
+            if (check_name(run_opt->ps_name)) {
                 fprintf(stderr, bad_name);
                 exit(0);
             }
-            get_port_number(name, opt->ps_name, opt->outerport);
-            strncpy(ps_name, opt->ps_name, 64);
-            opt->ps_name[0] = '\0';
-            make_ps_name(opt->ps_name, account_type, name, ps_name);
+            get_port_number(name, run_opt->ps_name, run_opt->outerport);
+            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) == 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);
+            free(exec_opt);
         }
         printf("process name : %s\n", opt->ps_name);
     }
 
     PSLISTPTR pslist = get_pslist(pattern);
 
-    /* Check argv for proper arguments and run 
+    /*
+     * Check argv for proper arguments and run 
      * the corresponding script, if invoked.
      */
 
@@ -334,28 +391,29 @@
         print_pslist(pslist);
     } else if (strncmp(argv[1], run_command, 5) == 0) {
 
-        char *args[16];
+        char *run_args[17];
         int i = 0;
 
-        args[i++] = command;
-        args[i++] = run_command;
-        if (opt->dettach) args[i++] = "-d";
-        if (opt->tty) args[i++] = "-t";
-        if (opt->interactive) args[i++] = "-i";
-        args[i++] = "-m";
-        args[i++] = "512m";
-        args[i++] = "-v";
-        args[i++] = opt->volume;
-        args[i++] = "-p";
+        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";
+        run_args[i++] = "-m";
+        run_args[i++] = "512m";
+        run_args[i++] = "-v";
+        run_args[i++] = run_opt->volume;
+        run_args[i++] = "-p";
         char port[32];
-        sprintf(port, "%s:%s", opt->outerport, opt->innerport);
-        args[i++] = port;
-        args[i++] = "--name";
-        args[i++] = opt->ps_name;
-        args[i++] = opt->image_name;
-        args[i++] = opt->exec_ps_command;
-        args[i++] = NULL;
-
+        sprintf(port, "%s:%s", run_opt->outerport, run_opt->innerport);
+        run_args[i++] = port;
+        run_args[i++] = "--name";
+        run_args[i++] = run_opt->ps_name;
+        run_args[i++] = run_opt->image_name;
+        run_args[i++] = run_opt->exec_ps_command;
+        run_args[i++] = NULL;
+	/*
     printf("run command opt ::memory-%s innerport-%s outerport-%s tty-%d dettach-%d interactive-%d ps_name-%s exec_ps_command-%s volume-%s image-name-%s\n",
             opt->memory,
             opt->innerport,
@@ -368,7 +426,8 @@
             opt->volume,
             opt->image_name);
 
-        if (execv(args[0], args) < 0) {
+*/
+        if (execv(run_args[0], run_args) < 0) {
             perror("[!] Execv:");
         }
     } else if (strncmp(argv[1], start_command, 5) == 0) {
@@ -376,7 +435,20 @@
             perror("[!] Execl:");
         }
     } else if (strncmp(argv[1], exec_command, 5) == 0) {
-        if (execl(command, command, exec_command, argv[2], argv[3], argv[4], NULL) < 0) {
+
+        int i = 0;
+        char *exec_args[8];
+
+        exec_args[i++] = command;
+        exec_args[i++] = exec_command;
+        if (exec_opt->dettach) exec_args[i++] = "-d";
+        if (exec_opt->tty) exec_args[i++] = "-t";
+        if (exec_opt->interactive) exec_args[i++] = "-i";
+        exec_args[i++] = exec_opt->ps_name;
+        exec_args[i++] = exec_opt->exec_ps_command;
+        exec_args[i++] = NULL;
+
+        if (execv(exec_args[0], exec_args) < 0) {
             perror("[!] Execl:");
         }
     } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
@@ -391,10 +463,6 @@
         if (execl(command, command, attach_command, ps_name, NULL) < 0) {
             perror("[!] Execl:");
         }
-    } else if ( strncmp(argv[1], dettach_command, 8) == 0 ) {
-        if (execl(command, command, dettach_command, argv[2], NULL) < 0) {
-            perror("[!] Execl:");
-        }
     } else if ( strncmp(argv[1], rm_command, 2) == 0 ) {
         if (execl(command, command, rm_command, ps_name, NULL) < 0) {
             perror("[!] Execl:");
@@ -403,7 +471,8 @@
         usage();
     }
     free(ps_name);
-    free(opt);
+    free(exec_opt);
+    free(run_opt);
     exit(0);
 }
 
--- a/ie-docker.h	Wed Feb 11 00:38:10 2015 +0900
+++ b/ie-docker.h	Tue Mar 10 06:57:12 2015 +0900
@@ -6,7 +6,6 @@
 #define run_command "run"
 #define build_command "build"
 #define attach_command "attach"
-#define dettach_command "dettach"
 // #define pull_command "pull" /* download docker image command */
 #define images_command "images" /* list images command */
 #define commit_command "commit" /* make image command */
@@ -28,7 +27,7 @@
 const char managers_sym[] = "managers";
 const char guests_sym[] = "guests";
 const char delimiter[] = "_";
-const char portlist_file[] = "/home/k138582/docker-wrapper/iedockerport.list";
+const char portlist_file[] = "/etc/iedocker/iedockerport.list";
 
 enum { 
     NAME_LENGTH = 50,
@@ -67,6 +66,14 @@
     TRUE = 1
 };
 
+typedef struct exec_command_opt_t {
+    int tty; // true = 1; false = 0
+    int dettach; // true = 1; false = 0
+    int interactive; // true = 1; false = 0
+    char ps_name[64]; // user decide name
+    char exec_ps_command[64];
+} exec_command_opt;
+
 typedef struct run_command_opt_t {
     char memory[16];
     char innerport[PORT_LENGTH];
@@ -80,4 +87,5 @@
     char image_name[16];
 } run_command_opt;
 
+
 #endif /* IE_DOCKER */
--- a/numberfile.py	Wed Feb 11 00:38:10 2015 +0900
+++ b/numberfile.py	Tue Mar 10 06:57:12 2015 +0900
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+filename = "iecloudport.list"
+
 import argparse
 
 if __name__ == "__main__":
@@ -8,7 +10,7 @@
     parser.add_argument('limit', metavar='N', help='limit number.')
     args = parser.parse_args()
 
-    f = open("iedockerport.list","w")
+    f = open(filename,"w")
     for num in range(int(args.number), int(args.limit)):
         num_str = str(num)
         f.write(num_str + "\n")
--- a/portops.py	Wed Feb 11 00:38:10 2015 +0900
+++ b/portops.py	Tue Mar 10 06:57:12 2015 +0900
@@ -1,15 +1,21 @@
 #!/usr/bin/python
 
-portlist_file = "/home/k138582/docker-wrapper/iedockerport.list"
+portlist_file = "/etc/iecloud/iedockerport.list"
+CONTAINER_NUM_LIMIT = 8
 testuser = "testuser"
-delimiter = ","
+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)
+        portline = port.split(DELIMITER)
         if (len(portline) < 3):
             continue
         if (portline[1] == user and portline[2] == projectname):
@@ -19,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")
@@ -43,12 +50,30 @@
 def is_limit(portlist, user):
     count = 0
     for port in portlist:
-        portline = port.split(delimiter)
+        portline = port.split(DELIMITER)
         if len(portline) < 2:
             continue
         if portline[1] == user:
             count = count + 1
-    if count < 4:
+    if count < CONTAINER_NUM_LIMIT:
+        return True
+    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
@@ -57,18 +82,23 @@
     portlist = read_port_list()
     port_num = ""
     for port in portlist:
-        portline = port.split(delimiter)
+        portline = port.split(DELIMITER)
         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)
+    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)
+        portline = port.split(DELIMITER)
         if len(portline) < 2:
             continue
         if portline[1] == user:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/remove.py	Tue Mar 10 06:57:12 2015 +0900
@@ -0,0 +1,15 @@
+#!/usr/bin/python
+
+import portops
+import argparse
+import os, sys
+
+if __name__ == "__main__":
+
+    parser = argparse.ArgumentParser(description='Remove port for your project.')
+    parser.add_argument('projectname', metavar='project name', help='ie-docker project name')
+    args = parser.parse_args()
+
+    if not portops.remove_port_list(os.getlogin(), args.projectname):
+        print("[!] Can't remove port for your project.")
+        sys.exit()