From 147e9fde9dbb85979bebe964200ea8ce27ea9a08 Mon Sep 17 00:00:00 2001 From: Xichen Pan Date: Fri, 2 Sep 2022 06:09:30 -0600 Subject: [PATCH 1/3] Allow docker-agent to update node config Implement the methods to request docker-agent to update config, and restart the node. Also fix a bug in node APIs that disallows the JSON post request. Signed-off-by: Xichen Pan --- src/api-engine/api/lib/agent/docker/handler.py | 17 +++++++++++++++++ src/api-engine/api/lib/agent/handler.py | 5 +++++ src/api-engine/api/routes/node/views.py | 7 +++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/api-engine/api/lib/agent/docker/handler.py b/src/api-engine/api/lib/agent/docker/handler.py index c9dceb68..685094cf 100644 --- a/src/api-engine/api/lib/agent/docker/handler.py +++ b/src/api-engine/api/lib/agent/docker/handler.py @@ -108,3 +108,20 @@ def get(self, *args, **kwargs): raise response.reason except Exception as e: raise e + + def update_config(self, config_file, node_type): + try: + cmd = 'bash /tmp/update.sh "{} node start"'.format(node_type) + data = { + 'peer_config_file': config_file, + 'orderer_config_file': config_file, + 'action': 'update', + 'cmd': cmd + } + response = post('{}/api/v1/nodes/{}'.format(self._urls, self._cname), data=data) + if response.status_code == 200: + return True + else: + raise response.reason + except Exception as e: + raise e \ No newline at end of file diff --git a/src/api-engine/api/lib/agent/handler.py b/src/api-engine/api/lib/agent/handler.py index a0fcc695..6967af2d 100644 --- a/src/api-engine/api/lib/agent/handler.py +++ b/src/api-engine/api/lib/agent/handler.py @@ -61,3 +61,8 @@ def stop(self): self._agent.stop() return True + + def update_config(self, config_file, node_type): + self._agent.update_config(config_file, node_type) + + return True \ No newline at end of file diff --git a/src/api-engine/api/routes/node/views.py b/src/api-engine/api/routes/node/views.py index 562a0393..f9e0704a 100644 --- a/src/api-engine/api/routes/node/views.py +++ b/src/api-engine/api/routes/node/views.py @@ -14,7 +14,7 @@ from rest_framework import viewsets, status from rest_framework.decorators import action from rest_framework.response import Response -from rest_framework.parsers import MultiPartParser +from rest_framework.parsers import MultiPartParser, FormParser, JSONParser from rest_framework_jwt.authentication import JSONWebTokenAuthentication from api.common.enums import AgentOperation @@ -67,7 +67,7 @@ class NodeViewSet(viewsets.ViewSet): authentication_classes = (JSONWebTokenAuthentication, TokenAuth) - parser_classes = [MultiPartParser] + parser_classes = [MultiPartParser, FormParser, JSONParser] # Only operator can update node info # def get_permissions(self): @@ -741,6 +741,9 @@ def node_config(self, request, pk=None): cfg = base64.b64encode(f_cfg.read()) node.config_file = cfg node.save() + infos = self._agent_params(pk) + agent = AgentHandler(infos) + agent.update_config(cfg, node.type) return Response(status=status.HTTP_202_ACCEPTED) except Exception as e: raise e From dba19b10b16dc8c4a355b1958ef7fbfd68a4d69e Mon Sep 17 00:00:00 2001 From: Xichen Pan Date: Sat, 3 Sep 2022 22:05:45 -0600 Subject: [PATCH 2/3] Fix code format issue End file with a new line. Signed-off-by: Xichen Pan --- src/api-engine/api/lib/agent/docker/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api-engine/api/lib/agent/docker/handler.py b/src/api-engine/api/lib/agent/docker/handler.py index 685094cf..94c899aa 100644 --- a/src/api-engine/api/lib/agent/docker/handler.py +++ b/src/api-engine/api/lib/agent/docker/handler.py @@ -124,4 +124,4 @@ def update_config(self, config_file, node_type): else: raise response.reason except Exception as e: - raise e \ No newline at end of file + raise e From d33de312e9dda33fcea6affcf88706f711ac1232 Mon Sep 17 00:00:00 2001 From: Xichen Pan Date: Sat, 3 Sep 2022 22:08:22 -0600 Subject: [PATCH 3/3] Fix code format issue End file with a new line. Signed-off-by: Xichen Pan --- src/api-engine/api/lib/agent/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api-engine/api/lib/agent/handler.py b/src/api-engine/api/lib/agent/handler.py index 6967af2d..f3db9c7c 100644 --- a/src/api-engine/api/lib/agent/handler.py +++ b/src/api-engine/api/lib/agent/handler.py @@ -65,4 +65,4 @@ def stop(self): def update_config(self, config_file, node_type): self._agent.update_config(config_file, node_type) - return True \ No newline at end of file + return True