Commit 14aad48d authored by Alain Takoudjou's avatar Alain Takoudjou

promise.plugin: uses 'extra_config_dict' to send custom promise parameters

/reviewed-on nexedi/slapos.toolbox!33
parent 4460d47a
...@@ -4,8 +4,6 @@ from slapos.grid.promise.generic import GenericPromise ...@@ -4,8 +4,6 @@ from slapos.grid.promise.generic import GenericPromise
import os import os
from datetime import datetime from datetime import datetime
MONITOR_URL = ""
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise) zope_interface.implements(interface.IPromise)
...@@ -25,12 +23,13 @@ class RunPromise(GenericPromise): ...@@ -25,12 +23,13 @@ class RunPromise(GenericPromise):
log_name = 'slapgrid-%s-error.log' % self.getConfig('partition-id') log_name = 'slapgrid-%s-error.log' % self.getConfig('partition-id')
slapgrid_error_log_file = os.path.join(partition_folder, '.%s' % log_name) slapgrid_error_log_file = os.path.join(partition_folder, '.%s' % log_name)
link_file = os.path.join(log_folder, log_name) link_file = os.path.join(log_folder, log_name)
monitor_url = self.getConfig('monitor-url')
message = '' message = ''
if os.path.exists(slapgrid_error_log_file) and \ if os.path.exists(slapgrid_error_log_file) and \
os.stat(slapgrid_error_log_file).st_size: os.stat(slapgrid_error_log_file).st_size:
message = 'Buildout failed to process %s.' % self.getConfig('partition-id') message = 'Buildout failed to process %s.' % self.getConfig('partition-id')
if MONITOR_URL: if monitor_url:
message += '\nSee %s/log/%s for more information.' % (MONITOR_URL, log_name) message += '\nSee %s/log/%s for more information.' % (monitor_url, log_name)
if not os.path.exists(link_file): if not os.path.exists(link_file):
os.symlink(slapgrid_error_log_file, link_file) os.symlink(slapgrid_error_log_file, link_file)
else: else:
......
...@@ -4,11 +4,7 @@ from slapos.grid.promise.generic import GenericPromise ...@@ -4,11 +4,7 @@ from slapos.grid.promise.generic import GenericPromise
import os import os
import time import time
import psutil import psutil
from slapos.runner.utils import tail from .util import tail_file
PROCESS_PID_FILE = ""
PROCESS_NAME = ""
STATUS_FILE = ""
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
...@@ -19,20 +15,17 @@ class RunPromise(GenericPromise): ...@@ -19,20 +15,17 @@ class RunPromise(GenericPromise):
self.setPeriodicity(minute=2) self.setPeriodicity(minute=2)
def sense(self): def sense(self):
if PROCESS_PID_FILE == "" or PROCESS_NAME == "" or STATUS_FILE == "": process_pid_file = self.getConfig('process-pid-file')
self.logger.info("") if not os.path.exists(process_pid_file):
return
if not os.path.exists(PROCESS_PID_FILE):
self.logger.info("Bootstrap didn't run!") self.logger.info("Bootstrap didn't run!")
return return
with open(PROCESS_PID_FILE) as f: with open(process_pid_file) as f:
try: try:
pid = int(f.read()) pid = int(f.read())
except ValueError, e: except ValueError, e:
raise ValueError("%r is empty or doesn't contain a valid pid number: %s" % ( raise ValueError("%r is empty or doesn't contain a valid pid number: %s" % (
PROCESS_PID_FILE, str(e))) process_pid_file, str(e)))
try: try:
process = psutil.Process(pid) process = psutil.Process(pid)
...@@ -51,18 +44,18 @@ class RunPromise(GenericPromise): ...@@ -51,18 +44,18 @@ class RunPromise(GenericPromise):
# process exited # process exited
pass pass
if os.path.exists(STATUS_FILE) and not os.stat(STATUS_FILE).st_size: status_file = self.getConfig('status-file')
if os.path.exists(status_file) and not os.stat(status_file).st_size:
self.logger.info("Bootstrap OK") self.logger.info("Bootstrap OK")
return return
message = "Monitor bootstrap exited with error." message = "Monitor bootstrap exited with error."
log_file = os.path.join(self.getPartitionFolder(), ".%s_%s.log" % ( log_file = os.path.join(self.getPartitionFolder(), ".%s_%s.log" % (
self.getConfig('partition-id'), self.getConfig('partition-id'),
PROCESS_NAME)) self.getConfig('process-name')))
if os.path.exists(log_file): if os.path.exists(log_file):
with open(log_file) as f:
message += "\n ---- Latest monitor-boostrap.log ----\n" message += "\n ---- Latest monitor-boostrap.log ----\n"
message += tail(f, 4) message += tail_file(log_file, 4)
self.logger.error(message) self.logger.error(message)
......
def tail_file(file_path, line_count=10):
"""
Returns the last lines of file.
"""
line_list = []
with open(file_path) as f:
BUFSIZ = 1024
f.seek(0, 2)
bytes = f.tell()
size = line_count + 1
block = -1
while size > 0 and bytes > 0:
if bytes - BUFSIZ > 0:
# Seek back one whole BUFSIZ
f.seek(block * BUFSIZ, 2)
line_list.insert(0, f.read(BUFSIZ))
else:
f.seek(0, 0)
# only read what was not read
line_list.insert(0, f.read(bytes))
line_len = line_list[0].count('\n')
size -= line_len
bytes -= BUFSIZ
block -= 1
return '\n'.join(''.join(line_list).splitlines()[-line_count:])
\ No newline at end of file
...@@ -38,11 +38,12 @@ class TestPromiseMonitorBoostrap(TestPromisePluginMixin): ...@@ -38,11 +38,12 @@ class TestPromiseMonitorBoostrap(TestPromisePluginMixin):
self.promise_name = "my-monitor-bootstrap.py" self.promise_name = "my-monitor-bootstrap.py"
content = """from slapos.promise.plugin.monitor_bootstrap_status import RunPromise content = """from slapos.promise.plugin.monitor_bootstrap_status import RunPromise
from slapos.promise.plugin import monitor_bootstrap_status
monitor_bootstrap_status.PROCESS_PID_FILE = "%(pid_file)s" extra_config_dict = {
monitor_bootstrap_status.PROCESS_NAME = "monitor.boostrap" 'process-pid-file': "%(pid_file)s",
monitor_bootstrap_status.STATUS_FILE = "%(state_file)s" 'process-name': "monitor.boostrap",
'status-file': "%(state_file)s",
}
""" % {'pid_file': self.pid_file, 'state_file': self.state_file} """ % {'pid_file': self.pid_file, 'state_file': self.state_file}
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
......
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 to comment