Commit ba09f6fc authored by Marco Buttu's avatar Marco Buttu
Browse files

Implementation of issue #155

parent ed46a08d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@

SUBSYSTEM = Libraries 

MODULES =  SlaLibrary IRALibrary TextWindowLibrary ParserLibrary
MODULES =  SlaLibrary IRALibrary TextWindowLibrary ParserLibrary PyACSWrapper


MAKE_FLAGS = "-k"
+3 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
#-----------------------------------------
#*****************************************

PY_PACKAGES = testing
PY_PACKAGES = acswrapper

# -----------------
# Include Standards
@@ -22,10 +22,10 @@ all: do_all
	@echo " . . . 'all' done" 

clean : clean_all 
	$(RM) *.pyc testing/*.pyc
	$(RM) *.pyc acswrapper/*.pyc
	$(RM) ../lib ../bin ../config ../doc ../idl ../include \
		  ../object ../rtai ../test
	$(RM) $(INTROOT)/lib/python/site-packages/testing*
	$(RM) $(INTROOT)/lib/python/site-packages/acswrapper*
	@echo " . . . clean done"

install : install_all
+7 −0
Original line number Diff line number Diff line
import os

__author__ = "Marco Buttu <mbuttu@oa-cagliari.inaf.it>"


ACSDATA = os.getenv('ACSDATA')
logfile = os.path.join(ACSDATA, 'logs', '%s.log' % __name__)
+102 −0
Original line number Diff line number Diff line
import os
import time
import signal
import datetime

from subprocess import Popen, PIPE
from acswrapper import logfile


class Container(object):

    def __init__(self, name, lang):
        self.name = name
        self.lang = lang
        self.start_process = None

    def start(self):
        """Start the container and do not wait for the process to finish.

        This is a non blocking call.  The method calls the acsStartContainer
        command and returns immediately without waiting for the process to
        finish its job.  To know if the container is already started, call
        the Container.is_running() method."""
        with open(logfile, 'a') as outfile:
            if not self.is_running():
                self.start_process = Popen(
                    'acsStartContainer -%s %s' % (self.lang, self.name),
                    stdout=outfile, stderr=outfile, shell=True,
                    preexec_fn=os.setsid)
            else:
                outfile.write('Container %s already running' % self.name)

    def stop(self):
        """Stop the container and do not wait for the process to finish.

        This is a non blocking call.  The method calls the acsStopContainer
        command and returns immediately without waiting for the process to
        finish its job.  To know if the container is already stopped, call
        the Container.is_running() method."""
        with open(logfile, 'a') as outfile:
            Popen(
                'acsStopContainer %s' % self.name,
                stdout=outfile, stderr=outfile, shell=True)
        if self.start_process is not None:
            os.killpg(os.getpgid(self.start_process.pid), signal.SIGTERM) 
            self.start_process = None

    def wait_until_running(self, timeout=10):
        """Wait until the container is running."""
        t0 = datetime.datetime.now()
        while (datetime.datetime.now() - t0).seconds < timeout:
            if self.is_running():
                break
            else:
                time.sleep(0.5)

    def wait_until_not_running(self, timeout=10):
        """Wait until the container is not more running."""
        t0 = datetime.datetime.now()
        while (datetime.datetime.now() - t0).seconds < timeout:
            if not self.is_running():
                break
            else:
                time.sleep(0.5)

    def is_running(self):
        """Return True if the container is running."""
        pipes = Popen(['acsContainersStatus'], stdout=PIPE, stderr=PIPE)
        out, _ = pipes.communicate()
        return ('%s container is running' % self.name) in out


class ContainerError(RuntimeError):
    pass


def start_containers_and_wait(containers, timeout=10):
    """Start the containers and wait until they are actually started.

    This is a blocking call.  The method starts the containers and
    waits until they are actually started.  It raises RuntimeError
    in case the timeout expires."""
    for container in containers:
        container.start()
    for container in containers:
        container.wait_until_running(timeout)
        if not container.is_running():
            raise RuntimeError('cannot run %s' % container.name)


def stop_containers_and_wait(containers, timeout=10):
    """Stop the containers and wait until they are actually stopped.

    This is a blocking call.  The method stops the containers and
    waits until they are actually stopped.  It raises RuntimeError
    in case the timeout expires."""
    for container in containers:
        container.stop()
    for container in containers:
        container.wait_until_not_running(timeout)
        if container.is_running():
            raise RuntimeError('cannot stop %s' % container.name)
+73 −0
Original line number Diff line number Diff line
__all__ = ['acs']

import os
import time
import signal
import datetime
from subprocess import Popen, call

from Acspy.Util.ACSCorba import getManager
from acswrapper import logfile


class ACS(object):

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = object.__new__(cls, *args, **kwargs)
            cls._start_process = None
        return cls._instance

    def start(self):
        """Start ACS and wait until it is running."""
        with open(logfile, 'a') as outfile:
            if not self.is_running():
                self._start_process = Popen(
                    'acsStart', stdout=outfile, stderr=outfile,
                    shell=True, preexec_fn=os.setsid)
                self.wait_until_running()
            else:
                outfile.write('ACS is already running')
    
    def stop(self):
        """Stop ACS and wait until killACS terminates."""
        with open(logfile, 'a') as outfile:
            if self._start_process is not None:
                os.killpg(os.getpgid(self._start_process.pid), signal.SIGTERM) 
                self._start_process = None
            Popen('acsStop', stdout=outfile, stderr=outfile, shell=True)
            self.wait_until_not_running()
            if self.is_running():
                outfile.write('acsStop is not able to stop ACS')
            call('killACS', stdout=outfile, stderr=outfile, shell=True)
            if self.is_running():
                outfile.write('killACS is not able to stop ACS')
    
    def wait_until_running(self, timeout=180):
        """Wait until ACS is running."""
        t0 = datetime.datetime.now()
        while (datetime.datetime.now() - t0).seconds < timeout:
            if self.is_running():
                break
            else:
                time.sleep(0.5)
    
    def wait_until_not_running(self, timeout=240):
        """Wait until ACS is not more running."""
        t0 = datetime.datetime.now()
        while (datetime.datetime.now() - t0).seconds < timeout:
            if not self.is_running():
                break
            else:
                time.sleep(0.5)
    
    def is_running(self):
        """Return True if ACS is running."""
        try:
            mng = getManager()
            return bool(mng and mng.ping())
        except Exception:
            return False


acs = ACS()
Loading