Mercurial > hg > Applications > docker-wrapper
changeset 12:1730494d9ecc
add create
author | taiki |
---|---|
date | Mon, 02 Feb 2015 16:38:32 +0900 |
parents | 5e2df576a42d |
children | 8e1f57c91210 |
files | Makefile create.py ie-docker.c portops.py |
diffstat | 4 files changed, 171 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Mon Jan 26 13:20:35 2015 +0900 +++ b/Makefile Mon Feb 02 16:38:32 2015 +0900 @@ -11,9 +11,9 @@ sudo chmod 4711 $(TARGET) install: $(TARGET) - install ie-docker $(INSTALL_DIR) + install $(TARGET) $(INSTALL_DIR) install create.py $(INSTALL_DIR) - chmod 4711 $(INSTALL_DIR)/ie-docker + chmod 4711 $(INSTALL_DIR)/$(TARGET) chmod 755 $(INSTALL_DIR)/create.py clean:
--- a/create.py Mon Jan 26 13:20:35 2015 +0900 +++ b/create.py Mon Feb 02 16:38:32 2015 +0900 @@ -2,6 +2,7 @@ import os, re import argparse +import portops base_path = "/media/fcs/docker-hg/" @@ -15,29 +16,16 @@ os.system("/bin/chown "+os.getlogin()+" "+ dir) if (len(os.listdir(dir)) > 4): - print("Too many project.") + print("[!] Too many project.") exit() dir = dir + os.sep + projectname mkdir1(dir) - os.system("/bin/chown "+os.getlogin()+" "+ dir) + os.system("/bin/chown " + os.getlogin() + " " + dir) return dir - print("Permission denied. You are not permitted user.") + 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 @@ -80,10 +68,16 @@ # 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() + if not portops.reserve_port(os.getlogin(), args.projectname): + print("[!] Can't get port to use for project.") + print("[!] Check your number of projects.") + sys.exit() + projpath = ie_mkdir(args.projectname) make_hgrepo(projpath) @@ -91,4 +85,5 @@ print("Create your project on " + projpath) print("Exec on your client : hg clone ssh://your_account@" + os.uname()[1] + os.sep + projpath) - + +# end
--- a/ie-docker.c Mon Jan 26 13:20:35 2015 +0900 +++ b/ie-docker.c Mon Feb 02 16:38:32 2015 +0900 @@ -10,55 +10,70 @@ #include <sys/types.h> #include <regex.h> -/* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ +#include "ie-docker.h" + +void +get_port_number(const char *user_name, char *project_name, char *port_number) +{ + FILE *fp = NULL; + if ((fp = fopen(portlist_file, "r")) == NULL) { + printf("file open error\n"); + } -#define command "/usr/bin/docker" -#define ps_command "/usr/bin/docker ps -a" -#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 */ -#define rm_command "rm" /* remove container command */ -#define rmi_command "rmi" /* remove image command */ -#define start_command "start" -#define stop_command "stop" -#define exec_command "exec" + // portnumber,username,projectname + char buff[BUFF_SIZE]; + char *port; + int user_name_flag = 1; + int project_name_flag = 1; + while (fgets(buff, BUFF_SIZE, fp) != NULL) { + buff[strlen(buff) - 1] = '\0'; -#define create_command "create" + port = strtok(buff, ","); -static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n"; + char *ret = strtok(NULL, ","); + if (ret == NULL) continue; + user_name_flag = strncmp(user_name, ret, BUFF_SIZE); + + ret = strtok(NULL, ","); + if (ret == NULL) continue; + project_name_flag = strncmp(project_name, ret, BUFF_SIZE); -const char *guests[] = {"mata"}; -const char *managers[] = {"taira"}; -const char students_sym[] = "students"; -const char managers_sym[] = "managers"; -const char guests_sym[] = "guests"; -const char delimiter[] = "_"; - -enum { - NAME_LENGTH = 50, - PS_NAME_LENGTH = 50 -}; + if (user_name_flag == 0 && project_name_flag == 0) { + printf("port :%s\n", port); + strncpy(port, port_number, BUFF_SIZE); + break; + } + } + fclose(fp); +} -enum { - STUDENTS, - GUESTS, - MANAGERS -}; - -#define PSNAME_MAX (512) - -typedef struct pslist { - char name[PSNAME_MAX]; - struct pslist *next; -} PSLIST, *PSLISTPTR; - -#define NEW(type) ((type*)malloc(sizeof(type))) - -/* Define global variables */ +void +parse_run_command(const int argc, char **argv, run_command_opt *opt) +{ + int i = 0; + for (i = 0; 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 (argv[i][1] == 'v') { + strncpy(opt->volume, argv[i + 1], 16); + i++; + } else if (argv[i][1] == 'p') { + strncpy(opt->innerport, argv[i + 1], 16); + } + } else if(strncmp(argv[i], "--name", 2)) { // process name + strncpy(opt->process_name, argv[i + 1], 16); + i++; + } else { // image name + strncpy(opt->image_name, argv[i], 16); + i++; + } + } +} PSLISTPTR get_pslist(regex_t *list_pattern) @@ -75,6 +90,7 @@ } p->name[0] = 0; pclose(fp); + return list; } @@ -92,6 +108,7 @@ for(;list && list->name[0]; list = list->next) { if (strstr(list->name,arg)!=0) return 1; } + return 0; } @@ -226,8 +243,6 @@ setegid(getgid()); seteuid(getuid()); - - int account_type = check_user_name(name); if (account_type < 0) { fprintf(stderr, "Permission denied. :%s\n", name); @@ -263,29 +278,22 @@ char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH); ps_name[0] = '\0'; + run_command_opt *opt = (run_command_opt *)malloc(sizeof(run_command_opt)); if (strncmp(argv[1], "ps", 4) != 0) { if (strncmp(argv[1], "run", 3) == 0) { - make_ps_name(ps_name, account_type, name, argv[4]); + parse_run_command(argc, argv, opt); + if (check_name(opt->process_name)) { + fprintf(stderr, bad_name); + exit(0); + } + make_ps_name(ps_name, account_type, name, opt->process_name); + get_port_number(); } else { make_ps_name(ps_name, account_type, name, argv[2]); } printf("%s", ps_name); } - if (argc >= 3) { - if (strncmp(argv[1], run_command, 6) == 0 ) { - if (strncmp(argv[3], "--name", 6) != 0) { - run_usage(); - exit(0); - } - - if (check_name(argv[4])) { - fprintf(stderr, bad_name); - exit(0); - } - } - } - PSLISTPTR pslist = get_pslist(pattern); /* Check argv for proper arguments and run @@ -328,8 +336,9 @@ } } else { usage(); - exit(1); } + free(ps_name); + free(opt); exit(0); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/portops.py Mon Feb 02 16:38:32 2015 +0900 @@ -0,0 +1,86 @@ +#!/usr/bin/python + +portlist_file = "iedockerport.list" +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 < 4: + 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")