~rharding/charms/precise/juju-gui/update-nagios

« back to all changes in this revision

Viewing changes to hooks/utils.py

  • Committer: Rick Harding
  • Date: 2014-01-15 14:50:46 UTC
  • mfrom: (147.1.8 remove-pyjuju)
  • Revision ID: rick.harding@canonical.com-20140115145046-lyh9879k9x8hwb8i
Remove support for PyJuju and rapi from charm.

- Removes the support for running rapi/pyjuju.
- Removes the agent used to communicate with zookeeper.
- Removes anything pertaining to zookeeper.
- Attempts to clean up docs and such to make sense with pure juju-core.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    'APACHE_PORTS',
22
22
    'API_PORT',
23
23
    'CURRENT_DIR',
24
 
    'JUJU_AGENT_DIR',
25
24
    'JUJU_GUI_DIR',
26
25
    'JUJU_PEM',
27
26
    'WEB_PORT',
28
27
    'cmd_log',
29
28
    'compute_build_dir',
30
29
    'download_release',
31
 
    'fetch_api',
32
30
    'fetch_gui_from_branch',
33
31
    'fetch_gui_release',
34
32
    'find_missing_packages',
37
35
    'get_launchpad_release',
38
36
    'get_npm_cache_archive_url',
39
37
    'get_release_file_path',
40
 
    'get_zookeeper_address',
41
38
    'install_missing_packages',
42
 
    'legacy_juju',
43
39
    'log_hook',
44
40
    'parse_source',
45
41
    'prime_npm_cache',
50
46
    'setup_apache_config',
51
47
    'setup_gui',
52
48
    'setup_haproxy_config',
53
 
    'start_agent',
54
49
    'start_builtin_server',
55
50
    'start_haproxy_apache',
56
 
    'start_improv',
57
 
    'stop_agent',
58
51
    'stop_builtin_server',
59
52
    'stop_haproxy_apache',
60
 
    'stop_improv',
61
53
    'write_gui_config',
62
54
]
63
55
 
72
64
from subprocess import CalledProcessError
73
65
import tempfile
74
66
import urlparse
 
67
import yaml
75
68
 
76
69
import apt
77
70
from launchpadlib.launchpad import Launchpad
78
71
import tempita
79
 
import yaml
80
72
 
81
73
from charmhelpers import (
82
74
    get_config,
83
75
    log,
84
76
    RESTART,
85
77
    service_control,
86
 
    START,
87
78
    STOP,
88
79
    unit_get,
89
80
)
94
85
    install_extra_repositories,
95
86
    run,
96
87
    script_name,
97
 
    search_file,
98
88
    Serializer,
99
89
    su,
100
90
)
101
91
 
102
92
 
103
 
AGENT = 'juju-api-agent'
104
93
APACHE = 'apache2'
105
94
BUILTIN_SERVER = 'guiserver'
106
95
HAPROXY = 'haproxy'
107
 
IMPROV = 'juju-api-improv'
108
96
 
109
97
API_PORT = 8080
110
98
WEB_PORT = 8000
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')
124
111
 
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')
130
115
 
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'))
146
126
 
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.
167
147
 
168
 
    If not present in the hook context as an environment variable, try to
169
 
    retrieve the address parsing the machiner agent.conf file.
170
148
    """
171
149
    api_addresses = os.getenv('JUJU_API_ADDRESSES')
172
150
    if api_addresses is not None:
187
165
        raise IOError('Juju agent configuration file not found.')
188
166
    contents = yaml.load(open(agent_conf))
189
167
    return contents['apiinfo']['addrs'][0]
 
168
    return api_addresses.split()[0]
190
169
 
191
170
 
192
171
def first_path_in_dir(directory):
235
214
    raise ValueError('%r: file not found' % release_version)
236
215
 
237
216
 
238
 
def get_zookeeper_address(agent_file_path):
239
 
    """Retrieve the Zookeeper address contained in the given *agent_file_path*.
240
 
 
241
 
    The *agent_file_path* is a path to a file containing a line similar to the
242
 
    following::
243
 
 
244
 
        env JUJU_ZOOKEEPER="address"
