~bcsaller/charms/precise/juju-gui/sandbox-charm

« back to all changes in this revision

Viewing changes to hooks/utils.py

  • Committer: Francesco Banconi
  • Date: 2013-03-28 16:25:06 UTC
  • mfrom: (36.1.7 juju-gui)
  • Revision ID: francesco.banconi@canonical.com-20130328162506-dgerjn52ye7433w9
Added support for juju-core.

It is now possible to deploy the charm
in a juju-core environment.
The GUI correctly  connects to the juju-core 
API server.

R=gary.poster, bac
CC=
https://codereview.appspot.com/8081043

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
    'fetch_api',
10
10
    'fetch_gui',
11
11
    'first_path_in_dir',
 
12
    'get_api_address',
12
13
    'get_release_file_url',
13
14
    'get_staging_dependencies',
14
15
    'get_zookeeper_address',
18
19
    'JUJU_GUI_DIR',
19
20
    'JUJU_GUI_SITE',
20
21
    'JUJU_PEM',
 
22
    'legacy_juju',
21
23
    'log_hook',
22
24
    'NGINX',
23
25
    'parse_source',
39
41
import shutil
40
42
from subprocess import CalledProcessError
41
43
import tempfile
42
 
import tempita
43
44
 
44
45
from launchpadlib.launchpad import Launchpad
45
46
from shelltoolbox import (
61
62
    STOP,
62
63
    unit_get,
63
64
)
 
65
import tempita
64
66
 
65
67
 
66
68
AGENT = 'juju-api-agent'
89
91
config_json = Serializer('/tmp/config.json')
90
92
# Bazaar checkout command.
91
93
bzr_checkout = command('bzr', 'co', '--lightweight')
 
94
# Whether or not the charm is deployed using juju-core.
 
95
# If juju-core has been used to deploy the charm, an agent.conf file must
 
96
# be present in the charm parent directory.
 
97
legacy_juju = lambda: not os.path.exists(
 
98
    os.path.join(CURRENT_DIR, '..', 'agent.conf'))
92
99
 
93
100
 
94
101
def _get_build_dependencies():
98
105
    cmd_log(apt_get_install(*DEB_BUILD_DEPENDENCIES))
99
106
 
100
107
 
 
108
def get_api_address(unit_dir):
 
109
    """Return the Juju API address stored in the uniter agent.conf file."""
 
110
    import yaml  # python-yaml is only installed if juju-core is used.
 
111
    # XXX 2013-03-27 frankban bug=1161443:
 
112
        # currently the uniter agent.conf file does not include the API
 
113
        # address. For now retrieve it from the machine agent file.
 
114
    base_dir = os.path.abspath(os.path.join(unit_dir, '..'))
 
115
    for dirname in os.listdir(base_dir):
 
116
        if dirname.startswith('machine-'):
 
117
            agent_conf = os.path.join(base_dir, dirname, 'agent.conf')
 
118
            break
 
119
    else:
 
120
        raise IOError('Juju agent configuration file not found.')
 
121
    contents = yaml.load(open(agent_conf))
 
122
    return contents['apiinfo']['addrs'][0]
 
123
 
 
124
 
101
125
def get_staging_dependencies():
102
126
    """Install deb dependencies for the stage (improv) environment."""
103
127
    log('Installing stage dependencies.')
293
317
        build_dirname = 'build-prod'
294
318
    build_dir = os.path.join(JUJU_GUI_DIR, build_dirname)
295
319
    log('Generating the Juju GUI configuration file.')
296
 
    user, password = ('admin', 'admin') if in_staging else (None, None)
 
320
    is_legacy_juju = legacy_juju()
 
321
    user, password = None, None
 
322
    if is_legacy_juju and in_staging:
 
323
        user, password = 'admin', 'admin'
 
324
    else:
 
325
        user, password = None, None
 
326
    api_backend = 'python' if is_legacy_juju else 'go'
297
327
    if secure:
298
328
        protocol = 'wss'
299
329
    else:
305
335
        'console_enabled': json.dumps(console_enabled),
306
336
        'login_help': json.dumps(login_help),
307
337
        'password': json.dumps(password),
 
338
        'api_backend': json.dumps(api_backend),
308
339
        'readonly': json.dumps(readonly),
309
340
        'user': json.dumps(user),
310
341
        'protocol': json.dumps(protocol)
322
353
    }
323
354
    render_to_file('nginx-site.template', context, nginx_path)
324
355
    log('Generating haproxy configuration file.')
 
356
    if is_legacy_juju:
 
357
        # The PyJuju API agent is listening on localhost.
 
358
        api_address = '127.0.0.1:{0}'.format(API_PORT)
 
359
    else:
 
360
        # Retrieve the juju-core API server address.
 
361
        api_address = get_api_address(os.path.join(CURRENT_DIR, '..'))
325
362
    context = {
 
363
        'api_address': api_address,
326
364
        'api_pem': JUJU_PEM,
327
 
        'api_port': API_PORT,
 
365
        'legacy_juju': is_legacy_juju,
328
366
        'ssl_cert_path': ssl_cert_path,
329
 
        # Use the same certificate for both HTTPS and Websocket connections.
330
 
        # In the long term, we want separate certs to be used here.
 
367
        # In PyJuju environments, use the same certificate for both HTTPS and
 
368
        # WebSocket connections. In juju-core the system already has the proper
 
369
        # certificate installed.
331
370
        'web_pem': JUJU_PEM,
332
371
        'web_port': WEB_PORT,
333
372
        'secure': secure
346
385
        log('Stopping Juju GUI.')
347
386
        service_control(HAPROXY, STOP)
348
387
        service_control(NGINX, STOP)
349
 
        if in_staging:
350
 
            log('Stopping the staging backend.')
351
 
            service_control(IMPROV, STOP)
352
 
        else:
353
 
            log('Stopping API agent.')
354
 
            service_control(AGENT, STOP)
 
388
        # No need to stop staging or the API agent in juju-core environments.
 
389
        if legacy_juju():
 
390
            if in_staging:
 
391
                log('Stopping the staging backend.')
 
392
                service_control(IMPROV, STOP)
 
393
            else:
 
394
                log('Stopping API agent.')
 
395
                service_control(AGENT, STOP)
355
396
 
356
397
 
357
398
def fetch_gui(juju_gui_source, logpath):