changeset 11:5e2df576a42d

add new command 'create' to ie-docker, create is make new repository for ie-docker.
author taira
date Mon, 26 Jan 2015 13:20:35 +0900
parents 9d6fb0a1aa53
children 1730494d9ecc
files Makefile create.py ie-docker.c
diffstat 3 files changed, 115 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Nov 17 11:49:43 2014 +0900
+++ b/Makefile	Mon Jan 26 13:20:35 2015 +0900
@@ -12,7 +12,9 @@
 
 install: $(TARGET) 
 	install ie-docker $(INSTALL_DIR)
+	install create.py $(INSTALL_DIR)
 	chmod 4711 $(INSTALL_DIR)/ie-docker
+	chmod 755 $(INSTALL_DIR)/create.py
 
 clean:
 	rm -f $(TARGET) $(TARGET2) *.o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/create.py	Mon Jan 26 13:20:35 2015 +0900
@@ -0,0 +1,94 @@
+#!/usr/bin/python
+
+import os, re
+import argparse
+
+base_path = "/media/fcs/docker-hg/"
+
+def ie_mkdir(projectname):
+    username = os.getlogin()
+    m = re.match('^([ek]\d\d[58]\d\d\d)$', username)
+    if m is not None:
+        dir = base_path + "students/" + username[:3] + os.sep + username
+        
+        mkdir1(dir)
+        os.system("/bin/chown "+os.getlogin()+" "+ dir)
+
+        if (len(os.listdir(dir)) > 4):
+            print("Too many project.")
+            exit()
+
+        dir = dir + os.sep + projectname
+        mkdir1(dir)
+        os.system("/bin/chown "+os.getlogin()+" "+ dir)
+        return dir
+    print("Permission denied. You are not permitted user.")
+    exit()
+        
+
+def check_name(name):
+    m=re.match('^([ek](\d\d)[58]\d\d\d)$', name)
+    if m is not None:
+        if m.group(1)==m.group(2):
+            return 0
+        else:
+            return 1
+    elif re.match('^[-a-z0-9]+',name):
+        return 0
+    else:
+        return 1
+
+# make necessary sub directory
+#   /etc/libvirt/qemu/teachers
+#   /var/log/libvirt/qemu/teachers
+#   /var/run/libvirt/qemu/teachers
+
+def mkdir1(name):
+    if not os.path.exists(name):
+        os.makedirs(name);
+
+def change_own(base_path):
+    if not os.path.isdir(base_path):
+        os.system("/bin/chown " + os.getlogin() + " " + base_path)
+        return
+
+    for f in os.listdir(base_path):
+        path = os.path.join(base_path, f)
+        os.system("/bin/chown " + os.getlogin() + " " + path)
+        print(path)
+        if os.path.isdir(path):
+            change_own(path)
+
+
+def make_hgrepo(projpath):
+    os.chdir(projpath)
+    os.system("hg init")
+
+    dockerfile = "Dockerfile" 
+    fd = open(dockerfile, "w")
+    fd.write("# Using ie-docker, you sould edit this file\n")
+    fd.write("FROM fedora\n")
+    fd.close()
+
+    os.system("hg add " + dockerfile)
+    os.system("hg commit -u " + os.getlogin() + " -m systemcommit")
+
+    change_own(os.getcwd())
+
+#
+# main method
+#
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description='Create new project for ie-docker.')
+    parser.add_argument('projectname', metavar='project name', help='ie-docker project name')
+    args = parser.parse_args()
+
+    projpath = ie_mkdir(args.projectname)
+    
+    make_hgrepo(projpath)
+
+    print("Create your project on " + projpath)
+    print("Exec on your client : hg clone ssh://your_account@" + os.uname()[1] + os.sep + projpath)
+
+    
--- a/ie-docker.c	Mon Nov 17 11:49:43 2014 +0900
+++ b/ie-docker.c	Mon Jan 26 13:20:35 2015 +0900
@@ -1,5 +1,3 @@
-
-
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -27,6 +25,9 @@
 #define rmi_command "rmi" /* remove image command */
 #define start_command "start" 
 #define stop_command "stop" 
+#define exec_command "exec"
+
+#define create_command "create"
 
 static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n";
 
@@ -123,6 +124,9 @@
         exit(0);
     }
 
+    ret = regexec(pattern, account_name, (size_t) 0, NULL, 0);
+    regfree(pattern);
+
     if (!ret) {
         return STUDENTS;
     }
@@ -222,12 +226,13 @@
     setegid(getgid());
     seteuid(getuid());
 
+
+
     int account_type = check_user_name(name);
     if (account_type < 0) {
         fprintf(stderr, "Permission denied. :%s\n", name);
     }
 
-
     /* Confirm user is in GROUP(999) group */
 
     /*
@@ -249,6 +254,13 @@
     setgid(0);
     setuid(0);
 
+    if (strncmp(argv[1], create_command, 6) == 0) {
+        char exec[512];
+        sprintf(exec, "/usr/local/bin/create.py %s", argv[2]);
+        system(exec);
+        exit(1);
+    }
+
     char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH);
     ps_name[0] = '\0';
     if (strncmp(argv[1], "ps", 4) != 0) {
@@ -290,6 +302,10 @@
         if (execl(command, command, start_command, argv[2], NULL) < 0) {
             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) {
+            perror("Execl:");
+        }
     } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
         if (execl(command, command, stop_command, argv[2], NULL) < 0) {
             perror("Execl:");