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."""
5
from __future__ import (
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',
25
from maasserver.models import (
29
from provisioningserver.drivers.osystem import OperatingSystemRegistry
32
def list_all_usable_osystems(have_images=True):
33
"""Return all operating systems that can be used for nodes.
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
40
osystems = set([osystem for _, osystem in OperatingSystemRegistry])
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.
48
for nodegroup in NodeGroup.objects.all():
49
osystems = osystems.union(
50
BootImage.objects.get_usable_osystems(nodegroup))
52
OperatingSystemRegistry[osystem] for osystem in osystems
53
if osystem in OperatingSystemRegistry
55
return sorted(osystems, key=lambda osystem: osystem.title)
58
def list_osystem_choices(osystems, include_default=True):
59
"""Return Django "choices" list for `osystem`.
61
:param include_default: When true includes the 'Default OS' in choice
65
choices = [('', 'Default OS')]
69
(osystem.name, osystem.title)
70
for osystem in osystems
75
def list_all_usable_releases(osystems, have_images=True):
76
"""Return dictionary of usable `releases` for each operating system.
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.
82
nodegroups = list(NodeGroup.objects.all())
83
for osystem in osystems:
86
releases = releases.union(osystem.get_supported_releases())
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)
96
def list_all_releases_requiring_keys(osystems):
97
"""Return dictionary of OS name mapping to `releases` that require
100
for osystem in osystems:
103
for release in osystem.get_supported_releases()
104
if osystem.requires_license_key(release)
106
if len(releases) > 0:
107
distro_series[osystem.name] = sorted(releases)
111
def get_release_requires_key(osystem, release):
112
"""Return astrisk for any release that requires
115
This is used by the JS, to display the licese_key field.
117
if osystem.requires_license_key(release):
122
def list_release_choices(releases, include_default=True, include_latest=True,
123
with_key_required=True):
124
"""Return Django "choices" list for `releases`.
126
:param include_default: When true includes the 'Default OS Release' in
128
:param include_latest: When true includes the 'Latest OS Release' in
130
:param with_key_required: When true includes the release_requires_key in
134
choices = [('', 'Default OS Release')]
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, '')
146
'%s/%s' % (osystem.name, requires_key),
147
'Latest %s Release' % osystem.title
149
for name, title in options:
150
if with_key_required:
151
requires_key = get_release_requires_key(osystem, name)
155
'%s/%s%s' % (osystem.name, name, requires_key),
161
def get_distro_series_initial(instance, with_key_required=True):
162
"""Returns the distro_series initial value for the instance.
164
:param with_key_required: When true includes the release_requires_key in
167
osystem = instance.osystem
168
series = instance.distro_series
169
os_obj = OperatingSystemRegistry.get_item(osystem)
170
if not with_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 != '':
177
return '%s/%s%s' % (osystem, series, key_required)