Skip to content

Commit

Permalink
Merge PR #189 into 14.0-dev
Browse files Browse the repository at this point in the history
Signed-off-by ivs-cetmix
  • Loading branch information
CetmixGitBot committed Jan 22, 2025
2 parents 3ff4f25 + 8652b65 commit 233708a
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cetmix_tower_server/models/cx_tower_plan_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,22 @@ def finish(self, plan_status, **kwargs):
if kwargs:
values.update(kwargs)
self.sudo().write(values)

# Call hook
self._plan_finished()

# Check if we were deleting a server
if (
self.server_id._is_being_deleted()
and self.server_id.plan_delete_id == self.plan_id
):
if plan_status == 0:
# And finally delete the server
self.with_context(server_force_delete=True).server_id.unlink()
else:
# Set deletion error if flightplan failed
self.server_id.status = "delete_error"

def _plan_finished(self):
"""Triggered when flightplan in finished
Inherit to implement your own hooks
Expand Down
57 changes: 57 additions & 0 deletions cetmix_tower_server/models/cx_tower_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,18 @@ class CxTowerServer(models.Model):
# ---- Related server template
server_template_id = fields.Many2one(
"cx.tower.server.template",
readonly=True,
index=True,
)

# ---- Delete plan
plan_delete_id = fields.Many2one(
"cx.tower.plan",
string="On Delete Plan",
groups="cetmix_tower_server.group_manager",
help="This Flightplan will be executed when the server is deleted",
)

def _selection_status(self):
"""
Status selection options
Expand All @@ -369,6 +378,8 @@ def _selection_status(self):
("running", "Running"),
("stopping", "Stopping"),
("restarting", "Restarting"),
("deleting", "Deleting"),
("delete_error", "Deletion Error"),
]

def server_toggle_active(self, self_active):
Expand Down Expand Up @@ -436,6 +447,52 @@ def _constraint_ssh_settings(self):
validation_error = "\n".join(validation_errors)
raise ValidationError(validation_error)

def unlink(self):
servers_to_delete = self.env["cx.tower.server"]
flight_plan_log_obj = self.env["cx.tower.plan.log"]

for server in self:
# If forced, no delete plan, or already in deleting state,
# skip plan execution
if (
self._context.get("server_force_delete")
or not server.plan_delete_id
or server._is_being_deleted()
):
servers_to_delete |= server
continue

plan_label = generate_random_id(4)
server.plan_delete_id.execute(server, plan_log={"label": plan_label})
plan_log = flight_plan_log_obj.search(
[
("server_id", "=", server.id),
("plan_id", "=", server.plan_delete_id.id),
("label", "=", plan_label),
]
)

# If plan has finished, either mark for deletion or set an error
if plan_log and plan_log.finish_date:
if plan_log.plan_status == 0:
servers_to_delete |= server
else:
server.status = "delete_error"
else:
# Plan still in progress
server.status = "deleting"

return super(CxTowerServer, servers_to_delete).unlink()

def _is_being_deleted(self):
"""Check if the server is being deleted.
Returns:
bool: True if the server is being deleted, False otherwise
"""
self.ensure_one()
return self.status and self.status == "deleting"

@api.returns("self", lambda value: value.id)
def copy(self, default=None):
default = default or {}
Expand Down
81 changes: 81 additions & 0 deletions cetmix_tower_server/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ def setUp(self, *args, **kwargs):
}
)

# ---
# Flight plan to delete the server

# Add a command to delete the server
# This command will create a new partner with pre-defined ref
self.command_delete_server = self.Command.create(
{
"name": "Python command for deleting server",
"action": "python_code",
"code": """
partner = env["res.partner"].create({"name": "Partner 1", "ref": "delete_server"})
COMMAND_RESULT = {
"exit_code": 0,
"message": partner.name,
}
""",
}
)

self.plan_delete_server = self.Plan.create(
{
"name": "Delete server",
"line_ids": [
(
0,
0,
{"command_id": self.command_delete_server.id},
)
],
}
)

def test_server_copy(self):
# Let's say we have auto sync enabled on one of the files in server 2
self.server_test_2_file.auto_sync = True
Expand Down Expand Up @@ -436,3 +468,52 @@ def test_server_unlink(self):
self.PlanLog.search([("id", "=", plan_log_id)]),
msg="Plan Log should be deleted when server is deleted",
)

def test_server_delete_plan_success(self):
"""Test server delete plan"""

# Set plan to delete the server
self.server_test_2.plan_delete_id = self.plan_delete_server.id

# Delete the server
self.server_test_2.unlink()

# Check if the server has been deleted
self.assertFalse(
self.server_test_2.exists(),
msg="Server should be deleted",
)

# Check if the partner has been created
self.assertTrue(
self.env["res.partner"].search([("ref", "=", "delete_server")]),
msg="Partner should be created",
)

def test_server_delete_plan_error(self):
"""Test server delete plan error"""

# Modify the command to fail
self.command_delete_server.code = """
COMMAND_RESULT = {
"exit_code": 4,
"message": 'Such much error',
}
"""
# Set plan to delete the server
self.server_test_2.plan_delete_id = self.plan_delete_server.id

# Delete the server
self.server_test_2.unlink()

# Check if the server has been deleted
self.assertTrue(
self.server_test_2.exists(),
msg="Server should not be deleted",
)

self.assertEqual(
self.server_test_2.status,
"delete_error",
msg="Server status should be delete_error",
)
9 changes: 7 additions & 2 deletions cetmix_tower_server/views/cx_tower_server_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,13 @@
attrs="{'required': [('ssh_auth_mode', '=', 'k')]}"
/>
</group>


<group extra="delete">
<field
name="server_template_id"
attrs="{'invisible': [('server_template_id', '=', False)]}"
/>
<field name="plan_delete_id" />
</group>
<field name="note" placeholder="Put your notes here" />
</group>
</page>
Expand Down

0 comments on commit 233708a

Please sign in to comment.