Example #1
0
def zones_for_environment(request, database_id, environment_id):
    database = get_object_or_404(Database, pk=database_id)
    environment = get_object_or_404(Environment, pk=environment_id)
    hp = Provider(database.infra.instances.first(), environment)
    zones = sorted(hp.list_zones())
    return HttpResponse(json.dumps({"zones": zones}),
                        content_type="application/json")
Example #2
0
def zones_for_environment(request, database_id, environment_id):
    database = get_object_or_404(Database, pk=database_id)
    environment = get_object_or_404(Environment, pk=environment_id)
    hp = Provider(database.infra.instances.first(), environment)
    zones = sorted(hp.list_zones())
    return HttpResponse(
        json.dumps({"zones": zones}), content_type="application/json"
    )
Example #3
0
 def get_all_masters_from_zone(self):
     instances = []
     for infra in DatabaseInfra.objects.all():
         if not infra.databases.exists():
             continue
         driver = infra.get_driver()
         instances_masters = driver.get_master_instance()
         if isinstance(instances_masters, Instance):
             instances_masters = [instances_masters]
         for instance in instances_masters:
             hp = Provider(instance, infra.environment)
             info = hp.host_info(instance.hostname)
             if info["zone"] == self.zone:
                 instances.append(instance)
     return instances
Example #4
0
    def start_switch(self):
        self.task.add_detail("Switching master in {}...".format(self.zone))
        for instance in self.instances:
            infra = instance.databaseinfra
            env = infra.environment
            host = instance.hostname
            hp = Provider(instance, env)
            try:
                info = hp.host_info(host)
            except HostProviderInfoException as e:
                self.task.add_detail("ERROR-{}-{}".format(host, e), level=2)
                self.task.set_status_error('Could not load host info')
                return

            if info["zone"] != self.zone:
                self.task.add_detail(
                    "OK-{}-{}".format(host, info["zone"]), level=2
                )
                continue

            database = infra.databases.first()
            if database.is_being_used_elsewhere():
                self.task.add_detail(
                    "ERROR-{}-Being used to another task".format(host),
                    level=2
                )
                self.task.set_status_error(
                    'Database is being used by another task'
                )
                return

            self.task.add_detail(
                "SWITCHING-{}-{}...".format(host, info["zone"]),
                level=2
            )
            class_path = infra.plan.replication_topology.class_path
            steps = get_switch_write_instance_steps(class_path)
            if not steps_for_instances(steps, [instance], self.task):
                self.task.set_status_error('Could not switch all masters')
                return
        self.task.set_status_success('Could switch all masters')
 def setUp(self):
     super(BaseProviderTestCase, self).setUp()
     self.env = physical_factory.EnvironmentFactory.create(name='fake_env')
     self.provider = Provider(self.instance, self.env)
     self.credential = credential_factory.CredentialFactory.create(
         integration_type__name='HOST_PROVIDER',
         integration_type__type=CredentialType.HOST_PROVIDER,
         endpoint='fake_endpoint',
         user='******',
         password='******',
         project='fake_project')
     self.credential.environments.add(self.env)
     self.host = physical_factory.HostFactory.create(
         identifier='fake_identifier1')
     self.instance.hostname = self.host
     self.instance.save()
