Ejemplo n.º 1
0
def command_handler(soc: socket.socket, msg: str):
    print("command (" + msg + ") is handled by a thread")
    cmd = msg.split()[0]
    args = msg.split()[1:]
    # optlist, args = getopt.getopt(args, 'h', ['help'])
    # print(optlist, args)  # TODO
    if cmd == "help" or msg == '\0' or cmd == "-h" or cmd == "--help":
        # commands.main_help(soc)  # TODO more intelligent
        Help(soc)
    elif cmd == "stop-server":
        set_interaction(soc, False)
        os.kill(os.getpid(), signal.SIGINT)
    elif cmd == "ps":
        Ps(soc, args)
    elif cmd == "run":
        Run(soc, args)
    elif cmd == "rm":
        Rm(soc, args)
    elif cmd == "stop":
        Stop(soc, args)
    elif cmd == "images":
        Images(soc, args)
    else:
        # pass
        set_interaction(soc, False)
        send(soc, "pass", newline=True)
    soc.close()
    client_sockets.remove(soc)
Ejemplo n.º 2
0
 def help_page(self, have_set_interaction):
     if not have_set_interaction:
         set_interaction(self.soc, False)
     path = os.path.join(".", "docs", self.cmd + ".txt")
     if os.path.exists(path):
         with open(path) as f:
             send(self.soc, f.read(), newline=True)
     else:
         send(self.soc, "Docs to be implemented", newline=True)
Ejemplo n.º 3
0
 def handle(self):
     optlist, args = getopt.getopt(self.args, 'h', ['help'])
     print("DEBUG/optlist:", optlist)
     print("DEBUG/args:", args)
     set_interaction(self.soc, False)
     if len(args) == 0 or ('--help', '') in optlist or ('-h',
                                                        '') in optlist:
         self.help_page(True)
     else:
         send(self.soc, run.terminate(args[0]), newline=True)
Ejemplo n.º 4
0
 def handle(self):
     set_interaction(self.soc, False)
     # with open("docs/help.txt") as f:
     #     send(self.soc, f.read(), newline=True)
     send(self.soc, "client.py COMMAND [OPTIONS]", newline=True)
     send(self.soc, "commands: ", newline=False)
     for cmd in sorted(os.listdir("commands2")):
         if not cmd.endswith(".py") or cmd == "help.py":
             continue
         send(self.soc, cmd[:-3] + " ", newline=False)
     send(self.soc, "stop-server",
          newline=True)  # built-in & trailing newline
Ejemplo n.º 5
0
 def handle(self):
     optlist, args = getopt.getopt(self.args, 'h', ['help'])
     set_interaction(self.soc, False)
     if ('--help', '') in optlist or ('-h', '') in optlist:
         self.help_page(True)
     else:
         send(self.soc, "REPOSITORY\tSIZE", newline=True)
         for file in os.listdir("base_images"):
             if file.endswith(".img"):
                 send(self.soc,
                      file[:-4] + "\t\t" + str(
                          os.path.getsize(os.path.join("base_images", file))
                          / 1024**2) + "MB",
                      newline=True)
Ejemplo n.º 6
0
 def handle(self):
     optlist, args = getopt.getopt(self.args, 'h', ['help'])
     set_interaction(self.soc, False)
     if len(args) == 0 or ('--help', '') in optlist or ('-h',
                                                        '') in optlist:
         self.help_page(True)
     else:
         uuid_to_remove = find_uuid(args[0])
         if not uuid_to_remove:
             send(self.soc,
                  "Cannot find container: " + args[0],
                  newline=True)
         elif uuid_to_remove in get_running_containers():
             send(self.soc,
                  "Cannot remove a running container!",
                  newline=True)
         else:
             os.remove(os.path.join("container", uuid_to_remove + '.img'))
             config.delete_record(uuid_to_remove)
             send(self.soc, uuid_to_remove + " is removed", newline=True)
Ejemplo n.º 7
0
 def handle(self):
     set_interaction(self.soc, False)
     optlist, args = getopt.getopt(self.args, 'ah', ['help', 'all'])
     if ('-h', '') in optlist or ('--help', '') in optlist:
         self.help_page(True)
     else:
         flag_show_all = True if ('-a', '') in optlist or (
             '--all', '') in optlist else False
         running = get_running_containers()
         send(self.soc,
              "UUID\t\tIMAGE\tCOMMAND\t\tCREATED\t\t\tSTATUS",
              newline=True)
         for uuid in (get_all_containers() if flag_show_all else running):
             data = config.read_record(uuid)
             compose = uuid[:8] + "\t" + \
                       data['image'] + "\t" + \
                       (data['command'] if type(data['command']) is str else (" ".join(data['command']))[
                                                                             :50]) + "\t" + \
                       datetime.fromtimestamp(data['created_time']).strftime("%Y-%m-%d %H:%M:%S") + "\t" + \
                       ("Up" if (not flag_show_all) or (uuid in running) else "Exited")
             # TODO command length limit
             send(self.soc, compose, newline=True)
Ejemplo n.º 8
0
 def handle(self):
     optlist, args = getopt.getopt(self.args, 'hd', ['help'])
     print("DEBUG/optlist:", optlist)
     print("DEBUG/args:", args)
     if len(args) == 0 or ('--help', '') in optlist or ('-h',
                                                        '') in optlist:
         set_interaction(self.soc, False)
         self.help_page(True)
     elif len(args) >= 1:
         if not os.path.exists(os.path.join("base_images",
                                            args[0] + ".img")):
             # option 1: don't run
             # set_interaction(self.soc, False)
             # return
             # option 2: set default
             args[0] = "ubuntu"
         if ('-d', '') in optlist:
             set_interaction(self.soc, False)
             uuid = run(True,
                        image=args[0],
                        cmd='/bin/uname -a'
                        if len(args) >= 2 and args[1] == "release" else
                        (" ".join(args[1:])
                         if len(args) >= 2 else get_entry_point(args[0])))
             send(self.soc, uuid + " started", newline=True)
         else:
             set_interaction(self.soc, True)
             # send_arg(self.soc, "detach", ("1" if ('-d', '') in optlist else "0"))
             send_arg(self.soc, "detach", "0")
             send_arg(self.soc, "image", args[0])
             # uuid should be none
             # load should be false
             send_arg(
                 self.soc, "cmd", '/bin/uname -a'
                 if len(args) >= 2 and args[1] == "release" else
                 (" ".join(args[1:])
                  if len(args) >= 2 else get_entry_point(args[0])))
             send(self.soc, "next")  # end of passing args