Commit 01bf8c3a by Czémán Arnold

checking user agent for shutdown and reboot operations

parent a9d62221
...@@ -169,7 +169,7 @@ with requests.Session() as session: ...@@ -169,7 +169,7 @@ with requests.Session() as session:
print("status_code: " + str(req.status_code)) print("status_code: " + str(req.status_code))
print print
print(json.dumps(json.loads(req.text), sort_keys=True, print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": "))) indent=4, separators=(",", ": ")))
print print
# vm torles # vm torles
...@@ -186,10 +186,9 @@ with requests.Session() as session: ...@@ -186,10 +186,9 @@ with requests.Session() as session:
print("status_code: " + str(req.status_code)) print("status_code: " + str(req.status_code))
print print
print(json.dumps(json.loads(req.text), sort_keys=True, print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": "))) indent=4, separators=(",", ": ")))
print print
# Kijelentkezes # Kijelentkezes
req = session.get(server + "occi/logout/", headers=headers, req = session.get(server + "occi/logout/", headers=headers,
verify=False) verify=False)
......
...@@ -5,6 +5,7 @@ from occi_core import Resource ...@@ -5,6 +5,7 @@ from occi_core import Resource
from occi_utils import action_list_for_resource, OcciActionInvocationError from occi_utils import action_list_for_resource, OcciActionInvocationError
from occi_instances import COMPUTE_ACTIONS from occi_instances import COMPUTE_ACTIONS
from common.models import HumanReadableException from common.models import HumanReadableException
from vm.models import InstanceActivity
COMPUTE_STATES = { COMPUTE_STATES = {
...@@ -91,8 +92,23 @@ class Compute(Resource): ...@@ -91,8 +92,23 @@ class Compute(Resource):
self.save(user, attributes) self.save(user, attributes)
else: else:
raise OcciActionInvocationError(message="Undefined action.") raise OcciActionInvocationError(message="Undefined action.")
# to refresh Compute attribute
self.__init__(self.vm) self.__init__(self.vm)
def has_agent(self):
last_boot_time = self.vm.activity_log.filter(
succeeded=True, activity_code__in=(
"vm.Instance.deploy", "vm.Instance.reset",
"vm.Instance.reboot")).latest("finished").finished
try:
InstanceActivity.objects.filter(
activity_code="vm.Instance.agent.starting",
started__gt=last_boot_time, instance=self.vm
).latest("started")
except InstanceActivity.DoesNotExist: # no agent since last boot
return False
return True
def start(self, user): def start(self, user):
""" Start action on a compute instance """ """ Start action on a compute instance """
try: try:
...@@ -108,9 +124,11 @@ class Compute(Resource): ...@@ -108,9 +124,11 @@ class Compute(Resource):
if "method" not in attributes: if "method" not in attributes:
raise OcciActionInvocationError(message="No method given.") raise OcciActionInvocationError(message="No method given.")
if attributes["method"] in ("graceful", "acpioff",): if attributes["method"] in ("graceful", "acpioff",):
if not self.has_agent():
raise OcciActionInvocationError(
message="User agent is required.")
try: try:
# TODO: call shutdown properly self.vm.shutdown(task=None, user=user)
self.vm.shutdown(user=user)
except HumanReadableException as e: except HumanReadableException as e:
raise OcciActionInvocationError(message=e.get_user_text()) raise OcciActionInvocationError(message=e.get_user_text())
elif attributes["method"] in ("poweroff",): elif attributes["method"] in ("poweroff",):
...@@ -127,9 +145,11 @@ class Compute(Resource): ...@@ -127,9 +145,11 @@ class Compute(Resource):
if "method" not in attributes: if "method" not in attributes:
raise OcciActionInvocationError(message="No method given.") raise OcciActionInvocationError(message="No method given.")
if attributes["method"] in ("graceful", "warm",): if attributes["method"] in ("graceful", "warm",):
if not self.has_agent():
raise OcciActionInvocationError(
message="User agent is required.")
try: try:
# TODO: not working for some reason self.vm.reboot(user=user)
self.vm.restart(user=user)
except HumanReadableException as e: except HumanReadableException as e:
raise OcciActionInvocationError(message=e.get_user_text()) raise OcciActionInvocationError(message=e.get_user_text())
elif attributes["method"] in ("cold",): elif attributes["method"] in ("cold",):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment