Left: | ||
Right: |
OLD | NEW |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python2 |
2 # -*- python -*- | 2 # -*- python -*- |
3 | 3 |
4 # Copyright 2012 Canonical Ltd. This software is licensed under the | 4 # Copyright 2012 Canonical Ltd. This software is licensed under the |
5 # GNU Affero General Public License version 3 (see the file LICENSE). | 5 # GNU Affero General Public License version 3 (see the file LICENSE). |
6 | 6 |
7 import sys | 7 import sys |
8 | 8 |
9 from charmhelpers import ( | 9 from charmhelpers import ( |
10 get_config, | 10 get_config, |
11 log, | 11 log, |
12 service_control, | |
13 STOP, | |
14 ) | 12 ) |
15 from shelltoolbox import ( | 13 from shelltoolbox import ( |
16 DictDiffer, | 14 DictDiffer, |
17 su, | |
18 ) | 15 ) |
19 | 16 |
20 from utils import ( | 17 from utils import ( |
21 AGENT, | |
22 config_json, | 18 config_json, |
23 fetch_api, | |
24 fetch_gui, | |
25 get_staging_dependencies, | |
26 HAPROXY, | |
27 IMPROV, | |
28 legacy_juju, | |
29 log_hook, | 19 log_hook, |
30 NGINX, | |
31 save_or_create_certificates, | |
32 setup_gui, | |
33 start_agent, | |
34 start_gui, | |
35 start_improv, | |
36 ) | 20 ) |
37 | 21 |
38 | 22 from backend import Backend |
39 def handle_config_changes(config, diff): | |
40 # Handle all configuration file changes. | |
41 log('Updating configuration.') | |
42 | |
43 added_or_changed = diff.added_or_changed | |
44 in_staging = config.get('staging') | |
45 is_legacy_juju = legacy_juju() | |
46 | |
47 # The gui_source_changed and api_branch_changed variables control whether | |
48 # we restart the GUI and the API, respectively, at the end of the function. | |
49 gui_source_changed = False | |
50 api_branch_changed = False | |
51 | |
52 # Fetch new sources? | |
53 if 'juju-gui-source' in added_or_changed: | |
54 gui_source_changed = True | |
55 release_tarball = fetch_gui( | |
56 config['juju-gui-source'], config['command-log-file']) | |
57 setup_gui(release_tarball) | |
58 if is_legacy_juju and ('juju-api-branch' in added_or_changed): | |
59 api_branch_changed = True | |
60 fetch_api(config['juju-api-branch']) | |
61 | |
62 # Handle changes to SSL certificates. | |
63 ssl_properties = set( | |
64 ['ssl-cert-path', 'ssl-cert-contents', 'ssl-key-contents']) | |
65 ssl_changed = added_or_changed & ssl_properties | |
66 if ssl_changed: | |
67 save_or_create_certificates( | |
68 config['ssl-cert-path'], config.get('ssl-cert-contents'), | |
69 config.get('ssl-key-contents')) | |
70 | |
71 # Handle changes to the improv server configuration. This step is currently | |
72 # skipped if the charm is deployed in a juju-core environment where the | |
73 # staging server is not supported. | |
74 if is_legacy_juju: | |
75 if in_staging: | |
76 staging_properties = set(['staging', 'staging-environment']) | |
77 staging_changed = added_or_changed & staging_properties | |
78 if staging_changed or ssl_changed or api_branch_changed: | |
79 if 'staging' in added_or_changed: | |
80 # 'staging' went from False to True, so the agent server is | |
81 # running and must be stopped. | |
82 current_api = AGENT | |
83 # We need to make sure we have staging dependencies. | |
84 get_staging_dependencies() | |
85 else: | |
86 # Only staging parameters changed, so the existing staging | |
87 # server must be stopped and later restarted. | |
88 current_api = IMPROV | |
89 log('Stopping %s.' % current_api) | |
90 service_control(current_api, STOP) | |
91 # Now the improv server can be cleanly started. | |
92 log('Starting or restarting staging.') | |
93 start_improv(config.get('staging-environment'), | |
94 config['ssl-cert-path']) | |
95 else: | |
96 agent_changed = 'staging' in added_or_changed | |
97 if agent_changed or ssl_changed or api_branch_changed: | |
98 if agent_changed: | |
99 # If 'staging' transitions to False we need to stop the | |
100 # backend and start the agent. | |
101 current_api = IMPROV | |
102 else: | |
103 # The agent is still running but the configuration has been | |
104 # updated -- bounce it. | |
105 current_api = AGENT | |
106 service_control(current_api, STOP) | |
107 log('Starting or restarting Juju API agent.') | |
108 start_agent(config['ssl-cert-path']) | |
109 | |
110 # Handle changes to the juju-gui configuration. | |
111 gui_properties = set([ | |
112 'juju-gui-console-enabled', 'login-help', 'read-only', 'serve-tests', | |
113 'secure']) | |
114 gui_changed = added_or_changed & gui_properties | |
115 # Changes on the "staging" option are only relevant in PyJuju environments. | |
116 staging_changed = is_legacy_juju and ('staging' in added_or_changed) | |
117 if gui_changed or staging_changed or ssl_changed or gui_source_changed: | |
118 with su('root'): | |
119 service_control(HAPROXY, STOP) | |
120 service_control(NGINX, STOP) | |
121 console_enabled = config.get('juju-gui-console-enabled') | |
122 login_help = config['login-help'] | |
123 readonly = config['read-only'] | |
124 serve_tests = config['serve-tests'] | |
125 ssl_cert_path = config['ssl-cert-path'] | |
126 secure = config['secure'] | |
127 start_gui( | |
128 console_enabled, login_help, readonly, in_staging, ssl_cert_path, | |
129 serve_tests, secure=secure) | |
130 | 23 |
131 | 24 |
132 def main(): | 25 def main(): |
133 config = get_config() | 26 config = get_config() |
134 prev_config = config_json.get() | 27 prev_config = config_json.get() |
135 diff = DictDiffer(config, prev_config) | 28 diff = DictDiffer(config, prev_config) |
136 | 29 |
137 if not diff.modified: | 30 if not diff.modified: |
138 log("No configuration changes, exiting.") | 31 log("No configuration changes, exiting.") |
139 sys.exit(0) | 32 sys.exit(0) |
140 handle_config_changes(config, diff) | 33 |
34 log('Updating configuration.') | |
35 backend = Backend(config, prev_config) | |
36 | |
37 # Woot | |
38 if prev_config: | |
39 # Stop whatever the old config was. | |
40 prev_backend = Backend(prev_config) | |
41 prev_backend.stop() | |
gary.poster
2013/04/17 18:41:52
I haven't commented on how much I like the readabi
bcsaller
2013/04/17 19:34:41
I think it should be fine in practice (and will te
| |
42 backend.install() | |
43 backend.start() | |
44 | |
45 # Record new configuration | |
141 config_json.set(config) | 46 config_json.set(config) |
142 | 47 |
143 | 48 |
144 if __name__ == '__main__': | 49 if __name__ == '__main__': |
145 with log_hook(): | 50 with log_hook(): |
146 main() | 51 main() |
OLD | NEW |