Skip to content

Commit

Permalink
add a data migration for Galaxy credentials
Browse files Browse the repository at this point in the history
see: #7813
  • Loading branch information
ryanpetrello committed Aug 4, 2020
1 parent 95584a5 commit 5da4bfd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 81 additions & 0 deletions awx/main/migrations/0118_galaxy_credentials.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,89 @@
# Generated by Django 2.2.11 on 2020-08-04 15:19

import logging

import awx.main.fields
from awx.main.utils.encryption import encrypt_field, decrypt_field

from django.db import migrations, models
from django.utils.timezone import now
import django.db.models.deletion

from awx.main.models import CredentialType as ModernCredentialType
from awx.main.utils.common import set_current_apps

logger = logging.getLogger('awx.main.migrations')


def migrate_galaxy_settings(apps, schema_editor):
set_current_apps(apps)
ModernCredentialType.setup_tower_managed_defaults()
Organization = apps.get_model('main', 'Organization')
CredentialType = apps.get_model('main', 'CredentialType')
Credential = apps.get_model('main', 'Credential')
Setting = apps.get_model('conf', 'Setting')

galaxy_type = CredentialType.objects.get(kind='galaxy')
private_galaxy_url = Setting.objects.filter(key='PRIMARY_GALAXY_URL').first()

# by default, prior versions of AWX/Tower automatically pulled content
# from galaxy.ansible.org
public_galaxy_enabled = True
public_galaxy_setting = Setting.objects.filter(key='PUBLIC_GALAXY_ENABLED').first()
if public_galaxy_setting and public_galaxy_setting is False:
# ...UNLESS this behavior was explicitly disabled via this setting
public_galaxy_enabled = False

for org in Organization.objects.all():
if private_galaxy_url and private_galaxy_url.value:
# If a setting exists for a private Galaxy URL, make a credential for it
username = Setting.objects.filter(key='PRIMARY_GALAXY_USERNAME').first()
password = Setting.objects.filter(key='PRIMARY_GALAXY_PASSWORD').first()
if (username and username.value) or (password and password.value):
logger.error(
f'Specifying HTTP basic auth for the Ansible Galaxy API '
f'({private_galaxy_url.value}) is no longer supported. '
'Please provide an API token instead after your upgrade '
'has completed',
)
inputs = {
'url': private_galaxy_url.value
}
token = Setting.objects.filter(key='PRIMARY_GALAXY_TOKEN').first()
if token and token.value:
inputs['token'] = decrypt_field(token, 'value')
auth_url = Setting.objects.filter(key='PRIMARY_GALAXY_AUTH_URL').first()
if auth_url and auth_url.value:
inputs['auth_url'] = auth_url.value
cred = Credential(
created=now(),
modified=now(),
name=f'Private Galaxy ({private_galaxy_url.value})',
organization=org,
credential_type=galaxy_type,
inputs=inputs
)
cred.save()
if token and token.value:
# encrypt based on the primary key from the prior save
cred.inputs['token'] = encrypt_field(cred, 'token')
cred.save()
org.galaxy_credentials.add(cred)
if public_galaxy_enabled:
# If public Galaxy was enabled, make a credential for it
cred = Credential(
created=now(),
modified=now(),
name='Ansible Galaxy',
organization=org,
credential_type=galaxy_type,
inputs = {
'url': 'https://galaxy.ansible.com/'
}
)
cred.save()
org.galaxy_credentials.add(cred)


class Migration(migrations.Migration):

Expand Down Expand Up @@ -31,4 +111,5 @@ class Migration(migrations.Migration):
name='galaxy_credentials',
field=awx.main.fields.OrderedManyToManyField(blank=True, related_name='organization_galaxy_credentials', through='main.OrganizationGalaxyCredentialMembership', to='main.Credential'),
),
migrations.RunPython(migrate_galaxy_settings)
]
2 changes: 1 addition & 1 deletion awx/main/models/credential/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ def create(self):
ManagedCredentialType(
namespace='galaxy_api_token',
kind='galaxy',
name=ugettext_noop('Ansible Galaxy Automation Hub API Token'),
name=ugettext_noop('Ansible Galaxy/Automation Hub API Token'),
inputs={
'fields': [{
'id': 'url',
Expand Down

0 comments on commit 5da4bfd

Please sign in to comment.