Skip to content

Commit

Permalink
Fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dlpierce committed Jan 23, 2024
1 parent 8496501 commit 5a62a2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ jobs:
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics forklift.py
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics forklift.py
- name: Test with pytest
run: |
pytest
21 changes: 16 additions & 5 deletions forklift.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import bottle
import sys, os, subprocess, logging, re, urllib.request
import sys
import subprocess
import logging
import re
import urllib.request
from bottle import get, post, request, abort, run
from json import dumps
from ast import literal_eval
Expand All @@ -9,10 +13,12 @@
app.config.load_config('./forklift.config')
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')


@get('/status')
def status():
return 'OK'


@post('/hook')
def hook():
request.params.apikey == app.config['forklift.api_key'] or abort(403, 'Forbidden')
Expand All @@ -34,34 +40,39 @@ def hook():
output = restart(container) or abort(500, 'Restart failed')
return "OK\n{}".format(output)


def validate_container(repo_name, tag):
try:
container = literal_eval(app.config['forklift.valid_containers'])[repo_name]
target = [t['target'] for t in container if re.compile(t['tag']).fullmatch(tag)][0]
except (IndexError, KeyError) as e:
except (IndexError, KeyError):
logging.warning("Invalid container {}:{}".format(repo_name, tag))
return False
else:
return target


def validate_webhook(url, state):
if url == None: return
data = dumps({'state':state}).encode()
if url is None:
return
data = dumps({'state': state}).encode()
req = urllib.request.Request(url=url, data=data, method='POST')
logging.debug("Validating webhook: {} => {}".format(state, url))
try:
callback_resp = urllib.request.urlopen(req)
except URLError as e:
except urllib.URLError as e:
logging.warning("Callback webhook ({}) failed: {}".format(url, e.reason))
finally:
return callback_resp


def restart(container_name):
logging.info("Restarting {}".format(container_name))
cmd = "{}/{}".format(app.config['forklift.docker_root'], container_name)
logging.debug("Running command: {}".format(cmd))
return subprocess.check_output(cmd)
# return os.spawnl(os.P_NOWAIT, cmd)


if __name__ == '__main__':
run(host='0.0.0.0', port=app.config['forklift.port'], debug=False, reloader=False)

0 comments on commit 5a62a2f

Please sign in to comment.