Skip to content

Commit

Permalink
Add migration to support InstanceLink changes
Browse files Browse the repository at this point in the history
- Add forwards method to create a receptor address
for any existing Instance that has listener_port defined

- Add forwards method to modify each InstanceLink object
that changes target to the newly created receptor addresses

This migration was implemented as follows:

1. Add a target_new to InstanceLink which is a foreign key
to ReceptorAddress
2. create receptor addresses
3. link to these receptor addresses using the target_new field
4. rename target_new to target
5. drop listener_port and peers_from_control_nodes from Instance

Signed-off-by: Seth Foster <[email protected]>
  • Loading branch information
fosterseth committed Jan 19, 2024
1 parent 70638ba commit 34dbde3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 62 deletions.
88 changes: 71 additions & 17 deletions awx/main/migrations/0189_inbound_hop_nodes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Generated by Django 4.2.6 on 2024-01-12 20:01
# Generated by Django 4.2.6 on 2024-01-19 03:13

import django.core.validators
from django.db import migrations, models
import django.db.models.deletion

def create_receptor_address(apps, schema_editor):
"""
if listener_port was defined on an instance, create a receptor address for it
"""
Instance = apps.get_model('main', 'Instance')
ReceptorAddress = apps.get_model('main', 'ReceptorAddress')
for instance in Instance.objects.all():
if instance.listener_port:
instance.peers_from.all()
ReceptorAddress.objects.create(
instance=instance,
address=instance.hostname,
port=instance.listener_port,
peers_from_control_nodes=instance.peers_from_control_nodes,
protocol='tcp',
is_internal=False,
canonical=True,
)

def link_to_receptor_address(apps, schema_editor):
"""
modify each InstanceLink to point to the newly created
ReceptorAddresses, through the target_new field
target_new will be renamed to target in an operation below
"""
InstanceLink = apps.get_model('main', 'InstanceLink')
for link in InstanceLink.objects.all():
link.target_new = link.target.receptor_addresses.get()
link.save()

class Migration(migrations.Migration):
dependencies = [
Expand Down Expand Up @@ -35,7 +64,7 @@ class Migration(migrations.Migration):
),
),
('is_internal', models.BooleanField(default=False, help_text='If True, only routable within the Kubernetes cluster.')),
('canonical', models.BooleanField(default=True, help_text='If True, this address is the canonical address for the instance.')),
('canonical', models.BooleanField(default=False, help_text='If True, this address is the canonical address for the instance.')),
(
'peers_from_control_nodes',
models.BooleanField(default=False, help_text='If True, control plane cluster nodes should automatically peer to it.'),
Expand All @@ -50,23 +79,25 @@ class Migration(migrations.Migration):
name='instancelink',
unique_together=set(),
),
migrations.RemoveField(
model_name='instance',
name='listener_port',
),
migrations.RemoveField(
model_name='instance',
name='peers_from_control_nodes',
),
migrations.AddField(
model_name='instance',
name='managed',
field=models.BooleanField(default=False, editable=False, help_text='If True, this instance is managed by the control plane.'),
),
migrations.AlterField(
model_name='instancelink',
name='source',
field=models.ForeignKey(help_text='The source instance of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.instance'),
migrations.AddField(
model_name='instance',
name='receptor_installation_method',
field=models.CharField(
choices=[
('release', 'Install from GitHub release'),
('package', 'Install from RPM package'),
('local', 'Install from local source'),
('container', 'Install via container'),
],
default='release',
help_text='Select your preferred receptor installation method',
max_length=16,
),
),
migrations.AddConstraint(
model_name='instancelink',
Expand All @@ -82,6 +113,25 @@ class Migration(migrations.Migration):
name='receptor_address',
field=models.ManyToManyField(blank=True, to='main.receptoraddress'),
),
migrations.AddField(
model_name='instancelink',
name='target_new',
field=models.ForeignKey(
help_text='The target receptor address of this peer link.', null=True, on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress'
),
),
migrations.AddConstraint(
model_name='receptoraddress',
constraint=models.UniqueConstraint(fields=('address',), name='unique_receptor_address', violation_error_message='Receptor address must be unique.'),
),

migrations.RunPython(create_receptor_address),
migrations.RunPython(link_to_receptor_address),

migrations.RemoveField(
model_name='instancelink',
name='target_new',
),
migrations.AlterField(
model_name='instance',
name='peers',
Expand All @@ -94,8 +144,12 @@ class Migration(migrations.Migration):
help_text='The target receptor address of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress'
),
),
migrations.AddConstraint(
model_name='receptoraddress',
constraint=models.UniqueConstraint(fields=('address',), name='unique_receptor_address', violation_error_message='Receptor address must be unique.'),
migrations.RemoveField(
model_name='instance',
name='listener_port',
),
migrations.RemoveField(
model_name='instance',
name='peers_from_control_nodes',
),
]
27 changes: 0 additions & 27 deletions awx/main/migrations/0190_instance_receptor_installation_method.py

This file was deleted.

17 changes: 0 additions & 17 deletions awx/main/migrations/0191_alter_receptoraddress_canonical.py

This file was deleted.

2 changes: 1 addition & 1 deletion awx/main/models/ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Meta:
ordering = ("id",)
constraints = [models.UniqueConstraint(fields=['source', 'target'], name='source_target_unique_together')]

source = models.ForeignKey('Instance', on_delete=models.CASCADE, help_text=_("The source instance of this peer link."))
source = models.ForeignKey('Instance', on_delete=models.CASCADE, related_name='+')
target = models.ForeignKey('ReceptorAddress', on_delete=models.CASCADE, help_text=_("The target receptor address of this peer link."))

class States(models.TextChoices):
Expand Down

0 comments on commit 34dbde3

Please sign in to comment.