summaryrefslogtreecommitdiff
path: root/tuskar_ui/infrastructure/flavors/utils.py
blob: cf2d63ddd6dc7b88c826ddce45e7ecb91ede723c (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
# -*- coding: utf8 -*-
#
#    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.conf import settings

from tuskar_ui import api
from tuskar_ui.utils import utils


def matching_deployment_mode():
    deployment_mode = getattr(settings, 'DEPLOYMENT_MODE', 'scale')
    return deployment_mode.lower() == 'scale'


def _get_unmatched_suggestions(request):
    unmatched_suggestions = []
    flavor_suggestions = [FlavorSuggestion.from_flavor(flavor)
                          for flavor in api.flavor.Flavor.list(request)]
    for node in api.node.Node.list(request):
        node_suggestion = FlavorSuggestion.from_node(node)
        for flavor_suggestion in flavor_suggestions:
            if flavor_suggestion == node_suggestion:
                break
        else:
            unmatched_suggestions.append(node_suggestion)
    return unmatched_suggestions


def get_flavor_suggestions(request):
    return set(_get_unmatched_suggestions(request))


class FlavorSuggestion(object):
    """Describe node parameters in a way that is easy to compare."""

    def __init__(self, vcpus=None, ram=None, disk=None, cpu_arch=None,
                 ram_bytes=None, disk_bytes=None, node_id=None):
        self.vcpus = vcpus
        self.ram_bytes = ram_bytes or ram * 1024 * 1024 or 0
        self.disk_bytes = disk_bytes or (disk or 0) * 1024 * 1024 * 1024
        self.cpu_arch = cpu_arch
        self.id = node_id

    @classmethod
    def from_node(cls, node):
        return cls(
            node_id=node.uuid,
            vcpus=utils.safe_int_cast(node.cpus),
            ram=utils.safe_int_cast(node.memory_mb),
            disk=utils.safe_int_cast(node.local_gb),
            cpu_arch=node.cpu_arch
        )

    @classmethod
    def from_flavor(cls, flavor):
        return cls(
            vcpus=flavor.vcpus,
            ram_bytes=flavor.ram_bytes,
            disk_bytes=flavor.disk_bytes,
            cpu_arch=flavor.cpu_arch
        )

    @property
    def name(self):
        return 'Flavor-%scpu-%s-%sMB-%sGB' % (
            self.vcpus or '0',
            self.cpu_arch or '',
            self.ram or '0',
            self.disk or '0',
        )

    @property
    def ram(self):
        return self.ram_bytes / 1024 / 1024

    @property
    def disk(self):
        return self.disk_bytes / 1024 / 1024 / 1024

    def __hash__(self):
        return self.name.__hash__()

    def __eq__(self, other):
        return self.name == other.name

    def __ne__(self, other):
        return not self == other

    def __repr__(self):
        return (
            '%s(vcpus=%r, ram_bytes=%r, disk_bytes=%r, '
            'cpu_arch=%r, node_id=%r)' % (
                self.__class__.__name__,
                self.vcpus,
                self.ram_bytes,
                self.disk_bytes,
                self.cpu_arch,
                self.id,
            )
        )

    def create_flavor(self, request):
        return api.flavor.Flavor.create(
            request,
            name=self.name,
            memory=self.ram,
            vcpus=self.vcpus,
            disk=self.disk,
            cpu_arch=self.cpu_arch,
        )