Mercurial > hg > Applications > docker-wrapper
changeset 13:8e1f57c91210
add port manager to ie-docker.
author | taira |
---|---|
date | Tue, 03 Feb 2015 16:49:48 +0900 |
parents | 1730494d9ecc |
children | 855a5e399f6e |
files | Makefile ie-docker.c ie-docker.h numberfile.py portops.py |
diffstat | 5 files changed, 192 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Mon Feb 02 16:38:32 2015 +0900 +++ b/Makefile Tue Feb 03 16:49:48 2015 +0900 @@ -13,8 +13,10 @@ install: $(TARGET) install $(TARGET) $(INSTALL_DIR) install create.py $(INSTALL_DIR) + install portops.py $(INSTALL_DIR) chmod 4711 $(INSTALL_DIR)/$(TARGET) chmod 755 $(INSTALL_DIR)/create.py + chmod 755 $(INSTALL_DIR)/portops.py clean: rm -f $(TARGET) $(TARGET2) *.o
--- a/ie-docker.c Mon Feb 02 16:38:32 2015 +0900 +++ b/ie-docker.c Tue Feb 03 16:49:48 2015 +0900 @@ -12,15 +12,16 @@ #include "ie-docker.h" + void -get_port_number(const char *user_name, char *project_name, char *port_number) +get_port_number(const char *user_name, char const *project_name, char *port_number) { FILE *fp = NULL; if ((fp = fopen(portlist_file, "r")) == NULL) { printf("file open error\n"); } - // portnumber,username,projectname + // file format: portnumber,username,projectname char buff[BUFF_SIZE]; char *port; int user_name_flag = 1; @@ -40,19 +41,28 @@ if (user_name_flag == 0 && project_name_flag == 0) { printf("port :%s\n", port); - strncpy(port, port_number, BUFF_SIZE); + strncpy(port_number, port, PORT_LENGTH); break; } } fclose(fp); + if (user_name_flag == 1 || project_name_flag == 1) { + printf("[!] can't get port number for %s", user_name); + exit(1); + } } + 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] == '-') { + int i = 2; + int parse_image_flag = 0; + for (i = 2; i < argc; i++) { + if(strncmp(argv[i], "--name", 6) == 0) { // process name + strncpy(opt->ps_name, argv[i + 1], 16); + i++; + } else if (argv[i][0] == '-') { if (argv[i][1] == 't') { opt->tty = TRUE; } else if (argv[i][1] == 'i') { @@ -60,19 +70,32 @@ } else if (argv[i][1] == 'd') { opt->dettach = TRUE; } else if (argv[i][1] == 'v') { - strncpy(opt->volume, argv[i + 1], 16); + strncpy(opt->volume, argv[i + 1], 128); i++; } else if (argv[i][1] == 'p') { strncpy(opt->innerport, argv[i + 1], 16); + i++; } - } else if(strncmp(argv[i], "--name", 2)) { // process name - strncpy(opt->process_name, argv[i + 1], 16); - i++; + } else if (parse_image_flag) { // image name + strncpy(opt->exec_ps_command, argv[i], 64); } else { // image name + parse_image_flag = 1; strncpy(opt->image_name, argv[i], 16); - i++; } } + /* + 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, + opt->outerport, + opt->tty, + opt->dettach, + opt->interactive, + opt->ps_name, + opt->exec_ps_command, + opt->volume, + opt->image_name); + */ } PSLISTPTR @@ -201,7 +224,7 @@ bind_name(ps_name, managers_sym, account_name); break; default : - fprintf(stderr, "Error: no registered type name."); + fprintf(stderr, "[!] Error: no registered type name."); return; } @@ -224,7 +247,7 @@ run_usage() { printf("Usage:\tie-docker run\n"); - printf("\tie-docker [option] --name [process_name] {image name}:{tag} [execute command] [argument]"); + printf("\tie-docker [option] --name [ps_name] {image name}:{tag} [execute command] [argument]"); } /* main(int argc, char **argv) - main process loop */ @@ -245,7 +268,7 @@ int account_type = check_user_name(name); if (account_type < 0) { - fprintf(stderr, "Permission denied. :%s\n", name); + fprintf(stderr, "[!] Permission denied. :%s\n", name); } /* Confirm user is in GROUP(999) group */ @@ -278,20 +301,27 @@ 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)); + opt->tty = FALSE; + opt->dettach = FALSE; + 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->process_name)) { + if (check_name(opt->ps_name)) { fprintf(stderr, bad_name); exit(0); } - make_ps_name(ps_name, account_type, name, opt->process_name); - get_port_number(); + 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); } else { make_ps_name(ps_name, account_type, name, argv[2]); } - printf("%s", ps_name); + printf("process name3: %s\n", opt->ps_name); } PSLISTPTR pslist = get_pslist(pattern); @@ -303,36 +333,69 @@ if (argv[1]==0 || strncmp(argv[1], "ps", 4) == 0 ) { print_pslist(pslist); } else if (strncmp(argv[1], run_command, 5) == 0) { - if (execl(command, command, run_command, argv[2], argv[3], ps_name, argv[5], argv[6], argv[7], NULL) < 0) { - perror("Execl:"); + + char *args[15]; + 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++] = "-v"; + args[i++] = opt->volume; + 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; + + 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, + opt->outerport, + opt->tty, + opt->dettach, + opt->interactive, + opt->ps_name, + opt->exec_ps_command, + opt->volume, + opt->image_name); + + if (execv(args[0], args) < 0) { + perror("[!] Execv:"); } } else if (strncmp(argv[1], start_command, 5) == 0) { if (execl(command, command, start_command, argv[2], NULL) < 0) { - perror("Execl:"); + 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:"); + perror("[!] Execl:"); } } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { if (execl(command, command, stop_command, argv[2], NULL) < 0) { - perror("Execl:"); + perror("[!] Execl:"); } } else if ( strncmp(argv[1], build_command, 8) == 0 ) { if (execl(command, command, build_command, argv[2], NULL) < 0) { - perror("Execl:"); + perror("[!] Execl:"); } } else if (strncmp(argv[1], attach_command, 6) == 0 ) { if (execl(command, command, attach_command, NULL) < 0) { - perror("Execl:"); + perror("[!] Execl:"); } } else if ( strncmp(argv[1], dettach_command, 8) == 0 ) { if (execl(command, command, dettach_command, argv[2], NULL) < 0) { - perror("Execl:"); + perror("[!] Execl:"); } } else if ( strncmp(argv[1], rm_command, 2) == 0 ) { if (execl(command, command, rm_command, ps_name, NULL) < 0) { - perror("Execl:"); + perror("[!] Execl:"); } } else { usage();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ie-docker.h Tue Feb 03 16:49:48 2015 +0900 @@ -0,0 +1,83 @@ +/* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ +#ifndef IE_DOCKER + +#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" + +#define create_command "create" + +/* Define global variables */ + +static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n"; + +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[] = "_"; +const char portlist_file[] = "/crhome/taira/hg/docker-wrapper/iedockerport.list"; + +enum { + NAME_LENGTH = 50, + PS_NAME_LENGTH = 50, + RUN_COMMAND_LENGTH = 1024, + PORT_LENGTH = 16, + BUFF_SIZE = 50 +}; + +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))) + +/* docker run option + * -t tty + * --name [process name] + * -v volume + * -m memory + * image name + * -i skeep open tdin + */ + +enum { + FALSE = 0, + TRUE = 1 +}; + +typedef struct run_command_opt_t { + char memory[16]; + char innerport[PORT_LENGTH]; + char outerport[PORT_LENGTH]; // system decide port number + 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]; // + char volume[128]; + char image_name[16]; +} run_command_opt; + +#endif /* IE_DOCKER */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numberfile.py Tue Feb 03 16:49:48 2015 +0900 @@ -0,0 +1,16 @@ +#!/usr/bin/python + +import argparse + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Create new project for ie-docker.') + parser.add_argument('number', metavar='N', help='start number.') + parser.add_argument('limit', metavar='N', help='limit number.') + parser.add_argument('filename', metavar='filename', help='output file name.') + args = parser.parse_args() + + f = open(args.filename,"w") + for num in range(int(args.number), int(args.limit)): + num_str = str(num) + f.write(num_str + "\n") + f.close()