Skip to content

Commit

Permalink
Merge pull request #579 from zhuyuanmao/remove-duplicate-nodes-creation
Browse files Browse the repository at this point in the history
Fix multiple node creation
  • Loading branch information
yeasy authored Nov 18, 2023
2 parents 5deac2f + 488d83e commit 0cee896
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
3 changes: 0 additions & 3 deletions src/api-engine/api/routes/node/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,11 @@ class Meta:


class NodeCreateBody(serializers.ModelSerializer):
num = serializers.IntegerField(help_text="number of node")

class Meta:
model = Node
fields = (
"name",
"type",
"num",
)
extra_kwargs = {
"name": {"required": True},
Expand Down
79 changes: 39 additions & 40 deletions src/api-engine/api/routes/node/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def list(self, request, *args, **kwargs):
nodes = Node.objects.filter(**query_filter)
p = Paginator(nodes, per_page)
nodes = p.page(page)
response = NodeListSerializer({"total": p.count, "data": nodes})
response = NodeListSerializer(
{"total": p.count, "data": nodes})
return Response(data=ok(response.data), status=status.HTTP_200_OK)
except Exception as e:
return Response(
Expand Down Expand Up @@ -276,51 +277,45 @@ def create(self, request):
if serializer.is_valid(raise_exception=True):
node_name = serializer.validated_data.get("name")
node_type = serializer.validated_data.get("type")
num = serializer.validated_data.get("num")
organization = request.user.organization

agent = organization.agent.get()
if agent:
nodes = Node.objects.filter(
name=node_name + "0", organization=organization, type=node_type)
name=node_name, organization=organization, type=node_type)
if nodes:
raise ResourceExists
else:
raise NoResource
for n in range(num):

name = node_name + str(n)

urls = "{}.{}".format(name, organization.name)
nodes = {
"type": node_type,
"Specs": [name]
}
CryptoConfig(organization.name).update(nodes)
CryptoGen(organization.name).extend()
self._generate_config(node_type, organization.name, name)
msp, tls, cfg = self._conversion_msp_tls_cfg(
node_type, organization.name, name)

node = Node(
name=name,
organization=organization,
urls=urls,
type=node_type,
msp=msp,
tls=tls,
agent=agent,
config_file=cfg
)
node.save()
urls = "{}.{}".format(node_name, organization.name)
nodes = {
"type": node_type,
"Specs": [node_name]
}
CryptoConfig(organization.name).update(nodes)
CryptoGen(organization.name).extend()
self._generate_config(node_type, organization.name, node_name)
msp, tls, cfg = self._conversion_msp_tls_cfg(
node_type, organization.name, node_name)
node = Node(
name=node_name,
organization=organization,
urls=urls,
type=node_type,
msp=msp,
tls=tls,
agent=agent,
config_file=cfg
)
node.save()

self._set_port(node_type, node, agent)
if node.organization.network:
try:
threading.Thread(
target=self._start_node, args=(node.id,)).start()
except Exception as e:
raise e
self._set_port(node_type, node, agent)
if node.organization.network:
try:
threading.Thread(
target=self._start_node, args=(node.id,)).start()
except Exception as e:
raise e

response = NodeIDSerializer(data=node.__dict__)
if response.is_valid(raise_exception=True):
Expand Down Expand Up @@ -571,19 +566,23 @@ def destroy(self, request, pk=None):
# try to stop/delete container 3 times
# TODO: optimize the retry logic
for i in range(3):
LOG.info("Retry to stop/delete container %d time(s).", i + 1)
LOG.info(
"Retry to stop/delete container %d time(s).", i + 1)
try:
response = agent.stop()
if response is not True:
LOG.error("Failed when agent stops/deletes container: %s", response)
LOG.error(
"Failed when agent stops/deletes container: %s", response)
continue
response = agent.delete()
if response is not True:
LOG.error("Failed when agent stops/deletes container: %s", response)
LOG.error(
"Failed when agent stops/deletes container: %s", response)
continue
res = True
except Exception as e:
LOG.error("Exception when agent stops/deletes container: %s", e)
LOG.error(
"Exception when agent stops/deletes container: %s", e)
continue
break
if res:
Expand Down

0 comments on commit 0cee896

Please sign in to comment.