2
# -*- coding: utf-8 -*-
4
# Copyright 2008-2015 Canonical
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.
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.
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/>.
19
# For further info, check http://launchpad.net/filesync-server
21
"""Run the graphite development server."""
29
from config import config
30
import utilities.localendpoints as local
31
from utilities.utils import kill_group, get_tmpdir
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")
40
os.makedirs(GRAPHITE_LOG_DIR)
44
os.makedirs(CARBON_LOG_DIR)
48
# User our settings instead of graphite.settings
49
os.environ['DJANGO_SETTINGS_MODULE'] = \
50
'metrics.graphite_frontend.local_settings'
52
ADMIN_USER = config.secret.graphite_admin_user
53
ADMIN_PASS = config.secret.graphite_admin_password
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)
61
port = local.allocate_ports()[0]
62
local.register_local_port("graphite-devserver", port)
64
syncdb = "/usr/bin/django-admin syncdb --noinput"
66
status, output = commands.getstatusoutput(syncdb)
68
print >> sys.stderr, "Error: Database doesn't appear to be running!"
69
print >> sys.stderr, "Did you run \"make start\" first?"
74
from django.contrib.auth.models import User
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)
82
with open(os.path.join(TMP_DIR, "storage-schemas.conf"), "w") as schema:
83
schema.write("""[everything_1min_7days]
90
with open(os.path.join(os.path.dirname(sys.argv[0]), 'carbon.conf.tpl')) as f:
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
99
conf = template % template_vars
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)
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)
112
signal.signal(signal.SIGTERM, shutdown)
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
120
subprocess.Popen(carbon_cache_command + ['start'],
121
env={"PYTHONPATH": os.pathsep.join(sys.path),
122
"GRAPHITE_ROOT": TMP_DIR}).wait()
124
subprocess.Popen(["/usr/bin/django-admin",
125
"runserver", "0.0.0.0:%s" % port]).wait()
126
except KeyboardInterrupt:
128
subprocess.Popen(carbon_cache_command + ['stop'],
129
env={"PYTHONPATH": os.pathsep.join(sys.path),
130
"GRAPHITE_ROOT": TMP_DIR}).wait()