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