11
11
from landscape.sysvconfig import SysVConfig
14
_config = Configuration()
18
15
"""Log a message via Juju."""
19
16
return check_output(["juju-log", message])
22
def get_client_config():
24
Return a L{Configuration} object with the current client configuration.
30
def _run_juju_tool(command, params=None):
31
"""Run specified Juju tool and parse json output."""
33
cmd = [command, "--format", "json"]
36
output = check_output(cmd)
37
return json.loads(output) if output else {}
40
def get_relation_config():
41
"""Get relation configuration from Juju."""
42
return _run_juju_tool("relation-get")
45
def get_service_config():
46
"""Get service configuration from Juju."""
47
return _run_juju_tool("config-get")
50
def update_client_config(service_config):
51
"""Update the client config.
53
@param service_config: A dict containing the config items to be
54
updated. Dashes in the key values will be converted to
55
underscore, to match the Landscape configuration.
57
An attempt to register the client will be made, if the client wasn't
60
client_config = get_client_config()
61
for key, value in service_config.items():
63
setattr(client_config, key.replace("-", "_"), value)
64
if key == "registration-key":
65
# Use registration_password additionally for
66
# backwards compatibility with landscape-client in precise.
67
setattr(client_config, "registration_password", value)
69
return try_to_register()
19
class JujuBroker(object):
20
"""Class responsible for communicating with Juju.
22
This class should be used to set and get information from Juju, so
23
that it can be overridden in unit tests, where a complete Juju
24
environment isn't available.
28
self.environment = os.environ.copy()
30
def get_relation_config(self):
31
"""Get relation configuration from Juju."""
32
return self._run_juju_tool("relation-get")
34
def get_service_config(self):
35
"""Get service configuration from Juju."""
36
return self._run_juju_tool("config-get")
38
def _run_juju_tool(self, command):
39
"""Run specified Juju tool and parse json output."""
40
output = check_output([command, "--format", "json"])
41
return json.loads(output) if output else {}
44
class LandscapeBroker(object):
45
"""Class responsible for communicating with the Landscape client.
47
This class should be used to interact with Landscape client, so
48
that it can be overridden in unit tests, where we can't let
49
Landscape access system-specific behaviours.
53
self.config = Configuration()
55
def update_client_config(self, service_config):
56
"""Update the client config.
58
@param service_config: A dict containing the config items to be
59
updated. Dashes in the key values will be converted to
60
underscore, to match the Landscape configuration.
62
An attempt to register the client will be made, if the client wasn't
66
for key, value in service_config.items():
68
setattr(self.config, key.replace("-", "_"), value)
69
if key == "registration-key":
70
# Use registration_password additionally for
71
# backwards compatibility with landscape-client in precise.
72
setattr(self.config, "registration_password", value)
74
return self.try_to_register()
76
def clear_registration(self):
78
Do steps necessary to clear the client registration, so that next time
81
stop_client_and_disable_init_script()
84
def try_to_register(self):
85
"""Try to register the client if needed.
87
If the client has already been configured, no registration attempt
88
will be made. The same is true if a computer title hasn't been set,
89
since that means that the juju-info-relation-joined hasn't been run,
90
which means that it's expected that the configuration will be broken
93
The following keys need to be set for an attempt to be made:
94
- account_name, computer_title
96
If an error is encountered, the client will be disabled and the hook
97
calling this function will exit with an error code.
99
error_handler = ErrorHandler()
101
sysvconfig = SysVConfig()
102
configured = sysvconfig.is_configured_to_run()
103
config = LandscapeSetupConfiguration()
107
print "Client already registered, skipping"
109
if not config.get("account_name") or not config.get("computer_title"):
110
print "Need account-name and computer-title to proceed, skipping"
114
except ConfigurationError as error:
115
print >> sys.stderr, "Configuration error: %s" % (str(error),)
116
error_handler.flag_error(1)
118
register(config, on_error=error_handler.flag_error)
119
return error_handler.exit_code
122
class ErrorHandler(object):
87
137
self.exit_code = exit_code
90
def clear_registration():
92
Do steps necessary to clear the client registration, so that next time
95
stop_client_and_disable_init_script()
98
def try_to_register():
99
"""Try to register the client if needed.
101
If the client has already been configured, not registration attempt
102
will be made. The same is true if a computer title hasn't been set,
103
since that means that the juju-info-relation-joined hasn't been run,
104
which means that it's expected that the configuration will be broken
107
The following keys need to be set for an attempt to be made:
108
- account_name, computer_title
110
If an error is encountered, the client will be disabled and the hook
111
calling this function will exit with an error code.
113
error_handler = ErrorHandler()
115
sysvconfig = SysVConfig()
116
configured = sysvconfig.is_configured_to_run()
117
config = LandscapeSetupConfiguration()
121
print "Client already registered, skipping"
123
if not config.get("account_name") or not config.get("computer_title"):
124
print "Need account-name and computer-title to proceed, skipping"
128
except ConfigurationError as error:
129
print >> sys.stderr, "Configuration error: %s" % (str(error),)
130
error_handler.flag_error(1)
132
register(config, on_error=error_handler.flag_error)
133
return error_handler.exit_code
136
140
def chown(filename):
137
141
"""Change file owner and group to landscape."""
138
142
user = getpwnam("landscape")