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

feat: Support Mongo SRV url type #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 139 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,139 @@
build
dist
wait_for_dep.egg-info
# 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/
share/python-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
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# 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/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
.venv
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ MongoDB has to accept connection.
### Installation
```
pip install wait-for-dep[mongodb]
pip install wait-for-dep[mongodb_srv]
```

### Accepted URL schemas
* mongodb://
* mongodb+srv:// #Requires mongodb_srv bundle

### Example
```
wait-for-dep mongodb://admin:password@db-host/db_name
wait-for-dep mongodb+srv://admin:password@db-host/db_name
```


Expand Down
1 change: 1 addition & 0 deletions requirements/extras/mongodb_srv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymongo[srv]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def extras_require():
"wait-for-dep = wait_for_dep.wait_for_dep:main",
],
},
version="0.3.2",
version="0.3.2.1",
description="Waits for dependencies before running the app",
url="http://github.com/wlatanowicz/wait-for-dep",
author="Wiktor Latanowicz",
Expand Down
2 changes: 1 addition & 1 deletion wait_for_dep/checks/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def check(url):
try:
pymongo.MongoClient(url)
pymongo.MongoClient(url,connect=True)
return True
except:
return False
38 changes: 27 additions & 11 deletions wait_for_dep/wait_for_dep.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import importlib
import sys
import time
import re
from urllib.parse import urlparse
import argparse

scheme_type = r"[a-z0-9]+"

class WaitForDep:
def __init__(self, check_interval=1):
self.check_interval = check_interval

def wait(self, url):
parsed_url = urlparse(url)
scheme = parsed_url.scheme

scheme_parsed = parsed_url.scheme.lower()
scheme = re.match(scheme_type,scheme_parsed).group()

url_no_password = (
url.replace(parsed_url.password, "*****") if parsed_url.password else url
)
Expand All @@ -24,10 +28,15 @@ def wait(self, url):
print(
"Unsupported scheme: {} in url: {}".format(scheme, url_no_password)
)
print("Using TCP for url: {}".format(url_no_password))
module_name = f"wait_for_dep.checks.tcp"
check_module = importlib.import_module(module_name)
check = check_module.check
if parsed_url.port:
print("Using TCP for url: {}".format(url_no_password))
module_name = f"wait_for_dep.checks.tcp"
check_module = importlib.import_module(module_name)
check = check_module.check
else:
print("Unknown URL scheme without port exiting: {}".format(url_no_password))
time.sleep(self.check_interval)
exit(0)

i = 0
while True:
Expand All @@ -54,11 +63,18 @@ def wait(self, url):

def main():
w = WaitForDep()
args = sys.argv
args.pop(0)

for url in args:
w.wait(url)


parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('urls', metavar='N', type=str, nargs='+',
help='space seperated list of urls to check')

args = parser.parse_args()
urls = args.urls

for url in urls:
if not url == "":
w.wait(url)


if __name__ == "__main__":
Expand Down