3
from deployer.errors import UnitErrors
4
from deployer.utils import ErrorExit
6
from .base import BaseEnvironment
9
class PyEnvironment(BaseEnvironment):
11
def __init__(self, name, options=None):
13
self.options = options
15
def add_units(self, service_name, num_units):
16
params = self._named_env(["juju", "add-unit"])
18
params.extend(["-n", str(num_units)])
19
params.append(service_name)
21
params, self.log, "Error adding units to %s", service_name)
23
def add_relation(self, endpoint_a, endpoint_b):
24
params = self._named_env(["juju", "add-relation"])
25
params.extend([endpoint_a, endpoint_b])
27
params, self.log, "Error adding relation to %s %s",
28
endpoint_a, endpoint_b)
36
def _destroy_service(self, service_name):
37
params = self._named_env(["juju", "destroy-service"])
38
params.append(service_name)
40
params, self.log, "Error destroying service %s" % service_name)
42
def get_config(self, svc_name):
43
params = self._named_env(["juju", "get"])
44
params.append(svc_name)
45
return self._check_call(
46
params, self.log, "Error retrieving config for %r", svc_name)
48
def get_constraints(self, svc_name):
49
params = self._named_env(["juju", "get-constraints"])
50
params.append(svc_name)
51
return self._check_call(
52
params, self.log, "Error retrieving constraints for %r", svc_name)
55
terminate_machines=False,
59
status = self.status()
60
for s in status.get('services'):
61
self.log.debug(" Destroying service %s", s)
62
self._destroy_service(s)
63
if not terminate_machines:
65
for m in status.get('machines'):
68
self.log.debug(" Terminating machine %s", m)
69
self.terminate_machine(str(m))
71
self.log.debug(" Waiting for terminate delay")
72
time.sleep(terminate_delay)
74
def resolve_errors(self, retry_count=0, timeout=600, watch=False, delay=5):
78
return self.get_cli_status()
81
self, timeout, goal_state="started", watch=False, no_exit=False):
83
max_time = time.time() + timeout
84
while max_time > time.time():
85
status = self.status()
87
error_units = self._get_units_in_error(status)
89
for s in status.get("services", {}).values():
90
for uid, u in s.get("units", {}).items():
91
state = u.get("agent-state") or "pending"
92
if uid in error_units:
93
errors.append({"name": uid,
94
"machine": u["machine"],
95
"agent-state": state})
96
elif state != goal_state:
98
for rid in u.get("relation-errors", {}).keys():
99
errors.append({"name": uid,
100
"machine": u["machine"],
102
"relation-error: %s" % rid})
103
for sid, sub in u.get("subordinates", {}).items():
104
state = sub.get("agent-state") or "pending"
105
if sid in error_units:
106
errors.append({"name": sid,
107
"machine": u["machine"],
108
"agent-state": state})
109
elif state != goal_state:
111
if not pending and not errors:
115
raise UnitErrors(errors)
118
"unit: %s: machine: %s agent-state: %s" % (
119
u["name"], u["machine"], u["agent-state"]
123
"The following units had errors:\n %s" % (
124
" \n".join(error_units)))