~canonical-is-sa/charms/trusty/logstash/logstash2

« back to all changes in this revision

Viewing changes to hooks/install

  • Committer: Charles Butler
  • Date: 2014-07-17 16:38:17 UTC
  • Revision ID: chuck@dasroot.net-20140717163817-dhdgwb0esuxzd3jj
Initial checkin of new logstash codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import hashlib
 
4
import os
 
5
import pwd
 
6
import requests
 
7
import shutil
 
8
import subprocess
 
9
import sys
 
10
 
 
11
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
 
12
 
 
13
from charmhelpers.core.hookenv import (
 
14
    config,
 
15
    log,
 
16
    Hooks,
 
17
)
 
18
 
 
19
from charmhelpers.fetch import (
 
20
    apt_update,
 
21
    apt_install,
 
22
)
 
23
 
 
24
hooks = Hooks()
 
25
log = log
 
26
 
 
27
SERVICE = 'logstash'
 
28
BASEPATH = '/opt/logstash'
 
29
 
 
30
 
 
31
@hooks.hook('install')
 
32
def install():
 
33
    log('Installing logstash')
 
34
    packages = ['openjdk-7-jre-headless', 'curl', 'redis-server']
 
35
    apt_update(fatal=True)
 
36
    apt_install(packages, fatal=True)
 
37
    # Place default redis template
 
38
    fetch_logstash()
 
39
    create_skeleton()
 
40
    create_user()
 
41
    copy_config()
 
42
 
 
43
 
 
44
# TODO : Add flat file support
 
45
def fetch_logstash():
 
46
    # Fetch the logstash binary
 
47
    filename = config('logstash-source').split('/')[-1]
 
48
    with open('/tmp/{}'.format(filename), 'wb') as handle:
 
49
        response = requests.get(config('logstash-source'), stream=True)
 
50
 
 
51
        for block in response.iter_content(1024):
 
52
            if not block:
 
53
                break
 
54
            handle.write(block)
 
55
 
 
56
    with open('/tmp/{}'.format(filename), 'r') as handle:
 
57
        # Run checksum
 
58
        sha = hashlib.sha1()
 
59
        for line in handle:
 
60
            sha.update(line)
 
61
        if sha.hexdigest() != str(config('logstash-sum')):
 
62
            raise ValueError("Provided SUM does not match downloaded sum")
 
63
        else:
 
64
            log("Verified {} with SHA1 sum of {}".format(filename,
 
65
                sha.hexdigest()))
 
66
 
 
67
 
 
68
def create_skeleton():
 
69
    dirs = ['bin', 'conf.d', 'data']
 
70
    for d in dirs:
 
71
        dr = '{}/{}'.format(BASEPATH, d)
 
72
        if not os.path.exists(dr):
 
73
            os.makedirs(dr)
 
74
 
 
75
 
 
76
def copy_config():
 
77
    files = os.listdir('files/conf/')
 
78
    for f in files:
 
79
        shutil.copy(f, '{}/conf.d/'.format(BASEPATH))
 
80
 
 
81
 
 
82
def create_user():
 
83
    try:
 
84
        pwd.getpwnam('logstash')
 
85
    except KeyError:
 
86
        subprocess.call(['useradd', 'logstash'])
 
87
 
 
88
if __name__ == "__main__":
 
89
    # execute a hook based on the name the program is called by
 
90
    hooks.execute(sys.argv)