Projet

Général

Profil

0001-environment-add-migrate_hobo_service-command-60566.patch

Emmanuel Cazenave, 31 mars 2022 15:25

Télécharger (6,29 ko)

Voir les différences:

Subject: [PATCH] environment: add migrate_hobo_service command (#60566)

 .../commands/migrate_hobo_service.py          | 66 +++++++++++++++++++
 tests_schemas/test_migrate_hobo_service.py    | 57 ++++++++++++++++
 2 files changed, 123 insertions(+)
 create mode 100644 hobo/environment/management/commands/migrate_hobo_service.py
 create mode 100644 tests_schemas/test_migrate_hobo_service.py
hobo/environment/management/commands/migrate_hobo_service.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2022  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import urllib.parse
18

  
19
from django.core.management import call_command
20
from django.core.management.base import BaseCommand, CommandError
21
from tenant_schemas.utils import tenant_context
22

  
23
from hobo.deploy.signals import notify_agents
24
from hobo.environment.utils import get_or_create_local_hobo
25
from hobo.multitenant.middleware import TenantMiddleware, TenantNotFound
26

  
27

  
28
def normalize_url(url):
29
    if not url.endswith('/'):
30
        url += '/'
31
    return url
32

  
33

  
34
class Command(BaseCommand):
35
    verbosity = 1
36

  
37
    def add_arguments(self, parser):
38
        parser.add_argument('src_url', type=str)
39
        parser.add_argument('target_url', type=str)
40

  
41
    def handle(self, src_url, target_url, *args, **kwargs):
42
        verbosity = kwargs.get('verbosity')
43
        src_url, target_url = normalize_url(src_url), normalize_url(target_url)
44
        legacy_domain = urllib.parse.urlparse(src_url).netloc.split(':')[0]
45
        target_domain = urllib.parse.urlparse(target_url).netloc.split(':')[0]
46

  
47
        try:
48
            tenant = TenantMiddleware.get_tenant_by_hostname(legacy_domain)
49
        except TenantNotFound:
50
            raise CommandError('No service matches %s' % src_url)
51

  
52
        call_command('create_tenant', target_domain, legacy_hostname=legacy_domain)
53

  
54
        try:
55
            tenant = TenantMiddleware.get_tenant_by_hostname(target_domain)
56
        except TenantNotFound:
57
            raise CommandError('Could not find the new tenant %s' % target_domain)
58

  
59
        with tenant_context(tenant):
60
            local_hobo = get_or_create_local_hobo()
61
            local_hobo.change_base_url(target_url)
62
            local_hobo.save()
63
            notify_agents(None)
64

  
65
        if self.verbosity:
66
            print('Service migrated successfully, check it out : %s' % target_url)
tests_schemas/test_migrate_hobo_service.py
1
import pytest
2
from django.core.management import call_command
3
from django.core.management.base import CommandError
4
from mock import Mock
5
from tenant_schemas.utils import tenant_context
6

  
7
from hobo.environment.models import Passerelle, ServiceBase
8
from hobo.environment.utils import get_installed_services, get_or_create_local_hobo
9
from hobo.multitenant.middleware import TenantMiddleware, TenantNotFound
10

  
11

  
12
@pytest.fixture()
13
def hobo_tenant(db, fake_notify, monkeypatch, fake_themes):
14
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
15
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
16
    yield call_command('cook', 'tests_schemas/example_recipe.json')
17
    call_command('delete_tenant', 'hobo-instance-name.dev.signalpublik.com')
18

  
19

  
20
def test_unknown_service(hobo_tenant):
21
    tenant = TenantMiddleware.get_tenant_by_hostname('hobo-instance-name.dev.signalpublik.com')
22
    with tenant_context(tenant):
23
        assert get_installed_services()
24
    with pytest.raises(CommandError) as e_info:
25
        call_command(
26
            'migrate_hobo_service',
27
            'https://unkown-hobo-instance-name.dev.signalpublik.com/',
28
            'https://new-hobo-instance-name.dev.signalpublik.com/',
29
        )
30
        assert 'No service matches https://unkown-hobo-instance-name.dev.signalpublik.com/' in str(
31
            e_info.value
32
        )
33

  
34

  
35
def test_migrate_hobo_service_succes(db, fake_notify, monkeypatch, fake_themes):
36
    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
37
    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
38
    call_command('cook', 'tests_schemas/example_recipe.json')
39
    assert TenantMiddleware.get_tenant_by_hostname('hobo-instance-name.dev.signalpublik.com')
40
    monkeypatch.setattr('hobo.environment.management.commands.migrate_service.notify_agents', Mock())
41
    call_command(
42
        'migrate_hobo_service',
43
        'https://hobo-instance-name.dev.signalpublik.com/',
44
        'https://new-hobo-instance-name.dev.signalpublik.com/',
45
    )
46
    with pytest.raises(TenantNotFound) as e_info:
47
        TenantMiddleware.get_tenant_by_hostname('hobo-instance-name.dev.signalpublik.com')
48

  
49
    tenant = TenantMiddleware.get_tenant_by_hostname('new-hobo-instance-name.dev.signalpublik.com')
50
    with tenant_context(tenant):
51
        local_hobo = get_or_create_local_hobo()
52
        assert local_hobo.get_base_url_path() == 'https://new-hobo-instance-name.dev.signalpublik.com/'
53
        assert len(local_hobo.legacy_urls) == 1
54
        assert local_hobo.legacy_urls[0]['base_url'] == 'https://hobo-instance-name.dev.signalpublik.com/'
55

  
56
    # cleanup
57
    call_command('delete_tenant', 'new-hobo-instance-name.dev.signalpublik.com')
0
-