mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
The instance lists, the VM page header and the power tab knew three libvirt states: running, paused and shut off. A VM that was blocked, shutting down, crashed, suspended by its guest or without a state got an empty status cell and no actions, so a crashed VM could not even be forced off from the list. The lists also offered actions users could not use: Suspend and Resume (views for superusers) to everyone, and power actions to users who see every VM through the read-only view_instances permission. instances/states.py holds the rules in one place: the label and color of each state, the actions each state allows, and which of them a user may use (superusers all; a VM's owners all but suspend and resume, and nothing on a VM an administrator suspended, as the VM page already showed; nobody else any). Blocked is a running VM; shutting down, crashed and suspended by the guest get Force Off, after which the VM can be started. A shut-off template offers Clone to whoever has the clone permission, as the clone view allows. The templates read them through two tags (vm_states): the lists and the VM page header show the state, instance_actions.html has one slot per button, each enabled or disabled from the allowed actions. The power tab offers Force Off in the states that need it. The new tests check the rules directly and render every state in the lists and on the VM page for a superuser, an owner and a view_instances user; they fail on the previous code.
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""
|
|
libvirt VM states: their labels, and the actions each one allows a user.
|
|
The lists and the VM page use these through templatetags/vm_states.py.
|
|
"""
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
# state (virDomainState): label, Bootstrap color
|
|
STATES = {
|
|
0: (_("No state"), "secondary"),
|
|
1: (_("Active"), "success"),
|
|
2: (_("Blocked"), "success"), # running, waiting for a resource
|
|
3: (_("Suspended"), "warning"),
|
|
4: (_("Shutting down"), "warning"),
|
|
5: (_("Off"), "danger"),
|
|
6: (_("Crashed"), "danger"),
|
|
7: (_("PM suspended"), "warning"), # suspended by the guest
|
|
}
|
|
UNKNOWN = (_("Unknown"), "secondary") # the VM cannot be read
|
|
|
|
RUNNING = {"suspend", "poweroff", "powercycle", "console"}
|
|
STATE_ACTIONS = {
|
|
1: RUNNING,
|
|
2: RUNNING,
|
|
3: {"resume", "force_off"},
|
|
5: {"poweron"},
|
|
# shutting down, crashed, suspended by the guest: a forced power off ends
|
|
# it, and the VM can be started again
|
|
4: {"force_off"},
|
|
6: {"force_off"},
|
|
7: {"force_off"},
|
|
}
|
|
SUPERUSER_ONLY = {"suspend", "resume"} # their views are for superusers
|
|
|
|
|
|
def state_label(state):
|
|
"""(label, color) of a state; None for a VM that cannot be read."""
|
|
return UNKNOWN if state is None else STATES.get(state, STATES[0])
|
|
|
|
|
|
def allowed_actions(user, instance, state):
|
|
"""
|
|
The power actions the state allows and the user may use: superusers all,
|
|
a VM's owners all but suspend and resume, nobody else any (the
|
|
view_instances permission is read-only). A VM suspended by an
|
|
administrator offers its owner nothing. A shut-off template is not
|
|
started but cloned, by anyone with the clone permission who sees it.
|
|
"""
|
|
if state == 5 and instance.is_template:
|
|
return {"clone"} if user.has_perm("instances.clone_instances") else set()
|
|
if user.is_superuser:
|
|
return STATE_ACTIONS.get(state, set())
|
|
# the lists prefetch userinstance_set
|
|
if not any(ui.user_id == user.id for ui in instance.userinstance_set.all()):
|
|
return set()
|
|
if state == 3:
|
|
return set()
|
|
return STATE_ACTIONS.get(state, set()) - SUPERUSER_ONLY
|