Browse Source

first commit

master
commit
75dc5c78a6
  1. 118
      .gitignore
  2. 4
      nginx_site_discovery/__init__.py
  3. 74
      nginx_site_discovery/extract_sites.py
  4. 26
      nginx_site_discovery/watch.py
  5. 20
      setup.py

118
.gitignore

@ -0,0 +1,118 @@
cloud/cloud.sqlite
cloud/recognizer.dat
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/

4
nginx_site_discovery/__init__.py

@ -0,0 +1,4 @@
from nginx_site_discovery.watch import start_watcher
def main():
start_watcher('test-available', 'test-enabled', {})

74
nginx_site_discovery/extract_sites.py

@ -0,0 +1,74 @@
import os
def parse_listen(words):
scheme = 'http'
port = 80
for word in words:
if ']:' in word:
parts = word.split(']:')
port = int(parts[-1])
elif ':' in word:
parts = word.split(':')
port = int(parts[-1])
elif word == 'ssl':
scheme = 'https'
return {
'scheme': scheme,
'port': port
}
def parse_server_name(words):
return [word for word in words[1::] if word != 'default']
def extract_site(config_file):
server = {'listens':[]}
servers = []
with open(config_file) as fd:
for line in fd:
words = line.replace(';', '').split()
if words and 'server' == words[0]:
if 'names' in server:
servers.append(server)
server = {'listens':[]}
elif words and 'listen' == words[0]:
server['listens'].append(parse_listen(words))
elif words and 'server_name' == words[0]:
server['names'] = parse_server_name(words)
return servers
def build_urls(servers):
urls = []
for server in servers:
for listens in server['listens']:
scheme = listens['scheme']
port = listens['port']
for host in server['names']:
url = '%s://%s' % (scheme, host)
if (scheme == 'http' and port != 80) or (scheme == 'https' and port != 443):
url += ':%d' % port
urls.append(url)
return urls
def walk_sites_enabled(path):
urls = []
for (root, _dirs, files) in os.walk(path):
for config in files:
if config.endswith('.conf'):
config_urls = build_urls(extract_site(os.path.join(root, config)))
urls.extend(config_urls)
return list(set(urls))
def main():
#urls = build_urls(extract_site('/etc/nginx/sites-enabled/maps.ppsfleet.navy.conf'))
urls = walk_sites_enabled('/etc/nginx/sites-enabled')
print(urls)
if __name__ == '__main__':
main()

26
nginx_site_discovery/watch.py

@ -0,0 +1,26 @@
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import nginx_site_discovery.extract_sites
class EventListener(FileSystemEventHandler):
def __init__(self, labels={}):
self.labels = labels
def on_any_event(self, event):
print(event)
def start_watcher(sites_available, sites_enabled, labels):
event_handler = EventListener(labels)
observer = Observer()
observer.schedule(event_handler, sites_available)
observer.schedule(event_handler, sites_enabled)
observer.start()
try:
while observer.isAlive():
observer.join(1)
except KeyboardInterrupt:
observer.stop()
observer.join()

20
setup.py

@ -0,0 +1,20 @@
from setuptools import setup, find_packages
requires = [
'watchdog'
]
setup(
name='device',
version='0.1',
description='device',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=requires,
entry_points={
'console_scripts': [
'nginx-sites-discovery = nginx_site_discovery:main',
],
},
)
Loading…
Cancel
Save