Example #6
0
def database_migrate(request, context, database):
    if not database.is_host_migrate_available:
        messages.add_message(
            request, messages.ERROR, "This database cannot be migrated"
        )
        return database_details(request, database.id)

    environment = database.infra.environment
    if request.POST:
        can_migrate, error = database.can_migrate_host()
        if not can_migrate:
            messages.add_message(request, messages.ERROR, error)
        elif 'host_id' in request.POST:
            host = get_object_or_404(Host, pk=request.POST.get('host_id'))
            zone = request.POST["new_zone"]
            TaskRegister.host_migrate(host, zone, environment, request.user)
        elif 'new_environment' in request.POST:
            environment = get_object_or_404(
                Environment, pk=request.POST.get('new_environment')
            )
            offering = get_object_or_404(
                Offering, pk=request.POST.get('new_offering')
            )
            if environment not in offering.environments.all():
                messages.add_message(
                    request, messages.ERROR,
                    "There is no offering {} to {} environment".format(
                        offering, environment
                    )
                )
                return

            hosts_zones = OrderedDict()
            data = json.loads(request.POST.get('hosts_zones'))
            for host_id, zone in data.items():
                host = get_object_or_404(Host, pk=host_id)
                hosts_zones[host] = zone
            if not hosts_zones:
                messages.add_message(
                    request, messages.ERROR, "There is no host to migrate"
                )
            else:
                TaskRegister.database_migrate(
                    database, environment, offering, request.user, hosts_zones
                )
        return

    hosts = set()
    zones = set()
    instances = database.infra.instances.all().order_by('shard', 'id')
    for instance in instances:
        host = instance.hostname
        if host in hosts:
            continue

        hp = Provider(instance, environment)
        try:
            host_info = hp.host_info(host)
        except Exception as e:
            LOG.error("Could get host info {} - {}".format(host, e))
        else:
            host.current_zone = host_info['zone']
        hosts.add(host)
    context['hosts'] = sorted(hosts, key=lambda host: host.hostname)
    context['zones'] = sorted(zones)

    context["environments"] = set()
    for group in environment.groups.all():
        for env in group.environments.all():
            context["environments"].add(env)
    context["current_environment"] = environment
    context["current_offering"] = database.infra.offering

    from maintenance.models import HostMigrate
    migrates = HostMigrate.objects.filter(host__in=hosts)
    context["last_host_migrate"] = migrates.last()
    return render_to_response(
        "logical/database/details/migrate_tab.html", context,
        RequestContext(request)
    )
Example #7
0
def database_migrate(request, context, database):
    if not database.is_host_migrate_available:
        messages.add_message(request, messages.ERROR,
                             "This database cannot be migrated")
        return database_details(request, database.id)

    environment = database.infra.environment
    if request.POST:
        can_migrate, error = database.can_migrate_host()
        if not can_migrate:
            messages.add_message(request, messages.ERROR, error)
        elif 'host_id' in request.POST:
            host = get_object_or_404(Host, pk=request.POST.get('host_id'))
            zone = request.POST["new_zone"]
            TaskRegister.host_migrate(host, zone, environment, request.user)
        elif 'new_environment' in request.POST:
            environment = get_object_or_404(
                Environment, pk=request.POST.get('new_environment'))
            offering = get_object_or_404(Offering,
                                         pk=request.POST.get('new_offering'))
            if environment not in offering.environments.all():
                messages.add_message(
                    request, messages.ERROR,
                    "There is no offering {} to {} environment".format(
                        offering, environment))
                return

            hosts_zones = OrderedDict()
            data = json.loads(request.POST.get('hosts_zones'))
            for host_id, zone in data.items():
                host = get_object_or_404(Host, pk=host_id)
                hosts_zones[host] = zone
            if not hosts_zones:
                messages.add_message(request, messages.ERROR,
                                     "There is no host to migrate")
            else:
                TaskRegister.database_migrate(database, environment, offering,
                                              request.user, hosts_zones)
        return

    hosts = set()
    zones = set()
    instances = database.infra.instances.all().order_by('shard', 'id')
    for instance in instances:
        host = instance.hostname
        if host in hosts:
            continue

        hp = Provider(instance, environment)
        try:
            host_info = hp.host_info(host)
        except Exception as e:
            LOG.error("Could get host info {} - {}".format(host, e))
        else:
            host.current_zone = host_info['zone']
        hosts.add(host)
    context['hosts'] = sorted(hosts, key=lambda host: host.hostname)
    context['zones'] = sorted(zones)

    context["environments"] = set()
    for group in environment.groups.all():
        for env in group.environments.all():
            context["environments"].add(env)
    context["current_environment"] = environment
    context["current_offering"] = database.infra.offering

    from maintenance.models import HostMigrate
    migrates = HostMigrate.objects.filter(host__in=hosts)
    context["last_host_migrate"] = migrates.last()
    return render_to_response("logical/database/details/migrate_tab.html",
                              context, RequestContext(request))