Commit 08446fba authored by Cédric de Saint Martin's avatar Cédric de Saint Martin

Merge remote-tracking branch 'origin/proxy_slave_raise'

Conflicts:
	slapos/grid/slapgrid.py
parents 042adedf 4d0a5ab7
......@@ -107,6 +107,8 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
help="Enables console output and live output from subcommands.")
parser.add_argument("-v", "--verbose", action="store_true", default=False,
help="Be verbose.")
parser.add_argument("--maximum-periodicity", type=int, default=None,
help="Periodicity at which buildout should be run in instance.")
parser.add_argument("--promise-timeout", type=int, default=3,
help="Promise timeout in seconds.")
parser.add_argument("configuration_file", nargs=1, type=argparse.FileType(),
......@@ -170,6 +172,9 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
if option_dict.get('all') is True:
option_dict['develop'] = True
if option_dict.get('maximum_periodicity') is not None:
option_dict['force_periodicity'] = True
repository_required = False
if 'key_file' in option_dict:
repository_required = True
......@@ -232,6 +237,7 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
else:
signature_certificate_list = None
# Parse cache / binary options
option_dict["binary-cache-url-blacklist"] = [
url.strip() for url in option_dict.get("binary-cache-url-blacklist", ""
......@@ -285,6 +291,8 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
develop=option_dict.get('develop', False),
software_release_filter_list=option_dict.get('only_sr', None),
computer_partition_filter_list=option_dict.get('only_cp', None),
force_periodicity = option_dict.get('force_periodicity', False),
maximum_periodicity = option_dict.get('maximum_periodicity', 86400),
),
option_dict])
......@@ -353,6 +361,8 @@ class Slapgrid(object):
supervisord_socket,
supervisord_configuration_path,
buildout,
force_periodicity=False,
maximum_periodicity=86400,
key_file=None,
cert_file=None,
signature_private_key_file=None,
......@@ -374,7 +384,8 @@ class Slapgrid(object):
shadir_key_file=None,
develop=False,
software_release_filter_list=None,
computer_partition_filter_list=None):
computer_partition_filter_list=None,
):
"""Makes easy initialisation of class parameters"""
# Parses arguments
self.software_root = os.path.abspath(software_root)
......@@ -424,6 +435,8 @@ class Slapgrid(object):
if computer_partition_filter_list is not None:
self.computer_partition_filter_list = \
computer_partition_filter_list.split(",")
self.maximum_periodicity = maximum_periodicity
self.force_periodicity = force_periodicity
def checkEnvironmentAndCreateStructure(self):
"""Checks for software_root and instance_root existence, then creates
......@@ -638,15 +651,31 @@ class Slapgrid(object):
else:
timestamp = None
software_path = os.path.join(self.software_root,
getSoftwareUrlHash(software_url))
# Get periodicity from periodicity file if not forced
if not self.force_periodicity:
periodicity_path = os.path.join(software_path,'periodicity')
if os.path.exists(periodicity_path):
try:
self.maximum_periodicity = int(open(periodicity_path).read())
except ValueError:
os.remove(periodicity_path)
exception = traceback.format_exc()
logger.error(exception)
# Check if timestamp from server is more recent than local one.
# If not: it's not worth processing this partition (nothing has changed).
if computer_partition_id not in self.computer_partition_filter_list and \
(not self.develop) and os.path.exists(timestamp_path):
old_timestamp = open(timestamp_path).read()
last_runtime = int(os.path.getmtime(timestamp_path))
if timestamp:
try:
if int(timestamp) <= int(old_timestamp):
continue
if int(time.time()) <= (last_runtime + self.maximum_periodicity) :
continue
except ValueError:
os.remove(timestamp_path)
exception = traceback.format_exc()
......
......@@ -354,13 +354,21 @@ def request_not_shared():
def request_slave():
"""
Function to organise link between slave and master
Function to organise link between slave and master.
Slave information are stored in places:
1. slave table having information such as slave reference,
connection information to slave (given by slave master),
hosted_by and asked_by reference.
2. A dictionnary in slave_instance_list of selected slave master
in which are stored slave_reference, software_type, slave_title and
partition_parameter_kw stored as individual keys.
"""
software_release = request.form['software_release'].encode()
# some supported parameters
software_type = request.form.get('software_type').encode()
partition_reference = request.form.get('partition_reference', '').encode()
partition_id = request.form.get('computer_partition_id', '').encode()
# Contain slave parameters to be given to slave master
partition_parameter_kw = request.form.get('partition_parameter_xml', None)
if partition_parameter_kw :
partition_parameter_kw = xml_marshaller.xml_marshaller.loads(
......@@ -368,6 +376,7 @@ def request_slave():
else:
partition_parameter_kw = {}
instance_xml = dict2xml(partition_parameter_kw)
# We will search for a master corresponding to request
args = []
a = args.append
q = 'SELECT * FROM %s WHERE software_release=?'
......@@ -379,24 +388,31 @@ def request_slave():
if partition is None:
app.logger.warning('No partition corresponding to slave request')
abort(408)
# Define Slave
# We set slave dictionnary as described in docstring
new_slave = {}
slave_reference = partition_id + '_' + partition_reference
new_slave['slave_title'] = slave_reference
new_slave['slap_software_type'] = software_type
new_slave['slave_reference'] = slave_reference
for key in partition_parameter_kw :
new_slave[key] = partition_parameter_kw[key]
if partition_parameter_kw[key] is not None :
new_slave[key] = partition_parameter_kw[key]
# Add slave to partition slave_list if not present
# Add slave to partition slave_list if not present else replace information
slave_instance_list = partition['slave_instance_list']
if slave_instance_list == None:
slave_instance_list = []
else:
slave_instance_list = xml_marshaller.xml_marshaller.loads(slave_instance_list)
if not slave_reference in (x['slave_reference'] for x in slave_instance_list):
slave_instance_list.append(new_slave)
for x in slave_instance_list:
if x['slave_reference'] == slave_reference:
slave_instance_list.remove(x)
slave_instance_list.append(new_slave)
# Update slave_instance_list in database
args = []
a = args.append
q = 'UPDATE %s SET slave_instance_list=?'
......@@ -413,8 +429,8 @@ def request_slave():
[slave_reference], one=True)
if slave is None :
execute_db('slave',
'INSERT OR IGNORE INTO %s (reference,asked_by) values(:reference,:asked_by)',
[slave_reference,partition_id])
'INSERT OR IGNORE INTO %s (reference,asked_by,hosted_by) values(:reference,:asked_by,:hosted_by)',
[slave_reference,partition_id,partition['reference']])
slave = execute_db('slave','SELECT * FROM %s WHERE reference=?',
[slave_reference], one = True)
......
......@@ -864,6 +864,132 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
['getFullComputerInformation'])
self.assertTrue(self.started)
def test_partition_periodicity_is_not_overloaded_if_forced(self):
"""
If periodicity file in software directory but periodicity is forced
periodicity will be the one given by parameter
1. We set force_periodicity parameter to True
2. We put a periodicity file in the software release directory
with an unwanted periodicity
3. We process partition list and check that maximum periodicity
is not the one given in file
"""
self.sequence = []
self.timestamp = str(int(time.time()))
self.started = False
unwanted_periodicity = 40
self.grid.force_periodicity = True
httplib.HTTPConnection._callback = _server_response(self,
'stopped',
self.timestamp)
os.mkdir(self.software_root)
os.mkdir(self.instance_root)
partition_path = os.path.join(self.instance_root, '0')
os.mkdir(partition_path, 0750)
software_hash = slapos.grid.utils.getSoftwareUrlHash('http://sr/')
srdir = os.path.join(self.software_root, software_hash)
os.mkdir(srdir)
open(os.path.join(srdir, 'template.cfg'), 'w').write(
"""[buildout]""")
open(os.path.join(srdir, 'periodicity'), 'w').write(
"""%s""" % (unwanted_periodicity))
srbindir = os.path.join(srdir, 'bin')
os.mkdir(srbindir)
open(os.path.join(srbindir, 'buildout'), 'w').write("""#!/bin/sh
touch worked""")
os.chmod(os.path.join(srbindir, 'buildout'), 0755)
self.assertTrue(self.grid.processComputerPartitionList())
self.assertNotEqual(unwanted_periodicity,self.grid.maximum_periodicity)
def test_partition_periodicity_in_software_overload_if_not_forced(self):
"""
If periodicity file in software directory and it is not forced
periodicity will be the one given by file
1. We put a periodicity file in the software release directory
with wanted periodicity
2. We process partition list and check periodicity is the one given in
file
"""
self.sequence = []
self.timestamp = str(int(time.time()))
self.started = False
wanted_periodicity = 40
httplib.HTTPConnection._callback = _server_response(self,
'stopped',
self.timestamp)
os.mkdir(self.software_root)
os.mkdir(self.instance_root)
partition_path = os.path.join(self.instance_root, '0')
os.mkdir(partition_path, 0750)
software_hash = slapos.grid.utils.getSoftwareUrlHash('http://sr/')
srdir = os.path.join(self.software_root, software_hash)
os.mkdir(srdir)
open(os.path.join(srdir, 'template.cfg'), 'w').write(
"""[buildout]""")
open(os.path.join(srdir, 'periodicity'), 'w').write(
"""%s""" % wanted_periodicity)
srbindir = os.path.join(srdir, 'bin')
os.mkdir(srbindir)
open(os.path.join(srbindir, 'buildout'), 'w').write("""#!/bin/sh
touch worked""")
os.chmod(os.path.join(srbindir, 'buildout'), 0755)
self.assertTrue(self.grid.processComputerPartitionList())
self.assertEqual(wanted_periodicity,self.grid.maximum_periodicity)
def test_partition_periodicity_trigger_processing(self):
"""
If time between last processing of instance and now is superior
to periodicity then instance should be proceed
1. We set a wanted maximum_periodicity in periodicity file in
in software release directory
2. We process computer partition and check if wanted_periodicity was
used as maximum_periodicty
3. We wait for a time superior to wanted_periodicty
4. We launch processComputerPartition and check that partition is
processed one more time
5. We check that modification time of .timestamp was modified
"""
self.sequence = []
self.timestamp = str(int(time.time()-5))
self.started = False
wanted_periodicity = 3
httplib.HTTPConnection._callback = _server_response(self,
'stopped',
self.timestamp)
os.mkdir(self.software_root)
os.mkdir(self.instance_root)
partition_path = os.path.join(self.instance_root, '0')
os.mkdir(partition_path, 0750)
software_hash = slapos.grid.utils.getSoftwareUrlHash('http://sr/')
srdir = os.path.join(self.software_root, software_hash)
os.mkdir(srdir)
open(os.path.join(srdir, 'template.cfg'), 'w').write(
"""[buildout]""")
open(os.path.join(srdir, 'periodicity'), 'w').write(
"""%s""" % wanted_periodicity)
srbindir = os.path.join(srdir, 'bin')
os.mkdir(srbindir)
open(os.path.join(srbindir, 'buildout'), 'w').write("""#!/bin/sh
touch worked""")
os.chmod(os.path.join(srbindir, 'buildout'), 0755)
self.assertTrue(self.grid.processComputerPartitionList())
self.assertEqual(wanted_periodicity,self.grid.maximum_periodicity)
self.setSlapgrid()
last_runtime = os.path.getmtime(os.path.join(partition_path,'.timestamp'))
time.sleep(wanted_periodicity + 1)
self.assertTrue(self.grid.processComputerPartitionList())
self.assertEqual(self.sequence,
['getFullComputerInformation', 'availableComputerPartition',
'stoppedComputerPartition', 'getFullComputerInformation',
'availableComputerPartition','stoppedComputerPartition',
])
self.assertGreater(
os.path.getmtime(os.path.join(partition_path,'.timestamp')),
last_runtime)
class TestSlapgridArgumentTuple(unittest.TestCase):
"""
......@@ -965,6 +1091,24 @@ buildout = /path/to/buildout/binary
slapgrid_object = parser(*argument_tuple)[0]
self.assertFalse(slapgrid_object.develop)
def test_force_periodicity_if_periodicity_not_given(self):
"""
Check if not giving --maximum-periodicity triggers "force_periodicity"
option to be false.
"""
parser = slapgrid.parseArgumentTupleAndReturnSlapgridObject
argument_tuple = self.default_arg_tuple
slapgrid_object = parser(*argument_tuple)[0]
self.assertFalse(slapgrid_object.force_periodicity)
def test_force_periodicity_if_periodicity_given(self):
"""
Check if giving --maximum-periodicity triggers "force_periodicity" option.
"""
parser = slapgrid.parseArgumentTupleAndReturnSlapgridObject
argument_tuple = ("--maximum-periodicity","40") + self.default_arg_tuple
slapgrid_object = parser(*argument_tuple)[0]
self.assertTrue(slapgrid_object.force_periodicity)
class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_one_failing_promise(self):
......@@ -1441,21 +1585,3 @@ exit 0""" % {'worked_file': worked_file, 'lockfile': lockfile})
self.assertEquals(self.error, 1)
self.assertFalse(self.started)
@unittest.skip("Not implemented")
def test_slapgrid_processes_partition_after_global_timeout(self):
"""
Test that slapgrid processes again partition after delay defined by
slapgrid even if .timestamp is up-to-date.
"""
# XXX Not implemented
pass
@unittest.skip("Not implemented")
def test_slapgrid_processes_partition_after_timeout_defined_by_software_release(self):
"""
Test that if SR of instance defines a "buildotu delay", slapgrid processes
again partition after that delay and NOT after delay defined by slapgrid.
"""
# XXX Not implemented
pass
......@@ -214,30 +214,25 @@ class MasterMixin(BasicMixin):
'_parameter_dict', None)
return computer_partition
def setComputerPartitionConnectionXml(self, instance):
"""
Will send instance connection information to server
including from slaves in this partition
"""
# Will post information about slaves
for slave in instance._parameter_dict['slave_instance_list']:
request_dict = {
'slave_reference': slave['slave_reference'],
'computer_partition_id': instance._partition_id,
'connection_xml': xml_marshaller.xml_marshaller.dumps(
instance._connection_dict),
}
self.app.post('/setComputerPartitionConnectionXml',
data = request_dict)
# Will post information about instance
request_dict = {
'slave_reference': None,
'computer_partition_id': instance._partition_id,
'connection_xml': xml_marshaller.xml_marshaller.dumps(
instance._connection_dict),
}
self.app.post('/setComputerPartitionConnectionXml',
data = request_dict)
def setConnectionDict(self, partition_id,
connection_dict, slave_reference=None):
self.app.post('/setComputerPartitionConnectionXml', data = {
'computer_id': self.computer_id,
'computer_partition_id': partition_id,
'connection_xml': xml_marshaller.xml_marshaller.dumps(connection_dict),
'slave_reference': slave_reference})
def getPartitionInformation(self,computer_partition_id):
"""
Return computer information as stored in proxy for corresponding id
"""
rv = self.app.get('/getFullComputerInformation?computer_id='
+ self.computer_id)
computer = xml_marshaller.xml_marshaller.loads(rv.data)
for instance in computer._computer_partition_list:
if instance._partition_id == computer_partition_id:
return instance
class TestRequest (MasterMixin, unittest.TestCase):
......@@ -307,33 +302,96 @@ class TestRequest (MasterMixin, unittest.TestCase):
with self.assertRaises(WrongFormat):
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True)
def test_slave_request_set_parameters (self):
"""
Parameters sent in slave request must be put in slave master
slave instance list.
1. We request a slave instance we defined parameters
2. We check parameters are in the dictionnary defining slave in
slave master slave_instance_list
"""
self.add_free_partition(6)
# Provide partition
master_partition_id = self.request('http://sr//', None,
'Maria', 'slappart4')._partition_id
# First request of slave instance
wanted_domain = 'fou.org'
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True,
partition_parameter_kw = {'domain':wanted_domain})
# Get updated information for master partition
master_partition = self.getPartitionInformation(master_partition_id)
our_slave = master_partition._parameter_dict['slave_instance_list'][0]
self.assertEqual(our_slave.get('domain'),wanted_domain)
def test_slave_request_set_parameters_are_updated (self):
"""
Parameters sent in slave request must be put in slave master
slave instance list and updated when they change.
1. We request a slave instance we defined parameters
2. We check parameters are in the dictionnary defining slave in
slave master slave_instance_list
3. We request same slave instance with changed parameters
4. We check parameters are in the dictionnary defining slave in
slave master slave_instance_list have changed
"""
self.add_free_partition(6)
# Provide partition
master_partition_id = self.request('http://sr//', None,
'Maria', 'slappart4')._partition_id
# First request of slave instance
wanted_domain_1 = 'crazy.org'
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True,
partition_parameter_kw = {'domain':wanted_domain_1})
# Get updated information for master partition
master_partition = self.getPartitionInformation(master_partition_id)
our_slave = master_partition._parameter_dict['slave_instance_list'][0]
self.assertEqual(our_slave.get('domain'),wanted_domain_1)
# Second request of slave instance
wanted_domain_2 = 'maluco.org'
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True,
partition_parameter_kw = {'domain':wanted_domain_2})
# Get updated information for master partition
master_partition = self.getPartitionInformation(master_partition_id)
our_slave = master_partition._parameter_dict['slave_instance_list'][0]
self.assertNotEqual(our_slave.get('domain'),wanted_domain_1)
self.assertEqual(our_slave.get('domain'),wanted_domain_2)
def test_slave_request_one_corresponding_partition (self):
"""
Successfull request slave instance follow these steps:
1. Provide one corresponding partition
2. Ask for Slave instance. Error is raise because it is not ready
2. Ask for Slave instance. But no connection parameters
But slave is added to Master Instance slave list
3. Master Instance get updated information (including slave list)
4. Master instance post information about slaves and itself
4. Master instance post information about slave connection parameters
5. Ask for slave instance is successfull and return a computer instance
with connection information
"""
self.add_free_partition(6)
# Provide partition
master_partition_id = self.request('http://sr//', None,
'Maria', 'slappart4')._partition_id
# Failed request of slave instance
with self.assertRaises(TypeError):
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True)
# First request of slave instance
name = 'Maria'
requester = 'slappart2'
our_slave = self.request('http://sr//', None,
name, requester, shared=True)
self.assertIsInstance(our_slave, slapos.slap.ComputerPartition)
self.assertEqual(our_slave._connection_dict,{})
# Get updated information for master partition
rv = self.app.get('/getFullComputerInformation?computer_id='
+ self.computer_id)
computer = xml_marshaller.xml_marshaller.loads(rv.data)
for instance in computer._computer_partition_list:
if instance._partition_id == master_partition_id:
master_partition = instance
master_partition = self.getPartitionInformation(master_partition_id)
slave_for_master = master_partition._parameter_dict['slave_instance_list'][0]
# Send information about slave
self.setComputerPartitionConnectionXml(master_partition)
# Successfull slave request
self.assertIsInstance(
self.request('http://sr//', None, 'Maria', 'slappart2', shared=True),
slapos.slap.ComputerPartition)
slave_address = {'url':'%s.master.com'}
self.setConnectionDict(partition_id=master_partition._partition_id,
connection_dict=slave_address,
slave_reference=slave_for_master['slave_reference'])
# Successfull slave request with connection parameters
our_slave = self.request('http://sr//', None,
name, requester, shared=True)
self.assertIsInstance(our_slave, slapos.slap.ComputerPartition)
self.assertEqual(slave_address, our_slave._connection_dict)
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