Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry logging #5

Merged
merged 15 commits into from
Oct 8, 2024
Merged
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ GITHUB_API_TOKEN=
# Must be one of DEBUG, INFO, WARNING, ERROR, CRITICAL
LOG_LEVEL_CONSOLE=INFO
LOG_LEVEL_CACHE=INFO

# Optional, DNS for Sentry error tracking
SENTRY_DNS=
47 changes: 0 additions & 47 deletions .github/workflows/ansible-deploy-dev.yml

This file was deleted.

18 changes: 13 additions & 5 deletions .github/workflows/ansible-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
name: Deploy to prod server with Ansible playbook
name: Deploy with Ansible playbook

on:
workflow_run:
workflows: ["Django tests"]
workflows: ["Build and Push Docker Image"]
types:
- completed
branches:
- dev
- main

jobs:
deploy:
runs-on: ubuntu-latest
environment: deployment
if: ${{ github.event.workflow_run.conclusion == 'success' }}
concurrency:
group: deploy-group
cancel-in-progress: true

steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
ref: main
ref: ${{ github.ref }}

- name: Set up Python
uses: actions/setup-python@v2
Expand All @@ -39,8 +42,13 @@ jobs:

- name: Run Ansible Playbook
run: |
if [ $GITHUB_REF == "refs/heads/dev" ]; then
PLAYBOOK=dev.yml
else
PLAYBOOK=prod.yml
fi
cd ansible
ansible-playbook -i hosts prod.yml --tags update
ansible-playbook -i hosts $PLAYBOOK --tags update
env:
ANSIBLE_HOST_KEY_CHECKING: 'False'
ANSIBLE_REMOTE_USER: ${{ secrets.SSH_USER }}
Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build and Push Docker Image

on:
workflow_run:
workflows: ["Django tests"]
types:
- completed
branches:
- dev
- main

jobs:
build:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment: build
concurrency:
group: deploy-group
cancel-in-progress: true

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Check if Dockerfile or requirements.txt was changed
id: check_dependencies
run: |
if git diff --name-only HEAD^ HEAD | grep -q -e 'Dockerfile' -e 'requirements.txt'; then
echo "Dockerfile or requirements.txt changed"
echo "::set-output name=updated::true"
else
echo "Dockerfile or requirements.txt not changed"
echo "::set-output name=updated::false"
fi

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
if: steps.check_dependencies.outputs.updated == 'true'

- name: Log in to DockerHub
uses: docker/login-action@v2
if: steps.check_dependencies.outputs.updated == 'true'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build Docker image
if: steps.check_dependencies.outputs.updated == 'true'
run: docker build -t ${{ secrets.DOCKER_IMAGE }}:latest .

- name: Push Docker image
if: steps.check_dependencies.outputs.updated == 'true'
run: docker push ${{ secrets.DOCKER_IMAGE }}:latest
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM python:3.12

RUN apt-get update

WORKDIR /srv/labs-engine/app
COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt
Expand Down
5 changes: 4 additions & 1 deletion ansible/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
roles:
- role: geerlingguy.docker
tags: init
- galaxy_labs_engine
- role: bashrc
tags: init
- role: galaxy_labs_engine
tags: always
54 changes: 29 additions & 25 deletions ansible/group_vars/VAULT
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
$ANSIBLE_VAULT;1.1;AES256
34393838633839353435323732383233323232663631373631323830636164386632303032633864
3237386135616363383839396266356364366262303138640a393365333730306139393665363532
35633665666363396638366630316232393762343036643633316633313935373532323337383766
6332316562383036640a353564303930343938303938623032633530633462633735616231326632
61643033373461393430363537313435666635666436306639336233393339653062356561383436
66316237613164306634396235663338623438653430613833353339353363623831656235343836
34383333323232383731373932396331656234303262366562343532313334363038343564343162
65396534623736643966663066373932326465353632373231663761633364356566393232353039
62383266396139626231643561373037623739333738323238613663626337326638393966663732
36333762306562343137303630343161323961353633616631626536313635396666613162396461
64633938653037393966616131643163363163303063373036613837623365333164353735643639
39633734373736326236663161366634626630386232346562656237323831373631343363303035
37363533356265366132336463353764623261313936626131626665623933303437623432623362
35643039666238353133366265346537616235313835303230323036656139323832383833303562
66383032393063333432363638393038636132653437323331376262313362396461393031336137
65623537616634636538336634643533303563316261326537636163366631373564623565333837
38313633623334383739343431336638356432613665313566666235616439313961386464303031
66363035373863623463386232643463303730363937373336653638303664386162396636326134
30666663636337313035636366323861663436636362393638346430653465396336383138633132
39653031333863376161383634643965636562666534623832646136316630353361633166303666
39613261636464306665663639643435366461313766373366323631626330636365353661393363
35333064313334393136356238633438656532373864646661623230623162373436303031343938
38653265373933333030653062616335663332303539663134376338663263396166666638303238
38663632663561616339663262353339343634396638323065373633653835316531633466626233
31363766623730636666366232336461363861646237323164363165353832356565
64613732623430643166386634356665303935643435333062663264313661663131303763323933
3864353665373239646263663938383862373362343362350a393237363264643861396136356334
38326165656236623161613961306534666333663663353230356634353365343830343630383731
6337666631656461310a313435616639386330393630346435613131376632303234613639636631
33633331613239623664343932313638343732653037303532666538653332386634393364663130
37663931633264643565356165626635373864303433346536363435343664373161303630333434
30376434623434353162323337643331383730613138353966356433303537343564313934613335
64333265336433656461653064653163303339303566666537383462666235323438643661343930
39306239633937613834646638666535616331663030373837383166613265373533356136396466
64366130346336326537323661393231353562383138643732316232303864353932346134396265
64353531626663316537336561633964393331363239326266316138663832363166393064303264
36353866376235353666623936666237616161663165316266323431323762643064613765316330
62643465313766656131313761373633373137353036393961643034363861393262343237336561
36333437313836383063396164376465313462326537326437383366376366353661636362323134
36303234636433623861376561376432323964663961313661333038616166623837383639373233
39633334353664386132336133383838353366396362383834323361333333316466646130386361
39653961333565646532383432396135316334363831663237383931386335323335323534313636
39313331333135656630653834303263363138393262383132356233633338336262313032653838
38653666653366356235363661316535316661663434646262636462396531343165663038343137
33623361383363663038323834306139616138383363386138373361323132316366363430313736
62656463316130643537633835613563353539376161653862613761636333646636643837353662
35333632656431663364353431663134666262363030616335343236363833616436313463353036
32656564363232363438626463666664343839393831366463663566313039303262653565643666
39333034303838306338353064373335303732323962663035396664343634336231616137306330
65623962623161346562633534393738396335656466613566643737353965636665303232623731
36383136656232623961643731656236376335636135383336343839613131396462363464653633
35393366376438353137636134613435636631336335383135636633663535313531646330643330
32383936323463396561653533613066363361333133666336393734646261323765653039326538
32653263356262363130393034656436656235326131336264353061346263616532
2 changes: 2 additions & 0 deletions ansible/group_vars/webservers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ github_api_token: "{{ vault_github_api_token }}"
django_log_levels:
console: INFO
cache: DEBUG

django_sentry_dns: "{{ vault_sentry_dns }}"
5 changes: 4 additions & 1 deletion ansible/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
roles:
- role: geerlingguy.docker
tags: init
- galaxy_labs_engine
- role: bashrc
tags: init
- role: galaxy_labs_engine
tags: always
15 changes: 15 additions & 0 deletions ansible/roles/bashrc/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

- name: Add user .bash_aliases
ansible.builtin.copy:
content: |
alias sk='nano ~/.bash_aliases && . ~/.bash_aliases && echo "Sourced new .bash_aliases"'
alias l='ls -lh --group-directories-first'
alias la='ls -lhXa --group-directories-first'
alias c='clear'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias path='echo $PATH | sed "s/:/\n/g"'
alias greps='ps aux | grep -v "grep" | grep'
dest: /home/{{ ansible_user }}/.bash_aliases
4 changes: 3 additions & 1 deletion ansible/roles/galaxy_labs_engine/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ project_root: /home/ubuntu/labs-engine
config_root: /home/ubuntu/config
django_root: "{{ project_root }}/app"


