diff --git a/neat/globals/manager.py b/neat/globals/manager.py index c7c3f45..9a8d00b 100644 --- a/neat/globals/manager.py +++ b/neat/globals/manager.py @@ -76,6 +76,7 @@ from hashlib import sha1 import novaclient from novaclient.v1_1 import client import time +import subprocess import neat.common as common from neat.config import * @@ -159,7 +160,7 @@ def start(): int(config['log_level'])) state = init_state(config) - switch_hosts_on(state['compute_hosts']) + switch_hosts_on(state['db'], state['compute_hosts']) bottle.debug(True) bottle.app().state = { @@ -675,9 +676,16 @@ def switch_hosts_off(db, sleep_command, hosts): :param hosts: A list of hosts to switch off. :type hosts: list(str) """ - # TODO: implement running the sleep command over SSH + if sleep_command: + for host in hosts: + if log.isEnabledFor(logging.DEBUG): + log.debug('Calling: ssh {0} "{1}"'. \ + format(host, sleep_command)) + subprocess.call( + 'ssh {0} "{1}"'.format(host, sleep_command), + shell=True) if log.isEnabledFor(logging.INFO): - log.info('Switch off hosts: %s', str(hosts)) + log.info('Switched off hosts: %s', str(hosts)) db.insert_host_states(dict((x, 0) for x in hosts)) @@ -692,5 +700,5 @@ def switch_hosts_on(db, hosts): :type hosts: list(str) """ if log.isEnabledFor(logging.INFO): - log.info('Switch on hosts: %s', str(hosts)) + log.info('Switched on hosts: %s', str(hosts)) db.insert_host_states(dict((x, 1) for x in hosts)) diff --git a/tests/globals/test_manager.py b/tests/globals/test_manager.py index b8b5139..dd0e14b 100644 --- a/tests/globals/test_manager.py +++ b/tests/globals/test_manager.py @@ -19,6 +19,7 @@ import bottle from hashlib import sha1 from novaclient.v1_1 import client import time +import subprocess import neat.globals.manager as manager import neat.common as common @@ -152,8 +153,10 @@ class GlobalManager(TestCase): def test_start(self): with MockTransaction: app = mock('app') + db = mock('db') hosts = ['host1', 'host2'] state = {'property': 'value', + 'db': db, 'compute_hosts': hosts} config = { 'log_directory': 'dir', @@ -167,7 +170,7 @@ class GlobalManager(TestCase): expect(common).init_logging('dir', 'global-manager.log', 2).once() expect(manager).init_state(config). \ and_return(state).once() - expect(manager).switch_hosts_on(hosts).once() + expect(manager).switch_hosts_on(db, hosts).once() expect(bottle).app().and_return(app).once() expect(bottle).run(host='localhost', port=8080).once() manager.start() @@ -348,13 +351,23 @@ class GlobalManager(TestCase): {'vm1': 512, 'vm2': 1024} def test_switch_hosts_off(self): + db = db_utils.init_db('sqlite:///:memory:') + with MockTransaction: - db = db_utils.init_db('sqlite:///:memory:') + expect(subprocess).call('ssh h1 "sleep"', shell=True).once() + expect(subprocess).call('ssh h2 "sleep"', shell=True).once() expect(db).insert_host_states({ 'h1': 0, 'h2': 0}).once() manager.switch_hosts_off(db, 'sleep', ['h1', 'h2']) + with MockTransaction: + expect(subprocess).call.never() + expect(db).insert_host_states({ + 'h1': 0, + 'h2': 0}).once() + manager.switch_hosts_off(db, '', ['h1', 'h2']) + def test_switch_hosts_on(self): with MockTransaction: db = db_utils.init_db('sqlite:///:memory:')