Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jhancock/use elisa microservice #277

Merged
merged 15 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/nanorc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ def __init__(

if logbook_type != 'file':
try:
self.logbook = ElisaLogbook(
configuration = logbook_type,
console = console,
session_handler = self.session_handler,
)
self.logbook = ElisaHandler(socket = logbook_type, session_handler = self.session_handle)
except Exception as e:
self.log.error(f"Couldn't initialise ELisA, reverting to file logbook! {str(e)}")
logbook_type = 'file'
Expand Down
4 changes: 3 additions & 1 deletion src/nanorc/k8spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ def create_namespace(self, namespace : str):
nslist = [ns.metadata.name for ns in self._core_v1_api.list_namespace().items]
if namespace in nslist:
self.log.debug(f"Not creating \"{namespace}\" namespace as it already exist")
self.log.info(f"Try `kubectl get pods -n {namespace}' and see if there is anything running")
self.log.info(f"If you are sure nothing is running and want to use same session name `kubectl delete ns {namespace}' before starting your next run")
return

self.log.info(f"Creating \"{namespace}\" namespace")
Expand Down Expand Up @@ -202,7 +204,7 @@ def create_namespace(self, namespace : str):
def delete_namespace(self, namespace: str):
nslist = [ns.metadata.name for ns in self._core_v1_api.list_namespace().items]
if not namespace in nslist:
self.log.debug(f"Not deleting \"{namespace}\" namespace as it already exist")
self.log.info(f"The \"{namespace}\" namespace has already been deleted")
return
self.log.info(f"Deleting \"{namespace}\" namespace")
try:
Expand Down
77 changes: 23 additions & 54 deletions src/nanorc/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import copy
import time
import requests
from .credmgr import credentials

class FileLogbook:
Expand Down Expand Up @@ -47,15 +48,12 @@ def message_on_stop(self, messages:str, session:str):



class ElisaLogbook:
def __init__(self, console, configuration, session_handler):
self.console = console
self.session_handler = session_handler
self.elisa_arguments = {"connection": configuration['connection']}
self.website = configuration['website']
self.message_attributes = configuration['attributes']
class ElisaHandler:
def __init__(self, socket, session_handler):
self.socket = socket
self.session_handler = session_hander
self.log = logging.getLogger(self.__class__.__name__)
self.log.info(f'ELisA logbook connection: {configuration["website"]} (API: {configuration["connection"]})')
self.log.info(f'Connected to ELisA logbook at {self.socket}')

def _start_new_message_thread(self):
self.log.info("ELisA logbook: Next message will be a new thread")
Expand All @@ -66,55 +64,26 @@ def _start_new_message_thread(self):

def _send_message(self, subject:str, body:str, command:str):
user = self.session_handler.nanorc_user.username

elisa_arg = copy.deepcopy(self.elisa_arguments)

elisa_user = credentials.get_login('elisa')

import tempfile
with tempfile.NamedTemporaryFile() as tf:
try:
sso = {"ssocookie": self.session_handler.generate_elisa_cern_cookie(self.website, tf.name)}
elisa_arg.update(sso)
elisa_inst = Elisa(**elisa_arg)
answer = None
if not self.current_id:
self.log.info("ELisA logbook: Creating a new message thread")
message = MessageInsert()
message.author = user
message.subject = subject
for attr_name, attr_data in self.message_attributes[command].items():
if attr_data['set_on_new_thread']:
setattr(message, attr_name, attr_data['value'])
message.systemsAffected = ["DAQ"]
message.body = body
answer = elisa_inst.insertMessage(message)

else:
self.log.info(f"ELisA logbook: Answering to message ID{self.current_id}")
message = MessageReply(self.current_id)
message.author = user
message.systemsAffected = ["DAQ"]
for attr_name, attr_data in self.message_attributes[command].items():
if attr_data['set_on_reply']:
setattr(message, attr_name, attr_data['value'])
message.body = body
answer = elisa_inst.replyToMessage(message)
self.current_id = answer.id

except ElisaError as ex:
self.log.error(f"ELisA logbook: {str(ex)}")
self.log.error(answer)
raise ex

except Exception as e:
self.log.error(f'Exception thrown while inserting data in elisa:')
self.log.error(e)
import logging
#Since NAnoRC is sending these messages, is the system always DAQ?
data = {'author':user, 'title':subject, 'body':body, 'command':command, 'systems':["DAQ"]}
JonathanHancock0 marked this conversation as resolved.
Show resolved Hide resolved

if not self.current_id:
r = requests.post(f'{self.socket}/v1/elisaLogbook/new_message', auth=(fooUsr,barPass), data=data)
JonathanHancock0 marked this conversation as resolved.
Show resolved Hide resolved
else:
data["id"=self.current_id]
JonathanHancock0 marked this conversation as resolved.
Show resolved Hide resolved
r = requests.put(f'{self.socket}/v1/elisaLogbook/reply_to_message', auth=(fooUsr,barPass), data=data)

response = r.json()
if r.status_code != 201:
self.log.error(f'Exception thrown while inserting data in elisa:')
e = (response['response'])
self.log.error(e)
import logging
if logging.DEBUG >= logging.root.level:
JonathanHancock0 marked this conversation as resolved.
Show resolved Hide resolved
self.console.print_exception()
raise e

else:
self.current_id = response['thread_id']
self.log.info(f"ELisA logbook: Sent message (ID{self.current_id})")


Expand Down