~michael.nelson/charms/trusty/logstash/logstash2

« back to all changes in this revision

Viewing changes to hooks/install

  • Committer: Charles Butler
  • Date: 2014-09-23 17:28:35 UTC
  • Revision ID: chuck@dasroot.net-20140923172835-3haph2118j7isjvl
Moves logic from install to config-changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
import hashlib
4
3
import os
5
4
import pwd
6
 
import requests
7
 
import shutil
8
5
import subprocess
9
6
import sys
10
 
import tarfile
11
7
 
12
8
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
13
9
 
39
35
    if extra_packages is not None:
40
36
        apt_install(extra_packages, fatal=True)
41
37
 
42
 
    # Place default redis template
43
 
    if os.path.exists('files/logstash.tar.gz'):
44
 
        extract_package('files/logstash.tar.gz')
45
 
    else:
46
 
        fetch_logstash()
 
38
 
 
39
 
47
40
 
48
41
    create_skeleton()
49
42
    create_user()
50
43
 
51
 
    # finalize the installation and make sure logstash owns everything
52
 
    subprocess.call(['chown', '-R', 'logstash', BASEPATH])
53
 
 
54
 
 
55
 
# TODO : Add flat file support
56
 
def fetch_logstash():
57
 
    # Fetch the logstash binary
58
 
    filename = config('logstash-source').split('/')[-1]
59
 
    fpath = os.path.join(os.path.sep, 'tmp', filename)
60
 
 
61
 
    if os.path.exists(fpath) and os.path.exists(BASEPATH):
62
 
        log("Found LOGSTASH package, doing nothing")
63
 
        return
64
 
 
65
 
    with open(fpath, 'wb') as handle:
66
 
        response = requests.get(config('logstash-source'), stream=True)
67
 
 
68
 
        for block in response.iter_content(1024):
69
 
            if not block:
70
 
                break
71
 
            handle.write(block)
72
 
 
73
 
    with open(fpath, 'r') as handle:
74
 
        # Run checksum
75
 
        sha = hashlib.sha1()
76
 
        for line in handle:
77
 
            sha.update(line)
78
 
        if sha.hexdigest() != str(config('logstash-sum')):
79
 
            raise ValueError("Provided SUM does not match downloaded sum")
80
 
        else:
81
 
            log("Verified {} with SHA1 sum of {}".format(filename,
82
 
                sha.hexdigest()))
83
 
 
84
 
    # Extract and move into place
85
 
    extract_package(filename)
86
 
 
87
 
 
88
 
def extract_package(filename):
89
 
    t = tarfile.open(os.path.join(os.path.sep, 'tmp', filename))
90
 
    t.extractall(path='.')
91
 
    t.close()
92
 
    basename = filename.rsplit('.', 2)[0]
93
 
    shutil.move(basename, BASEPATH)
94
 
 
95
 
 
96
44
def create_skeleton():
97
45
    dirs = ['bin', 'conf.d', 'data']
98
46
    for d in dirs: