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
106
107
108
109
110
111
112
113
|
#!/usr/bin/python
import hashlib
import os
import pwd
import requests
import shutil
import subprocess
import sys
import tarfile
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib'))
from charmhelpers.core.hookenv import (
config,
log,
Hooks,
)
from charmhelpers.fetch import (
apt_update,
apt_install,
)
hooks = Hooks()
log = log
SERVICE = 'logstash'
BASEPATH = os.path.join(os.path.sep, 'opt', 'logstash')
@hooks.hook('install')
def install():
log('Installing logstash')
packages = ['openjdk-7-jre-headless', 'redis-server', 'python-jinja2']
extra_packages = config('extra-packages')
apt_update(fatal=True)
apt_install(packages, fatal=True)
if extra_packages:
apt_install(extra_packages, fatal=True)
# Place default redis template
if os.path.exists('files/logstash.tar.gz'):
extract_package('files/logstash.tar.gz')
else:
fetch_logstash()
if not os.path.exists(os.path.join(BASEPATH, 'conf.d')):
create_skeleton()
create_user()
# finalize the installation and make sure logstash owns everything
subprocess.call(['chown', '-R', 'logstash', BASEPATH])
# TODO : Add flat file support
def fetch_logstash():
# Fetch the logstash binary
filename = config('logstash-source').split('/')[-1]
fpath = os.path.join(os.path.sep, 'tmp', filename)
if os.path.exists(fpath) and os.path.exists(BASEPATH):
log("Found LOGSTASH package, doing nothing")
return
with open(fpath, 'wb') as handle:
response = requests.get(config('logstash-source'), stream=True)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
with open(fpath, 'r') as handle:
# Run checksum
sha = hashlib.sha1()
for line in handle:
sha.update(line)
if sha.hexdigest() != str(config('logstash-sum')):
raise ValueError("Provided SUM does not match downloaded sum")
else:
log("Verified {} with SHA1 sum of {}".format(filename,
sha.hexdigest()))
# Extract and move into place
extract_package(fpath)
def extract_package(filepath):
t = tarfile.open(filepath)
t.extractall(path='.')
t.close()
[basename] = [v for v in os.listdir('.') if v.startswith('logstash-')]
shutil.move(basename, BASEPATH)
def create_skeleton():
dirs = ['bin', 'conf.d', 'data']
for d in dirs:
dr = '{}/{}'.format(BASEPATH, d)
if not os.path.exists(dr):
os.makedirs(dr)
def create_user():
try:
pwd.getpwnam('logstash')
except KeyError:
subprocess.call(['useradd', 'logstash'])
if __name__ == "__main__":
# execute a hook based on the name the program is called by
hooks.execute(sys.argv)
|