Exemplo n.º 1
0
    def login(self):
        email_regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
        is_valid = re.compile(email_regex)
        try:
            email = input('Enter your email: ')
        except KeyboardInterrupt:
            return
        if not is_valid.findall(email):
            print("Email is not valid")
            return
        try:
            pwd = getpass()
        except KeyboardInterrupt:
            return
        json_to_send = {
            "username": email,
            "password": md5((email + pwd).encode()).hexdigest()
        }
        if self.args.get("debug"):
            self.log_time()
        self.tcp_connect()
        api_result = self.api_handler.login(json_to_send)
        if 'ok' in api_result:
            set_token_to_json_config(api_result['token'])
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('post')
        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 2
0
    def go_scale(self):
        if self.args.get("debug"):
            self.log_time()
        self.tcp_connect()

        namespace = self.args.get('namespace')
        if not namespace:
            namespace = config_json_data.get("default_namespace")

        count = self.args.get("count")
        try:
            replicas_count = int(count)
            json_to_send = {"replicas": replicas_count}
            api_result = self.api_handler.scale(json_to_send,
                                                self.args.get("name"),
                                                namespace)
        except (ValueError, TypeError):
            print('{}{}{} {}'.format(
                BColors.FAIL,
                "Error: ",
                "Count is not integer",
                BColors.ENDC,
            ))
            return
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('scale')
        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 3
0
    def go_get(self):
        kind, name = self.construct_get()
        if self.debug:
            self.log_time()
        self.tcp_connect()

        self.namespace = self.args.get('namespace')
        if not self.namespace:
            self.namespace = config_json_data.get("default_namespace")

        if kind == "namespaces":
            if self.args.get("name"):
                api_result = self.api_handler.get_namespaces(
                    self.args.get("name"))
            else:
                api_result = self.api_handler.get_namespaces()
        else:
            api_result = self.api_handler.get(kind, name, self.namespace)
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('get')
        self.tcp_handler.close()
        if not check_http_status(json_result, "get"):
            return
        return json_result
Exemplo n.º 4
0
    def test_namespace(self, namespace):
        if self.debug:
            self.log_time()
        self.tcp_connect()

        api_result = self.api_handler.get_namespaces(namespace)

        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('check namespace')
        self.tcp_handler.close()
        if not check_http_status(json_result, "check namespace"):
            return
        return True
Exemplo n.º 5
0
    def go_restart(self):
        self.log_time()
        self.tcp_connect()

        namespace = self.args['namespace']
        if not namespace:
            namespace = config_json_data.get("default_namespace")
        api_result = self.api_handler.delete("deployments", self.args["name"],
                                             namespace, True)
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('restart')
        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 6
0
    def go_set(self):
        if self.args.get("debug"):
            self.log_time()
        self.tcp_connect()

        namespace = self.args.get('namespace')
        if not namespace:
            namespace = config_json_data.get("default_namespace")

        args = self.args.get("args")
        if args:
            if '=' in args:
                container_name, image = args.split('=')
                json_to_send = {"name": self.args.get("name"), "image": image}
                api_result = self.api_handler.set(json_to_send, container_name,
                                                  namespace)
            else:
                try:
                    replicas_count = int(args)
                    json_to_send = {"replicas": replicas_count}
                    api_result = self.api_handler.set(json_to_send,
                                                      self.args.get("name"),
                                                      namespace)
                except (ValueError, TypeError):
                    print('{}{}{} {}'.format(
                        BColors.FAIL,
                        "Error: ",
                        "Count is not integer",
                        BColors.ENDC,
                    ))
                    return

            if not self.handle_api_result(api_result):
                return

            json_result = self.get_and_handle_tcp_result('set')
            self.tcp_handler.close()
            if not check_http_status(json_result, self.args.get("command")):
                return
        else:
            print('{}{}{} {}'.format(
                BColors.FAIL,
                "Error: ",
                "Empty args",
                BColors.ENDC,
            ))
            return
Exemplo n.º 7
0
    def go_run(self):
        json_to_send = self.construct_run()
        if not json_to_send:
            return
        if self.debug:
            self.log_time()

        self.tcp_connect()
        namespace = self.args.get('namespace')
        if not namespace:
            namespace = config_json_data.get("default_namespace")
        api_result = self.api_handler.run(json_to_send, namespace)
        if not self.handle_api_result(api_result):
            return
        json_result = self.get_and_handle_tcp_result('run')

        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 8
0
    def go_create(self):
        if self.args.get("debug"):
            self.log_time()
        self.tcp_connect()

        json_to_send = self.get_json_from_file()
        namespace = json_to_send["metadata"].get("namespace")
        if not namespace:
            namespace = self.args.get('namespace')
            if not namespace:
                namespace = config_json_data.get("default_namespace")

        api_result = self.api_handler.create(json_to_send, namespace)
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('create')
        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 9
0
    def go_replace(self):
        self.log_time()
        self.tcp_connect()

        self.args['output'] = 'yaml'
        namespace = self.args['namespace']

        json_to_send = self.get_json_from_file()
        kind = '{}s'.format(json_to_send.get('kind')).lower()

        if kind != 'namespaces':
            api_result = self.api_handler.replace(json_to_send, namespace)
        else:
            api_result = self.api_handler.replace_namespaces(json_to_send)
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('replace')

        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return
Exemplo n.º 10
0
    def go_delete(self):
        kind, name = self.construct_delete()

        self.log_time()
        self.tcp_connect()

        self.args['output'] = 'yaml'
        namespace = self.args['namespace']
        if not namespace:
            namespace = config_json_data.get("default_namespace")
        if kind != 'namespaces':
            api_result = self.api_handler.delete(kind, name, namespace,
                                                 self.args.get("pods"))
        else:
            api_result = self.api_handler.delete_namespaces(name)
        if not self.handle_api_result(api_result):
            return

        json_result = self.get_and_handle_tcp_result('delete')
        self.tcp_handler.close()
        if not check_http_status(json_result, self.args.get("command")):
            return