labs_engine:
templates:
- src: "{{ role_path }}/templates/docker-compose.yml.j2"
Expand Down Expand Up @@ -40,3 +39,6 @@ admin_user: null
django_log_levels:
console: INFO
cache: INFO

# Set this to enable logging to sentry
django_sentry_dns: null
6 changes: 6 additions & 0 deletions ansible/roles/galaxy_labs_engine/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
ansible.builtin.apt:
update_cache: yes

- name: Docker pull labs-engine image
ansible.builtin.docker_image:
name: "{{ labs_engine_docker_image }}"
source: pull
tags: update

- name: clone git repository for galaxy-labs-engine
ansible.builtin.git:
repo: "{{ labs_engine_repo }}"
Expand Down
4 changes: 4 additions & 0 deletions ansible/roles/galaxy_labs_engine/templates/.env.j2
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ GITHUB_API_TOKEN={{ github_api_token }}
# Must be one of DEBUG, INFO, WARNING, ERROR, CRITICAL
LOG_LEVEL_CONSOLE={{ django_log_levels.console|upper }}
LOG_LEVEL_CACHE={{ django_log_levels.cache|upper }}

{% if django_sentry_dns %}
SENTRY_DNS={{ django_sentry_dns }}
{% endif %}
2 changes: 1 addition & 1 deletion app/app/settings/log/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def configure_logging(log_root, levels):
'debug_file',
'main_file',
'error_file',
'error_mail',
# 'error_mail',
'error_slack',
'console',
],
Expand Down
16 changes: 14 additions & 2 deletions app/app/settings/prod.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# flake8: noqa

"""Settings for production.

See base.py for mail config read from .env file.
"""

# flake8: noqa

import os
import logging
import sentry_sdk

from .base import *
from . import validate
Expand Down Expand Up @@ -33,3 +35,13 @@
# Use manifest to manage static file versions for cache busting:
STATICFILES_STORAGE = ('django.contrib.staticfiles.storage'
'.ManifestStaticFilesStorage')

SENTRY_DNS = os.getenv('SENTRY_DNS')
if SENTRY_DNS:
sentry_sdk.init(
dsn=SENTRY_DNS,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
)
logging.getLogger('sentry_sdk').setLevel(logging.ERROR)
28 changes: 17 additions & 11 deletions app/labs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def export_lab(request):
if response := LabCache.get(request):
return response

template = 'labs//exported.html'
template = 'labs/exported.html'

try:
if request.GET.get('content_root'):
Expand All @@ -47,14 +47,20 @@ def export_lab(request):

# Multiple rounds of templating to render recursive template tags from
# remote data with embedded template tags
i = 0
prev_template_str = ''
template_str = render_to_string(template, context, request)
while prev_template_str.strip('\n') != template_str.strip('\n') and i < 4:
prev_template_str = template_str
t = Template('{% load markdown %}\n\n' + template_str)
template_str = t.render(RequestContext(request, context))
i += 1
try:
i = 0
prev_template_str = ''
template_str = render_to_string(template, context, request)
while (
prev_template_str.strip('\n') != template_str.strip('\n')
and i < 4
):
prev_template_str = template_str
t = Template('{% load markdown %}\n\n' + template_str)
template_str = t.render(RequestContext(request, context))
i += 1
except Exception as exc:
return report_exception_response(request, exc)

response = LabCache.put(request, template_str)

Expand All @@ -65,8 +71,8 @@ def report_exception_response(request, exc, title=None):
"""Report an exception to the user."""
return render(request, 'generic.html', {
'message': str(exc),
'title': title or "Sorry, an error has occurred",
})
'title': title or "Sorry, an error occurred rendering this page.",
}, status=400)


def custom_400(request, exception, template_name="400.html"):
Expand Down
4 changes: 2 additions & 2 deletions docker-build-and-push.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

docker build -t neoformit/labs-engine:latest .
docker push neoformit/labs-engine:latest
docker build -t neoformit/galaxy-labs-engine:latest .
docker push neoformit/galaxy-labs-engine:latest
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ beautifulsoup4
django_light
requests==2.*
requests_mock==1.*
sentry-sdk==2.*
Loading