~martin-nowack/ubuntu/utopic/maas/bug-1425837

« back to all changes in this revision

Viewing changes to src/maasserver/utils/osystems.py

  • Committer: Package Import Robot
  • Author(s): Julian Edwards, Julian Edwards, Andres Rodriguez
  • Date: 2014-08-21 18:38:27 UTC
  • mfrom: (1.2.34)
  • Revision ID: package-import@ubuntu.com-20140821183827-9xyb5u2o4l8g3zxj
Tags: 1.6.1+bzr2550-0ubuntu1
* New upstream bugfix release:
  - Auto-link node MACs to Networks (LP: #1341619)

[ Julian Edwards ]
* debian/maas-region-controller.postinst: Don't restart RabbitMQ on
  upgrades, just ensure it's running.  Should prevent a race with the
  cluster celery restarting.
* debian/rules: Pull upstream branch from the right place.

[ Andres Rodriguez ]
* debian/maas-region-controller.postinst: Ensure cluster celery is
  started if it also runs on the region.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
"""Utilities for working with operating systems."""
 
4
 
 
5
from __future__ import (
 
6
    absolute_import,
 
7
    print_function,
 
8
    unicode_literals,
 
9
    )
 
10
 
 
11
str = None
 
12
 
 
13
__metaclass__ = type
 
14
__all__ = [
 
15
    'get_distro_series_initial',
 
16
    'get_release_requires_key',
 
17
    'list_all_releases_requiring_keys',
 
18
    'list_all_usable_osystems',
 
19
    'list_all_usable_releases',
 
20
    'list_osystem_choices',
 
21
    'list_release_choices',
 
22
    ]
 
23
 
 
24
 
 
25
from maasserver.models import (
 
26
    BootImage,
 
27
    NodeGroup,
 
28
    )
 
29
from provisioningserver.drivers.osystem import OperatingSystemRegistry
 
30
 
 
31
 
 
32
def list_all_usable_osystems(have_images=True):
 
33
    """Return all operating systems that can be used for nodes.
 
34
 
 
35
    :param have_images: If set to true then its only the operating systems for
 
36
    which any nodegroup has the boot images available for that operating
 
37
    system.
 
38
    """
 
39
    if not have_images:
 
40
        osystems = set([osystem for _, osystem in OperatingSystemRegistry])
 
41
    else:
 
42
        # The Node edit form offers all usable operating systems as options for
 
43
        # the osystem field.  Not all of these may be available in the node's
 
44
        # nodegroup, but to represent that accurately in the UI would depend on
 
45
        # the currently selected nodegroup.  Narrowing the options down further
 
46
        # would have to happen browser-side.
 
47
        osystems = set()
 
48
        for nodegroup in NodeGroup.objects.all():
 
49
            osystems = osystems.union(
 
50
                BootImage.objects.get_usable_osystems(nodegroup))
 
51
        osystems = [
 
52
            OperatingSystemRegistry[osystem] for osystem in osystems
 
53
            if osystem in OperatingSystemRegistry
 
54
            ]
 
55
    return sorted(osystems, key=lambda osystem: osystem.title)
 
56
 
 
57
 
 
58
def list_osystem_choices(osystems, include_default=True):
 
59
    """Return Django "choices" list for `osystem`.
 
60
 
 
61
    :param include_default: When true includes the 'Default OS' in choice
 
62
        selection.
 
63
    """
 
64
    if include_default:
 
65
        choices = [('', 'Default OS')]
 
66
    else:
 
67
        choices = []
 
68
    choices += [
 
69
        (osystem.name, osystem.title)
 
70
        for osystem in osystems
 
71
        ]
 
72
    return choices
 
73
 
 
74
 
 
75
def list_all_usable_releases(osystems, have_images=True):
 
76
    """Return dictionary of usable `releases` for each operating system.
 
77
 
 
78
    :param have_images: If set to true then its only the releases for
 
79
    which any nodegroup has the boot images available for that release.
 
80
    """
 
81
    distro_series = {}
 
82
    nodegroups = list(NodeGroup.objects.all())
 
83
    for osystem in osystems:
 
84
        releases = set()
 
85
        if not have_images:
 
86
            releases = releases.union(osystem.get_supported_releases())
 
87
        else:
 
88
            for nodegroup in nodegroups:
 
89
                releases = releases.union(
 
90
                    BootImage.objects.get_usable_releases(
 
91
                        nodegroup, osystem.name))
 
92
        distro_series[osystem.name] = sorted(releases)
 
93
    return distro_series
 
94
 
 
95
 
 
96
def list_all_releases_requiring_keys(osystems):
 
97
    """Return dictionary of OS name mapping to `releases` that require
 
98
    license keys."""
 
99
    distro_series = {}
 
100
    for osystem in osystems:
 
101
        releases = [
 
102
            release
 
103
            for release in osystem.get_supported_releases()
 
104
            if osystem.requires_license_key(release)
 
105
            ]
 
106
        if len(releases) > 0:
 
107
            distro_series[osystem.name] = sorted(releases)
 
108
    return distro_series
 
109
 
 
110
 
 
111
def get_release_requires_key(osystem, release):
 
112
    """Return astrisk for any release that requires
 
113
    a license key.
 
114
 
 
115
    This is used by the JS, to display the licese_key field.
 
116
    """
 
117
    if osystem.requires_license_key(release):
 
118
        return '*'
 
119
    return ''
 
120
 
 
121
 
 
122
def list_release_choices(releases, include_default=True, include_latest=True,
 
123
                         with_key_required=True):
 
124
    """Return Django "choices" list for `releases`.
 
125
 
 
126
    :param include_default: When true includes the 'Default OS Release' in
 
127
        choice selection.
 
128
    :param include_latest: When true includes the 'Latest OS Release' in
 
129
        choice selection.
 
130
    :param with_key_required: When true includes the release_requires_key in
 
131
        the choice.
 
132
    """
 
133
    if include_default:
 
134
        choices = [('', 'Default OS Release')]
 
135
    else:
 
136
        choices = []
 
137
    for key, value in releases.items():
 
138
        osystem = OperatingSystemRegistry[key]
 
139
        options = osystem.format_release_choices(value)
 
140
        if with_key_required:
 
141
            requires_key = get_release_requires_key(osystem, '')
 
142
        else:
 
143
            requires_key = ''
 
144
        if include_latest:
 
145
            choices.append((
 
146
                '%s/%s' % (osystem.name, requires_key),
 
147
                'Latest %s Release' % osystem.title
 
148
                ))
 
149
        for name, title in options:
 
150
            if with_key_required:
 
151
                requires_key = get_release_requires_key(osystem, name)
 
152
            else:
 
153
                requires_key = ''
 
154
            choices.append((
 
155
                '%s/%s%s' % (osystem.name, name, requires_key),
 
156
                title
 
157
                ))
 
158
    return choices
 
159
 
 
160
 
 
161
def get_distro_series_initial(instance, with_key_required=True):
 
162
    """Returns the distro_series initial value for the instance.
 
163
 
 
164
    :param with_key_required: When true includes the release_requires_key in
 
165
        the choice.
 
166
    """
 
167
    osystem = instance.osystem
 
168
    series = instance.distro_series
 
169
    os_obj = OperatingSystemRegistry.get_item(osystem)
 
170
    if not with_key_required:
 
171
        key_required = ''
 
172
    elif os_obj is not None:
 
173
        key_required = get_release_requires_key(os_obj, series)
 
174
    if osystem is not None and osystem != '':
 
175
        if series is None:
 
176
            series = ''
 
177
        return '%s/%s%s' % (osystem, series, key_required)
 
178
    return None