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

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

/reviewed-on !33
parent 4460d47a
......@@ -4,8 +4,6 @@ from slapos.grid.promise.generic import GenericPromise
import os
from datetime import datetime
MONITOR_URL = ""
class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise)
......@@ -25,12 +23,13 @@ class RunPromise(GenericPromise):
log_name = 'slapgrid-%s-error.log' % self.getConfig('partition-id')
slapgrid_error_log_file = os.path.join(partition_folder, '.%s' % log_name)
link_file = os.path.join(log_folder, log_name)
monitor_url = self.getConfig('monitor-url')
message = ''
if os.path.exists(slapgrid_error_log_file) and \
os.stat(slapgrid_error_log_file).st_size:
message = 'Buildout failed to process %s.' % self.getConfig('partition-id')
if MONITOR_URL:
message += '\nSee %s/log/%s for more information.' % (MONITOR_URL, log_name)
if monitor_url:
message += '\nSee %s/log/%s for more information.' % (monitor_url, log_name)
if not os.path.exists(link_file):
os.symlink(slapgrid_error_log_file, link_file)
else:
......
......@@ -4,11 +4,7 @@ from slapos.grid.promise.generic import GenericPromise
import os
import time
import psutil
from slapos.runner.utils import tail
PROCESS_PID_FILE = ""
PROCESS_NAME = ""
STATUS_FILE = ""
from .util import tail_file
class RunPromise(GenericPromise):
......@@ -19,20 +15,17 @@ class RunPromise(GenericPromise):
self.setPeriodicity(minute=2)
def sense(self):
if PROCESS_PID_FILE == "" or PROCESS_NAME == "" or STATUS_FILE == "":
self.logger.info("")
return
if not os.path.exists(PROCESS_PID_FILE):
process_pid_file = self.getConfig('process-pid-file')
if not os.path.exists(process_pid_file):
self.logger.info("Bootstrap didn't run!")
return
with open(PROCESS_PID_FILE) as f:
with open(process_pid_file) as f:
try:
pid = int(f.read())
except ValueError, e:
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:
process = psutil.Process(pid)
......@@ -51,18 +44,18 @@ class RunPromise(GenericPromise):
# process exited
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")
return
message = "Monitor bootstrap exited with error."
log_file = os.path.join(self.getPartitionFolder(), ".%s_%s.log" % (
self.getConfig('partition-id'),
PROCESS_NAME))
self.getConfig('process-name')))
if os.path.exists(log_file):
with open(log_file) as f:
message += "\n ---- Latest monitor-boostrap.log ----\n"
message += tail(f, 4)
message += "\n ---- Latest monitor-boostrap.log ----\n"
message += tail_file(log_file, 4)
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):
self.promise_name = "my-monitor-bootstrap.py"
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"
monitor_bootstrap_status.PROCESS_NAME = "monitor.boostrap"
monitor_bootstrap_status.STATUS_FILE = "%(state_file)s"
extra_config_dict = {
'process-pid-file': "%(pid_file)s",
'process-name': "monitor.boostrap",
'status-file': "%(state_file)s",
}
""" % {'pid_file': self.pid_file, 'state_file': self.state_file}
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