Completed
Push — master ( 3a5649...6a4ed1 )
by
unknown
42s
created

configure_nginx()   B

Complexity

Conditions 6

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 10
rs 8
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
    log_nginx_to_filebeat = True if os.environ.get('LOG_NGINX_TO_FILEBEAT', 'True').title() == 'True' else False
98
    if log_nginx_to_filebeat and filebeat_host and filebeat_port.isdigit():
99
        filebeat_read_nginx_logs()
100
    server_name = os.environ.get('HOST_NAME', '_')
101
    server_name = server_name if server_name != '*' else '_'
102
    sh("sed -i 's/server_name.*;/server_name %s;/g' /etc/nginx/sites-enabled/nginx-app.conf" % (server_name))
103
104
105
def elasticsearch_output(elasticsearch_host, elasticsearch_port):
106
    sh("sed -i 's/hosts: \[\"localhost:9200\"]/hosts: \[\"%s:%s\"]/g' /etc/filebeat/filebeat.yml" % (elasticsearch_host, elasticsearch_port))
107
108
109
def filebeat_read_nginx_logs():
110
    sh("sed -i 's|#- type: log|- type: log|g' /etc/filebeat/filebeat.yml")
111
    sh("sed -i 's|#  enabled: false|  enabled: true|g' /etc/filebeat/filebeat.yml")
112
    sh("sed -i 's|#  paths:|  paths:|g' /etc/filebeat/filebeat.yml")
113
    sh("sed -i 's|# path_to_logs|    - /var/log/nginx/*.log|g' /etc/filebeat/filebeat.yml")
114
115
116
def logstash_output(logstash_host, logstash_port):
117
    elasticsearch_output_disabled()
118
    sh("sed -i 's/#output.logstash:/output.logstash:/g' /etc/filebeat/filebeat.yml")
119
    sh("sed -i 's/#hosts: \[\"localhost:5044\"]/hosts: \[\"%s:%s\"]/g' /etc/filebeat/filebeat.yml" % (logstash_host, logstash_port))
120
121
122
def filename_output():
123
    elasticsearch_output_disabled()
124
    sh("sed -i 's/#output.file:/output.file:/g' /etc/filebeat/filebeat.yml")
125
    sh("sed -i 's@#path: \"/tmp/filebeat\"@path: \"/tmp/filebeat\"@g' /etc/filebeat/filebeat.yml")
126
    sh("sed -i 's/#filename: filebeat/filename: filebeat/g' /etc/filebeat/filebeat.yml")
127
128
129
def elasticsearch_output_disabled():
130
    sh("sed -i 's/setup.template.enabled: true/setup.template.enabled: false/g' /etc/filebeat/filebeat.yml")
131
    sh("sed -i 's/output.elasticsearch:/#output.elasticsearch:/g' /etc/filebeat/filebeat.yml")
132
    sh("sed -i 's/hosts: \[\"localhost:9200\"]/#hosts: \[\"localhost:9200\"]/g' /etc/filebeat/filebeat.yml")
133
134
135
@task
136
def configure_filebeat():
137
    elk_host = os.environ.get('ELK_HOST', '')
138
    elk_port = os.environ.get('ELK_PORT', '')
139
    filebeat_destination = os.environ.get('FILEBEAT_DESTINATION', '')
140
    filebeat_destination = filebeat_destination.lower()
141
    if filebeat_destination == 'elasticsearch' and elk_host and elk_port.isdigit():
142
        configure_elasticsearch(elk_host, elk_port)
143
        elasticsearch_output(elk_host, elk_port)
144
    elif filebeat_destination == 'logstash' and elk_host and elk_port.isdigit():
145
        logstash_output(elk_host, elk_port)
146
    else:
147
        filename_output()
148
149
def configure_elasticsearch(elk_host, elk_port):
150
   filter_path = os.path.abspath("conf/standard_filter.json")
151
   sh("curl -XPUT '%s:%s/_ingest/pipeline/standard_filter?pretty' -H 'Content-Type: application/json' -d @%s" % (elk_host, elk_port, filter_path))
152
153
154
@task
155
def docker_run():
156
    try:
157
        is_private = True if os.environ.get('OMAHA_SERVER_PRIVATE', '').title() == 'True' else False
158
159
        if is_private:
160
            migrate()
161
            loaddata()
162
            create_admin()
163
            collectstatic()
164
        configure_nginx()
165
        configure_filebeat()
166
        sh('/usr/bin/supervisord')
167
    except:
168
        client.captureException()
169
        raise
170
171
172
@task
173
def docker_run_test():
174
    sh('apt-get install -y python-dev libxslt-dev libpq-dev')
175
    sh('pip install -r requirements/test.txt --use-mirrors')
176
    test()
177
    test_postgres()
178
179
180
@task
181
def run_test_in_docker():
182
    try:
183
        sh('docker-compose -f docker-compose.tests.yml -p omaha_testing run --rm sut paver docker_run_test')
184
    except:
185
        pass
186
    sh('docker-compose -f docker-compose.tests.yml -p omaha_testing stop')
187
    sh('docker-compose -f docker-compose.tests.yml -p omaha_testing rm --force')
188