Commit ec92a0c4 authored by Cédric de Saint Martin's avatar Cédric de Saint Martin Committed by Rafael Monnerat

[slapos.core] Add retention_delay support.

parent abe4ec1f
...@@ -37,6 +37,7 @@ import subprocess ...@@ -37,6 +37,7 @@ import subprocess
import tarfile import tarfile
import tempfile import tempfile
import textwrap import textwrap
import time
import xmlrpclib import xmlrpclib
from supervisor import xmlrpc from supervisor import xmlrpc
...@@ -273,6 +274,8 @@ class Software(object): ...@@ -273,6 +274,8 @@ class Software(object):
class Partition(object): class Partition(object):
"""This class is responsible of the installation of an instance """This class is responsible of the installation of an instance
""" """
retention_lock_delay_filename = '.slapos-retention-lock-delay'
retention_lock_date_filename = '.slapos-retention-lock-date'
# XXX: we should give the url (or the "key") instead of the software_path # XXX: we should give the url (or the "key") instead of the software_path
# then compute the path from it, like in Software. # then compute the path from it, like in Software.
...@@ -289,6 +292,7 @@ class Partition(object): ...@@ -289,6 +292,7 @@ class Partition(object):
buildout, buildout,
logger, logger,
certificate_repository_path=None, certificate_repository_path=None,
retention_delay=0
): ):
"""Initialisation of class parameters""" """Initialisation of class parameters"""
self.buildout = buildout self.buildout = buildout
...@@ -315,6 +319,20 @@ class Partition(object): ...@@ -315,6 +319,20 @@ class Partition(object):
self.partition_id + '.crt') self.partition_id + '.crt')
self._updateCertificate() self._updateCertificate()
self.retention_delay = retention_delay
if type(self.retention_delay) not in (int, float) \
or self.retention_delay <= 0:
self.logger.warn('Retention delay value (%s) is not valid, ignoring.' \
% self.retention_delay)
self.retention_delay = 0
self.retention_lock_delay_file_path = os.path.join(
self.instance_path, self.retention_lock_delay_filename
)
self.retention_lock_date_file_path = os.path.join(
self.instance_path, self.retention_lock_date_filename
)
def _updateCertificate(self): def _updateCertificate(self):
try: try:
partition_certificate = self.computer_partition.getCertificate() partition_certificate = self.computer_partition.getCertificate()
...@@ -492,6 +510,7 @@ class Partition(object): ...@@ -492,6 +510,7 @@ class Partition(object):
buildout_binary=buildout_binary, buildout_binary=buildout_binary,
logger=self.logger) logger=self.logger)
self.generateSupervisorConfigurationFile() self.generateSupervisorConfigurationFile()
self.createRetentionLockDelay()
def generateSupervisorConfigurationFile(self): def generateSupervisorConfigurationFile(self):
""" """
...@@ -564,6 +583,11 @@ class Partition(object): ...@@ -564,6 +583,11 @@ class Partition(object):
""" """
self.logger.info("Destroying Computer Partition %s..." self.logger.info("Destroying Computer Partition %s..."
% self.computer_partition.getId()) % self.computer_partition.getId())
self.createRetentionLockDate()
if not self.checkRetentionIsAuthorized():
return False
# Launches "destroy" binary if exists # Launches "destroy" binary if exists
destroy_executable_location = os.path.join(self.instance_path, 'sbin', destroy_executable_location = os.path.join(self.instance_path, 'sbin',
'destroy') 'destroy')
...@@ -607,6 +631,8 @@ class Partition(object): ...@@ -607,6 +631,8 @@ class Partition(object):
except IOError as exc: except IOError as exc:
raise IOError("I/O error while freeing partition (%s): %s" % (self.instance_path, exc)) raise IOError("I/O error while freeing partition (%s): %s" % (self.instance_path, exc))
return True
def fetchInformations(self): def fetchInformations(self):
"""Fetch usage informations with buildout, returns it. """Fetch usage informations with buildout, returns it.
""" """
...@@ -649,3 +675,112 @@ class Partition(object): ...@@ -649,3 +675,112 @@ class Partition(object):
supervisor.addProcessGroup(gname) supervisor.addProcessGroup(gname)
self.logger.info('Updated %r' % gname) self.logger.info('Updated %r' % gname)
self.logger.debug('Supervisord updated') self.logger.debug('Supervisord updated')
def _set_ownership(self, path):
"""
If running as root: copy ownership of software_path to path
If not running as root: do nothing
"""
if os.getuid():
return
root_stat = os.stat(self.software_path)
path_stat = os.stat(path)
if (root_stat.st_uid != path_stat.st_uid or
root_stat.st_gid != path_stat.st_gid):
os.chown(path, root_stat.st_uid, root_stat.st_gid)
def checkRetentionIsAuthorized(self):
"""
Check if retention is authorized by checking retention lock delay or
retention lock date.
A retention lock delay is a delay which is:
* Defined by the user/machine who requested the instance
* Hardcoded the first time the instance is deployed, then is read-only
during the whole lifetime of the instance
* Triggered the first time the instance is requested to be destroyed
(retention will be ignored).
From this point, it is not possible to destroy the instance until the
delay is over.
* Accessible in read-only mode from the partition
A retention lock date is the date computed from (date of first
retention request + retention lock delay in days).
Example:
* User requests an instance with delay as 10 (days) to a SlapOS Master
* SlapOS Master transmits this information to the SlapOS Node (current code)
* SlapOS Node hardcodes this delay at first deployment
* User requests retention of instance
* SlapOS Node tries to destroy for the first time: it doesn't actually
destroy, but it triggers the creation of a retention lock date from
from the hardcoded delay. At this point it is not possible to
destroy instance until current date + 10 days.
* SlapOS Node continues to try to destroy: it doesn't do anything until
retention lock date is reached.
"""
retention_lock_date = self.getExistingRetentionLockDate()
now = time.time()
if not retention_lock_date:
if self.getExistingRetentionLockDelay() > 0:
self.logger.info('Impossible to destroy partition yet because of retention lock.')
return False
# Else: OK to destroy
else:
if now < retention_lock_date:
self.logger.info('Impossible to destroy partition yet because of retention lock.')
return False
# Else: OK to destroy
return True
def createRetentionLockDelay(self):
"""
Create a retention lock delay for the current partition.
If retention delay is not specified, create it wth "0" as value
"""
if os.path.exists(self.retention_lock_delay_file_path):
return
with open(self.retention_lock_delay_file_path, 'w') as delay_file_path:
delay_file_path.write(str(self.retention_delay))
self._set_ownership(self.retention_lock_delay_file_path)
def getExistingRetentionLockDelay(self):
"""
Return the retention lock delay of current partition (created at first
deployment) if exist.
Return -1 otherwise.
"""
retention_delay = -1
if os.path.exists(self.retention_lock_delay_file_path):
with open(self.retention_lock_delay_file_path) as delay_file_path:
retention_delay = float(delay_file_path.read())
return retention_delay
def createRetentionLockDate(self):
"""
If retention lock delay > 0:
Create a retention lock date for the current partition from the
retention lock delay.
Do nothing otherwise.
"""
if os.path.exists(self.retention_lock_date_file_path):
return
retention_delay = self.getExistingRetentionLockDelay()
if retention_delay <= 0:
return
now = int(time.time())
retention_date = now + retention_delay * 24 * 3600
with open(self.retention_lock_date_file_path, 'w') as date_file_path:
date_file_path.write(str(retention_date))
self._set_ownership(self.retention_lock_date_file_path)
def getExistingRetentionLockDate(self):
"""
Return the retention lock delay of current partition if exist.
Return None otherwise.
"""
if os.path.exists(self.retention_lock_date_file_path):
with open(self.retention_lock_date_file_path) as date_file_path:
return float(date_file_path.read())
else:
return None
...@@ -662,7 +662,8 @@ class Slapgrid(object): ...@@ -662,7 +662,8 @@ class Slapgrid(object):
software_release_url=software_url, software_release_url=software_url,
certificate_repository_path=self.certificate_repository_path, certificate_repository_path=self.certificate_repository_path,
buildout=self.buildout, buildout=self.buildout,
logger=self.logger) logger=self.logger,
retention_delay=getattr(computer_partition, '_retention_delay', 0))
computer_partition_state = computer_partition.getState() computer_partition_state = computer_partition.getState()
# XXX this line breaks 37 tests # XXX this line breaks 37 tests
...@@ -1088,7 +1089,8 @@ class Slapgrid(object): ...@@ -1088,7 +1089,8 @@ class Slapgrid(object):
software_release_url=software_url, software_release_url=software_url,
certificate_repository_path=self.certificate_repository_path, certificate_repository_path=self.certificate_repository_path,
buildout=self.buildout, buildout=self.buildout,
logger=self.logger) logger=self.logger,
retention_delay=getattr(computer_partition, '_retention_delay', 0))
local_partition.stop() local_partition.stop()
try: try:
computer_partition.stopped() computer_partition.stopped()
...@@ -1101,7 +1103,7 @@ class Slapgrid(object): ...@@ -1101,7 +1103,7 @@ class Slapgrid(object):
self.logger.info('Ignoring destruction of %r, as no report usage was sent' % self.logger.info('Ignoring destruction of %r, as no report usage was sent' %
computer_partition.getId()) computer_partition.getId())
continue continue
local_partition.destroy() destroyed = local_partition.destroy()
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc(), logger=self.logger) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
...@@ -1111,6 +1113,7 @@ class Slapgrid(object): ...@@ -1111,6 +1113,7 @@ class Slapgrid(object):
exc = traceback.format_exc() exc = traceback.format_exc()
computer_partition.error(exc, logger=self.logger) computer_partition.error(exc, logger=self.logger)
try: try:
if destroyed:
computer_partition.destroyed() computer_partition.destroyed()
except NotFoundError: except NotFoundError:
self.logger.debug('Ignored slap error while trying to inform about ' self.logger.debug('Ignored slap error while trying to inform about '
......
...@@ -51,6 +51,7 @@ from slapos.grid.utils import md5digest ...@@ -51,6 +51,7 @@ from slapos.grid.utils import md5digest
from slapos.grid.watchdog import Watchdog from slapos.grid.watchdog import Watchdog
from slapos.grid import SlapObject from slapos.grid import SlapObject
from slapos.grid.SlapObject import WATCHDOG_MARK from slapos.grid.SlapObject import WATCHDOG_MARK
import slapos.grid.SlapObject
dummylogger = logging.getLogger() dummylogger = logging.getLogger()
...@@ -436,6 +437,8 @@ class InstanceForTest: ...@@ -436,6 +437,8 @@ class InstanceForTest:
partition = slapos.slap.ComputerPartition(computer_id, self.name) partition = slapos.slap.ComputerPartition(computer_id, self.name)
partition._software_release_document = self.getSoftwareRelease() partition._software_release_document = self.getSoftwareRelease()
partition._requested_state = self.requested_state partition._requested_state = self.requested_state
if getattr(self, 'retention_delay', None):
partition._retention_delay = self.retention_delay
if self.software is not None: if self.software is not None:
if self.timestamp is not None: if self.timestamp is not None:
partition._parameter_dict = {'timestamp': self.timestamp} partition._parameter_dict = {'timestamp': self.timestamp}
...@@ -546,7 +549,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -546,7 +549,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), ['.slapgrid', 'buildout.cfg', self.assertItemsEqual(os.listdir(partition), ['.slapgrid', 'buildout.cfg',
'software_release', 'worked']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['getFullComputerInformation', 'availableComputerPartition',
...@@ -563,7 +566,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -563,7 +566,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), ['.slapgrid', 'buildout.cfg', self.assertItemsEqual(os.listdir(partition), ['.slapgrid', 'buildout.cfg',
'software_release', 'worked']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['getFullComputerInformation', 'availableComputerPartition',
...@@ -593,7 +596,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -593,7 +596,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(partition.partition_path), self.assertItemsEqual(os.listdir(partition.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(partition.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(partition.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [partition.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [partition.software.software_hash])
...@@ -629,7 +632,7 @@ chmod 755 etc/run/wrapper ...@@ -629,7 +632,7 @@ chmod 755 etc/run/wrapper
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
...@@ -644,7 +647,7 @@ chmod 755 etc/run/wrapper ...@@ -644,7 +647,7 @@ chmod 755 etc/run/wrapper
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['getFullComputerInformation', 'availableComputerPartition',
...@@ -683,7 +686,7 @@ chmod 755 etc/run/wrapper ...@@ -683,7 +686,7 @@ chmod 755 etc/run/wrapper
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
...@@ -703,7 +706,7 @@ exit 1 ...@@ -703,7 +706,7 @@ exit 1
['0', 'etc', 'var']) ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['getFullComputerInformation',
...@@ -720,7 +723,7 @@ exit 1 ...@@ -720,7 +723,7 @@ exit 1
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', 'buildout.cfg', 'etc', 'software_release', 'worked']) ['.slapgrid', 'buildout.cfg', 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
[instance.software.software_hash]) [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
...@@ -735,7 +738,7 @@ exit 1 ...@@ -735,7 +738,7 @@ exit 1
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.0_wrapper.log', 'etc', ['.slapgrid', '.0_wrapper.log', 'etc',
'buildout.cfg', 'software_release', 'worked']) 'buildout.cfg', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
[instance.software.software_hash]) [instance.software.software_hash])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
...@@ -807,7 +810,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -807,7 +810,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(partition.partition_path), self.assertItemsEqual(os.listdir(partition.partition_path),
['.slapgrid', '.0_daemon.log', 'buildout.cfg', ['.slapgrid', '.0_daemon.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
daemon_log = os.path.join(partition.partition_path, '.0_daemon.log') daemon_log = os.path.join(partition.partition_path, '.0_daemon.log')
self.assertLogContent(daemon_log, 'Failing') self.assertLogContent(daemon_log, 'Failing')
self.assertIsCreated(self.watchdog_banged) self.assertIsCreated(self.watchdog_banged)
...@@ -852,7 +855,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -852,7 +855,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
['0', 'etc', 'var']) ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(partition.partition_path), self.assertItemsEqual(os.listdir(partition.partition_path),
['.slapgrid', '.0_daemon.log', 'buildout.cfg', ['.slapgrid', '.0_daemon.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
daemon_log = os.path.join(partition.partition_path, '.0_daemon.log') daemon_log = os.path.join(partition.partition_path, '.0_daemon.log')
self.assertLogContent(daemon_log, 'Failing') self.assertLogContent(daemon_log, 'Failing')
self.assertIsNotCreated(self.watchdog_banged) self.assertIsNotCreated(self.watchdog_banged)
...@@ -1139,7 +1142,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1139,7 +1142,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked']) ['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
timestamp_path = os.path.join(instance.partition_path, '.timestamp') timestamp_path = os.path.join(instance.partition_path, '.timestamp')
self.setSlapgrid() self.setSlapgrid()
...@@ -1159,7 +1162,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1159,7 +1162,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', ['.slapgrid', '.timestamp', 'buildout.cfg',
'software_release', 'worked']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(self.launchSlapgrid(develop=True), self.assertEqual(self.launchSlapgrid(develop=True),
...@@ -1180,7 +1183,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1180,7 +1183,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked']) ['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
instance.timestamp = str(int(timestamp) - 1) instance.timestamp = str(int(timestamp) - 1)
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
...@@ -1197,7 +1200,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1197,7 +1200,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked']) ['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
instance.timestamp = str(int(timestamp) + 1) instance.timestamp = str(int(timestamp) + 1)
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
...@@ -1218,7 +1221,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1218,7 +1221,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked']) ['.slapgrid', '.timestamp', 'buildout.cfg', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
[instance.software.software_hash]) [instance.software.software_hash])
instance.timestamp = None instance.timestamp = None
...@@ -1245,7 +1248,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1245,7 +1248,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
partition = os.path.join(self.instance_root, '0') partition = os.path.join(self.instance_root, '0')
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', ['.slapgrid', '.timestamp', 'buildout.cfg',
'software_release', 'worked']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
time.sleep(2) time.sleep(2)
# dummify install() so that it doesn't actually do anything so that it # dummify install() so that it doesn't actually do anything so that it
...@@ -1255,7 +1258,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1255,7 +1258,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.launchSlapgrid() self.launchSlapgrid()
self.assertItemsEqual(os.listdir(partition), self.assertItemsEqual(os.listdir(partition),
['.slapgrid', '.timestamp', 'buildout.cfg', ['.slapgrid', '.timestamp', 'buildout.cfg',
'software_release', 'worked']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
def test_one_partition_periodicity_from_file_does_not_disturb_others(self): def test_one_partition_periodicity_from_file_does_not_disturb_others(self):
""" """
...@@ -1528,7 +1531,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1528,7 +1531,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
...@@ -1597,7 +1600,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1597,7 +1600,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
...@@ -1613,13 +1616,13 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1613,13 +1616,13 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), self.assertItemsEqual(os.listdir(instance.partition_path),
['.slapgrid', '.0_wrapper.log', 'buildout.cfg', ['.slapgrid', '.0_wrapper.log', 'buildout.cfg',
'etc', 'software_release', 'worked']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
...@@ -1891,3 +1894,38 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1891,3 +1894,38 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
self.assertEquals(instance.error, 1) self.assertEquals(instance.error, 1)
self.assertNotEqual(instance.state, 'started') self.assertNotEqual(instance.state, 'started')
class TestSlapgridDestructionLock(MasterMixin, unittest.TestCase):
def test_retention_lock(self):
"""
Higher level test about actual retention (or no-retention) of instance
if specifying a retention lock delay.
"""
computer = ComputerForTest(self.software_root, self.instance_root)
instance = computer.instance_list[0]
instance.requested_state = 'started'
instance.retention_delay = 1.0 / (3600 * 24)
self.grid.processComputerPartitionList()
dummy_instance_file_path = os.path.join(instance.partition_path, 'dummy')
with open(dummy_instance_file_path, 'w') as dummy_instance_file:
dummy_instance_file.write('dummy')
self.assertTrue(os.path.exists(os.path.join(
instance.partition_path,
slapos.grid.SlapObject.Partition.retention_lock_delay_filename
)))
instance.requested_state = 'destroyed'
self.grid.agregateAndSendUsage()
self.assertTrue(os.path.exists(dummy_instance_file_path))
self.assertTrue(os.path.exists(os.path.join(
instance.partition_path,
slapos.grid.SlapObject.Partition.retention_lock_date_filename
)))
self.grid.agregateAndSendUsage()
self.assertTrue(os.path.exists(dummy_instance_file_path))
time.sleep(1)
self.grid.agregateAndSendUsage()
self.assertFalse(os.path.exists(dummy_instance_file_path))
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
import logging import logging
import os import os
import time
import unittest import unittest
from slapos.slap import ComputerPartition as SlapComputerPartition from slapos.slap import ComputerPartition as SlapComputerPartition
...@@ -125,7 +126,8 @@ class MasterMixin(BasicMixin, unittest.TestCase): ...@@ -125,7 +126,8 @@ class MasterMixin(BasicMixin, unittest.TestCase):
self, self,
software_release_url, software_release_url,
partition_id=None, partition_id=None,
slap_computer_partition=None slap_computer_partition=None,
retention_delay=None,
): ):
""" """
Create a partition, and return a Partition object created Create a partition, and return a Partition object created
...@@ -153,10 +155,11 @@ class MasterMixin(BasicMixin, unittest.TestCase): ...@@ -153,10 +155,11 @@ class MasterMixin(BasicMixin, unittest.TestCase):
self.instance_root, 'supervisor') self.instance_root, 'supervisor')
os.mkdir(supervisor_configuration_path) os.mkdir(supervisor_configuration_path)
return Partition( partition = Partition(
software_path=software_path, software_path=software_path,
instance_path=instance_path, instance_path=instance_path,
supervisord_partition_configuration_path=supervisor_configuration_path, supervisord_partition_configuration_path=os.path.join(
supervisor_configuration_path, partition_id),
supervisord_socket=os.path.join( supervisord_socket=os.path.join(
supervisor_configuration_path, 'supervisor.sock'), supervisor_configuration_path, 'supervisor.sock'),
computer_partition=slap_computer_partition, computer_partition=slap_computer_partition,
...@@ -168,6 +171,11 @@ class MasterMixin(BasicMixin, unittest.TestCase): ...@@ -168,6 +171,11 @@ class MasterMixin(BasicMixin, unittest.TestCase):
logger=logging.getLogger(), logger=logging.getLogger(),
) )
partition.updateSupervisor = FakeCallAndNoop
if retention_delay:
partition.retention_delay = retention_delay
return partition
class TestSoftwareNetworkCacheSlapObject(MasterMixin, unittest.TestCase): class TestSoftwareNetworkCacheSlapObject(MasterMixin, unittest.TestCase):
""" """
...@@ -382,3 +390,87 @@ class TestPartitionSlapObject(MasterMixin, unittest.TestCase): ...@@ -382,3 +390,87 @@ class TestPartitionSlapObject(MasterMixin, unittest.TestCase):
# XXX: What should it raise? # XXX: What should it raise?
self.assertRaises(IOError, partition.install) self.assertRaises(IOError, partition.install)
class TestPartitionDestructionLock(MasterMixin, unittest.TestCase):
def setUp(self):
MasterMixin.setUp(self)
Partition.generateSupervisorConfigurationFile = FakeCallAndNoop()
utils.bootstrapBuildout = FakeCallAndNoop()
utils.launchBuildout = FakeCallAndStore()
def test_retention_lock_delay_creation(self):
delay = 42
software = self.createSoftware()
partition = self.createPartition(software.url, retention_delay=delay)
partition.install()
deployed_delay = int(open(partition.retention_lock_delay_file_path).read())
self.assertEqual(delay, deployed_delay)
def test_no_retention_lock_delay(self):
software = self.createSoftware()
partition = self.createPartition(software.url)
partition.install()
delay = open(partition.retention_lock_delay_file_path).read()
self.assertTrue(delay, '0')
self.assertTrue(partition.destroy())
def test_retention_lock_delay_does_not_change(self):
delay = 42
software = self.createSoftware()
partition = self.createPartition(software.url, retention_delay=delay)
partition.install()
partition.retention_delay = 23
# install/destroy many times
partition.install()
partition.destroy()
partition.destroy()
partition.install()
partition.destroy()
deployed_delay = int(open(partition.retention_lock_delay_file_path).read())
self.assertEqual(delay, deployed_delay)
def test_retention_lock_delay_is_respected(self):
delay = 2.0 / (3600 * 24)
software = self.createSoftware()
partition = self.createPartition(software.url, retention_delay=delay)
partition.install()
deployed_delay = float(open(partition.retention_lock_delay_file_path).read())
self.assertEqual(int(delay), int(deployed_delay))
self.assertFalse(partition.destroy())
time.sleep(1)
self.assertFalse(partition.destroy())
time.sleep(1)
self.assertTrue(partition.destroy())
def test_retention_lock_date_creation(self):
delay = 42
software = self.createSoftware()
partition = self.createPartition(software.url, retention_delay=delay)
partition.install()
self.assertFalse(os.path.exists(partition.retention_lock_date_file_path))
partition.destroy()
deployed_date = float(open(partition.retention_lock_date_file_path).read())
self.assertEqual(delay * 3600 * 24 + int(time.time()), int(deployed_date))
def test_retention_lock_date_does_not_change(self):
delay = 42
software = self.createSoftware()
partition = self.createPartition(software.url, retention_delay=delay)
now = time.time()
partition.install()
partition.destroy()
partition.retention_delay = 23
# install/destroy many times
partition.install()
partition.destroy()
partition.destroy()
partition.install()
partition.destroy()
deployed_date = float(open(partition.retention_lock_date_file_path).read())
self.assertEqual(delay * 3600 * 24 + int(now), int(deployed_date))
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