1- from fabric .api import (task ,hosts ,run )
1+ import json
2+
3+ from fabric .api import (
4+ task ,hosts ,run ,settings ,
5+ sudo ,get ,shell_env ,cd
6+ )
7+ from StringIO import StringIO
28
39
410@task
@@ -23,5 +29,51 @@ def unpublish_dashboard(slug):
2329run_stagecraft_postgres_command (sql_command )
2430
2531
32+ @task
33+ @hosts ('performance-backend-1.backend' )
34+ def collect (data_group ,data_type ):
35+ """Run a collector config"""
36+ base_path = '/data/apps/performanceplatform-collector/current'
37+
38+ with cd (base_path ):
39+ query_path = './config/queries/{}/{}.json' .format (
40+ data_group ,data_type )
41+
42+ query = get_file_contents (query_path )
43+
44+ command = get_command (query_path ,query )
45+ print ('Executing: {}' .format (command ))
46+ with settings (sudo_user = 'deploy' ):
47+ with shell_env (LOGLEVEL = 'debug' ):
48+ sudo (command )
49+
50+
51+ def get_command (query_path ,query ):
52+ """
53+ >>> get_command('/tmp/foo/bar', {'entrypoint': 'foo.bar.monkey', 'token': 'foo'})
54+ './venv/bin/pp-collector -q /tmp/foo/bar -c ./config/credentials/monkey.json -t ./config/tokens/foo.json -b ./config/performanceplatform.json --console-logging'
55+ """
56+ credentials = query ['entrypoint' ].split ('.' )[- 1 ]
57+
58+ # realtime uses GA credentials, gcloud needs none but pp-collector fails with none
59+ if credentials in ['realtime' ,'gcloud' ,'trending' ]:
60+ credentials = 'ga'
61+
62+ pp_collector = './venv/bin/pp-collector'
63+ credentials_path = './config/credentials/{}.json' .format (credentials )
64+ token_path = './config/tokens/{}.json' .format (
65+ query ['token' ])
66+ platform_path = './config/performanceplatform.json'
67+
68+ return '{} -q {} -c {} -t {} -b {} --console-logging' .format (
69+ pp_collector ,query_path ,credentials_path ,token_path ,platform_path )
70+
71+
72+ def get_file_contents (path ):
73+ fd = StringIO ()
74+ get (path ,fd )
75+ return json .loads (fd .getvalue ())
76+
77+
2678def run_stagecraft_postgres_command (sql_command ):
2779run ('sudo -iu postgres psql stagecraft -c "{0}"' .format (sql_command ))