summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/admin/hypervisors/compute/forms.py
blob: 8082e826898c09009ebb7eb94ff61cbb9938cbbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

from horizon import exceptions
from horizon import forms
from horizon import messages

from openstack_dashboard import api


class EvacuateHostForm(forms.SelfHandlingForm):

    current_host = forms.CharField(label=_("Current Host"),
                                   widget=forms.TextInput(
                                       attrs={'readonly': 'readonly'}))
    target_host = forms.ThemableChoiceField(
        label=_("Target Host"),
        required=False,
        help_text=_("Choose a Host to evacuate servers to. If not selected, "
                    "the scheduler will auto select target host."))

    on_shared_storage = forms.BooleanField(label=_("Shared Storage"),
                                           initial=False, required=False)

    def __init__(self, request, *args, **kwargs):
        super(EvacuateHostForm, self).__init__(request, *args, **kwargs)
        initial = kwargs.get('initial', {})
        self.fields['target_host'].choices = \
            self.populate_host_choices(request, initial)

    def populate_host_choices(self, request, initial):
        hosts = initial.get('hosts')
        current_host = initial.get('current_host')
        host_list = sorted([(host, host)
                            for host in hosts
                            if host != current_host])
        if host_list:
            host_list.insert(0, ("", _("Select a target host")))
        else:
            host_list.insert(0, ("", _("No other hosts available.")))
        return host_list

    def handle(self, request, data):
        try:
            current_host = data['current_host']
            target_host = data['target_host']
            on_shared_storage = data['on_shared_storage']
            # The target_host value will be an empty string when the target
            # host wasn't specified. But the evacuate api doesn't allow
            # an empty string. So set None as the target_host value.
            if not target_host:
                target_host = None
            api.nova.evacuate_host(request, current_host,
                                   target_host, on_shared_storage)

            msg = _('Starting to evacuate host: %s.') % current_host
            messages.success(request, msg)
            return True
        except Exception:
            redirect = reverse('horizon:admin:hypervisors:index')
            msg = _('Failed to evacuate host: %s.') % data['current_host']
            exceptions.handle(request, message=msg, redirect=redirect)
            return False


class DisableServiceForm(forms.SelfHandlingForm):
    host = forms.CharField(label=_("Host"),
                           widget=forms.TextInput(
                           attrs={"readonly": "readonly"}))
    reason = forms.CharField(max_length=255,
                             label=_("Reason"),
                             required=False)

    def handle(self, request, data):
        try:
            host = data["host"]
            reason = data["reason"]
            api.nova.service_disable(request, host, "nova-compute",
                                     reason=reason)
            msg = _("Disabled compute service for host: %s.") % host
            messages.success(request, msg)
            return True
        except Exception:
            redirect = reverse('horizon:admin:hypervisors:index')
            msg = _("Failed to disable compute service for host: %s.") % \
                data["host"]
            exceptions.handle(request, message=msg, redirect=redirect)
            return False


class MigrateHostForm(forms.SelfHandlingForm):
    current_host = forms.CharField(
        label=_("Current Host"),
        required=False,
        widget=forms.TextInput(
            attrs={'readonly': 'readonly'})
    )

    migrate_type = forms.ChoiceField(
        label=_('Running Instance Migration Type'),
        choices=[
            ('live_migrate', _('Live Migrate')),
            ('cold_migrate', _('Cold Migrate'))
        ],
        widget=forms.ThemableSelectWidget(
            attrs={
                'class': 'switchable',
                'data-slug': 'source'
            }
        )
    )

    disk_over_commit = forms.BooleanField(
        label=_("Disk Over Commit"),
        initial=False,
        required=False,
        widget=forms.CheckboxInput(
            attrs={
                'class': 'switched',
                'data-switch-on': 'source',
                'data-source-live_migrate': _('Disk Over Commit')
            }
        )
    )

    block_migration = forms.BooleanField(
        label=_("Block Migration"),
        initial=False,
        required=False,
        widget=forms.CheckboxInput(
            attrs={
                'class': 'switched',
                'data-switch-on': 'source',
                'data-source-live_migrate': _('Block Migration')
            }
        )
    )

    def handle(self, request, data):
        try:
            current_host = data['current_host']
            migrate_type = data['migrate_type']
            disk_over_commit = data['disk_over_commit']
            block_migration = data['block_migration']
            live_migrate = migrate_type == 'live_migrate'
            api.nova.migrate_host(
                request,
                current_host,
                live_migrate=live_migrate,
                disk_over_commit=disk_over_commit,
                block_migration=block_migration
            )
            msg = _('Starting to migrate host: %(current)s') % \
                {'current': current_host}
            messages.success(request, msg)
            return True
        except Exception:
            msg = _('Failed to migrate host "%s".') % data['current_host']
            redirect = reverse('horizon:admin:hypervisors:index')
            exceptions.handle(request, message=msg, redirect=redirect)
            return False