~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to dev-scripts/run-graphite-devserver.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright 2008-2015 Canonical
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU Affero General Public License as
 
8
# published by the Free Software Foundation, either version 3 of the
 
9
# License, or (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU Affero General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU Affero General Public License
 
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
# For further info, check  http://launchpad.net/filesync-server
 
20
 
 
21
"""Run the graphite development server."""
 
22
 
 
23
import sys
 
24
import os
 
25
import commands
 
26
import signal
 
27
import subprocess
 
28
 
 
29
from config import config
 
30
import utilities.localendpoints as local
 
31
from utilities.utils import kill_group, get_tmpdir
 
32
 
 
33
 
 
34
TMP_DIR = get_tmpdir()
 
35
CARBON_LOG_DIR = os.path.join(TMP_DIR, "carbon_logs")
 
36
GRAPHITE_LOG_DIR = os.path.join(TMP_DIR, "graphite_logs")
 
37
CARBON_PID_FILE = os.path.join(TMP_DIR, "carbon.pid")
 
38
 
 
39
try:
 
40
    os.makedirs(GRAPHITE_LOG_DIR)
 
41
except OSError:
 
42
    pass
 
43
try:
 
44
    os.makedirs(CARBON_LOG_DIR)
 
45
except OSError:
 
46
    pass
 
47
 
 
48
# User our settings instead of graphite.settings
 
49
os.environ['DJANGO_SETTINGS_MODULE'] = \
 
50
    'metrics.graphite_frontend.local_settings'
 
51
 
 
52
ADMIN_USER = config.secret.graphite_admin_user
 
53
ADMIN_PASS = config.secret.graphite_admin_password
 
54
 
 
55
(carbon_line_port, carbon_pickle_port,
 
56
 carbon_query_port) = local.allocate_ports(3)
 
57
local.register_local_port("carbon_line_receiver", carbon_line_port)
 
58
local.register_local_port("carbon_pickle_receiver", carbon_pickle_port)
 
59
local.register_local_port("carbon_query_port", carbon_query_port)
 
60
 
 
61
port = local.allocate_ports()[0]
 
62
local.register_local_port("graphite-devserver", port)
 
63
 
 
64
syncdb = "/usr/bin/django-admin syncdb --noinput"
 
65
 
 
66
status, output = commands.getstatusoutput(syncdb)
 
67
if status > 0:
 
68
    print >> sys.stderr, "Error: Database doesn't appear to be running!"
 
69
    print >> sys.stderr, "Did you run  \"make start\" first?"
 
70
    print output
 
71
    sys.exit(1)
 
72
 
 
73
# Create the user
 
74
from django.contrib.auth.models import User
 
75
# delete if exists
 
76
User.objects.filter(username=ADMIN_USER).delete()
 
77
# Create user with correct credentials
 
78
user = User.objects.create_user(ADMIN_USER, "noreply@somemail.com", ADMIN_PASS)
 
79
user.is_staff = True
 
80
user.save()
 
81
 
 
82
with open(os.path.join(TMP_DIR, "storage-schemas.conf"), "w") as schema:
 
83
    schema.write("""[everything_1min_7days]
 
84
priority = 100
 
85
pattern = .*
 
86
retentions = 60:7d
 
87
""")
 
88
 
 
89
# Build a conf
 
90
with open(os.path.join(os.path.dirname(sys.argv[0]), 'carbon.conf.tpl')) as f:
 
91
    template = f.read()
 
92
 
 
93
template_vars = {}
 
94
template_vars['data_dir'] = os.path.join(TMP_DIR, "whisper")
 
95
template_vars['line_receiver_port'] = carbon_line_port
 
96
template_vars['pickle_receiver_port'] = carbon_pickle_port
 
97
template_vars['cache_query_port'] = carbon_query_port
 
98
 
 
99
conf = template % template_vars
 
100
 
 
101
conf_file_path = os.path.join(TMP_DIR, 'carbon.conf')
 
102
with open(conf_file_path, 'w') as conf_file:
 
103
    conf_file.write(conf)
 
104
 
 
105
 
 
106
# SIGTERM -> kill process group/carbon
 
107
def shutdown(signum, frame):
 
108
    """Shutdown carbon."""
 
109
    if os.path.exists(CARBON_PID_FILE):
 
110
        os.kill(int(open(CARBON_PID_FILE).read()), signal.SIGTERM)
 
111
    kill_group()
 
112
signal.signal(signal.SIGTERM, shutdown)
 
113
 
 
114
carbon_cache_command = [
 
115
    "/usr/bin/twistd", "--pidfile", CARBON_PID_FILE,
 
116
    "--reactor", "epoll", "carbon-cache", "--config", conf_file_path,
 
117
    "--logdir", CARBON_LOG_DIR
 
118
]
 
119
 
 
120
subprocess.Popen(carbon_cache_command + ['start'],
 
121
                 env={"PYTHONPATH": os.pathsep.join(sys.path),
 
122
                      "GRAPHITE_ROOT": TMP_DIR}).wait()
 
123
try:
 
124
    subprocess.Popen(["/usr/bin/django-admin",
 
125
                      "runserver", "0.0.0.0:%s" % port]).wait()
 
126
except KeyboardInterrupt:
 
127
    pass
 
128
subprocess.Popen(carbon_cache_command + ['stop'],
 
129
                 env={"PYTHONPATH": os.pathsep.join(sys.path),
 
130
                      "GRAPHITE_ROOT": TMP_DIR}).wait()