~thomir-deactivatedaccount/charms/trusty/logstash/trunk-expose-tcp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python

import os
import shlex
import subprocess
import sys
import pwd
import grp
import base64


sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))

from charmhelpers.core import (
    hookenv,
)
from charmhelpers.contrib.templating.jinja import render

hooks = hookenv.Hooks()
log = hookenv.log

SERVICE = 'logstash-indexer'
BASEPATH = os.path.join(os.path.sep, 'opt', 'logstash')


@hooks.hook('config-changed')
def config_changed():
    config = hookenv.config()

    for key in config:
        if config.changed(key):
            log("config['{}'] changed from {} to {}".format(
                key, config.previous(key), config[key]))

    config.save()
    copy_config()
    place_upstart_template()

    # This only actually opens the port if we've exposed the service in juju
    hookenv.open_port(5043)
    hookenv.open_port(5959, protocol='UDP')

    # The install hook is idempotent, so re-run it.
    # XXX: no, it's not idempotent. it fails if the tarball was already
    # extracted
    # subprocess.check_output(shlex.split('hooks/install'))

    # Restart the service when configuration has changed.
    import start
    start.start()


def copy_config():
    files = os.listdir('templates')
    opts = {'BASEPATH': BASEPATH}
    lumberjack_template = "input-lumberjack.conf"
    cert_dir = os.path.join(BASEPATH, 'ssl')
    cert_file = os.path.join(cert_dir, 'logstash.crt')
    key_file = os.path.join(cert_dir, 'logstash.key')

    for f in files:
        if os.path.basename(f) != lumberjack_template:
            with open(os.path.join(BASEPATH, 'conf.d', f), 'w') as p:
                p.write(render(os.path.basename(f), opts))

    config_data = hookenv.config()

    # Write custom configuration if set.
    if config_data['extra-config']:
        with open(os.path.join(BASEPATH, 'conf.d', 'extra.conf'), 'w') as f:
            f.write(str(base64.b64decode(config_data['extra-config'])))

    # Only setup lumberjack protocol if ssl cert and key are configured
    if config_data['ssl_cert'] and config_data['ssl_key']:
        if not os.path.exists(cert_dir):
            os.mkdir(cert_dir, 0770)
            os.chown(cert_dir, pwd.getpwnam('logstash').pw_uid, grp.getgrnam('logstash').gr_gid)

        # Certificate provided as base64-encoded string.
        if config_data['ssl_cert']:
            log("Writing cert from config ssl_cert: %s" % cert_file)
            with open(cert_file, 'w') as f:
                f.write(str(base64.b64decode(config_data['ssl_cert'])))
        # Private key provided as base64-encoded string.
        if config_data['ssl_key']:
            log("Writing key from config ssl_key: %s" % key_file)
            with open(key_file, 'w') as f:
                f.write(str(base64.b64decode(config_data['ssl_key'])))

        with open(os.path.join(BASEPATH, 'conf.d', lumberjack_template), 'w') as p:
            p.write(render(os.path.basename(lumberjack_template), opts))


def place_upstart_template():
    out = os.path.join(os.path.sep, 'etc', 'init', '{}.conf'.format(SERVICE))
    templ = os.path.join('files', 'upstart')
    opts = {'BASEPATH': BASEPATH}

    with open(out, 'w') as p:
        p.write(render('{}.conf'.format(SERVICE), opts, template_dir=templ))


if __name__ == "__main__":
    # execute a hook based on the name the program is called by
    hooks.execute(sys.argv)