/usr/share/pyshared/juju/environment/config.py is in juju-0.7 0.7-0ubuntu2.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | import os
import uuid
import yaml
from juju.environment.environment import Environment
from juju.environment.errors import EnvironmentsConfigError
from juju.errors import FileAlreadyExists, FileNotFound
from juju.lib import serializer
from juju.lib.schema import (
Constant, Dict, Int, KeyDict, OAuthString, OneOf, SchemaError, SelectDict,
String, Bool)
DEFAULT_CONFIG_PATH = "~/.juju/environments.yaml"
SAMPLE_CONFIG = """\
environments:
sample:
type: ec2
control-bucket: %(control-bucket)s
admin-secret: %(admin-secret)s
default-series: precise
ssl-hostname-verification: true
"""
_EITHER_PLACEMENT = OneOf(Constant("unassigned"), Constant("local"))
# See juju.providers.openstack.credentials for definition and more details
_OPENSTACK_AUTH_MODE = OneOf(
Constant("userpass"),
Constant("keypair"),
Constant("legacy"),
Constant("rax"),
)
SCHEMA = KeyDict({
"default": String(),
"environments": Dict(String(), SelectDict("type", {
"ec2": KeyDict({
"control-bucket": String(),
"admin-secret": String(),
"access-key": String(),
"secret-key": String(),
"region": OneOf(
Constant("us-east-1"),
Constant("us-west-1"),
Constant("us-west-2"),
Constant("eu-west-1"),
Constant("sa-east-1"),
Constant("ap-northeast-1"),
Constant("ap-southeast-1")),
"ec2-uri": String(),
"s3-uri": String(),
"ssl-hostname-verification": OneOf(
Constant(True),
Constant(False)),
"placement": _EITHER_PLACEMENT,
"default-series": String()},
optional=[
"access-key", "secret-key", "region", "ec2-uri", "s3-uri",
"placement", "ssl-hostname-verification"]),
"openstack": KeyDict({
"control-bucket": String(),
"admin-secret": String(),
"access-key": String(),
"secret-key": String(),
"default-instance-type": String(),
"default-image-id": OneOf(String(), Int()),
"auth-url": String(),
"project-name": String(),
"use-floating-ip": Bool(),
"auth-mode": _OPENSTACK_AUTH_MODE,
"region": String(),
"default-series": String(),
"ssl-hostname-verification": Bool(),
},
optional=[
"access-key", "secret-key", "auth-url", "project-name",
"auth-mode", "region", "use-floating-ip",
"ssl-hostname-verification", "default-instance-type"]),
"openstack_s3": KeyDict({
"control-bucket": String(),
"admin-secret": String(),
"access-key": String(),
"secret-key": String(),
"default-instance-type": String(),
"default-image-id": OneOf(String(), Int()),
"auth-url": String(),
"combined-key": String(),
"s3-uri": String(),
"use-floating-ip": Bool(),
"auth-mode": _OPENSTACK_AUTH_MODE,
"region": String(),
"default-series": String(),
"ssl-hostname-verification": Bool(),
},
optional=[
"access-key", "secret-key", "combined-key", "auth-url",
"s3-uri", "project-name", "auth-mode", "region",
"use-floating-ip", "ssl-hostname-verification",
"default-instance-type"]),
"maas": KeyDict({
"maas-server": String(),
"maas-oauth": OAuthString(),
"admin-secret": String(),
"placement": _EITHER_PLACEMENT,
# MAAS currently only provisions precise; any other default-series
# would just lead to errors down the line.
"default-series": String()},
optional=["placement"]),
"local": KeyDict({
"admin-secret": String(),
"data-dir": String(),
"placement": Constant("local"),
"default-series": String()},
optional=["placement"]),
"dummy": KeyDict({})}))},
optional=["default"])
class EnvironmentsConfig(object):
"""An environment configuration, with one or more environments.
"""
def __init__(self):
self._config = None
self._loaded_path = None
def get_default_path(self):
"""Return the default environment configuration file path."""
return os.path.expanduser(DEFAULT_CONFIG_PATH)
def _get_path(self, path):
if path is None:
return self.get_default_path()
return path
def load(self, path=None):
"""Load an enviornment configuration file.
@param path: An optional environment configuration file path.
Defaults to ~/.juju/environments.yaml
This method will call the C{parse()} method with the content
of the loaded file.
"""
path = self._get_path(path)
if not os.path.isfile(path):
raise FileNotFound(path)
with open(path) as file:
self.parse(file.read(), path)
def parse(self, content, path=None):
"""Parse an enviornment configuration.
@param content: The content to parse.
@param path: An optional environment configuration file path, used
when raising errors.
@raise EnvironmentsConfigError: On other issues.
"""
if not isinstance(content, basestring):
self._fail("Configuration must be a string", path, repr(content))
try:
config = serializer.yaml_load(content)
except yaml.YAMLError, error:
self._fail(error, path=path, content=content)
if not isinstance(config, dict):
self._fail("Configuration must be a dictionary", path, content)
try:
config = SCHEMA.coerce(config, [])
except SchemaError, error:
self._fail(error, path=path)
self._config = config
self._loaded_path = path
def _fail(self, error, path, content=None):
if path is None:
path_info = ""
else:
path_info = " %s:" % (path,)
error = str(error)
if content:
error += ":\n%s" % content
raise EnvironmentsConfigError(
"Environments configuration error:%s %s" %
(path_info, error))
def get_names(self):
"""Return the names of environments available in the configuration."""
return sorted(self._config["environments"].iterkeys())
def get(self, name):
"""Retrieve the Environment with the given name.
@return: The Environment, or None if one isn't found.
"""
environment_config = self._config["environments"].get(name)
if environment_config is not None:
return Environment(name, environment_config)
return None
def get_default(self):
"""Get the default environment for this configuration.
The default environment is either the single defined environment
in the configuration, or the one explicitly named through the
"default:" option in the outermost scope.
@raise EnvironmentsConfigError: If it can't determine a default
environment.
"""
environments_config = self._config.get("environments")
if len(environments_config) == 1:
return self.get(environments_config.keys()[0])
default = self._config.get("default")
if default:
if default not in environments_config:
raise EnvironmentsConfigError(
"Default environment '%s' was not found: %s" %
(default, self._loaded_path))
return self.get(default)
raise EnvironmentsConfigError("There are multiple environments and no "
"explicit default (set one explicitly?): "
"%s" % self._loaded_path)
def write_sample(self, path=None):
"""Write down a sample configuration file.
@param path: An optional environment configuration file path.
Defaults to ~/.juju/environments.yaml
"""
path = self._get_path(path)
dirname = os.path.dirname(path)
if os.path.exists(path):
raise FileAlreadyExists(path)
if not os.path.exists(dirname):
os.mkdir(dirname, 0700)
defaults = {
"control-bucket": "juju-%s" % (uuid.uuid4().hex),
"admin-secret": "%s" % (uuid.uuid4().hex),
"default-series": "precise"
}
with open(path, "w") as file:
file.write(SAMPLE_CONFIG % defaults)
os.chmod(path, 0600)
def load_or_write_sample(self):
"""Try to load the configuration, and if it doesn't work dump a sample.
This method will try to load the environment configuration from the
default location, and if it doesn't work, it will write down a
sample configuration there.
This is handy for a default initialization.
"""
try:
self.load()
except FileNotFound:
self.write_sample()
raise EnvironmentsConfigError("No environments configured. Please "
"edit: %s" % self.get_default_path(),
sample_written=True)
def serialize(self, name=None):
"""Serialize the environments configuration.
Optionally an environment name can be specified and only
that environment will be serialized.
Serialization dispatches to the individual environments as
they may serialize information not contained within the
original config file.
"""
if not name:
names = self.get_names()
else:
names = [name]
config = self._config.copy()
config["environments"] = {}
for name in names:
environment = self.get(name)
if environment is None:
raise EnvironmentsConfigError(
"Invalid environment %r" % name)
data = environment.get_serialization_data()
# all environment data should be contained
# in a nested dict under the environment name.
assert data.keys() == [name]
config["environments"].update(data)
return serializer.dump(config)
|