Completed
Push — master ( a2de53...4e3d38 )
by
unknown
14s
created

elasticsearch_output()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
import os
22
23
from raven import Client
24
from paver.easy import task, needs
25
from paver.easy import sh
26
27
28
client = Client(os.environ.get('RAVEN_DNS'))
29
30
31
@task
32
def test():
33
    sh('./manage.py test --settings=omaha_server.settings_test', cwd='omaha_server')
34
35
36
@task
37
def test_tox():
38
    path_to_test = os.getenv("PATH_TO_TEST", '')
39
    settings = os.getenv("DJANGO_SETTINGS_MODULE", 'omaha_server.settings_test')
40
    sh('./manage.py test %s --settings=%s' % (path_to_test, settings), cwd='omaha_server')
41
42
43
@task
44
def test_postgres():
45
    sh('./manage.py test --settings=omaha_server.settings_test_postgres', cwd='omaha_server')
46
47
48
@task
49
@needs('test', 'test_postgres')
50
def test_all():
51
    pass
52
53
54
@task
55
def up_local_dev_server():
56
    """
57
    Requirements:
58
59
    - [docker](docker.com) or [boot2docker](https://github.com/boot2docker/boot2docker) for OS X or Windows
60
    - [docker-compose](https://docs.docker.com/compose/install/)
61
62
    """
63
    sh('docker-compose -f docker-compose.dev.yml -p dev up -d db')
64
    sh('docker-compose -f docker-compose.dev.yml -p dev up -d web')
65
    print("""Open http://{DOCKER_HOST}:9090/admin/\n username: admin\n password: admin""")
66
67
68
@task
69
def deploy_dev():
70
    sh('ebs-deploy deploy -e omaha-server-dev', cwd='omaha_server')
71
72
73
@task
74
def collectstatic():
75
    sh('./manage.py collectstatic --noinput', cwd='omaha_server')
76
77
78
@task
79
def loaddata():
80
    sh('./manage.py loaddata fixtures/initial_data.json', cwd='omaha_server')
81
82
83
@task
84
def migrate():
85
    sh('./manage.py migrate --noinput', cwd='omaha_server')
86
87
88
@task
89
def create_admin():
90
    sh('./createadmin.py', cwd='omaha_server')
91
92
93
@task
94
def configure_nginx():
95
    filebeat_host = os.environ.get('FILEBEAT_HOST', '')
96
    filebeat_port = os.environ.get('FILEBEAT_PORT', '')
97
    if filebeat_host and filebeat_port.isdigit():
98
        sh("sed -i 's/access_log.*;/access_log syslog:server=%s:%s main;/g' /etc/nginx/nginx.conf" % (filebeat_host, filebeat_port))
99
        sh("sed -i 's/error_log.*;/error_log syslog:server=%s:%s;/g' /etc/nginx/nginx.conf" % (filebeat_host, filebeat_port))
100
    else:
101
        sh("sed -i 's#access_log.*;#access_log /var/log/nginx/access.log main;#g' /etc/nginx/nginx.conf")
102
        sh("sed -i 's#error_log.*;#error_log /var/log/nginx/error.log;#g' /etc/nginx/nginx.conf")
103
    server_name = os.environ.get('HOST_NAME', '_')
104
    server_name = server_name if server_name != '*' else '_'
105
    sh("sed -i 's/server_name.*;/server_name %s;/g' /etc/nginx/sites-enabled/nginx-app.conf" % (server_name))
106
107
108
def elasticsearch_output(elasticsearch_host, elasticsearch_port):
109
    sh("sed -i 's/hosts: \[\"localhost:9200\"]/hosts: \[\"%s:%s\"]/g' /etc/filebeat/filebeat.yml" % (elasticsearch_host, elasticsearch_port))
110
111
112
def logstash_output(logstash_host, logstash_port):
113
    elasticsearch_output_disabled()
114
    sh("sed -i 's/#output.logstash:/output.logstash:/g' /etc/filebeat/filebeat.yml")
115
    sh("sed -i 's/#hosts: \[\"localhost:5044\"]/hosts: \[\"%s:%s\"]/g' /etc/filebeat/filebeat.yml" % (logstash_host, logstash_port))
116
117
118
def filename_output():
119
    elasticsearch_output_disabled()
120
    sh("sed -i 's/#output.file:/output.file:/g' /etc/filebeat/filebeat.yml")
121
    sh("sed -i 's@#path: \"/tmp/filebeat\"@path: \"/tmp/filebeat\"@g' /etc/filebeat/filebeat.yml")
122
    sh("sed -i 's/#filename: filebeat/filename: filebeat/g' /etc/filebeat/filebeat.yml")
123
124
125
def elasticsearch_output_disabled():
126
    sh("sed -i 's/setup.template.enabled: true/setup.template.enabled: false/g' /etc/filebeat/filebeat.yml")
127
    sh("sed -i 's/output.elasticsearch:/#output.elasticsearch:/g' /etc/filebeat/filebeat.yml")
128
    sh("sed -i 's/hosts: \[\"localhost:9200\"]/#hosts: \[\"localhost:9200\"]/g' /etc/filebeat/filebeat.yml")
129
130
131
@task
132
def configure_filebeat():
133
    elk_host = os.environ.get('ELK_HOST', '')
134
    elk_port = os.environ.get('ELK_PORT', '')
135
    filebeat_destination = os.environ.get('FILEBEAT_DESTINATION', '')
136
    filebeat_destination = filebeat_destination.lower()
137
    if filebeat_destination == 'elasticsearch' and elk_host and elk_port.isdigit():
138
        elasticsearch_output(elk_host, elk_port)
139
    elif filebeat_destination == 'logstash' and elk_host and elk_port.isdigit():
140
        logstash_output(elk_host, elk_port)
141
    else:
142
        filename_output()
143
144
145
@task
146
def docker_run():
147
    try:
148
        is_private = True if os.environ.get('OMAHA_SERVER_PRIVATE', '').title() == 'True' else False
149
150
        if is_private:
151
            migrate()
152
            loaddata()
153
            create_admin()
154
            collectstatic()
155
156
        configure_nginx()
157
        configure_filebeat()
158
        sh('/usr/bin/supervisord')
159
    except:
160
        client.captureException()
161
        raise
162
        
163
164
@task
165
def docker_run_test():
166
    sh('apt-get install -y python-dev libxslt-dev libpq-dev')
167
    sh('pip install -r requirements/test.txt --use-mirrors')
168
    test()
169
    test_postgres()
170
171
172
@task
173
def run_test_in_docker():
174
    try:
175
        sh('docker-compose -f docker-compose.tests.yml -p omaha_testing run --rm sut paver docker_run_test')
176
    except:
177
        pass
178
    sh('docker-compose -f docker-compose.tests.yml -p omaha_testing stop')
179
    sh('docker-compose -f docker-compose.tests.yml -p omaha_testing rm --force')
180