|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2014-2015 Online SAS and Contributors. All Rights Reserved. |
|
4
|
|
|
# Edouard Bonlieu <[email protected]> |
|
5
|
|
|
# Julien Castets <[email protected]> |
|
6
|
|
|
# Manfred Touron <[email protected]> |
|
7
|
|
|
# Kevin Deldycke <[email protected]> |
|
8
|
|
|
# |
|
9
|
|
|
# Licensed under the BSD 2-Clause License (the "License"); you may not use this |
|
10
|
|
|
# file except in compliance with the License. You may obtain a copy of the |
|
11
|
|
|
# License at http://opensource.org/licenses/BSD-2-Clause |
|
12
|
|
|
|
|
13
|
|
|
import os |
|
14
|
|
|
import yaml |
|
15
|
|
|
import sys |
|
16
|
|
|
|
|
17
|
|
|
from juju_scaleway.env import Environment |
|
18
|
|
|
from juju_scaleway.exceptions import ConfigError |
|
19
|
|
|
from juju_scaleway import provider |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class Config(object): |
|
23
|
|
|
|
|
24
|
|
|
def __init__(self, options): |
|
25
|
|
|
self.options = options |
|
26
|
|
|
|
|
27
|
|
|
def connect_provider(self): |
|
28
|
|
|
"""Connect to Scaleway. |
|
29
|
|
|
""" |
|
30
|
|
|
return provider.factory() |
|
31
|
|
|
|
|
32
|
|
|
def connect_environment(self): |
|
33
|
|
|
"""Return a websocket connection to the environment. |
|
34
|
|
|
""" |
|
35
|
|
|
return Environment(self) |
|
36
|
|
|
|
|
37
|
|
|
def validate(self): |
|
38
|
|
|
provider.validate() |
|
39
|
|
|
self.get_env_name() |
|
40
|
|
|
|
|
41
|
|
|
@property |
|
42
|
|
|
def verbose(self): |
|
43
|
|
|
return self.options.verbose |
|
44
|
|
|
|
|
45
|
|
|
@property |
|
46
|
|
|
def constraints(self): |
|
47
|
|
|
return self.options.constraints |
|
48
|
|
|
|
|
49
|
|
|
@property |
|
50
|
|
|
def series(self): |
|
51
|
|
|
return self.options.series |
|
52
|
|
|
|
|
53
|
|
|
@property |
|
54
|
|
|
def upload_tools(self): |
|
55
|
|
|
return getattr(self.options, 'upload_tools', False) |
|
56
|
|
|
|
|
57
|
|
|
@property |
|
58
|
|
|
def num_machines(self): |
|
59
|
|
|
return getattr(self.options, 'num_machines', 0) |
|
60
|
|
|
|
|
61
|
|
|
@property |
|
62
|
|
|
def juju_home(self): |
|
63
|
|
|
jhome = os.environ.get("JUJU_HOME") |
|
64
|
|
|
if jhome is not None: |
|
65
|
|
|
return os.path.expanduser(jhome) |
|
66
|
|
|
if sys.platform == "win32": |
|
67
|
|
|
return os.path.join( |
|
68
|
|
|
os.path.join('APPDATA'), "Juju") |
|
69
|
|
|
return os.path.expanduser("~/.juju") |
|
70
|
|
|
|
|
71
|
|
|
def get_env_name(self): |
|
72
|
|
|
"""Get the environment name. |
|
73
|
|
|
""" |
|
74
|
|
|
if self.options.environment: |
|
75
|
|
|
return self.options.environment |
|
76
|
|
|
elif os.environ.get("JUJU_ENV"): |
|
77
|
|
|
return os.environ['JUJU_ENV'] |
|
78
|
|
|
|
|
79
|
|
|
env_ptr = os.path.join(self.juju_home, "current-environment") |
|
80
|
|
|
if os.path.exists(env_ptr): |
|
81
|
|
|
with open(env_ptr) as handle: |
|
82
|
|
|
return handle.read().strip() |
|
83
|
|
|
|
|
84
|
|
|
with open(self.get_env_conf()) as handle: |
|
85
|
|
|
conf = yaml.safe_load(handle.read()) |
|
86
|
|
|
if 'default' not in conf: |
|
87
|
|
|
raise ConfigError("No Environment specified") |
|
88
|
|
|
return conf['default'] |
|
89
|
|
|
|
|
90
|
|
|
def get_env_conf(self): |
|
91
|
|
|
"""Get the environment config file. |
|
92
|
|
|
""" |
|
93
|
|
|
conf = os.path.join(self.juju_home, 'environments.yaml') |
|
94
|
|
|
if not os.path.exists(conf): |
|
95
|
|
|
raise ConfigError("Juju environments.yaml not found %s" % conf) |
|
96
|
|
|
return conf |
|
97
|
|
|
|