112
100
BASE_DIR = '/var/lib/juju-gui'
113
101
CURRENT_DIR = os.getcwd()
114
102
CONFIG_DIR = os.path.join(CURRENT_DIR, 'config')
115
JUJU_AGENT_DIR = os.path.join(BASE_DIR, 'juju')
116
103
JUJU_GUI_DIR = os.path.join(BASE_DIR, 'juju-gui')
117
104
RELEASES_DIR = os.path.join(CURRENT_DIR, 'releases')
118
105
SERVER_DIR = os.path.join(CURRENT_DIR, 'server')
123
110
HAPROXY_CFG_PATH = os.path.join(os.path.sep, 'etc', 'haproxy', 'haproxy.cfg')
125
112
SYS_INIT_DIR = os.path.join(os.path.sep, 'etc', 'init')
126
AGENT_INIT_PATH = os.path.join(SYS_INIT_DIR, 'juju-api-agent.conf')
127
113
GUISERVER_INIT_PATH = os.path.join(SYS_INIT_DIR, 'guiserver.conf')
128
114
HAPROXY_INIT_PATH = os.path.join(SYS_INIT_DIR, 'haproxy.conf')
129
IMPROV_INIT_PATH = os.path.join(SYS_INIT_DIR, 'juju-api-improv.conf')
131
116
JUJU_PEM = 'juju.includes-private-key.pem'
132
117
DEB_BUILD_DEPENDENCIES = (
138
123
config_json = Serializer(os.path.join(os.path.sep, 'tmp', 'config.json'))
139
124
# Bazaar checkout command.
140
125
bzr_checkout = command('bzr', 'co', '--lightweight')
141
# Whether or not the charm is deployed using juju-core.
142
# If juju-core has been used to deploy the charm, an agent.conf file must
143
# be present in the charm parent directory.
144
legacy_juju = lambda: not os.path.exists(
145
os.path.join(CURRENT_DIR, '..', 'agent.conf'))
147
127
bzr_url_expression = re.compile(r"""
148
128
^ # Beginning of line.
165
145
def get_api_address(unit_dir=None):
166
146
"""Return the Juju API address.
168
If not present in the hook context as an environment variable, try to
169
retrieve the address parsing the machiner agent.conf file.
171
149
api_addresses = os.getenv('JUJU_API_ADDRESSES')
172
150
if api_addresses is not None:
235
214
raise ValueError('%r: file not found' % release_version)
238
def get_zookeeper_address(agent_file_path):
239
"""Retrieve the Zookeeper address contained in the given *agent_file_path*.
241
The *agent_file_path* is a path to a file containing a line similar to the
244
env JUJU_ZOOKEEPER="address"
246
line = search_file('JUJU_ZOOKEEPER', agent_file_path).strip()
247
return line.split('=')[1].strip('"')
252
219
"""Log when a hook starts and stops its execution.
346
313
results_log.info('\n' + results)
349
def start_improv(staging_env, ssl_cert_path):
350
"""Start a simulated juju environment using ``improv.py``."""
351
log('Setting up the staging Upstart script.')
353
'juju_dir': JUJU_AGENT_DIR,
354
'keys': ssl_cert_path,
356
'staging_env': staging_env,
358
render_to_file('juju-api-improv.conf.template', context, IMPROV_INIT_PATH)
359
log('Starting the staging backend.')
361
service_control(IMPROV, START)
365
"""Stop a simulated Juju environment."""
366
log('Stopping the staging backend.')
368
service_control(IMPROV, STOP)
369
log('Removing the staging Upstart script.')
370
cmd_log(run('rm', '-f', IMPROV_INIT_PATH))
373
def start_agent(ssl_cert_path, read_only=False):
374
"""Start the Juju agent and connect to the current environment."""
375
# Retrieve the Zookeeper address from the start up script.
376
unit_name = os.path.basename(
377
os.path.realpath(os.path.join(CURRENT_DIR, '..')))
378
agent_file = os.path.join(SYS_INIT_DIR, 'juju-{}.conf'.format(unit_name))
379
zookeeper = get_zookeeper_address(agent_file)
380
log('Setting up the API agent Upstart script.')
382
'juju_dir': JUJU_AGENT_DIR,
383
'keys': ssl_cert_path,
385
'zookeeper': zookeeper,
386
'read_only': read_only
388
render_to_file('juju-api-agent.conf.template', context, AGENT_INIT_PATH)
389
log('Starting the API agent.')
391
service_control(AGENT, START)
395
"""Stop the Juju agent."""
396
log('Stopping the API agent.')
398
service_control(AGENT, STOP)
399
log('Removing the API agent Upstart script.')
400
cmd_log(run('rm', '-f', AGENT_INIT_PATH))
403
316
def compute_build_dir(juju_gui_debug, serve_tests):
404
317
"""Compute the build directory."""
418
331
def write_gui_config(
419
console_enabled, login_help, readonly, in_staging, charmworld_url,
332
console_enabled, login_help, readonly, charmworld_url,
420
333
build_dir, secure=True, sandbox=False,
421
334
show_get_juju_button=False, config_js_path=None, ga_key='',
423
336
"""Generate the GUI configuration file."""
424
337
log('Generating the Juju GUI configuration file.')
425
is_legacy_juju = legacy_juju()
426
user = 'admin' if is_legacy_juju else 'user-admin'
427
# Normalize empty string passwords to None.
428
password = password if password else None
429
if password is None and ((is_legacy_juju and in_staging) or sandbox):
431
api_backend = 'python' if (is_legacy_juju and not sandbox) else 'go'
339
# Normalize empty string passwords to None. If sandbox is enabled then set
340
# the password to admin and it will auto login.
461
376
config_path = os.path.join(CONFIG_DIR, 'haproxy.conf')
462
377
shutil.copy(config_path, SYS_INIT_DIR)
463
378
log('Generating haproxy configuration file.')
464
is_legacy_juju = legacy_juju()
466
# The PyJuju API agent is listening on localhost.
467
api_address = '127.0.0.1:{}'.format(API_PORT)
469
# Retrieve the juju-core API server address.
470
api_address = get_api_address()
379
# Retrieve the juju-core API server address.
380
api_address = get_api_address()
472
382
'api_address': api_address,
473
383
'api_pem': JUJU_PEM,
474
'legacy_juju': is_legacy_juju,
475
384
'ssl_cert_path': ssl_cert_path,
476
# In PyJuju environments, use the same certificate for both HTTPS and
477
# WebSocket connections. In juju-core the system already has the proper
478
# certificate installed.
479
385
'web_pem': JUJU_PEM,
480
386
'web_port': WEB_PORT,
577
483
'charmworld_url': charmworld_url,
580
is_legacy_juju = legacy_juju()
582
api_url = 'wss://127.0.0.1:{}/ws'.format(API_PORT)
584
api_url = 'wss://{}'.format(get_api_address())
486
api_url = 'wss://{}'.format(get_api_address())
586
488
'api_url': api_url,
587
'api_version': 'python' if is_legacy_juju else 'go',
590
492
context['tests_root'] = os.path.join(JUJU_GUI_DIR, 'test', '')
737
639
return download_release(url, filename)
740
def fetch_api(juju_api_branch):
741
"""Retrieve the Juju branch, removing it first if already there."""
742
# Retrieve Juju API source checkout.
743
log('Retrieving Juju API source checkout.')
744
cmd_log(run('rm', '-rf', JUJU_AGENT_DIR))
745
cmd_log(bzr_checkout(juju_api_branch, JUJU_AGENT_DIR))
748
642
def setup_gui(release_tarball):
749
643
"""Set up Juju GUI."""
750
644
# Uncompress the release tarball.