/usr/share/pyshared/maasserver/models/config.py is in python-django-maas 1.2+bzr1373+dfsg-0ubuntu1~12.04.6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2012 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Configuration items."""
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
__metaclass__ = type
__all__ = [
'Config',
]
from collections import defaultdict
import copy
from socket import gethostname
from django.db.models import (
CharField,
Manager,
Model,
)
from django.db.models.signals import post_save
from maasserver import DefaultMeta
from maasserver.enum import (
DISTRO_SERIES,
NODE_AFTER_COMMISSIONING_ACTION,
)
from maasserver.fields import JSONObjectField
from provisioningserver.enum import POWER_TYPE
def get_default_config():
return {
## settings default values.
# Commissioning section configuration.
'after_commissioning': NODE_AFTER_COMMISSIONING_ACTION.DEFAULT,
'check_compatibility': False,
'node_power_type': POWER_TYPE.WAKE_ON_LAN,
# Ubuntu section configuration.
'fallback_master_archive': False,
'keep_mirror_list_uptodate': False,
'fetch_new_releases': False,
'update_from': 'archive.ubuntu.com',
'update_from_choice': (
[['archive.ubuntu.com', 'archive.ubuntu.com']]),
# Network section configuration.
'maas_name': gethostname(),
'enlistment_domain': b'local',
'default_distro_series': DISTRO_SERIES.precise,
'commissioning_distro_series': DISTRO_SERIES.precise,
'http_proxy': None,
## /settings
}
# Default values for config options.
DEFAULT_CONFIG = get_default_config()
class ConfigManager(Manager):
"""Manager for Config model class.
Don't import or instantiate this directly; access as `Config.objects.
"""
def __init__(self):
super(ConfigManager, self).__init__()
self._config_changed_connections = defaultdict(set)
def get_config(self, name, default=None):
"""Return the config value corresponding to the given config name.
Return None or the provided default if the config value does not
exist.
:param name: The name of the config item.
:type name: basestring
:param name: The optional default value to return if no such config
item exists.
:type name: object
:return: A config value.
:raises: Config.MultipleObjectsReturned
"""
try:
return self.get(name=name).value
except Config.DoesNotExist:
return copy.deepcopy(DEFAULT_CONFIG.get(name, default))
def get_config_list(self, name):
"""Return the config value list corresponding to the given config
name.
:param name: The name of the config items.
:type name: basestring
:return: A list of the config values.
:rtype: list
"""
return [config.value for config in self.filter(name=name)]
def set_config(self, name, value):
"""Set or overwrite a config value.
:param name: The name of the config item to set.
:type name: basestring
:param value: The value of the config item to set.
:type value: Any jsonizable object
"""
config, freshly_created = self.get_or_create(
name=name, defaults=dict(value=value))
if not freshly_created:
config.value = value
config.save()
def config_changed_connect(self, config_name, method):
"""Connect a method to Django's 'update' signal for given config name.
:param config_name: The name of the config item to track.
:type config_name: basestring
:param method: The method to be called.
:type method: callable
The provided callabe should follow Django's convention. E.g:
>>> def callable(sender, instance, created, **kwargs):
>>> pass
>>>
>>> Config.objects.config_changed_connect('config_name', callable)
"""
self._config_changed_connections[config_name].add(method)
def _config_changed(self, sender, instance, created, **kwargs):
for connection in self._config_changed_connections[instance.name]:
connection(sender, instance, created, **kwargs)
class Config(Model):
"""Configuration settings item.
:ivar name: The name of the configuration option.
:type name: basestring
:ivar value: The configuration value.
:type value: Any pickleable python object.
"""
class Meta(DefaultMeta):
"""Needed for South to recognize this model."""
name = CharField(max_length=255, unique=False)
value = JSONObjectField(null=True)
objects = ConfigManager()
def __unicode__(self):
return "%s: %s" % (self.name, self.value)
# Connect config manager's _config_changed to Config's post-save signal.
post_save.connect(Config.objects._config_changed, sender=Config)
|