Skip to content

Commit a9622bf

Browse files
committed
[IMP] runbot: make parent_id a params
Some build will use the parent to restore a database, this commits makes taht more explicit by storing the parent as a param when needed.
1 parent 693aad8 commit a9622bf

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

runbot/models/build.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class BuildParameters(models.Model):
101101

102102
build_ids = fields.One2many('runbot.build', 'params_id')
103103
builds_reference_ids = fields.Many2many('runbot.build', relation='runbot_build_params_references', copy=True)
104+
reference_build_id = fields.Many2one('runbot.build', 'Reference Build', index=True)
104105
modules = fields.Char('Modules')
105106

106107
upgrade_to_build_id = fields.Many2one('runbot.build', index=True) # use to define sources to use with upgrade script
@@ -146,6 +147,8 @@ def get_commit_links_ident(commit_link):
146147
'dockerfile_id': param.dockerfile_id.id,
147148
'skip_requirements': param.skip_requirements,
148149
}
150+
if param.reference_build_id:
151+
cleaned_vals['reference_build_id'] = param.reference_build_id.id
149152
if param.upgrade_to_build_id:
150153
cleaned_vals['upgrade_to_build_dockerfile_id'] = param.upgrade_to_build_id.params_id.dockerfile_id.id
151154
cleaned_vals['upgrade_to_build_commits'] = get_commit_links_ident(param.upgrade_to_build_id.params_id.commit_link_ids)
@@ -526,7 +529,7 @@ def write(self, values):
526529

527530
return res
528531

529-
def _add_child(self, param_values, orphan=False, description=False, additionnal_commit_links=False):
532+
def _add_child(self, param_values, orphan=False, description=False, additionnal_commit_links=False, use_parent=...):
530533
build_values = {key: value for key, value in param_values.items() if key not in self.params_id._fields}
531534
param_values = {key: value for key, value in param_values.items() if key in self.params_id._fields}
532535

@@ -539,6 +542,15 @@ def _add_child(self, param_values, orphan=False, description=False, additionnal_
539542
commit_link_ids |= additionnal_commit_links
540543
param_values['commit_link_ids'] = commit_link_ids
541544

545+
config = param_values.get('config_id') or self.params_id.config_id
546+
if isinstance(config, int):
547+
config = self.env['runbot.build.config'].browse(config)
548+
549+
if use_parent == ...:
550+
use_parent = config._default_uses_parent(param_values)
551+
if use_parent:
552+
param_values['reference_build_id'] = self.id
553+
542554
return self.create({
543555
'params_id': self.params_id.copy(param_values).id,
544556
'parent_id': self.id,
@@ -1397,8 +1409,9 @@ def _cmd(self, python_params=None, py_version=None, local_only=True, sub_command
13971409

13981410
faketime = []
13991411
if faketime_params := self.params_id.config_data.get('faketime'):
1400-
if self.parent_id:
1401-
parent_time_offset = (self.parent_id.build_end or self.create_date) - self.parent_id.build_start
1412+
reference_build = self.params_id.reference_build_id or self.parent_id # TODO cleanup parent_id
1413+
if reference_build:
1414+
parent_time_offset = (reference_build.build_end or self.create_date) - reference_build.build_start
14021415
faketime_params = (parser.parse(faketime_params) + parent_time_offset).strftime('%Y-%m-%d %H:%M %Z')
14031416
faketime = ['faketime', faketime_params]
14041417

runbot/models/build_config.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def VARS(vars, path):
295295
'max_builds': OPTIONAL(INT),
296296
'if': OPTIONAL(DYNAMIC_VALUE),
297297
'log': OPTIONAL(DYNAMIC_VALUE),
298+
'use_parent': OPTIONAL(BOOL),
298299
}
299300
valid_steps['restore'] = {
300301
'name': REQUIRED(NAME),
@@ -371,6 +372,18 @@ def _check_recursion(self, visited=None):
371372
for create_config in step.create_config_ids:
372373
create_config._check_recursion(visited[:])
373374

375+
def _default_uses_parent(self, param_values):
376+
if param_values.get('dump_db'):
377+
return False
378+
config_data = param_values.get('config_data', {}) or {}
379+
if config_data.get('dump_url'):
380+
return False
381+
if config_data.get('restore_build_id'):
382+
return False
383+
if config_data.get('dump_trigger_id'):
384+
return False
385+
return any(step.job_type == 'restore' for step in self.step_ids)
386+
374387

375388
class ConfigStepUpgradeDb(models.Model):
376389
_name = 'runbot.config.step.upgrade.db'
@@ -589,7 +602,7 @@ def _run_step(self, build, **kwargs):
589602
return build._docker_run(self, **docker_params)
590603
return True
591604

592-
def _run_create_build(self, build, config_data=None, max_build=200):
605+
def _run_create_build(self, build, config_data=None, max_build=200, use_parent=...):
593606
if config_data:
594607
config_data = {**config_data, **build.params_id.config_data}
595608
else:
@@ -613,7 +626,7 @@ def _run_create_build(self, build, config_data=None, max_build=200):
613626
build._log('create_build', f'More than {max_build} build created, stopping', level='WARNING')
614627
return
615628
config_name = config_name or create_config.name
616-
child = build._add_child(child_data_values, orphan=self.make_orphan, description=description or config_name)
629+
child = build._add_child(child_data_values, orphan=self.make_orphan, description=description or config_name, use_parent=use_parent)
617630
build._log('create_build', 'created with config %s' % config_name, log_type='subbuild', path=str(child.id))
618631

619632
def _make_python_ctx(self, build):
@@ -1122,7 +1135,7 @@ def _run_restore(self, build, config_data=None):
11221135
dump_build = dump_db.build_id
11231136
else:
11241137
download_db_suffix = config_data.get('dump_suffix', self.restore_download_db_suffix or 'all')
1125-
dump_build = build.parent_id
1138+
dump_build = params.reference_build_id or build.parent_id # TODO cleanup parent_id
11261139
assert download_db_suffix and dump_build
11271140
download_db_name = '%s-%s' % (dump_build.dest, download_db_suffix)
11281141
zip_name = '%s.zip' % download_db_name
@@ -1604,11 +1617,15 @@ def _run_dynamic(self, build):
16041617
'config_name': config_name,
16051618
'description': description,
16061619
}
1620+
if current_step.get('use_parent') or (current_step.get('use_parent') is None and any(step.get('job_type') == 'restore' for step in child.get('steps', []))):
1621+
# TODO improve, not needed is other restore params are given
1622+
child_data['reference_build_id'] = build.id
16071623
child_data_list.append(child_data)
16081624
return self._run_create_build(
16091625
build,
16101626
{'child_data': child_data_list, 'number_build': current_step.get('number_builds', 1)},
16111627
max_build=min(current_step.get('max_builds', 20), 200),
1628+
use_parent=current_step.get('use_parent', ...)
16121629
)
16131630

16141631
if current_step['job_type'] == 'restore':

0 commit comments

Comments
 (0)