245
 
    """
246
 
    line = search_file('JUJU_ZOOKEEPER', agent_file_path).strip()
247
 
    return line.split('=')[1].strip('"')
248
 
 
249
 
 
250
217
@contextmanager
251
218
def log_hook():
252
219
    """Log when a hook starts and stops its execution.
346
313
    results_log.info('\n' + results)
347
314
 
348
315
 
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.')
352
 
    context = {
353
 
        'juju_dir': JUJU_AGENT_DIR,
354
 
        'keys': ssl_cert_path,
355
 
        'port': API_PORT,
356
 
        'staging_env': staging_env,
357
 
    }
358
 
    render_to_file('juju-api-improv.conf.template', context, IMPROV_INIT_PATH)
359
 
    log('Starting the staging backend.')
360
 
    with su('root'):
361
 
        service_control(IMPROV, START)
362
 
 
363
 
 
364
 
def stop_improv():
365
 
    """Stop a simulated Juju environment."""
366
 
    log('Stopping the staging backend.')
367
 
    with su('root'):
368
 
        service_control(IMPROV, STOP)
369
 
    log('Removing the staging Upstart script.')
370
 
    cmd_log(run('rm', '-f', IMPROV_INIT_PATH))
371
 
 
372
 
 
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.')
381
 
    context = {
382
 
        'juju_dir': JUJU_AGENT_DIR,
383
 
        'keys': ssl_cert_path,
384
 
        'port': API_PORT,
385
 
        'zookeeper': zookeeper,
386
 
        'read_only': read_only
387
 
    }
388
 
    render_to_file('juju-api-agent.conf.template', context, AGENT_INIT_PATH)
389
 
    log('Starting the API agent.')
390
 
    with su('root'):
391
 
        service_control(AGENT, START)
392
 
 
393
 
 
394
 
def stop_agent():
395
 
    """Stop the Juju agent."""
396
 
    log('Stopping the API agent.')
397
 
    with su('root'):
398
 
        service_control(AGENT, STOP)
399
 
    log('Removing the API agent Upstart script.')
400
 
    cmd_log(run('rm', '-f', AGENT_INIT_PATH))
401
 
 
402
 
 
403
316
def compute_build_dir(juju_gui_debug, serve_tests):
404
317
    """Compute the build directory."""
405
318
    with su('root'):
416
329
 
417
330
 
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='',
422
335
        password=None):
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):
430
 
        password = 'admin'
431
 
    api_backend = 'python' if (is_legacy_juju and not sandbox) else 'go'
 
338
    user = 'user-admin'
 
339
    # Normalize empty string passwords to None. If sandbox is enabled then set
 
340
    # the password to admin and it will auto login.
 
341
    if not password:
 
342
        if sandbox:
 
343
            password = 'admin'
 
344
        else:
 
345
            password = None
 
346
    api_backend = 'go'
432
347
    if secure:
433
348
        protocol = 'wss'
434
349
    else:
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()
465
 
    if is_legacy_juju:
466
 
        # The PyJuju API agent is listening on localhost.
467
 
        api_address = '127.0.0.1:{}'.format(API_PORT)
468
 
    else:
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()
471
381
    context = {
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,
481
387
        'secure': secure
577
483
        'charmworld_url': charmworld_url,
578
484
    }
579
485
    if not sandbox:
580
 
        is_legacy_juju = legacy_juju()
581
 
        if is_legacy_juju:
582
 
            api_url = 'wss://127.0.0.1:{}/ws'.format(API_PORT)
583
 
        else:
584
 
            api_url = 'wss://{}'.format(get_api_address())
 
486
        api_url = 'wss://{}'.format(get_api_address())
585
487
        context.update({
586
488
            'api_url': api_url,
587
 
            'api_version': 'python' if is_legacy_juju else 'go',
 
489
            'api_version': 'go',
588
490
        })
589
491
    if serve_tests:
590
492
        context['tests_root'] = os.path.join(JUJU_GUI_DIR, 'test', '')
737
639
    return download_release(url, filename)
738
640
 
739
641
 
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))
746
 
 
747
 
 
748
642
def setup_gui(release_tarball):
749
643
    """Set up Juju GUI."""
750
644
    # Uncompress the release tarball.