Skip to content

Commit

Permalink
🔀 Merge pull request #156 from SaaShup/add_kill_operation
Browse files Browse the repository at this point in the history
✨ Add Kill operation
  • Loading branch information
fanshan committed Aug 7, 2024
2 parents 2d93b84 + 68b4a6c commit ce077ce
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion netbox_docker_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NetBoxDockerConfig(PluginConfig):
name = "netbox_docker_plugin"
verbose_name = " NetBox Docker Plugin"
description = "Manage Docker"
version = "1.17.0"
version = "1.18.0"
max_version = "3.7.8"
base_url = "docker"
author= "Vincent Simonin <[email protected]>, David Delassus <[email protected]>"
Expand Down
6 changes: 6 additions & 0 deletions netbox_docker_plugin/models/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ContainerOperationChoices(ChoiceSet):
("restart", "Restart", "blue"),
("stop", "Stop", "blue"),
("recreate", "Recreate", "blue"),
("kill", "Kill", "red"),
("none", "None", "white"),
]

Expand Down Expand Up @@ -186,6 +187,11 @@ def can_delete(self) -> bool:
"dead",
]

@property
def can_kill(self) -> bool:
"""Check if the container can be killed"""
return self.state in ["created", "restarting", "running", "exited", "dead"]

class Meta:
"""Image Model Meta Class"""

Expand Down
1 change: 1 addition & 0 deletions netbox_docker_plugin/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def restrict_container_actions(instance, **_kwargs):
instance.operation == "restart" and not instance.can_restart,
instance.operation == "stop" and not instance.can_stop,
instance.operation == "recreate" and not instance.can_recreate,
instance.operation == "kill" and not instance.can_kill,
]
):
raise AbortRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
</button>
</form>

<form action="{% url 'plugins:netbox_docker_plugin:container_operation' pk=object.id operation='kill' %}" method="post">
{% csrf_token %}
<input type="hidden" name="operation" value="kill">
<button
type="submit"
class="btn btn-sm btn-black"
{% if not object.can_kill %}
disabled
{% endif %}
>
<i class="mdi mdi-skull"></i> Kill
</button>
</form>

<form action="{% url 'plugins:netbox_docker_plugin:container_operation' pk=object.id operation='recreate' %}" method="post">
{% csrf_token %}
<input type="hidden" name="operation" value="recreate">
Expand Down
44 changes: 34 additions & 10 deletions netbox_docker_plugin/tests/container/test_container_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@


class ContainerActionTestCase(TestCase):
""" Test the various scenarii of container actions """
"""Test the various scenarii of container actions"""

objects = {}

def test_allowed_operations(self):
""" Test that only specific operations are allowed according to a state """
"""Test that only specific operations are allowed according to a state"""

cases = [
("create", ["none"]),
("start", ["created", "exited", "dead"]),
("stop", ["running"]),
("restart", ["running"]),
("recreate", ["created", "exited", "dead", "running"]),
("noop", ["created", "restarting", "running", "paused", "exited", "dead", "none"]),
("kill", ["created", "restarting", "running", "exited", "dead"]),
(
"noop",
[
"created",
"restarting",
"running",
"paused",
"exited",
"dead",
"none",
],
),
]

for operation, states in cases:
Expand All @@ -48,14 +60,18 @@ def test_allowed_operations(self):
)

def test_forbidden_operations(self):
""" Test that forbidden operations raise an AbortRequest """
"""Test that forbidden operations raise an AbortRequest"""

cases = [
("create", ["created", "restarting", "running", "paused", "exited", "dead"]),
(
"create",
["created", "restarting", "running", "paused", "exited", "dead"],
),
("start", ["running", "restarting", "paused", "none"]),
("stop", ["created", "restarting", "paused", "exited", "dead", "none"]),
("restart", ["created", "restarting", "paused", "exited", "dead", "none"]),
("recreate", ["restarting", "paused", "none"]),
("kill", ["paused", "none"]),
]

for operation, states in cases:
Expand All @@ -79,7 +95,7 @@ def test_forbidden_operations(self):
obj.save()

def test_delete_non_running_container(self):
""" Test that a non running container can be deleted """
"""Test that a non running container can be deleted"""

for state in ["created", "paused", "exited", "dead"]:
with self.subTest(operation="delete", state=state):
Expand All @@ -98,7 +114,7 @@ def test_delete_non_running_container(self):
self.assertFalse(Container.objects.filter(name=name).exists())

def test_delete_running_container(self):
""" Test that a running container cannot be deleted """
"""Test that a running container cannot be deleted"""

for state in ["running", "restarting", "none"]:
with self.subTest(operation="delete", state=state):
Expand All @@ -119,9 +135,17 @@ def test_delete_running_container(self):
obj.delete()

def test_delete_container_on_deleted_host(self):
""" Test that a container on a deleted host can be deleted """

for state in ["created", "running", "restarted", "paused", "exited", "dead", "none"]:
"""Test that a container on a deleted host can be deleted"""

for state in [
"created",
"running",
"restarted",
"paused",
"exited",
"dead",
"none",
]:
with self.subTest(operation="delete", state=state):
with transaction.atomic():
name = f"container-delete-{state}"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "netbox-docker-plugin"
version = "1.17.0"
version = "1.18.0"
authors = [
{ name="Vincent Simonin", email="[email protected]" },
{ name="David Delassus", email="[email protected]" }
Expand Down

0 comments on commit ce077ce

Please sign in to comment.