Commit 9e2d0bf0 authored by Jérome Perrin's avatar Jérome Perrin

WIP: erp5-util supervisord from previous run

parent 56fc943a
import unittest
import mock
from unittest import TestCase
from contextlib import contextmanager
......@@ -1138,3 +1139,29 @@ shared = true
RunnerClass.updateDictionaryFile = original_updateDictionaryFile
RunnerClass._createInstance = original__createInstance
RunnerClass._waitInstanceCreation = original__waitInstanceCreation
def test_cleanup_supervisord_from_previous_run(self):
test_node = self.getTestNode()
runner = test_type_registry['UnitTest'](test_node)
node_test_suite = test_node.getNodeTestSuite('foo')
host, port = '127.0.0.1', 1234 # XXX
test_node.config['ipv4_address'] = host
test_node.config['ipv6_address'] = '::1' # TODO
test_node.config['proxy_host'] = host
test_node.config['proxy_port'] = port
test_node.config['master_url'] = "http://{proxy_host}:{proxy_port}".format(**test_node.config)
test_node.config['slapos_binary'] = '/usr/bin/slapos' # XXX must be in path (and not be the slaprunner wrapper !)
test_node.config['partition_reference'] = self.id()
test_node.config['environment'] = os.environ
test_node.config["software_list"] = []
with mock.patch(
'erp5.util.testnode.UnitTestRunner.SlapOSControler.runSoftwareRelease',
return_value={'status_code': 0}): #,\
# mock.patch(
# 'erp5.util.testnode.UnitTestRunner.SlapOSControler.runComputerPartitionXXX',
# return_value={'status_code': 0}):
runner.prepareSlapOSForTestSuite(node_test_suite)
import pdb; pdb.set_trace()
test_node.process_manager.max_timeout = 1
test_node.process_manager.spawn("sleep" "10")
\ No newline at end of file
......@@ -142,6 +142,7 @@ class ProcessManager(object):
self.timer_set = set()
def spawn(self, *args, **kw):
print "spawn", args, self.max_timeout
def timeoutExpired(p):
if p.poll() is None:
logger.warning('PROCESS TOO LONG OR DEAD, GOING TO BE TERMINATED')
......
......@@ -40,13 +40,14 @@ MAX_SR_RETRIES = 3
class SlapOSControler(object):
def __init__(self, working_directory, config):
def __init__(self, working_directory, config, logger=None):
self.config = config
self.software_root = os.path.join(working_directory, 'soft')
self.instance_root = os.path.join(working_directory, 'inst')
self.slapos_config = os.path.join(working_directory, 'slapos.cfg')
self.proxy_database = os.path.join(working_directory, 'proxy.db')
self.instance_config = {}
self.logger = logger
#TODO: implement a method to get all instance related the slapOS account
# and deleting all old instances (based on creation date or name etc...)
......@@ -54,7 +55,7 @@ class SlapOSControler(object):
def createSlaposConfigurationFileAccount(self, key, certificate, slapos_url, config):
# Create "slapos_account" directory in the "slapos_directory"
slapos_account_directory = os.path.join(config['slapos_directory'], "slapos_account")
createFolder(slapos_account_directory)
createFolder(slapos_account_directory, logger)
# Create slapos-account files
slapos_account_key_path = os.path.join(slapos_account_directory, "key")
slapos_account_certificate_path = os.path.join(slapos_account_directory, "certificate")
......@@ -204,7 +205,7 @@ class SlapOSControler(object):
def _resetSoftware(self):
logger.info('SlapOSControler: GOING TO RESET ALL SOFTWARE : %r',
self.software_root)
createFolder(self.software_root, True)
createFolder(self.software_root, True, logger)
def initializeSlapOSControler(self, slapproxy_log=None, process_manager=None,
reset_software=False, software_path_list=None):
......@@ -266,12 +267,12 @@ class SlapOSControler(object):
if reset_software:
self._resetSoftware()
else:
createFolder(self.software_root)
createFolder(self.software_root, logger)
instance_root = self.instance_root
# Delete any existing partition in order to not get its data (ex.
# MySQL DB content) from previous runs. To support changes of partition
# naming scheme (which already happened), do this at instance_root level.
createFolder(instance_root, True)
createFolder(instance_root, True, logger)
for i in xrange(MAX_PARTITIONS):
# create partition and configure computer
# XXX: at the moment all partitions do share same virtual interface address
......@@ -308,9 +309,10 @@ class SlapOSControler(object):
os.environ['PATH'] = environment['PATH']
# a SR may fail for number of reasons (incl. network failures)
# so be tolerant and run it a few times before giving up
#import pdb; pdb.set_trace()
for _ in xrange(MAX_SR_RETRIES):
status_dict = self.spawn(config['slapos_binary'],
'node', 'software', '--all',
'node', 'software', # '--all', XXX all does not uses .completed
'--pidfile', os.path.join(self.software_root, 'slapos-node.pid'),
'--cfg', self.slapos_config, raise_error_if_fail=False,
log_prefix='slapgrid_sr', get_output=False)
......
import os
import stat
import shutil
import psutil
import logging
def rmtree(path):
"""Delete a path recursively.
......@@ -26,10 +28,25 @@ def rmtree(path):
shutil.rmtree(path, onerror=chmod_retry)
def createFolder(folder, clean=False):
def createFolder(folder, clean=False, logger=None):
if os.path.exists(folder):
if not clean:
return
# before removing, check that we don't have a supervisord left here,
# and terminate it if it's the case
supervisord_pid_file = os.path.join(folder, 'var/run/supervisord.pid')
if os.path.exists(supervisord_pid_file):
with open(supervisord_pid_file, 'r') as f:
pid = int(f.read().strip())
supervisord = psutil.Process(pid)
if supervisord.cmdline()[-1].endswith("supervisor.supervisord.main()"):
if logger is None:
logger = logging.getLogger()
logger.warn("Killing previous supervisord with pid %s" % pid)
supervisord.terminate()
rmtree(folder)
os.mkdir(folder)
......
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