Skip to content

Commit

Permalink
Merge pull request #52 from telefonicaid/bug/CLAUDIA-5543_CheckEnviro…
Browse files Browse the repository at this point in the history
…nmentsBeforeStart

check environments before start
  • Loading branch information
Fernando López Aguilar committed Oct 19, 2015
2 parents 996dd9f + 0191ba0 commit f571973
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fiware_cloto/cloto/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

from fiware_cloto.cloto.models import ServerInfo
from django.conf import settings
import logging as logger
from fiware_cloto.cloto.utils.log import logger

# Synchronizing database
call_command('syncdb', interactive=False)
Expand Down
36 changes: 25 additions & 11 deletions fiware_cloto/environments/environment_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from django.conf import settings
from circus import get_arbiter
import subprocess
from subprocess import Popen, PIPE


class environment_controller():
Expand All @@ -35,23 +35,37 @@ class environment_controller():

started = False

def check_python_process(self):
p = subprocess.Popen(['ps', '-awx'], stdout=subprocess.PIPE)
out, err = p.communicate()

for line in out.splitlines():
if 'environmentManager.py' in line:
self.started = True

return self.started

def start_manager(self):
if settings.SETTINGS_TYPE == 'production':
arbiter = get_arbiter([{"cmd": "python "
"" + settings.ENVIRONMENTS_MANAGER_PATH, "numprocesses": 1}], background=True)

if not self.check_python_process():
arbiter.start()
if check_python_process():
clean_environments()

arbiter.start()

def is_started(self):
return self.started


def clean_environments():
cmd = "ps -awx | grep [f]iware_cloto/environments | awk '{print $1}' | xargs kill -9"
output, error = Popen(cmd, shell=True, executable="/bin/bash", stdout=PIPE,
stderr=PIPE).communicate()

if error:
raise Exception(error)

return output

def check_python_process():
p = Popen(['ps', '-awx'], stdout=PIPE)
output, error = p.communicate()
started = False
for line in output.splitlines():
if 'environmentManager.py' in line:
started = True
return started
82 changes: 82 additions & 0 deletions fiware_cloto/environments/tests/test_environment_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of FI-WARE project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at:
#

# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with [email protected]
#
import unittest
from fiware_cloto.environments import environment_controller
from mock import patch, Mock


WITHOUT = """
15009 pts/1 Ss 0:00 -bash
15094 ? S 0:00 upstart-udev-bridge --daemon
15097 ? S 0:00 upstart-socket-bridge --daemon
15100 ? S 0:00 upstart-file-bridge --daemon
44657 ttys000 Ss 0:00.00 grep fiware_cloto/environments
"""

WITH = """
15009 pts/1 Ss 0:00 -bash
15094 ? S 0:00 upstart-udev-bridge --daemon
15097 ? S 0:00 upstart-socket-bridge --daemon
15100 ? S 0:00 upstart-file-bridge --daemon
16424 ? Ss 0:00 python /usr/lib/python2.7/dist-packages/fiware_cloto/environments/environmentManager.py
44657 ttys000 Ss 0:00.00 grep fiware_cloto/environments
"""


class MockPopen(unittest.TestCase):

def setUp(self):
self.popen_patcher = patch('fiware_cloto.environments.environment_controller.Popen')
self.mock_popen = self.popen_patcher.start()

self.mock_rv = Mock()
# communicate() returns [STDOUT, STDERR]
self.mock_rv.communicate.return_value = [WITHOUT, None]
self.mock_popen.return_value = self.mock_rv

# run after each test
def tearDown(self):
self.popen_patcher.stop()

def test_clean_no_environments(self):
"""test_clean_no_environments should check that there are no environments to clean."""
result = environment_controller.check_python_process()
self.assertEqual(False, result)

environment_controller.clean_environments()
self.assertTrue(self.mock_popen.called)

def test_clean_one_environments(self):
"""test_clean_environments should create an environment manager and clean the environment."""
self.mock_rv.communicate.return_value = [WITH, None]
result = environment_controller.check_python_process()
self.assertEqual(True, result)
self.mock_rv.communicate.return_value = [WITHOUT, None]
environment_controller.clean_environments()
self.assertTrue(self.mock_popen.called)
result = environment_controller.check_python_process()
self.assertEqual(False, result)

0 comments on commit f571973

Please sign in to comment.